File size: 1,803 Bytes
e93aee7
 
02a7ae7
 
e93aee7
 
 
 
02a7ae7
e93aee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import streamlit as st
import requests
import os
SECRET_TOKEN = os.getenv("SECRET_TOKEN")

st.title("How do you feel ?")

API_URL = "https://api-inference.huggingface.co/models/lxyuan/distilbert-base-multilingual-cased-sentiments-student"
headers = {"Authorization": "Bearer "+SECRET_TOKEN}

def query(payload):
	response = requests.post(API_URL, headers=headers, json=payload)
	return response.json()


def analyze_sentiment_Transformer(text):
    # Perform sentiment analysis
    results = query(text)
    first_dict = results[0]
    first_label = first_dict[0]
    sentiment = first_label['label']
    score = first_label['score']
    return {
         "sentiment":sentiment,
         "score":score
    }


if "messages" not in st.session_state:
    st.session_state.messages = []

for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

if prompt := st.chat_input("Tell me how you feel, whatever language"):
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)

    with st.chat_message("assistant"):
        response = analyze_sentiment_Transformer(prompt)
        sentiment = response['sentiment']
        score = response['score']
        if(sentiment == "positive"):
            st.balloons()
            fullresponse = f'happy to know you feel good with a score of '+str(score)
        elif (sentiment == "negative"):
            fullresponse = f'sorry to know you feel bad with a score of '+str(score)
            st.snow()
        else:
            fullresponse = f'Ok you feel neutral, hoping the best '+str(score)
        
        st.markdown(fullresponse)
    st.session_state.messages.append({"role": "assistant", "content": response})