vsrinivas commited on
Commit
8afa4ac
1 Parent(s): a77b984

Update funcs.py

Browse files
Files changed (1) hide show
  1. funcs.py +64 -69
funcs.py CHANGED
@@ -15,8 +15,6 @@ from ai71 import AI71
15
  if torch.cuda.is_available():
16
  model = model.to('cuda')
17
 
18
- # dials_embeddings = pd.read_pickle('dials_embeddings.pkl')
19
- # dials_embeddings = pd.read_pickle('https://huggingface.co/datasets/vsrinivas/CBT_dialogue_embed_ds/resolve/main/dials_embeddings.pkl')
20
  dials_embeddings = pd.read_pickle('https://huggingface.co/datasets/vsrinivas/CBT_dialogue_embed_ds/resolve/main/kaggle_therapy_embeddings.pkl')
21
  with open ('emotion_group_labels.txt') as file:
22
  emotion_group_labels = file.read().splitlines()
@@ -27,6 +25,8 @@ classifier = pipeline("zero-shot-classification", model ='facebook/bart-large-mn
27
  AI71_BASE_URL = "https://api.ai71.ai/v1/"
28
  AI71_API_KEY = os.getenv('AI71_API_KEY')
29
 
 
 
30
  # Detect emotions from patient dialogues
31
  def detect_emotions(text):
32
  emotion = classifier(text, candidate_labels=emotion_group_labels, batch_size=16)
@@ -72,8 +72,13 @@ def generate_triggers_img(items):
72
  triggers_img = Image.open('triggeres.png')
73
  return triggers_img
74
 
75
- # Generate therapist responses and patient triggers
76
- def get_doc_response_emotions(user_message, therapy_session_conversation):
 
 
 
 
 
77
  user_messages = []
78
  user_messages.append(user_message)
79
  emotion_set = detect_emotions(user_message)
@@ -92,75 +97,65 @@ def get_doc_response_emotions(user_message, therapy_session_conversation):
92
 
93
  therapy_session_conversation.append(["User: "+user_message, "Therapist: "+doc_response])
94
 
95
- session_conversation.extend(["User: "+user_message, "Therapist: "+doc_response])
96
 
97
  print(f"User's message: {user_message}")
98
  print(f"RAG Matching message: {dials_embeddings.iloc[top_match_index]['Patient']}")
99
- # print(f"Therapist's response: {dials_embeddings.iloc[top_match_index+1]['Doctor']}\n\n")
100
  print(f"Therapist's response: {dials_embeddings.iloc[top_match_index]['Doctor']}\n\n")
101
 
102
  return '', therapy_session_conversation, emotions_msg
103
 
104
- # Generate summarization and recommendations for teh session
105
- def summarize_and_recommend():
106
- session_time = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
107
- session_conversation_processed = session_conversation.copy()
108
- session_conversation_processed.insert(0, "Session_time: "+session_time)
109
- session_conversation_processed ='\n'.join(session_conversation_processed)
110
- print("Session conversation:", session_conversation_processed)
111
-
112
- AI71_BASE_URL = "https://api.ai71.ai/v1/"
113
-
114
- client = OpenAI(
115
- api_key=AI71_API_KEY,
116
- base_url=AI71_BASE_URL,
117
- )
118
-
119
- full_summary = ""
120
- for chunk in AI71(AI71_API_KEY).chat.completions.create(
121
- model="tiiuae/falcon-180b-chat",
122
- messages=[
123
- {"role": "system", "content": """You are an Expert Cognitive Behavioural Therapist and Precis writer.
124
- Summarize the below user content <<<session_conversation_processed>>> into useful, ethical, relevant and realistic phrases with a format
125
- Session Time:
126
- Summary of the patient messages: #in two to four sentences
127
- Summary of therapist messages: #in two to three sentences:
128
- Summary of the whole session: # in two to three sentences. Ensure the entire session summary strictly does not exceed 100 tokens."""},
129
- {"role": "user", "content": session_conversation_processed},
130
- ],
131
- stream=True,
132
- ):
133
- if chunk.choices[0].delta.content:
134
- summary = chunk.choices[0].delta.content
135
- # print("Chunk summary:", summary, sep="", end="", flush=True)
136
- full_summary += summary
137
- full_summary = full_summary.replace('User:', '').strip()
138
- print("\n")
139
- print("Full summary:", full_summary)
140
-
141
- full_recommendations = ""
142
- for chunk in AI71(AI71_API_KEY).chat.completions.create(
143
- model="tiiuae/falcon-180b-chat",
144
- messages=[
145
- {"role": "system", "content": """You are an expert Cognitive Behavioural Therapist.
146
- Based on the full summary <<<full_summary>>> provide clinically valid, useful, appropriate action plan for the Patient as a bullted list.
147
- The list shall contain both medical and non medical prescriptions, dos and donts. The format of response shall be in passive voice with proper tense.
148
- - The patient is referred to........ #in one sentence
149
- - The patient is advised to ........ #in one sentence
150
- - The patient is refrained from........ #in one sentence
151
- - It is suggested that tha patient ........ #in one sentence
152
- - Scheduled a follow-up session with the patient........#in one sentence
153
- *Ensure the list contains NOT MORE THAN 7 points"""},
154
- {"role": "user", "content": full_summary},
155
- ],
156
- stream=True,
157
- ):
158
- if chunk.choices[0].delta.content:
159
- rec = chunk.choices[0].delta.content
160
- # print("Chunk recommendation:", rec, sep="", end="", flush=True)
161
- full_recommendations += rec
162
- full_recommendations = full_recommendations.replace('User:', '').strip()
163
- print("\n")
164
- print("Full recommendations:", full_recommendations)
165
- session_conversation=[]
166
- return full_summary, full_recommendations
 
15
  if torch.cuda.is_available():
16
  model = model.to('cuda')
17
 
 
 
18
  dials_embeddings = pd.read_pickle('https://huggingface.co/datasets/vsrinivas/CBT_dialogue_embed_ds/resolve/main/kaggle_therapy_embeddings.pkl')
19
  with open ('emotion_group_labels.txt') as file:
20
  emotion_group_labels = file.read().splitlines()
 
25
  AI71_BASE_URL = "https://api.ai71.ai/v1/"
26
  AI71_API_KEY = os.getenv('AI71_API_KEY')
27
 
28
+ session_conversation=[]
29
+
30
  # Detect emotions from patient dialogues
31
  def detect_emotions(text):
32
  emotion = classifier(text, candidate_labels=emotion_group_labels, batch_size=16)
 
72
  triggers_img = Image.open('triggeres.png')
73
  return triggers_img
74
 
75
+
76
+ class process_session():
77
+ def __init__(self):
78
+ self.session_conversation=[]
79
+
80
+ def get_doc_response_emotions(self, user_message, therapy_session_conversation):
81
+
82
  user_messages = []
83
  user_messages.append(user_message)
84
  emotion_set = detect_emotions(user_message)
 
97
 
98
  therapy_session_conversation.append(["User: "+user_message, "Therapist: "+doc_response])
99
 
100
+ self.session_conversation.extend(["User: "+user_message, "Therapist: "+doc_response])
101
 
102
  print(f"User's message: {user_message}")
103
  print(f"RAG Matching message: {dials_embeddings.iloc[top_match_index]['Patient']}")
 
104
  print(f"Therapist's response: {dials_embeddings.iloc[top_match_index]['Doctor']}\n\n")
105
 
106
  return '', therapy_session_conversation, emotions_msg
107
 
108
+ def summarize_and_recommend(self):
109
+
110
+ session_time = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
111
+ session_conversation_processed = self.session_conversation.copy()
112
+ session_conversation_processed.insert(0, "Session_time: "+session_time)
113
+ session_conversation_processed ='\n'.join(session_conversation_processed)
114
+ print("Session conversation:", session_conversation_processed)
115
+
116
+ full_summary = ""
117
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
118
+ model="tiiuae/falcon-180b-chat",
119
+ messages=[
120
+ {"role": "system", "content": """You are an Expert Cognitive Behavioural Therapist and Precis writer.
121
+ Summarize the below user content <<<session_conversation_processed>>> into useful, ethical, relevant and realistic phrases with a format
122
+ Session Time:
123
+ Summary of the patient messages: #in two to four sentences
124
+ Summary of therapist messages: #in two to three sentences:
125
+ Summary of the whole session: # in two to three sentences. Ensure the entire session summary strictly does not exceed 100 tokens."""},
126
+ {"role": "user", "content": session_conversation_processed},
127
+ ],
128
+ stream=True,
129
+ ):
130
+ if chunk.choices[0].delta.content:
131
+ summary = chunk.choices[0].delta.content
132
+ full_summary += summary
133
+ full_summary = full_summary.replace('User:', '').strip()
134
+ print("\n")
135
+ print("Full summary:", full_summary)
136
+
137
+ full_recommendations = ""
138
+ for chunk in AI71(AI71_API_KEY).chat.completions.create(
139
+ model="tiiuae/falcon-180b-chat",
140
+ messages=[
141
+ {"role": "system", "content": """You are an expert Cognitive Behavioural Therapist.
142
+ Based on the full summary <<<full_summary>>> provide clinically valid, useful, appropriate action plan for the Patient as a bullted list.
143
+ The list shall contain both medical and non medical prescriptions, dos and donts. The format of response shall be in passive voice with proper tense.
144
+ - The patient is referred to........ #in one sentence
145
+ - The patient is advised to ........ #in one sentence
146
+ - The patient is refrained from........ #in one sentence
147
+ - It is suggested that tha patient ........ #in one sentence
148
+ - Scheduled a follow-up session with the patient........#in one sentence
149
+ *Ensure the list contains NOT MORE THAN 7 points"""},
150
+ {"role": "user", "content": full_summary},
151
+ ],
152
+ stream=True,
153
+ ):
154
+ if chunk.choices[0].delta.content:
155
+ rec = chunk.choices[0].delta.content
156
+ full_recommendations += rec
157
+ full_recommendations = full_recommendations.replace('User:', '').strip()
158
+ print("\n")
159
+ print("Full recommendations:", full_recommendations)
160
+ session_conversation=[]
161
+ return full_summary, full_recommendations