Your AI-powered medical conversation partner with Voice Support
""")
# Disclaimer
gr.HTML("""
ā ļø Medical Disclaimer
Important: This AI assistant is for informational and educational purposes only.
It is NOT a substitute for professional medical advice, diagnosis, or treatment.
Always seek the advice of your physician or other qualified health provider with any questions
you may have regarding a medical condition. Never disregard professional medical advice or
delay in seeking it because of something you have read here.
""")
with gr.Row():
with gr.Column(scale=7):
# Chatbot Interface
chatbot = gr.Chatbot(
height=500,
placeholder="
š Welcome to ChatDoctor!
I'm here to discuss your health concerns. Type or speak your question!
",
show_label=False,
avatar_images=(None, "š¤"),
)
with gr.Row():
msg = gr.Textbox(
placeholder="Type your message here... (e.g., 'I have a headache')",
show_label=False,
scale=9,
container=False
)
submit_btn = gr.Button("Send š¤", scale=1, variant="primary")
with gr.Row():
clear_btn = gr.Button("šļø Clear Chat", scale=1)
retry_btn = gr.Button("š Retry", scale=1)
with gr.Column(scale=3):
# Voice Input Section
gr.HTML("
š¤ Voice Features
")
audio_input = gr.Audio(
sources=["microphone"],
type="filepath",
label="šļø Speak Your Question",
show_download_button=False
)
transcribed_text = gr.Textbox(
label="š Transcribed Text",
placeholder="Your speech will appear here...",
interactive=False,
lines=3
)
send_voice_btn = gr.Button("Send Voice Message š", variant="primary")
gr.Markdown("---")
# Voice Output
tts_enabled = gr.Checkbox(
label="š Enable Text-to-Speech for responses",
value=True,
info="Hear the doctor's response"
)
audio_output = gr.Audio(
label="š AI Response Audio",
autoplay=False,
visible=True
)
# Examples
gr.Examples(
examples=[
"I have a persistent headache for 3 days. What should I do?",
"What are the symptoms of diabetes?",
"How can I improve my sleep quality?",
"I have a fever and sore throat. Should I be concerned?",
"What are some natural ways to reduce stress?",
],
inputs=msg,
label="š” Example Questions"
)
# Settings (collapsed by default)
with gr.Accordion("āļø Advanced Settings", open=False):
temperature_slider = gr.Slider(
minimum=0.1,
maximum=1.0,
value=TEMPERATURE,
step=0.1,
label="Temperature (Creativity)",
info="Higher values make responses more creative but less focused"
)
max_tokens_slider = gr.Slider(
minimum=50,
maximum=500,
value=MAX_NEW_TOKENS,
step=50,
label="Max Response Length",
info="Maximum number of tokens in response"
)
top_k_slider = gr.Slider(
minimum=1,
maximum=100,
value=TOP_K,
step=1,
label="Top K",
info="Limits vocabulary selection"
)
# Footer
gr.HTML("""
""")
# =============================
# Event Handlers
# =============================
def user_message(user_msg, history):
return "", history + [[user_msg, None]], None
def bot_response(history, temp, max_tok, top_k_val, tts_enabled_val):
global TEMPERATURE, MAX_NEW_TOKENS, TOP_K
TEMPERATURE = temp
MAX_NEW_TOKENS = int(max_tok)
TOP_K = int(top_k_val)
user_msg = history[-1][0]
bot_msg = chat_function(user_msg, history[:-1])
history[-1][1] = bot_msg
# Generate audio if TTS is enabled
audio_file = None
if tts_enabled_val and bot_msg and not bot_msg.startswith("Error:"):
audio_file = text_to_speech(bot_msg)
return history, audio_file
def transcribe_audio(audio_file):
"""Transcribe audio to text using Whisper"""
if audio_file is None:
return ""
try:
import whisper
model = whisper.load_model("base")
result = model.transcribe(audio_file)
return result["text"]
except ImportError:
return "Error: Please install whisper: pip install openai-whisper"
except Exception as e:
return f"Transcription error: {str(e)}"
def process_voice_input(audio_file, history, temp, max_tok, top_k_val, tts_enabled_val):
"""Process voice input: transcribe -> send -> get response"""
if audio_file is None:
return history, "", None, None
# Transcribe
transcribed = transcribe_audio(audio_file)
if transcribed.startswith("Error:"):
return history, transcribed, None, None
# Add to chat
history = history + [[transcribed, None]]
# Get response
global TEMPERATURE, MAX_NEW_TOKENS, TOP_K
TEMPERATURE = temp
MAX_NEW_TOKENS = int(max_tok)
TOP_K = int(top_k_val)
bot_msg = chat_function(transcribed, history[:-1])
history[-1][1] = bot_msg
# Generate audio if TTS is enabled
audio_file = None
if tts_enabled_val and bot_msg and not bot_msg.startswith("Error:"):
audio_file = text_to_speech(bot_msg)
return history, transcribed, None, audio_file
# Text input events
msg.submit(
user_message,
[msg, chatbot],
[msg, chatbot, audio_output],
queue=False
).then(
bot_response,
[chatbot, temperature_slider, max_tokens_slider, top_k_slider, tts_enabled],
[chatbot, audio_output]
)
submit_btn.click(
user_message,
[msg, chatbot],
[msg, chatbot, audio_output],
queue=False
).then(
bot_response,
[chatbot, temperature_slider, max_tokens_slider, top_k_slider, tts_enabled],
[chatbot, audio_output]
)
# Voice input events
audio_input.change(
transcribe_audio,
[audio_input],
[transcribed_text]
)
send_voice_btn.click(
process_voice_input,
[audio_input, chatbot, temperature_slider, max_tokens_slider, top_k_slider, tts_enabled],
[chatbot, transcribed_text, audio_input, audio_output]
)
# Clear and retry
clear_btn.click(lambda: (None, None, None), None, [chatbot, audio_output, transcribed_text], queue=False)
retry_btn.click(lambda: None, None, chatbot, queue=False)
# =============================
# Launch Interface
# =============================
if __name__ == "__main__":
print("\nš Launching ChatDoctor Gradio Interface with Voice Support...")
print("\nš¦ Required packages:")
print(" pip install gradio gTTS openai-whisper")
print("\nNote: Whisper will download models on first use (~100MB for base model)\n")
demo.queue()
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
show_error=True
)