Reshmarb commited on
Commit
082c62c
·
1 Parent(s): 1703157

added folder

Browse files
Files changed (1) hide show
  1. app.py +77 -31
app.py CHANGED
@@ -3,61 +3,101 @@ import gradio as gr
3
  from gtts import gTTS
4
  import uuid
5
  import os
6
- from dotenv import load_dotenv
7
 
8
- # Load environment variables
9
- load_dotenv()
10
 
11
- client = Groq(api_key=os.getenv("GROQ_API_KEY"))
 
 
 
 
 
12
 
13
  def initialize_messages():
14
- return [{"role": "system", "content": "You are a virtual healthcare assistant..." }] # Shortened for clarity
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  messages_prmt = initialize_messages()
17
 
18
- def customLLMBot(user_input, history):
19
  global messages_prmt
 
20
  messages_prmt.append({"role": "user", "content": user_input})
21
 
22
- try:
23
- # Call Groq API
24
- response = client.chat.completions.create(
25
- messages=messages_prmt,
26
- model="llama3-8b-8192",
27
- )
28
- LLM_reply = response.choices[0].message.content
29
- messages_prmt.append({"role": "assistant", "content": LLM_reply})
30
- except Exception as e:
31
- return [(user_input, "Error: Unable to fetch response.")], None
32
 
33
- # Generate audio response
34
  audio_file = f"response_{uuid.uuid4().hex}.mp3"
 
35
  try:
36
  tts = gTTS(LLM_reply, lang='en')
37
  tts.save(audio_file)
 
 
38
  except Exception as e:
39
- return [(user_input, LLM_reply)], None
 
 
 
 
 
40
 
41
- return [(user_input, LLM_reply)], audio_file
42
 
43
  def chatbot_ui():
44
  with gr.Blocks() as demo:
45
  gr.Markdown("# Healthcare Chatbot Doctor")
46
 
 
47
  with gr.Row():
48
- chatbot = gr.Chatbot(label="English Responses")
49
- user_input = gr.Textbox(
50
- label="Your Question",
51
- placeholder="Describe your symptoms or ask a health-related question...",
52
- lines=1,
53
- )
54
 
55
  with gr.Row():
56
- submit_btn = gr.Button("Submit")
 
57
  clear_btn = gr.Button("Clear")
58
 
59
- audio_output = gr.Audio(label="Audio Response")
 
 
 
 
60
 
 
61
  submit_action = submit_btn.click(
62
  customLLMBot,
63
  inputs=[user_input],
@@ -70,20 +110,26 @@ def chatbot_ui():
70
  outputs=[chatbot, audio_output],
71
  )
72
 
 
73
  for action in [submit_action, user_input_action]:
74
  action.then(
75
- lambda: "",
76
  inputs=[],
77
  outputs=user_input,
78
  )
79
 
 
80
  clear_btn.click(
81
- lambda: ([], "", None),
82
  inputs=[],
83
  outputs=[chatbot, user_input, audio_output],
84
  )
85
 
86
  return demo
87
 
88
- if __name__ == "__main__":
89
- chatbot_ui().launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
3
  from gtts import gTTS
4
  import uuid
5
  import os
 
6
 
 
 
7
 
8
+ client = Groq(api_key=os.getenv("GROQ_API_KEY_2"))
9
+
10
+ # client = Groq(
11
+ # api_key="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",
12
+ # )
13
+
14
 
15
  def initialize_messages():
16
+ return [{"role": "system",
17
+ "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.
18
+
19
+ 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.
20
+
21
+ *Prompt Template:*
22
+ - *Input*: Patient’s health concerns, including symptoms, questions, or specific issues they mention.
23
+ - *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.
24
+
25
+ *Examples:*
26
+
27
+ - *User:* "I have skin rash and itching. What could it be?"
28
+ *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."
29
+
30
+ - *User:* "What might cause nodal skin eruptions?"
31
+ *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."
32
+
33
+ - *User:* "I am a 22-year-old female diagnosed with hypothyroidism. I've gained 10 kg recently. What should I do?"
34
+ *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."
35
+
36
+ - *User:* "I’ve been feeling discomfort between my shoulder blades after sitting for long periods. What could this be?"
37
+ *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."
38
+
39
+ 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.'''
40
+ }]
41
+
42
 
43
  messages_prmt = initialize_messages()
44
 
45
+ def customLLMBot(user_input,history):
46
  global messages_prmt
47
+
48
  messages_prmt.append({"role": "user", "content": user_input})
49
 
50
+ response = client.chat.completions.create(
51
+ messages=messages_prmt,
52
+ model="llama3-8b-8192",
53
+ )
54
+ print(response)
55
+ LLM_reply = response.choices[0].message.content
56
+ messages_prmt.append({"role": "assistant", "content": LLM_reply})
57
+
58
+
 
59
 
 
60
  audio_file = f"response_{uuid.uuid4().hex}.mp3"
61
+
62
  try:
63
  tts = gTTS(LLM_reply, lang='en')
64
  tts.save(audio_file)
65
+
66
+
67
  except Exception as e:
68
+ return f"Error generating audio: {str(e)}", None
69
+
70
+ # Return user input and bot response in tuple format
71
+ return [(user_input,LLM_reply)],audio_file
72
+
73
+
74
 
 
75
 
76
  def chatbot_ui():
77
  with gr.Blocks() as demo:
78
  gr.Markdown("# Healthcare Chatbot Doctor")
79
 
80
+ # Use inline styles if you want custom width for user input
81
  with gr.Row():
82
+ chatbot = gr.Chatbot(label="English Responses")
83
+
84
+ user_input = gr.Textbox(label="Ask anything related to your health condition",
85
+ placeholder="Enter your symptoms here...",
86
+ elem_id="user-input",
87
+ lines=1)
88
 
89
  with gr.Row():
90
+
91
+ submit_btn = gr.Button("Submit") # Submit button with a send icon
92
  clear_btn = gr.Button("Clear")
93
 
94
+ with gr.Row():
95
+ audio_output = gr.Audio(label="Audio Response")
96
+
97
+
98
+
99
 
100
+ # Combine submit button and Enter key functionality
101
  submit_action = submit_btn.click(
102
  customLLMBot,
103
  inputs=[user_input],
 
110
  outputs=[chatbot, audio_output],
111
  )
112
 
113
+ # Reset the textbox after submission
114
  for action in [submit_action, user_input_action]:
115
  action.then(
116
+ lambda: "", # Clear input box
117
  inputs=[],
118
  outputs=user_input,
119
  )
120
 
121
+ # Clear button functionality
122
  clear_btn.click(
123
+ lambda: ([], "", None, [], None),
124
  inputs=[],
125
  outputs=[chatbot, user_input, audio_output],
126
  )
127
 
128
  return demo
129
 
130
+
131
+ # Launch the chatbot UI
132
+
133
+ chatbot_ui().launch(server_name="0.0.0.0", server_port=7860)
134
+
135
+ #chatbot_ui().launch(server_name="localhost", server_port=7860)