File size: 11,491 Bytes
14cb0d3
 
 
 
 
 
 
 
 
 
 
 
 
82a135d
14cb0d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5979c4
75f9996
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55acba8
75f9996
 
 
 
 
 
 
3422a89
b952f74
8afa4ac
 
1674fe1
 
8d48f34
f479218
913fa30
dad9e16
 
913fa30
f479218
1674fe1
f479218
1674fe1
8e47518
8afa4ac
 
 
 
 
 
55acba8
8afa4ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55acba8
8afa4ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
644327d
1674fe1
55acba8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import torch
import numpy as np
import io
import matplotlib.pyplot as plt
import pandas as pd
from sentence_transformers import SentenceTransformer
from transformers import pipeline
from datetime import datetime
from PIL import Image
import os
from datetime import datetime
from openai import OpenAI
from ai71 import AI71
import gradio as gr

if torch.cuda.is_available():
    model = model.to('cuda')

dials_embeddings = pd.read_pickle('https://huggingface.co/datasets/vsrinivas/CBT_dialogue_embed_ds/resolve/main/kaggle_therapy_embeddings.pkl')
with open ('emotion_group_labels.txt') as file:
    emotion_group_labels = file.read().splitlines()

embed_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
classifier = pipeline("zero-shot-classification", model ='facebook/bart-large-mnli')

AI71_BASE_URL = "https://api.ai71.ai/v1/"
AI71_API_KEY = os.getenv('AI71_API_KEY')

# Detect emotions from patient dialogues
def detect_emotions(text):
  emotion = classifier(text, candidate_labels=emotion_group_labels, batch_size=16)
  top_5_scores = [i/sum(emotion['scores'][:5]) for i in emotion['scores'][:5]]
  top_5_emotions = emotion['labels'][:5]
  emotion_set = {l: "{:.2%}".format(s) for l, s in zip(top_5_emotions, top_5_scores)}
  return emotion_set

# Measure cosine similarity between a pair of vectors
def cosine_distance(vec1,vec2):
  cosine = (np.dot(vec1, vec2)/(np.linalg.norm(vec1)*np.linalg.norm(vec2)))
  return cosine

# Generate an image of trigger emotions
def generate_triggers_img(items):
    labels = list(items.keys())
    values = [float(v.strip('%')) for v in items.values()]  # Convert to float for plotting

    new_items = {k:v for k, v in zip(labels, values)}
    new_items = dict(sorted(new_items.items(), key=lambda item: item[1]))
    labels = list(new_items.keys())
    values = list(new_items.values())

    fig, ax = plt.subplots(figsize=(10, 6))
    colors = plt.cm.viridis(np.linspace(0, 1, len(labels)))

    bars = ax.barh(labels, values, color=colors)

    for spine in ax.spines.values():
        spine.set_visible(False)

    ax.tick_params(axis='y', labelsize=18)
    ax.xaxis.set_visible(False)
    ax.yaxis.set_ticks_position('none')

    for bar in bars:
        width = bar.get_width()
        ax.text(width, bar.get_y() + bar.get_height()/2, f'{width:.2f}%',
                ha='left', va='center', fontweight='bold', fontsize=18)

    plt.tight_layout()
    plt.savefig('triggeres.png')
    triggers_img = Image.open('triggeres.png')
    return triggers_img

def get_doc_response_emotions(user_message, therapy_session_conversation):
    user_messages = []
    user_messages.append(user_message)
    emotion_set = detect_emotions(user_message)
    print(emotion_set)

    emotions_msg = generate_triggers_img(emotion_set)
    user_embedding = embed_model.encode(user_message, device='cuda' if torch.cuda.is_available() else 'cpu')

    similarities =[]
    for v in dials_embeddings['embeddings']:
      similarities.append(cosine_distance(user_embedding,v))

    top_match_index = similarities.index(max(similarities))
    # doc_response = dials_embeddings.iloc[top_match_index+1]['Doctor']
    doc_response = dials_embeddings.iloc[top_match_index]['Doctor']

    therapy_session_conversation.append(["User: "+user_message, "Therapist: "+doc_response])

    # session_conversation.extend(["User: "+user_message, "Therapist: "+doc_response])

    print(f"User's message: {user_message}")
    print(f"RAG Matching message: {dials_embeddings.iloc[top_match_index]['Patient']}")
    print(f"Therapist's response: {dials_embeddings.iloc[top_match_index]['Doctor']}\n\n")

    return '', therapy_session_conversation, emotions_msg

def summarize_and_recommend(therapy_session_conversation):
    print("tcs:", therapy_session_conversation, type(therapy_session_conversation))

    session_time = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    session_conversation = [item[0] for item in therapy_session_conversation]
    print(type(session_conversation), session_conversation)
    session_conversation = [x for x in session_conversation if x is not None]

    # session_conversation_processed = [session_time] + therapy_session_conversation
    # session_conversation_processed = session_conversation.copy()
    # session_conversation_processed.insert(0, "Session_time: "+session_time)
    # session_conversation_processed ='\n'.join(session_conversation_processed)
    
    session_conversation.insert(0, "Session_time: "+session_time)
    
    session_conversation_processed ='\n'.join(session_conversation)
    print("session_conversation_processed:", session_conversation_processed)

    full_summary = ""
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
        model="tiiuae/falcon-180b-chat",
        messages=[
            {"role": "system", "content": """You are an Expert Cognitive Behavioural Therapist and Precis writer.
            Summarize 'STRICTLY' the below user content <<<session_conversation_processed>>> 'ONLY' into useful, ethical, relevant and realistic phrases with a format
          Session Time:
          Summary of the patient messages: #in two to four sentences
          Summary of therapist messages: #in two to three sentences:
          Summary of the whole session: # in two to three sentences. Ensure the entire session summary strictly does not exceed 100 tokens."""},
            {"role": "user", "content": session_conversation_processed},
        ],
        stream=True,
    ):
        if chunk.choices[0].delta.content:
            summary = chunk.choices[0].delta.content
            full_summary += summary
    full_summary = full_summary.replace('User:', '').strip()
    print("\n")
    print("Full summary:", full_summary)

    full_recommendations = ""
    for chunk in AI71(AI71_API_KEY).chat.completions.create(
        model="tiiuae/falcon-180b-chat",
        messages=[
            {"role": "system", "content": """You are an expert Cognitive Behavioural Therapist.
            Based on 'STRICTLY' the full summary <<<full_summary>>> 'ONLY' provide clinically valid, useful, appropriate action plan for the Patient as a bullted list.
            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.
          - The patient is referred to........ #in one sentence
          - The patient is advised to ........ #in one sentence
          - The patient is refrained from........ #in one sentence
          - It is suggested  that tha patient ........ #in one sentence
          - Scheduled a follow-up session with the patient........#in one sentence
            *Ensure the list contains NOT MORE THAN 7 points"""},
            {"role": "user", "content": full_summary},
        ],
        stream=True,
    ):
        if chunk.choices[0].delta.content:
            rec = chunk.choices[0].delta.content
            full_recommendations += rec
    full_recommendations = full_recommendations.replace('User:', '').strip()
    print("\n")
    print("Full recommendations:", full_recommendations)
    chatbox=[]
    return full_summary, full_recommendations, chatbox


  


# class process_session():
  # def __init__(self):
  #   self.session_conversation=[]

  # def get_doc_response_emotions(self, user_message, therapy_session_conversation):

  #   user_messages = []
  #   user_messages.append(user_message)
  #   emotion_set = detect_emotions(user_message)
  #   print(emotion_set)

  #   emotions_msg = generate_triggers_img(emotion_set)
  #   user_embedding = embed_model.encode(user_message, device='cuda' if torch.cuda.is_available() else 'cpu')

  #   similarities =[]
  #   for v in dials_embeddings['embeddings']:
  #     similarities.append(cosine_distance(user_embedding,v))

  #   top_match_index = similarities.index(max(similarities))
  #   # doc_response = dials_embeddings.iloc[top_match_index+1]['Doctor']
  #   doc_response = dials_embeddings.iloc[top_match_index]['Doctor']

  #   therapy_session_conversation.append(["User: "+user_message, "Therapist: "+doc_response])

  #   self.session_conversation.extend(["User: "+user_message, "Therapist: "+doc_response])

  #   print(f"User's message: {user_message}")
  #   print(f"RAG Matching message: {dials_embeddings.iloc[top_match_index]['Patient']}")
  #   print(f"Therapist's response: {dials_embeddings.iloc[top_match_index]['Doctor']}\n\n")

  #   return '', therapy_session_conversation, emotions_msg

  # def summarize_and_recommend(self):

  #   session_time = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
  #   session_conversation_processed = self.session_conversation.copy()
  #   session_conversation_processed.insert(0, "Session_time: "+session_time)
  #   session_conversation_processed ='\n'.join(session_conversation_processed)
  #   print("Session conversation:", session_conversation_processed)

  #   full_summary = ""
  #   for chunk in AI71(AI71_API_KEY).chat.completions.create(
  #       model="tiiuae/falcon-180b-chat",
  #       messages=[
  #           {"role": "system", "content": """You are an Expert Cognitive Behavioural Therapist and Precis writer.
  #           Summarize the below user content <<<session_conversation_processed>>> into useful, ethical, relevant and realistic phrases with a format
  #         Session Time:
  #         Summary of the patient messages: #in two to four sentences
  #         Summary of therapist messages: #in two to three sentences:
  #         Summary of the whole session: # in two to three sentences. Ensure the entire session summary strictly does not exceed 100 tokens."""},
  #           {"role": "user", "content": session_conversation_processed},
  #       ],
  #       stream=True,
  #   ):
  #       if chunk.choices[0].delta.content:
  #           summary = chunk.choices[0].delta.content
  #           full_summary += summary
  #   full_summary = full_summary.replace('User:', '').strip()
  #   print("\n")
  #   print("Full summary:", full_summary)

  #   full_recommendations = ""
  #   for chunk in AI71(AI71_API_KEY).chat.completions.create(
  #       model="tiiuae/falcon-180b-chat",
  #       messages=[
  #           {"role": "system", "content": """You are an expert Cognitive Behavioural Therapist.
  #           Based on the full summary <<<full_summary>>> provide clinically valid, useful, appropriate action plan for the Patient as a bullted list.
  #           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.
  #         - The patient is referred to........ #in one sentence
  #         - The patient is advised to ........ #in one sentence
  #         - The patient is refrained from........ #in one sentence
  #         - It is suggested  that tha patient ........ #in one sentence
  #         - Scheduled a follow-up session with the patient........#in one sentence
  #           *Ensure the list contains NOT MORE THAN 7 points"""},
  #           {"role": "user", "content": full_summary},
  #       ],
  #       stream=True,
  #   ):
  #       if chunk.choices[0].delta.content:
  #           rec = chunk.choices[0].delta.content
  #           full_recommendations += rec
  #   full_recommendations = full_recommendations.replace('User:', '').strip()
  #   print("\n")
  #   print("Full recommendations:", full_recommendations)
  #   self.session_conversation=[]
  #   return full_summary, full_recommendations