mmerlange commited on
Commit
e93aee7
1 Parent(s): 5e5d4a9

create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+
4
+ st.title("How do you feel ?")
5
+
6
+ API_URL = "https://api-inference.huggingface.co/models/lxyuan/distilbert-base-multilingual-cased-sentiments-student"
7
+ headers = {"Authorization": "Bearer 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"}
8
+
9
+ def query(payload):
10
+ response = requests.post(API_URL, headers=headers, json=payload)
11
+ return response.json()
12
+
13
+
14
+ def analyze_sentiment_Transformer(text):
15
+ # Perform sentiment analysis
16
+ results = query(text)
17
+ first_dict = results[0]
18
+ first_label = first_dict[0]
19
+ sentiment = first_label['label']
20
+ score = first_label['score']
21
+ return {
22
+ "sentiment":sentiment,
23
+ "score":score
24
+ }
25
+
26
+
27
+ if "messages" not in st.session_state:
28
+ st.session_state.messages = []
29
+
30
+ for message in st.session_state.messages:
31
+ with st.chat_message(message["role"]):
32
+ st.markdown(message["content"])
33
+
34
+ if prompt := st.chat_input("Tell me how you feel, whatever language"):
35
+ st.session_state.messages.append({"role": "user", "content": prompt})
36
+ with st.chat_message("user"):
37
+ st.markdown(prompt)
38
+
39
+ with st.chat_message("assistant"):
40
+ response = analyze_sentiment_Transformer(prompt)
41
+ sentiment = response['sentiment']
42
+ score = response['score']
43
+ if(sentiment == "positive"):
44
+ st.balloons()
45
+ fullresponse = f'happy to know you feel good with a score of '+str(score)
46
+ elif (sentiment == "negative"):
47
+ fullresponse = f'sorry to know you feel bad with a score of '+str(score)
48
+ st.snow()
49
+ else:
50
+ fullresponse = f'Ok you feel neutral, hoping the best '+str(score)
51
+
52
+ st.markdown(fullresponse)
53
+ st.session_state.messages.append({"role": "assistant", "content": response})