Bndo commited on
Commit
7ab460d
Β·
verified Β·
1 Parent(s): 9e08ce6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -25
app.py CHANGED
@@ -1,10 +1,12 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
3
 
4
  # βœ… Connect to Hugging Face Zephyr-7B API
5
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
 
7
- # βœ… Define the system message for a mental health chatbot
8
  MENTAL_HEALTH_PROMPT = (
9
  "You are Bandar AI, an empathetic mental health AI assistant. "
10
  "You provide emotional support, self-care advice, and stress management tips. "
@@ -22,7 +24,24 @@ def is_mental_health_related(message):
22
  ]
23
  return any(word in message.lower() for word in keywords)
24
 
25
- # βœ… Function to handle chat responses
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  def respond(
27
  message,
28
  history: list[tuple[str, str]],
@@ -62,31 +81,43 @@ def respond(
62
  response += token
63
  yield response # βœ… Stream output correctly
64
 
65
- # βœ… Improved Chatbot UI for Mental Health
66
- demo = gr.ChatInterface(
67
- respond,
68
- additional_inputs=[
69
- gr.Textbox(
 
 
 
 
 
 
70
  value=MENTAL_HEALTH_PROMPT,
71
  label="System message (Defines AI behavior)",
72
- ),
73
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
74
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
75
- gr.Slider(
76
- minimum=0.1,
77
- maximum=1.0,
78
- value=0.95,
79
- step=0.05,
80
- label="Top-p (nucleus sampling)",
81
- ),
82
- ],
83
- title="🧠 Bandar AI - Your Mental Health Companion",
84
- description=(
85
- "πŸ’™ Bandar AI is here to support your mental well-being. "
86
- "Chat with me about stress, anxiety, or self-care. "
87
- "I offer guidance, but I'm NOT a medical professional. Always seek expert help when needed."
88
- ),
89
- )
 
 
 
 
 
 
90
 
91
  # βœ… Run the chatbot
92
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import pyttsx3 # For text-to-speech
4
+ import speech_recognition as sr # For speech-to-text
5
 
6
  # βœ… Connect to Hugging Face Zephyr-7B API
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
9
+ # βœ… Mental Health System Message
10
  MENTAL_HEALTH_PROMPT = (
11
  "You are Bandar AI, an empathetic mental health AI assistant. "
12
  "You provide emotional support, self-care advice, and stress management tips. "
 
24
  ]
25
  return any(word in message.lower() for word in keywords)
26
 
27
+ # βœ… Speech-to-Text Function (Voice Input)
28
+ def speech_to_text(audio):
29
+ recognizer = sr.Recognizer()
30
+ with sr.AudioFile(audio) as source:
31
+ audio_data = recognizer.record(source)
32
+ try:
33
+ return recognizer.recognize_google(audio) # Convert speech to text
34
+ except sr.UnknownValueError:
35
+ return "❌ Sorry, I couldn't understand the audio."
36
+
37
+ # βœ… Text-to-Speech Function (Voice Output)
38
+ def text_to_speech(response_text):
39
+ engine = pyttsx3.init()
40
+ engine.save_to_file(response_text, "response.mp3")
41
+ engine.runAndWait()
42
+ return "response.mp3"
43
+
44
+ # βœ… Chat Response Function
45
  def respond(
46
  message,
47
  history: list[tuple[str, str]],
 
81
  response += token
82
  yield response # βœ… Stream output correctly
83
 
84
+ # βœ… Improved Modern UI
85
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
86
+ gr.Markdown(
87
+ """
88
+ # 🧠 Bandar AI - Your Mental Health Companion
89
+ ### πŸ’™ Chat about emotions, anxiety, and self-care. Get support when you need it.
90
+ """
91
+ )
92
+
93
+ with gr.Row():
94
+ system_message = gr.Textbox(
95
  value=MENTAL_HEALTH_PROMPT,
96
  label="System message (Defines AI behavior)",
97
+ interactive=False,
98
+ )
99
+
100
+ chat_interface = gr.ChatInterface(
101
+ respond,
102
+ additional_inputs=[
103
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max tokens"),
104
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
105
+ gr.Slider(
106
+ minimum=0.1,
107
+ maximum=1.0,
108
+ value=0.95,
109
+ step=0.05,
110
+ label="Top-p (nucleus sampling)",
111
+ ),
112
+ ],
113
+ )
114
+
115
+ with gr.Row():
116
+ voice_input = gr.Microphone(label="🎀 Speak", type="filepath")
117
+ voice_output = gr.Audio(label="πŸ”Š AI Response", type="file")
118
+
119
+ voice_input.change(speech_to_text, inputs=voice_input, outputs=chat_interface.input_component)
120
+ chat_interface.output_component.change(text_to_speech, inputs=chat_interface.output_component, outputs=voice_output)
121
 
122
  # βœ… Run the chatbot
123
  if __name__ == "__main__":