Spaces:
Sleeping
Sleeping
chatbot without malayalam
Browse files- app.py +135 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from groq import Groq
|
2 |
+
import gradio as gr
|
3 |
+
from gtts import gTTS
|
4 |
+
import uuid
|
5 |
+
import os
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY_2"))
|
10 |
+
|
11 |
+
# client = Groq(
|
12 |
+
# api_key="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",
|
13 |
+
# )
|
14 |
+
|
15 |
+
|
16 |
+
def initialize_messages():
|
17 |
+
return [{"role": "system",
|
18 |
+
"content": '''You are Dr. HealthBuddy, a highly experienced and professional virtual doctor chatbot with over 40 years of expertise across all medical fields. You provide health-related information, symptom guidance, lifestyle tips, and actionable solutions using a dataset to reference common symptoms and conditions. Your goal is to offer concise, empathetic, and knowledgeable responses tailored to each patient’s needs.
|
19 |
+
|
20 |
+
You only respond to health-related inquiries and strive to provide the best possible guidance. Your responses should include clear explanations, actionable steps, and when necessary, advise patients to seek in-person care from a healthcare provider for a proper diagnosis or treatment. Maintain a friendly, professional, and empathetic tone in all your interactions.
|
21 |
+
|
22 |
+
*Prompt Template:*
|
23 |
+
- *Input*: Patient’s health concerns, including symptoms, questions, or specific issues they mention.
|
24 |
+
- *Response*: Start with a polite acknowledgment of the patient’s concern. Provide a clear, concise explanation and suggest practical, actionable steps based on the dataset. If needed, advise on when to consult a healthcare provider.
|
25 |
+
|
26 |
+
*Examples:*
|
27 |
+
|
28 |
+
- *User:* "I have skin rash and itching. What could it be?"
|
29 |
+
*Response:* "According to the data, skin rash and itching are common symptoms of conditions like fungal infections. You can try keeping the affected area dry and clean, and using over-the-counter antifungal creams. If the rash persists or worsens, please consult a dermatologist."
|
30 |
+
|
31 |
+
- *User:* "What might cause nodal skin eruptions?"
|
32 |
+
*Response:* "Nodal skin eruptions could be linked to conditions such as fungal infections. It's best to monitor the symptoms and avoid scratching. For a proper diagnosis, consider visiting a healthcare provider."
|
33 |
+
|
34 |
+
- *User:* "I am a 22-year-old female diagnosed with hypothyroidism. I've gained 10 kg recently. What should I do?"
|
35 |
+
*Response:* "Hi. You have done well managing your hypothyroidism. For effective weight loss, focus on a balanced diet rich in vegetables, lean proteins, and whole grains. Pair this with regular exercise like brisk walking or yoga. Also, consult your endocrinologist to ensure your thyroid levels are well-controlled. Let me know if you have more questions."
|
36 |
+
|
37 |
+
- *User:* "I’ve been feeling discomfort between my shoulder blades after sitting for long periods. What could this be?"
|
38 |
+
*Response:* "Hello. The discomfort between your shoulder blades could be related to posture or strain. Try adjusting your sitting position and consider ergonomic changes to your workspace. Over-the-counter pain relievers or hot compresses may help. If the pain persists, consult an orthopedic specialist for further evaluation."
|
39 |
+
|
40 |
+
Always ensure the tone remains compassionate, and offer educational insights while stressing that you are not a substitute for professional medical advice. Encourage users to consult a healthcare provider for any serious or persistent health concerns.'''
|
41 |
+
}]
|
42 |
+
|
43 |
+
|
44 |
+
messages_prmt = initialize_messages()
|
45 |
+
|
46 |
+
def customLLMBot(user_input,history):
|
47 |
+
global messages_prmt
|
48 |
+
|
49 |
+
messages_prmt.append({"role": "user", "content": user_input})
|
50 |
+
|
51 |
+
response = client.chat.completions.create(
|
52 |
+
messages=messages_prmt,
|
53 |
+
model="llama3-8b-8192",
|
54 |
+
)
|
55 |
+
print(response)
|
56 |
+
LLM_reply = response.choices[0].message.content
|
57 |
+
messages_prmt.append({"role": "assistant", "content": LLM_reply})
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
audio_file = f"response_{uuid.uuid4().hex}.mp3"
|
62 |
+
|
63 |
+
try:
|
64 |
+
tts = gTTS(LLM_reply, lang='en')
|
65 |
+
tts.save(audio_file)
|
66 |
+
|
67 |
+
|
68 |
+
except Exception as e:
|
69 |
+
return f"Error generating audio: {str(e)}", None
|
70 |
+
|
71 |
+
# Return user input and bot response in tuple format
|
72 |
+
return [(user_input,LLM_reply)],audio_file
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
def chatbot_ui():
|
78 |
+
with gr.Blocks() as demo:
|
79 |
+
gr.Markdown("# Healthcare Chatbot Doctor")
|
80 |
+
|
81 |
+
# Use inline styles if you want custom width for user input
|
82 |
+
with gr.Row():
|
83 |
+
chatbot = gr.Chatbot(label="English Responses")
|
84 |
+
|
85 |
+
user_input = gr.Textbox(label="Ask anything related to your health condition",
|
86 |
+
placeholder="Enter your symptoms here...",
|
87 |
+
elem_id="user-input",
|
88 |
+
lines=1)
|
89 |
+
|
90 |
+
with gr.Row():
|
91 |
+
|
92 |
+
submit_btn = gr.Button("Submit") # Submit button with a send icon
|
93 |
+
clear_btn = gr.Button("Clear")
|
94 |
+
|
95 |
+
with gr.Row():
|
96 |
+
audio_output = gr.Audio(label="Audio Response")
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
# Combine submit button and Enter key functionality
|
102 |
+
submit_action = submit_btn.click(
|
103 |
+
customLLMBot,
|
104 |
+
inputs=[user_input],
|
105 |
+
outputs=[chatbot, audio_output],
|
106 |
+
)
|
107 |
+
|
108 |
+
user_input_action = user_input.submit(
|
109 |
+
customLLMBot,
|
110 |
+
inputs=[user_input],
|
111 |
+
outputs=[chatbot, audio_output],
|
112 |
+
)
|
113 |
+
|
114 |
+
# Reset the textbox after submission
|
115 |
+
for action in [submit_action, user_input_action]:
|
116 |
+
action.then(
|
117 |
+
lambda: "", # Clear input box
|
118 |
+
inputs=[],
|
119 |
+
outputs=user_input,
|
120 |
+
)
|
121 |
+
|
122 |
+
# Clear button functionality
|
123 |
+
clear_btn.click(
|
124 |
+
lambda: ([], "", None, [], None),
|
125 |
+
inputs=[],
|
126 |
+
outputs=[chatbot, user_input, audio_output],
|
127 |
+
)
|
128 |
+
|
129 |
+
return demo
|
130 |
+
|
131 |
+
|
132 |
+
# Launch the chatbot UI
|
133 |
+
chatbot_ui().launch(server_name="0.0.0.0", server_port=7860)
|
134 |
+
|
135 |
+
#chatbot_ui().launch(server_name="localhost", server_port=7860)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
groq
|
2 |
+
gradio
|
3 |
+
gtts
|
4 |
+
|