Spaces:
Sleeping
Sleeping
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}) |