robertselvam commited on
Commit
bb5f283
1 Parent(s): b96e25e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -45
app.py CHANGED
@@ -20,38 +20,63 @@ class SentimentAnalyzer:
20
  pass
21
 
22
  def emotion_analysis(self,text):
23
- prompt = f""" Your task is find the top 3 emotion for this converstion {text}: <Sadness, Happiness, Fear, Disgust, Anger> and it's emotion score for the Mental Healthcare Doctor Chatbot and patient conversation text.\
 
 
 
24
  you 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 3 result having the highest score]
25
  The scores should be in the range of 0.0 to 1.0, where 1.0 represents the highest intensity of the emotion.
26
- """
27
- response = client.completions.create(
28
- model="text-davinci-003",
29
- prompt=prompt,
30
- temperature=0,
31
- max_tokens=60,
32
- top_p=1,
33
- frequency_penalty=0,
34
- presence_penalty=0
35
  )
36
- message = response.choices[0].text
37
- return message
 
 
38
 
39
  def analyze_sentiment_for_graph(self, text):
40
- prompt = f""" Your task is find the setiments for this converstion {text} : <labels = positive, negative, neutral> and it's sentiment score for the Mental Healthcare Doctor Chatbot and patient conversation text.\
 
 
 
 
41
  you are analyze the text and provide the output in the following json format heigher to lower order: '''["label1","label2","label3"][score1,score2,score3]'''
42
- """
43
- response = client.completions.create(
44
- model="text-davinci-003",
45
- prompt=prompt,
46
- temperature=0,
47
- max_tokens=60,
48
- top_p=1,
49
- frequency_penalty=0,
50
- presence_penalty=0
51
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  # Extract the generated text
54
- sentiment_scores = response.choices[0].text.strip()
55
  start_index = sentiment_scores.find("[")
56
  end_index = sentiment_scores.find("]")
57
  list1_text = sentiment_scores[start_index + 1: end_index]
@@ -79,16 +104,24 @@ class Summarizer:
79
  # openai.api_key=os.getenv("OPENAI_API_KEY")
80
  pass
81
  def generate_summary(self, text):
82
- completions = client.completions.create(
83
- model="text-davinci-003",
84
- prompt=f"""summarize the following conversation delimited by triple backticks. write within 30 words.```{text}``` """,
85
- max_tokens=60,
86
- n=1,
87
- stop=None,
88
- temperature=0.5,
 
 
 
 
 
 
89
  )
90
- message = completions.choices[0].text
91
- return message
 
 
92
 
93
  history_state = gr.State()
94
  summarizer = Summarizer()
@@ -161,8 +194,11 @@ class LangChain_Document_QA:
161
  patient_details = json.load(file)
162
  except:
163
  pass
164
-
165
- prompt = f"""Analyse the patient json If asked for information take it from {patient_details} \
 
 
 
166
  you first get patient details : <get name,age,gender,contact,address from patient> if not match patient json information start new chat else match patient \
167
  json information ask previous: <description,symptoms,diagnosis,treatment talk about patient> As an empathic AI Mental Healthcare Doctor Chatbot, provide effective solutions to patients' mental health concerns. \
168
  first start the conversation ask existing patient or new patient. if new patient get name,age,gender,contact,address from the patient and start. \
@@ -171,19 +207,20 @@ class LangChain_Document_QA:
171
  Chat History:['''{history_list}''']
172
  Patient: ['''{text}''']
173
  Perform as Mental Healthcare Doctor Chatbot
174
- """
175
- response = client.completions.create(
176
- model="text-davinci-003",
177
- prompt=prompt,
178
- temperature=0,
 
 
179
  max_tokens=500,
180
- top_p=1,
181
- frequency_penalty=0,
182
- presence_penalty=0.6,
183
  )
184
-
185
- message = response.choices[0].text.strip()
186
- if ":" in message:
 
187
  message = re.sub(r'^.*:', '', message)
188
  history[-1][1] = message.strip()
189
  history_state.value = history
 
20
  pass
21
 
22
  def emotion_analysis(self,text):
23
+ # Create a conversation for the OpenAI chat API
24
+ conversation = [
25
+ {"role": "system", "content": "You are a helpful assistant."},
26
+ {"role": "user", "content": f""" Your task is find the top 3 emotion for this converstion {text}: <Sadness, Happiness, Fear, Disgust, Anger> and it's emotion score for the Mental Healthcare Doctor Chatbot and patient conversation text.\
27
  you 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 3 result having the highest score]
28
  The scores should be in the range of 0.0 to 1.0, where 1.0 represents the highest intensity of the emotion.
29
+ """}
30
+ ]
31
+
32
+ # Call OpenAI GPT-3.5-turbo
33
+ chat_completion = client.chat.completions.create(
34
+ model = "gpt-3.5-turbo-1106",
35
+ messages = conversation,
36
+ max_tokens=500,
37
+ temperature=0
38
  )
39
+
40
+ response = chat_completion.choices[0].message.content
41
+ return response
42
+
43
 
44
  def analyze_sentiment_for_graph(self, text):
45
+
46
+ # Create a conversation for the OpenAI chat API
47
+ conversation = [
48
+ {"role": "system", "content": "You are a helpful assistant."},
49
+ {"role": "user", "content": f""" Your task is find the setiments for this converstion {text} : <labels = positive, negative, neutral> and it's sentiment score for the Mental Healthcare Doctor Chatbot and patient conversation text.\
50
  you are analyze the text and provide the output in the following json format heigher to lower order: '''["label1","label2","label3"][score1,score2,score3]'''
51
+ """}
52
+ ]
53
+
54
+ # Call OpenAI GPT-3.5-turbo
55
+ chat_completion = client.chat.completions.create(
56
+ model = "gpt-3.5-turbo-1106",
57
+ messages = conversation,
58
+ max_tokens=500,
59
+ temperature=0
60
  )
61
+
62
+ response = chat_completion.choices[0].message.content
63
+ return response
64
+
65
+ # prompt = f""" Your task is find the setiments for this converstion {text} : <labels = positive, negative, neutral> and it's sentiment score for the Mental Healthcare Doctor Chatbot and patient conversation text.\
66
+ # you are analyze the text and provide the output in the following json format heigher to lower order: '''["label1","label2","label3"][score1,score2,score3]'''
67
+ # """
68
+ # response = client.completions.create(
69
+ # model="text-davinci-003",
70
+ # prompt=prompt,
71
+ # temperature=0,
72
+ # max_tokens=60,
73
+ # top_p=1,
74
+ # frequency_penalty=0,
75
+ # presence_penalty=0
76
+ # )
77
 
78
  # Extract the generated text
79
+ sentiment_scores = response
80
  start_index = sentiment_scores.find("[")
81
  end_index = sentiment_scores.find("]")
82
  list1_text = sentiment_scores[start_index + 1: end_index]
 
104
  # openai.api_key=os.getenv("OPENAI_API_KEY")
105
  pass
106
  def generate_summary(self, text):
107
+
108
+ # Create a conversation for the OpenAI chat API
109
+ conversation = [
110
+ {"role": "system", "content": "You are a helpful assistant."},
111
+ {"role": "user", "content": f"""summarize the following conversation delimited by triple backticks. write within 30 words.```{text}``` """}
112
+ ]
113
+
114
+ # Call OpenAI GPT-3.5-turbo
115
+ chat_completion = client.chat.completions.create(
116
+ model = "gpt-3.5-turbo-1106",
117
+ messages = conversation,
118
+ max_tokens=500,
119
+ temperature=0
120
  )
121
+
122
+ response = chat_completion.choices[0].message.content
123
+ return response
124
+
125
 
126
  history_state = gr.State()
127
  summarizer = Summarizer()
 
194
  patient_details = json.load(file)
195
  except:
196
  pass
197
+
198
+ # Create a conversation for the OpenAI chat API
199
+ conversation = [
200
+ {"role": "system", "content": "You are a helpful assistant."},
201
+ {"role": "user", "content": f"""Analyse the patient json If asked for information take it from {patient_details} \
202
  you first get patient details : <get name,age,gender,contact,address from patient> if not match patient json information start new chat else match patient \
203
  json information ask previous: <description,symptoms,diagnosis,treatment talk about patient> As an empathic AI Mental Healthcare Doctor Chatbot, provide effective solutions to patients' mental health concerns. \
204
  first start the conversation ask existing patient or new patient. if new patient get name,age,gender,contact,address from the patient and start. \
 
207
  Chat History:['''{history_list}''']
208
  Patient: ['''{text}''']
209
  Perform as Mental Healthcare Doctor Chatbot
210
+ """}
211
+ ]
212
+
213
+ # Call OpenAI GPT-3.5-turbo
214
+ chat_completion = client.chat.completions.create(
215
+ model = "gpt-3.5-turbo-1106",
216
+ messages = conversation,
217
  max_tokens=500,
218
+ temperature=0
 
 
219
  )
220
+
221
+ response = chat_completion.choices[0].message.content
222
+
223
+ if ":" in response:
224
  message = re.sub(r'^.*:', '', message)
225
  history[-1][1] = message.strip()
226
  history_state.value = history