Karthikeyan commited on
Commit
681c09e
1 Parent(s): d359f3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -29
app.py CHANGED
@@ -18,28 +18,6 @@ class SentimentAnalyzer:
18
  def __init__(self):
19
  # self.model="facebook/bart-large-mnli"
20
  openai.api_key=os.getenv("OPENAI_API_KEY")
21
- def analyze_sentiment(self, text):
22
- prompt = f""" Your task is find the top 3 setiments : <labels = positive, negative, neutral> and it's sentiment score for the Mental Healthcare Doctor Chatbot and patient conversation text.\
23
- your are analyze the text and provide the output in the following json order: \"\"\"
24
- <\{result['labels'][0]: result['scores'][0], result['labels'][1]: result['scores'][1], result['labels'][2]: result['scores'][2] \}>\"\"\" \
25
- analyze the text : '''{text}'''
26
- """
27
- response = openai.Completion.create(
28
- model="text-davinci-003",
29
- prompt=prompt,
30
- temperature=1,
31
- max_tokens=60,
32
- top_p=1,
33
- frequency_penalty=0,
34
- presence_penalty=0
35
- )
36
-
37
- # Extract the generated text
38
- sentiment_scores_str = response.choices[0].text.strip()
39
- print(sentiment_scores_str)
40
- return sentiment_scores_str
41
-
42
-
43
  def emotion_analysis(self,text):
44
  prompt = f""" Your task is find the top 3 emotion : <Sadness, Happiness, Joy, Fear, Disgust, Anger> and it's emotion score for the Mental Healthcare Doctor Chatbot and patient conversation text.\
45
  your are analyze the text and provide the output in the following list format heigher to lower order: ["emotion1","emotion2","emotion3"][score1,score2,score3]''' [with top 1 result having the highest score]
@@ -81,7 +59,7 @@ class SentimentAnalyzer:
81
  list2_text = sentiment_scores[end_index + 2:-1]
82
  sentiment = list(map(str.strip, list1_text.split(",")))
83
  scores = list(map(float, list2_text.split(",")))
84
- score_dict={"Labels": sentiment, "Score": scores}
85
  print(score_dict)
86
  return score_dict
87
 
@@ -151,10 +129,11 @@ class LangChain_Document_QA:
151
 
152
  def _display_graph(self,sentiment_scores):
153
  df = pd.DataFrame(sentiment_scores)
154
- fig = px.bar(df, x='Score', y='Labels', orientation='h', labels={'Score': 'Score', 'Labels': 'Sentiment'})
155
  fig.update_layout(height=500, width=200)
156
  return fig
157
  def _display_graph_emotion(self,customer_emotion_score):
 
158
  fig = px.pie(customer_emotion_score, values='Score', names='Emotion', title='Emotion Distribution', hover_data=['Score'])
159
  #fig.update_traces(texttemplate='Emotion', textposition='outside')
160
  fig.update_layout(height=500, width=200)
@@ -214,11 +193,12 @@ class LangChain_Document_QA:
214
 
215
 
216
  def _text_box(self,customer_emotion,customer_sentiment_score):
217
- sentiment_data = customer_sentiment_score
218
- emotion_data = customer_emotion
219
- sentiment_str = ', '.join([f'{label}: {score}' for label, score in zip(sentiment_data['Labels'], sentiment_data['Score'])])
220
- emotion_str = ', '.join([f'{emotion}: {score}' for emotion, score in zip(emotion_data['Emotion'], emotion_data['Score'])])
221
- return f"Sentiment: {sentiment_str}, Emotion: {emotion_str}"
 
222
 
223
  def _on_sentiment_btn_click(self):
224
  client=self._history_of_chat()
 
18
  def __init__(self):
19
  # self.model="facebook/bart-large-mnli"
20
  openai.api_key=os.getenv("OPENAI_API_KEY")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def emotion_analysis(self,text):
22
  prompt = f""" Your task is find the top 3 emotion : <Sadness, Happiness, Joy, Fear, Disgust, Anger> and it's emotion score for the Mental Healthcare Doctor Chatbot and patient conversation text.\
23
  your are analyze the text and provide the output in the following list format heigher to lower order: ["emotion1","emotion2","emotion3"][score1,score2,score3]''' [with top 1 result having the highest score]
 
59
  list2_text = sentiment_scores[end_index + 2:-1]
60
  sentiment = list(map(str.strip, list1_text.split(",")))
61
  scores = list(map(float, list2_text.split(",")))
62
+ score_dict={"Sentiment": sentiment, "Score": scores}
63
  print(score_dict)
64
  return score_dict
65
 
 
129
 
130
  def _display_graph(self,sentiment_scores):
131
  df = pd.DataFrame(sentiment_scores)
132
+ fig = px.bar(df, x='Score', y='Sentiment', orientation='h', labels={'Score': 'Score', 'Labels': 'Sentiment'})
133
  fig.update_layout(height=500, width=200)
134
  return fig
135
  def _display_graph_emotion(self,customer_emotion_score):
136
+
137
  fig = px.pie(customer_emotion_score, values='Score', names='Emotion', title='Emotion Distribution', hover_data=['Score'])
138
  #fig.update_traces(texttemplate='Emotion', textposition='outside')
139
  fig.update_layout(height=500, width=200)
 
193
 
194
 
195
  def _text_box(self,customer_emotion,customer_sentiment_score):
196
+ # sentiment_data = customer_sentiment_score
197
+ # emotion_data = customer_emotion
198
+ # sentiment_str = ', '.join([f'{label}: {score}' for label, score in zip(sentiment_data['Labels'], sentiment_data['Score'])])
199
+ # emotion_str = ', '.join([f'{emotion}: {score}' for emotion, score in zip(emotion_data['Emotion'], emotion_data['Score'])])
200
+ # return f"Sentiment: {sentiment_str}, Emotion: {emotion_str}"
201
+ return f"Sentiment: \n{customer_sentiment_score[0]} \n {customer_sentiment_score[1]} Emotion: \n{customer_emotion[0]}\n{customer_emotion[1]}\n"
202
 
203
  def _on_sentiment_btn_click(self):
204
  client=self._history_of_chat()