wendywu commited on
Commit
a4d1b31
·
1 Parent(s): b134cff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -1
app.py CHANGED
@@ -13,14 +13,31 @@ def get_response(question):
13
 
14
  return assistant_answer.content
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
 
 
 
17
 
18
 
19
 
20
  # open ai
21
  chat = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
22
 
23
-
 
24
 
25
 
26
 
@@ -44,6 +61,54 @@ if user_input := st.chat_input("Say something"):
44
  # add assistant input to history
45
  st.session_state.messages.append({"role": "assistant", "content": assistant_input})
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  # display chat history
48
  for message in st.session_state.messages:
49
  with st.chat_message(message["role"]):
 
13
 
14
  return assistant_answer.content
15
 
16
+ def get_sentiment(user_input, nlp):
17
+ result = nlp(user_input)
18
+ sentiment = ""
19
+ if (result[0]['label'] == '1 star'):
20
+ sentiment = 'very negative'
21
+ elif (result[0]['label'] == '2 star'):
22
+ sentiment = 'negative'
23
+ elif (result[0]['label'] == '3 stars'):
24
+ sentiment = 'neutral'
25
+ elif (result[0]['label'] == '4 stars'):
26
+ sentiment = 'positive'
27
+ else:
28
+ sentiment = 'very positive'
29
 
30
+ prob = result[0]['score']
31
+
32
+ return sentiment, prob
33
 
34
 
35
 
36
  # open ai
37
  chat = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
38
 
39
+ # hugging-face model
40
+ nlp = pipeline(task='sentiment-analysis', model='nlptown/bert-base-multilingual-uncased-sentiment')
41
 
42
 
43
 
 
61
  # add assistant input to history
62
  st.session_state.messages.append({"role": "assistant", "content": assistant_input})
63
 
64
+
65
+
66
+ # sentiment analysis
67
+ sentiment, prob = get_sentiment(user_input, nlp)
68
+ if sentiment == "very negative":
69
+ rain(
70
+ emoji="😭",
71
+ font_size=20, # the size of emoji
72
+ falling_speed=3, # speed of raining
73
+ animation_length="infinite", # for how much time the animation will happen
74
+ )
75
+ elif sentiment == "negative":
76
+ rain(
77
+ emoji="🥺",
78
+ font_size=20, # the size of emoji
79
+ falling_speed=3, # speed of raining
80
+ animation_length="infinite", # for how much time the animation will happen
81
+ )
82
+ elif sentiment == "neutral":
83
+ rain(
84
+ emoji="😐",
85
+ font_size=20, # the size of emoji
86
+ falling_speed=3, # speed of raining
87
+ animation_length="infinite", # for how much time the animation will happen
88
+ )
89
+ elif sentiment == "positive":
90
+ rain(
91
+ emoji="😊",
92
+ font_size=20, # the size of emoji
93
+ falling_speed=3, # speed of raining
94
+ animation_length="infinite", # for how much time the animation will happen
95
+ )
96
+ elif sentiment == "very positive":
97
+ rain(
98
+ emoji="🤩",
99
+ font_size=20, # the size of emoji
100
+ falling_speed=3, # speed of raining
101
+ animation_length="infinite", # for how much time the animation will happen
102
+ )
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
  # display chat history
113
  for message in st.session_state.messages:
114
  with st.chat_message(message["role"]):