-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
126 lines (94 loc) · 4.54 KB
/
app.py
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
# from openai import AsyncOpenAI
import panel as pn
from panel.chat import ChatInterface
from groq import Groq
from langchain.prompts import ChatPromptTemplate
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain_groq import ChatGroq
from langchain_core.messages import SystemMessage
from langchain.memory import ChatMessageHistory
from langchain.prompts import PromptTemplate
pn.extension(design="material")
# aclient = AsyncOpenAI()
client = Groq(
)
histroy=ChatMessageHistory()
memory=ConversationBufferWindowMemory(k=5)
history=""
template_string="""
You are a psychiatrist, Sophia.
Your job is to listen to people, calm them and give good advice and advice against self harm or harming others.
Also, suggest seeing a therapist (once they become calm).
Try diagonizing the person's issue, and provide him a quick remedy for it.
One of the key ways of asking question, is to ask how a user feels (during beginning of session, or when user says he wants to do something that might be radical)
Also, asking why they are feeling sad, depressed is a great way to know the issue they are facing
Never say, you can't help them with something. Saying you can't help a person, looking for therapy is very disheartening
If they are suffering from depression or are not feeling well, ask them why they are feeling such a thing, or suggest something to cheer them up
If user asks you to repeat after them, please repeat what they said.
Always respond kindly, calmly with sympathy and empathy and in no more than 20 words, except if required.
It is important that not more than 2 questions are asked together
{history}
Patient: {input}
Sophia:
"""
prompt_template = ChatPromptTemplate.from_template(template_string)
async def callback(contents, user, instance):
global history
memory=ConversationBufferWindowMemory(k=5)
groq_chat = ChatGroq(
model_name="mixtral-8x7b-32768",
# temperature=0.85
)
conversation = ConversationChain(
llm=groq_chat,
)
customer_message = prompt_template.format_messages(
history=history,
input=contents
)
response = conversation(customer_message)
full_message=""
if response["response"].startswith("Sophia:"):
response["response"]=response["response"].split("Sophia:")[1]
if response["response"].startswith("Sophia says"):
response["response"]=response["response"].split("Sophia says")[1]
if "?" in response["response"]:
full_message=response["response"].split("?")[0]+"?"
else:
full_message = response["response"]
history+="Patient:"+contents+"\n"+"Sophia:"+full_message
message = ""
yield {"user": "Sophia", "value": "I live you", "avatar": "https://res.cloudinary.com/dbctnfsy1/image/upload/v1711417144/hackathon/ploomber/nm2upptc9rciixly6sga.png"}
for chunk in full_message:
part = chunk
if part is not None:
message += part
yield message
chat_interface = pn.chat.ChatInterface(
callback=callback,
user="Malay",
styles={
'max-height':'50vh'
}
)
pn.template.FastListTemplate(
favicon="https://res.cloudinary.com/dbctnfsy1/image/upload/v1711417144/hackathon/ploomber/nm2upptc9rciixly6sga.png",
meta_author="Malay Damani",
meta_keywords="mental health, mental wellness, chatbot, psychiatry, therapy, counseling, emotional support, empathy, advice, self-care",
meta_description = "Engage in supportive conversations with Sophia, a compassionate and knowledgeable mental health chatbot. Sophia is trained to provide guidance, empathy, and resources for managing various mental health challenges such as depression, anxiety, stress, and more. Whether you're seeking advice, emotional support, or simply someone to listen, Sophia is here to help. Explore topics related to self-care, coping strategies, therapy options, and ways to improve your mental well-being. Start a conversation with Sophia today and take a positive step towards better mental health.",
main=[chat_interface,pn.pane.Str(
'Made by Malay Damani 2024-2025.',
styles={'font-size': '12pt','text-align':"center","width":"100%"}
)],
sidebar=[],
busy_indicator=pn.indicators.BooleanStatus(value=False),
title="Chat with Sophia (Mental Health Helper Chatbot)",
raw_css= ["""
div.card-margin:nth-child(1) {
max-height: 80vh;
}
"""],
background_color="#f9f9f9", # Light gray for overall background
header_background="#e91e63" # Dark pink for header background
).servable()