Update funcs.py
Browse files
funcs.py
CHANGED
|
@@ -11,7 +11,6 @@ import os
|
|
| 11 |
from datetime import datetime
|
| 12 |
from openai import OpenAI
|
| 13 |
from ai71 import AI71
|
| 14 |
-
import gradio as gr
|
| 15 |
|
| 16 |
if torch.cuda.is_available():
|
| 17 |
model = model.to('cuda')
|
|
@@ -23,7 +22,6 @@ with open ('emotion_group_labels.txt') as file:
|
|
| 23 |
embed_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
| 24 |
classifier = pipeline("zero-shot-classification", model ='facebook/bart-large-mnli')
|
| 25 |
|
| 26 |
-
AI71_BASE_URL = "https://api.ai71.ai/v1/"
|
| 27 |
AI71_API_KEY = os.getenv('AI71_API_KEY')
|
| 28 |
|
| 29 |
# Detect emotions from patient dialogues
|
|
@@ -85,13 +83,10 @@ def get_doc_response_emotions(user_message, therapy_session_conversation):
|
|
| 85 |
similarities.append(cosine_distance(user_embedding,v))
|
| 86 |
|
| 87 |
top_match_index = similarities.index(max(similarities))
|
| 88 |
-
# doc_response = dials_embeddings.iloc[top_match_index+1]['Doctor']
|
| 89 |
doc_response = dials_embeddings.iloc[top_match_index]['Doctor']
|
| 90 |
|
| 91 |
therapy_session_conversation.append(["User: "+user_message, "Therapist: "+doc_response])
|
| 92 |
|
| 93 |
-
# session_conversation.extend(["User: "+user_message, "Therapist: "+doc_response])
|
| 94 |
-
|
| 95 |
print(f"User's message: {user_message}")
|
| 96 |
print(f"RAG Matching message: {dials_embeddings.iloc[top_match_index]['Patient']}")
|
| 97 |
print(f"Therapist's response: {dials_embeddings.iloc[top_match_index]['Doctor']}\n\n")
|
|
@@ -99,17 +94,10 @@ def get_doc_response_emotions(user_message, therapy_session_conversation):
|
|
| 99 |
return '', therapy_session_conversation, emotions_msg
|
| 100 |
|
| 101 |
def summarize_and_recommend(therapy_session_conversation):
|
| 102 |
-
print("tcs:", therapy_session_conversation, type(therapy_session_conversation))
|
| 103 |
|
| 104 |
session_time = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
| 105 |
session_conversation = [item[0] for item in therapy_session_conversation]
|
| 106 |
-
print(type(session_conversation), session_conversation)
|
| 107 |
session_conversation = [x for x in session_conversation if x is not None]
|
| 108 |
-
|
| 109 |
-
# session_conversation_processed = [session_time] + therapy_session_conversation
|
| 110 |
-
# session_conversation_processed = session_conversation.copy()
|
| 111 |
-
# session_conversation_processed.insert(0, "Session_time: "+session_time)
|
| 112 |
-
# session_conversation_processed ='\n'.join(session_conversation_processed)
|
| 113 |
|
| 114 |
session_conversation.insert(0, "Session_time: "+session_time)
|
| 115 |
|
|
@@ -161,95 +149,4 @@ def summarize_and_recommend(therapy_session_conversation):
|
|
| 161 |
print("\n")
|
| 162 |
print("Full recommendations:", full_recommendations)
|
| 163 |
chatbox=[]
|
| 164 |
-
return full_summary, full_recommendations, chatbox
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
# class process_session():
|
| 171 |
-
# def __init__(self):
|
| 172 |
-
# self.session_conversation=[]
|
| 173 |
-
|
| 174 |
-
# def get_doc_response_emotions(self, user_message, therapy_session_conversation):
|
| 175 |
-
|
| 176 |
-
# user_messages = []
|
| 177 |
-
# user_messages.append(user_message)
|
| 178 |
-
# emotion_set = detect_emotions(user_message)
|
| 179 |
-
# print(emotion_set)
|
| 180 |
-
|
| 181 |
-
# emotions_msg = generate_triggers_img(emotion_set)
|
| 182 |
-
# user_embedding = embed_model.encode(user_message, device='cuda' if torch.cuda.is_available() else 'cpu')
|
| 183 |
-
|
| 184 |
-
# similarities =[]
|
| 185 |
-
# for v in dials_embeddings['embeddings']:
|
| 186 |
-
# similarities.append(cosine_distance(user_embedding,v))
|
| 187 |
-
|
| 188 |
-
# top_match_index = similarities.index(max(similarities))
|
| 189 |
-
# # doc_response = dials_embeddings.iloc[top_match_index+1]['Doctor']
|
| 190 |
-
# doc_response = dials_embeddings.iloc[top_match_index]['Doctor']
|
| 191 |
-
|
| 192 |
-
# therapy_session_conversation.append(["User: "+user_message, "Therapist: "+doc_response])
|
| 193 |
-
|
| 194 |
-
# self.session_conversation.extend(["User: "+user_message, "Therapist: "+doc_response])
|
| 195 |
-
|
| 196 |
-
# print(f"User's message: {user_message}")
|
| 197 |
-
# print(f"RAG Matching message: {dials_embeddings.iloc[top_match_index]['Patient']}")
|
| 198 |
-
# print(f"Therapist's response: {dials_embeddings.iloc[top_match_index]['Doctor']}\n\n")
|
| 199 |
-
|
| 200 |
-
# return '', therapy_session_conversation, emotions_msg
|
| 201 |
-
|
| 202 |
-
# def summarize_and_recommend(self):
|
| 203 |
-
|
| 204 |
-
# session_time = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
| 205 |
-
# session_conversation_processed = self.session_conversation.copy()
|
| 206 |
-
# session_conversation_processed.insert(0, "Session_time: "+session_time)
|
| 207 |
-
# session_conversation_processed ='\n'.join(session_conversation_processed)
|
| 208 |
-
# print("Session conversation:", session_conversation_processed)
|
| 209 |
-
|
| 210 |
-
# full_summary = ""
|
| 211 |
-
# for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
| 212 |
-
# model="tiiuae/falcon-180b-chat",
|
| 213 |
-
# messages=[
|
| 214 |
-
# {"role": "system", "content": """You are an Expert Cognitive Behavioural Therapist and Precis writer.
|
| 215 |
-
# Summarize the below user content <<<session_conversation_processed>>> into useful, ethical, relevant and realistic phrases with a format
|
| 216 |
-
# Session Time:
|
| 217 |
-
# Summary of the patient messages: #in two to four sentences
|
| 218 |
-
# Summary of therapist messages: #in two to three sentences:
|
| 219 |
-
# Summary of the whole session: # in two to three sentences. Ensure the entire session summary strictly does not exceed 100 tokens."""},
|
| 220 |
-
# {"role": "user", "content": session_conversation_processed},
|
| 221 |
-
# ],
|
| 222 |
-
# stream=True,
|
| 223 |
-
# ):
|
| 224 |
-
# if chunk.choices[0].delta.content:
|
| 225 |
-
# summary = chunk.choices[0].delta.content
|
| 226 |
-
# full_summary += summary
|
| 227 |
-
# full_summary = full_summary.replace('User:', '').strip()
|
| 228 |
-
# print("\n")
|
| 229 |
-
# print("Full summary:", full_summary)
|
| 230 |
-
|
| 231 |
-
# full_recommendations = ""
|
| 232 |
-
# for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
| 233 |
-
# model="tiiuae/falcon-180b-chat",
|
| 234 |
-
# messages=[
|
| 235 |
-
# {"role": "system", "content": """You are an expert Cognitive Behavioural Therapist.
|
| 236 |
-
# Based on the full summary <<<full_summary>>> provide clinically valid, useful, appropriate action plan for the Patient as a bullted list.
|
| 237 |
-
# 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.
|
| 238 |
-
# - The patient is referred to........ #in one sentence
|
| 239 |
-
# - The patient is advised to ........ #in one sentence
|
| 240 |
-
# - The patient is refrained from........ #in one sentence
|
| 241 |
-
# - It is suggested that tha patient ........ #in one sentence
|
| 242 |
-
# - Scheduled a follow-up session with the patient........#in one sentence
|
| 243 |
-
# *Ensure the list contains NOT MORE THAN 7 points"""},
|
| 244 |
-
# {"role": "user", "content": full_summary},
|
| 245 |
-
# ],
|
| 246 |
-
# stream=True,
|
| 247 |
-
# ):
|
| 248 |
-
# if chunk.choices[0].delta.content:
|
| 249 |
-
# rec = chunk.choices[0].delta.content
|
| 250 |
-
# full_recommendations += rec
|
| 251 |
-
# full_recommendations = full_recommendations.replace('User:', '').strip()
|
| 252 |
-
# print("\n")
|
| 253 |
-
# print("Full recommendations:", full_recommendations)
|
| 254 |
-
# self.session_conversation=[]
|
| 255 |
-
# return full_summary, full_recommendations
|
|
|
|
| 11 |
from datetime import datetime
|
| 12 |
from openai import OpenAI
|
| 13 |
from ai71 import AI71
|
|
|
|
| 14 |
|
| 15 |
if torch.cuda.is_available():
|
| 16 |
model = model.to('cuda')
|
|
|
|
| 22 |
embed_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
| 23 |
classifier = pipeline("zero-shot-classification", model ='facebook/bart-large-mnli')
|
| 24 |
|
|
|
|
| 25 |
AI71_API_KEY = os.getenv('AI71_API_KEY')
|
| 26 |
|
| 27 |
# Detect emotions from patient dialogues
|
|
|
|
| 83 |
similarities.append(cosine_distance(user_embedding,v))
|
| 84 |
|
| 85 |
top_match_index = similarities.index(max(similarities))
|
|
|
|
| 86 |
doc_response = dials_embeddings.iloc[top_match_index]['Doctor']
|
| 87 |
|
| 88 |
therapy_session_conversation.append(["User: "+user_message, "Therapist: "+doc_response])
|
| 89 |
|
|
|
|
|
|
|
| 90 |
print(f"User's message: {user_message}")
|
| 91 |
print(f"RAG Matching message: {dials_embeddings.iloc[top_match_index]['Patient']}")
|
| 92 |
print(f"Therapist's response: {dials_embeddings.iloc[top_match_index]['Doctor']}\n\n")
|
|
|
|
| 94 |
return '', therapy_session_conversation, emotions_msg
|
| 95 |
|
| 96 |
def summarize_and_recommend(therapy_session_conversation):
|
|
|
|
| 97 |
|
| 98 |
session_time = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
| 99 |
session_conversation = [item[0] for item in therapy_session_conversation]
|
|
|
|
| 100 |
session_conversation = [x for x in session_conversation if x is not None]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
session_conversation.insert(0, "Session_time: "+session_time)
|
| 103 |
|
|
|
|
| 149 |
print("\n")
|
| 150 |
print("Full recommendations:", full_recommendations)
|
| 151 |
chatbox=[]
|
| 152 |
+
return full_summary, full_recommendations, chatbox
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|