Update funcs.py
Browse files
funcs.py
CHANGED
@@ -72,99 +72,98 @@ def generate_triggers_img(items):
|
|
72 |
triggers_img = Image.open('triggeres.png')
|
73 |
return triggers_img
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
return full_summary, full_recommendations
|
|
|
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 |
+
global session_conversation
|
78 |
+
|
79 |
+
user_messages = []
|
80 |
+
user_messages.append(user_message)
|
81 |
+
emotion_set = detect_emotions(user_message)
|
82 |
+
print(emotion_set)
|
83 |
+
|
84 |
+
emotions_msg = generate_triggers_img(emotion_set)
|
85 |
+
user_embedding = embed_model.encode(user_message, device='cuda' if torch.cuda.is_available() else 'cpu')
|
86 |
+
|
87 |
+
similarities =[]
|
88 |
+
for v in dials_embeddings['embeddings']:
|
89 |
+
similarities.append(cosine_distance(user_embedding,v))
|
90 |
+
|
91 |
+
top_match_index = similarities.index(max(similarities))
|
92 |
+
# doc_response = dials_embeddings.iloc[top_match_index+1]['Doctor']
|
93 |
+
doc_response = dials_embeddings.iloc[top_match_index]['Doctor']
|
94 |
+
|
95 |
+
therapy_session_conversation.append(["User: "+user_message, "Therapist: "+doc_response])
|
96 |
+
|
97 |
+
session_conversation.extend(["User: "+user_message, "Therapist: "+doc_response])
|
98 |
+
|
99 |
+
print(f"User's message: {user_message}")
|
100 |
+
print(f"RAG Matching message: {dials_embeddings.iloc[top_match_index]['Patient']}")
|
101 |
+
# print(f"Therapist's response: {dials_embeddings.iloc[top_match_index+1]['Doctor']}\n\n")
|
102 |
+
print(f"Therapist's response: {dials_embeddings.iloc[top_match_index]['Doctor']}\n\n")
|
103 |
+
|
104 |
+
return '', therapy_session_conversation, emotions_msg
|
105 |
+
|
106 |
+
# Generate summarization and recommendations for teh session
|
107 |
+
def summarize_and_recommend():
|
108 |
+
global session_conversation
|
109 |
+
session_time = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
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 |
+
print("Session conversation:", session_conversation_processed)
|
114 |
+
|
115 |
+
AI71_BASE_URL = "https://api.ai71.ai/v1/"
|
116 |
+
|
117 |
+
client = OpenAI(
|
118 |
+
api_key=AI71_API_KEY,
|
119 |
+
base_url=AI71_BASE_URL,
|
120 |
+
)
|
121 |
+
|
122 |
+
full_summary = ""
|
123 |
+
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
124 |
+
model="tiiuae/falcon-180b-chat",
|
125 |
+
messages=[
|
126 |
+
{"role": "system", "content": """You are an Expert Cognitive Behavioural Therapist and Precis writer.
|
127 |
+
Summarize the below user content <<<session_conversation_processed>>> into useful, ethical, relevant and realistic phrases with a format
|
128 |
+
Session Time:
|
129 |
+
Summary of the patient messages: #in two to four sentences
|
130 |
+
Summary of therapist messages: #in two to three sentences:
|
131 |
+
Summary of the whole session: # in two to three sentences. Ensure the entire session summary strictly does not exceed 100 tokens."""},
|
132 |
+
{"role": "user", "content": session_conversation_processed},
|
133 |
+
],
|
134 |
+
stream=True,
|
135 |
+
):
|
136 |
+
if chunk.choices[0].delta.content:
|
137 |
+
summary = chunk.choices[0].delta.content
|
138 |
+
# print("Chunk summary:", summary, sep="", end="", flush=True)
|
139 |
+
full_summary += summary
|
140 |
+
full_summary = full_summary.replace('User:', '').strip()
|
141 |
+
print("\n")
|
142 |
+
print("Full summary:", full_summary)
|
143 |
+
|
144 |
+
full_recommendations = ""
|
145 |
+
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
146 |
+
model="tiiuae/falcon-180b-chat",
|
147 |
+
messages=[
|
148 |
+
{"role": "system", "content": """You are an expert Cognitive Behavioural Therapist.
|
149 |
+
Based on the full summary <<<full_summary>>> provide clinically valid, useful, appropriate action plan for the Patient as a bullted list.
|
150 |
+
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.
|
151 |
+
- The patient is referred to........ #in one sentence
|
152 |
+
- The patient is advised to ........ #in one sentence
|
153 |
+
- The patient is refrained from........ #in one sentence
|
154 |
+
- It is suggested that tha patient ........ #in one sentence
|
155 |
+
- Scheduled a follow-up session with the patient........#in one sentence
|
156 |
+
*Ensure the list contains NOT MORE THAN 7 points"""},
|
157 |
+
{"role": "user", "content": full_summary},
|
158 |
+
],
|
159 |
+
stream=True,
|
160 |
+
):
|
161 |
+
if chunk.choices[0].delta.content:
|
162 |
+
rec = chunk.choices[0].delta.content
|
163 |
+
# print("Chunk recommendation:", rec, sep="", end="", flush=True)
|
164 |
+
full_recommendations += rec
|
165 |
+
full_recommendations = full_recommendations.replace('User:', '').strip()
|
166 |
+
print("\n")
|
167 |
+
print("Full recommendations:", full_recommendations)
|
168 |
+
session_conversation=[]
|
169 |
+
return full_summary, full_recommendations
|
|