Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,8 @@ import os
|
|
| 3 |
import requests
|
| 4 |
|
| 5 |
# Mistral API setup
|
| 6 |
-
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY", "yQdfM8MLbX9uhInQ7id4iUTwN4h4pDLX")
|
| 7 |
-
MISTRAL_API_URL = "https://api.mistral.ai/v1/chat/completions
|
| 8 |
|
| 9 |
def query_mistral(prompt, max_tokens=1024, temperature=0.7):
|
| 10 |
"""Query Mistral AI API"""
|
|
@@ -22,71 +22,64 @@ def query_mistral(prompt, max_tokens=1024, temperature=0.7):
|
|
| 22 |
|
| 23 |
try:
|
| 24 |
response = requests.post(MISTRAL_API_URL, headers=headers, json=payload, timeout=10)
|
| 25 |
-
response.raise_for_status()
|
| 26 |
return response.json()['choices'][0]['message']['content']
|
| 27 |
except requests.exceptions.RequestException as e:
|
| 28 |
-
return f"
|
| 29 |
except Exception as e:
|
| 30 |
-
return f"
|
| 31 |
|
| 32 |
-
def
|
| 33 |
-
"""
|
| 34 |
-
prompt = f"""
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
3. When to seek immediate care
|
| 44 |
-
|
| 45 |
-
Keep response under 200 words and in simple language."""
|
| 46 |
|
|
|
|
|
|
|
| 47 |
return query_mistral(prompt)
|
| 48 |
|
| 49 |
-
def chatbot_response(message, history):
|
| 50 |
-
"""
|
| 51 |
message_lower = message.lower()
|
| 52 |
-
|
|
|
|
| 53 |
if any(word in message_lower for word in ['hello', 'hi', 'hey']):
|
| 54 |
-
return "Hello! I'm
|
| 55 |
elif any(word in message_lower for word in ['thank', 'thanks']):
|
| 56 |
-
return "You're welcome!
|
| 57 |
-
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
"{message}"
|
| 61 |
-
|
| 62 |
-
Respond with:
|
| 63 |
-
1. Helpful medical information (but no diagnoses)
|
| 64 |
-
2. Questions to clarify symptoms if needed
|
| 65 |
-
3. Recommendation to see a doctor when appropriate
|
| 66 |
-
|
| 67 |
-
Keep response under 150 words and friendly."""
|
| 68 |
-
|
| 69 |
-
return query_mistral(prompt)
|
| 70 |
|
| 71 |
# Gradio Interface
|
| 72 |
-
with gr.Blocks(title="Medical
|
| 73 |
-
gr.Markdown("#
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
with gr.Tabs():
|
| 76 |
-
with gr.TabItem("Chat with
|
| 77 |
-
gr.Markdown("## Ask
|
| 78 |
chatbot = gr.ChatInterface(
|
| 79 |
-
fn=chatbot_response,
|
| 80 |
examples=[
|
| 81 |
-
"
|
| 82 |
-
"
|
| 83 |
-
"
|
| 84 |
],
|
| 85 |
-
title="
|
| 86 |
)
|
| 87 |
|
| 88 |
-
with gr.TabItem("Symptom
|
| 89 |
-
gr.Markdown("##
|
| 90 |
with gr.Row():
|
| 91 |
with gr.Column():
|
| 92 |
age = gr.Number(label="Your Age", minimum=1, maximum=120)
|
|
@@ -97,12 +90,15 @@ with gr.Blocks(title="Medical Chatbot", theme=gr.themes.Soft()) as demo:
|
|
| 97 |
label="How long have you had these symptoms?"
|
| 98 |
)
|
| 99 |
severity = gr.Slider(1, 10, step=1, label="Severity (1-10)")
|
| 100 |
-
analyze_btn = gr.Button("Analyze Symptoms", variant="primary")
|
| 101 |
with gr.Column():
|
| 102 |
-
analysis_output = gr.Textbox(label="
|
| 103 |
|
| 104 |
analyze_btn.click(
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
| 106 |
inputs=[age, gender, symptoms, duration, severity],
|
| 107 |
outputs=analysis_output
|
| 108 |
)
|
|
|
|
| 3 |
import requests
|
| 4 |
|
| 5 |
# Mistral API setup
|
| 6 |
+
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY", "yQdfM8MLbX9uhInQ7id4iUTwN4h4pDLX") # Replace with your actual API key
|
| 7 |
+
MISTRAL_API_URL = "https://api.mistral.ai/v1/chat/completions" # Removed the trailing space
|
| 8 |
|
| 9 |
def query_mistral(prompt, max_tokens=1024, temperature=0.7):
|
| 10 |
"""Query Mistral AI API"""
|
|
|
|
| 22 |
|
| 23 |
try:
|
| 24 |
response = requests.post(MISTRAL_API_URL, headers=headers, json=payload, timeout=10)
|
| 25 |
+
response.raise_for_status()
|
| 26 |
return response.json()['choices'][0]['message']['content']
|
| 27 |
except requests.exceptions.RequestException as e:
|
| 28 |
+
return f"Sorry, I'm having trouble connecting to my knowledge base. Please try again later. Error: {str(e)}"
|
| 29 |
except Exception as e:
|
| 30 |
+
return f"An unexpected error occurred. Please rephrase your question or try again later. Error: {str(e)}"
|
| 31 |
|
| 32 |
+
def personalize_response(name, message):
|
| 33 |
+
"""Personalize the chatbot response"""
|
| 34 |
+
prompt = f"""You are a friendly and empathetic personal medical assistant named Dr. Alex.
|
| 35 |
+
The user {name} asked: "{message}"
|
| 36 |
+
|
| 37 |
+
Respond with:
|
| 38 |
+
1. A caring, personalized greeting using the user's name
|
| 39 |
+
2. Helpful medical information (but no definitive diagnoses)
|
| 40 |
+
3. Follow-up questions if more information is needed
|
| 41 |
+
4. Clear recommendation on when to see a real doctor
|
| 42 |
+
5. A warm closing
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
Keep the response under 200 words, conversational, and supportive.
|
| 45 |
+
"""
|
| 46 |
return query_mistral(prompt)
|
| 47 |
|
| 48 |
+
def chatbot_response(message, history, name):
|
| 49 |
+
"""Personal medical assistant response function"""
|
| 50 |
message_lower = message.lower()
|
| 51 |
+
|
| 52 |
+
# Simple greetings
|
| 53 |
if any(word in message_lower for word in ['hello', 'hi', 'hey']):
|
| 54 |
+
return f"Hello {name}! I'm Dr. Alex, your personal medical assistant. How can I help you today?"
|
| 55 |
elif any(word in message_lower for word in ['thank', 'thanks']):
|
| 56 |
+
return f"You're very welcome, {name}! I'm happy to help. Is there anything else you'd like to discuss?"
|
| 57 |
+
|
| 58 |
+
# Personalize medical responses
|
| 59 |
+
return personalize_response(name, message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# Gradio Interface
|
| 62 |
+
with gr.Blocks(title="Personal Medical Assistant", theme=gr.themes.Soft()) as demo:
|
| 63 |
+
gr.Markdown("# 👨⚕️ Your Personal Medical Assistant")
|
| 64 |
+
|
| 65 |
+
# Get user's name for personalization
|
| 66 |
+
name = gr.Textbox(label="Your Name", placeholder="Please enter your name for personalized care")
|
| 67 |
|
| 68 |
with gr.Tabs():
|
| 69 |
+
with gr.TabItem("Chat with Dr. Alex"):
|
| 70 |
+
gr.Markdown("## Ask me anything about your health")
|
| 71 |
chatbot = gr.ChatInterface(
|
| 72 |
+
fn=lambda msg, hist: chatbot_response(msg, hist, name.value),
|
| 73 |
examples=[
|
| 74 |
+
"I've had a headache for 2 days",
|
| 75 |
+
"What are symptoms of dehydration?",
|
| 76 |
+
"How can I improve my sleep?"
|
| 77 |
],
|
| 78 |
+
title="Dr. Alex - Your Personal Health Assistant"
|
| 79 |
)
|
| 80 |
|
| 81 |
+
with gr.TabItem("Symptom Analysis"):
|
| 82 |
+
gr.Markdown(f"## {name.value}, let's analyze your symptoms")
|
| 83 |
with gr.Row():
|
| 84 |
with gr.Column():
|
| 85 |
age = gr.Number(label="Your Age", minimum=1, maximum=120)
|
|
|
|
| 90 |
label="How long have you had these symptoms?"
|
| 91 |
)
|
| 92 |
severity = gr.Slider(1, 10, step=1, label="Severity (1-10)")
|
| 93 |
+
analyze_btn = gr.Button("Analyze My Symptoms", variant="primary")
|
| 94 |
with gr.Column():
|
| 95 |
+
analysis_output = gr.Textbox(label="Dr. Alex's Analysis", lines=10, interactive=False)
|
| 96 |
|
| 97 |
analyze_btn.click(
|
| 98 |
+
lambda age, gender, symptoms, duration, severity: personalize_response(
|
| 99 |
+
name.value,
|
| 100 |
+
f"As a {age}-year-old {gender}, I've had these symptoms: {symptoms} for {duration} with severity {severity}/10. What could this be?"
|
| 101 |
+
),
|
| 102 |
inputs=[age, gender, symptoms, duration, severity],
|
| 103 |
outputs=analysis_output
|
| 104 |
)
|