Delete app.py
Browse files
app.py
DELETED
|
@@ -1,204 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from groq import GroqClient
|
| 3 |
-
|
| 4 |
-
# ==============================
|
| 5 |
-
# Initialize Groq client
|
| 6 |
-
# ==============================
|
| 7 |
-
client = GroqClient(api_key="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")
|
| 8 |
-
|
| 9 |
-
# ==============================
|
| 10 |
-
# System Prompt for Doctor
|
| 11 |
-
# ==============================
|
| 12 |
-
SYSTEM_PROMPT = """
|
| 13 |
-
You are Dr. HealBot, a calm, knowledgeable, and empathetic doctor talking to a patient.
|
| 14 |
-
|
| 15 |
-
GOAL:
|
| 16 |
-
Have a natural conversation — ask 3-4 short medical questions to understand the patient's condition,
|
| 17 |
-
then start giving practical advice including:
|
| 18 |
-
- possible over-the-counter medicines (generic name only)
|
| 19 |
-
- simple lifestyle or habit changes
|
| 20 |
-
- nutrition or exercise guidance
|
| 21 |
-
- when to see a real doctor
|
| 22 |
-
|
| 23 |
-
TONE & STYLE:
|
| 24 |
-
- Speak like a real doctor, short and direct sentences (1-2 lines max).
|
| 25 |
-
- Be warm but professional.
|
| 26 |
-
- Use plain language — no medical jargon unless necessary.
|
| 27 |
-
- No bullet points or lists — just natural speech.
|
| 28 |
-
- Only one question per response, until enough info is gathered.
|
| 29 |
-
- After about 4 patient answers, switch to giving advice.
|
| 30 |
-
|
| 31 |
-
CONVERSATION FLOW EXAMPLE:
|
| 32 |
-
Doctor: How can I help you?
|
| 33 |
-
Patient: I’ve had a cough for 2 weeks.
|
| 34 |
-
Doctor: Is it dry or with phlegm?
|
| 35 |
-
Patient: With phlegm.
|
| 36 |
-
Doctor: Do you have fever or chest pain?
|
| 37 |
-
Patient: Mild fever.
|
| 38 |
-
Doctor: Do you smoke or have allergies?
|
| 39 |
-
Patient: I smoke.
|
| 40 |
-
Doctor: Sounds like a mild chest infection. You can try paracetamol for fever and warm fluids.
|
| 41 |
-
Cut down on smoking and rest. If symptoms persist beyond 5 days, see a doctor.
|
| 42 |
-
|
| 43 |
-
ALWAYS END with a gentle reminder:
|
| 44 |
-
"Please consult a qualified doctor if it doesn’t improve or if symptoms worsen."
|
| 45 |
-
"""
|
| 46 |
-
|
| 47 |
-
# ==============================
|
| 48 |
-
# Initial greeting
|
| 49 |
-
# ==============================
|
| 50 |
-
INITIAL_MESSAGE = "How can I help you today?"
|
| 51 |
-
|
| 52 |
-
# ==============================
|
| 53 |
-
# Chat logic
|
| 54 |
-
# ==============================
|
| 55 |
-
def chat_with_doctor(message, history):
|
| 56 |
-
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 57 |
-
|
| 58 |
-
# Build chat history
|
| 59 |
-
for chat in history:
|
| 60 |
-
if isinstance(chat, dict):
|
| 61 |
-
messages.append(chat)
|
| 62 |
-
elif isinstance(chat, (list, tuple)) and len(chat) == 2:
|
| 63 |
-
if chat[0]:
|
| 64 |
-
messages.append({"role": "user", "content": chat[0]})
|
| 65 |
-
if chat[1]:
|
| 66 |
-
messages.append({"role": "assistant", "content": chat[1]})
|
| 67 |
-
|
| 68 |
-
# Add current patient message
|
| 69 |
-
messages.append({"role": "user", "content": message})
|
| 70 |
-
|
| 71 |
-
try:
|
| 72 |
-
# Count how many patient turns have occurred
|
| 73 |
-
patient_turns = sum(1 for chat in history if isinstance(chat, (list, tuple)) and chat[0])
|
| 74 |
-
|
| 75 |
-
# After 4 patient turns, guide the model to provide recommendations
|
| 76 |
-
if patient_turns >= 4:
|
| 77 |
-
messages.append({
|
| 78 |
-
"role": "system",
|
| 79 |
-
"content": (
|
| 80 |
-
"Now begin giving specific recommendations based on the patient's symptoms. "
|
| 81 |
-
"Include possible generic medicines (like paracetamol, ibuprofen, etc.), "
|
| 82 |
-
"lifestyle and nutrition tips, and when to seek medical attention. "
|
| 83 |
-
"Keep it short and empathetic, like a real doctor speaking naturally."
|
| 84 |
-
)
|
| 85 |
-
})
|
| 86 |
-
|
| 87 |
-
# Generate the response using Groq LLM
|
| 88 |
-
chat_completion = client.chat.completions.create(
|
| 89 |
-
messages=messages,
|
| 90 |
-
model="llama-3.3-70b-versatile",
|
| 91 |
-
temperature=0.6,
|
| 92 |
-
max_tokens=120,
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
response = chat_completion.choices[0].message.content.strip()
|
| 96 |
-
|
| 97 |
-
# Append to history
|
| 98 |
-
history.append([message, response])
|
| 99 |
-
return history
|
| 100 |
-
|
| 101 |
-
except Exception as e:
|
| 102 |
-
error_msg = f"⚠️ Error: {str(e)}. Please check your API connection and try again."
|
| 103 |
-
history.append([message, error_msg])
|
| 104 |
-
return history
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
def reset_conversation():
|
| 108 |
-
"""Reset the chat to start fresh"""
|
| 109 |
-
return []
|
| 110 |
-
|
| 111 |
-
# ==============================
|
| 112 |
-
# Custom CSS
|
| 113 |
-
# ==============================
|
| 114 |
-
custom_css = """
|
| 115 |
-
#chatbot {
|
| 116 |
-
height: 600px;
|
| 117 |
-
}
|
| 118 |
-
.gradio-container {
|
| 119 |
-
font-family: 'Arial', sans-serif;
|
| 120 |
-
}
|
| 121 |
-
#warning {
|
| 122 |
-
background-color: #fff3cd;
|
| 123 |
-
border: 1px solid #ffc107;
|
| 124 |
-
border-radius: 8px;
|
| 125 |
-
padding: 15px;
|
| 126 |
-
margin: 10px 0;
|
| 127 |
-
color: #856404;
|
| 128 |
-
}
|
| 129 |
-
"""
|
| 130 |
-
|
| 131 |
-
# ==============================
|
| 132 |
-
# Gradio Interface
|
| 133 |
-
# ==============================
|
| 134 |
-
with gr.Blocks(css=custom_css, title="AI Medical Consultant") as demo:
|
| 135 |
-
gr.Markdown(
|
| 136 |
-
"""
|
| 137 |
-
# 🏥 AI Medical Consultant
|
| 138 |
-
### Realistic Doctor-Patient Conversation • Medicine • Lifestyle • Nutrition
|
| 139 |
-
"""
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
gr.HTML(
|
| 143 |
-
"""
|
| 144 |
-
<div id="warning">
|
| 145 |
-
<strong>⚠️ Medical Disclaimer:</strong><br>
|
| 146 |
-
This AI provides general health information only. It is <b>NOT</b> a substitute for
|
| 147 |
-
professional medical advice, diagnosis, or treatment.<br>
|
| 148 |
-
Always consult qualified healthcare providers for medical concerns.<br>
|
| 149 |
-
For emergencies, call your local emergency number immediately.
|
| 150 |
-
</div>
|
| 151 |
-
"""
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
-
chatbot = gr.Chatbot(
|
| 155 |
-
value=[[None, INITIAL_MESSAGE]],
|
| 156 |
-
elem_id="chatbot",
|
| 157 |
-
height=600,
|
| 158 |
-
show_label=False,
|
| 159 |
-
avatar_images=(None, "https://api.dicebear.com/7.x/bottts/svg?seed=doctor"),
|
| 160 |
-
type="tuples"
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
with gr.Row():
|
| 164 |
-
msg = gr.Textbox(
|
| 165 |
-
placeholder="Describe your symptoms or ask a question...",
|
| 166 |
-
show_label=False,
|
| 167 |
-
scale=9,
|
| 168 |
-
lines=2
|
| 169 |
-
)
|
| 170 |
-
submit_btn = gr.Button("Send 📤", scale=1, variant="primary")
|
| 171 |
-
|
| 172 |
-
with gr.Row():
|
| 173 |
-
clear_btn = gr.Button("🔄 Start New Consultation", variant="secondary")
|
| 174 |
-
|
| 175 |
-
gr.Markdown(
|
| 176 |
-
"""
|
| 177 |
-
### 💡 Tips for Best Results:
|
| 178 |
-
- Be specific about your symptoms (location, severity, duration)
|
| 179 |
-
- Mention any relevant medical history or medications
|
| 180 |
-
- Ask follow-up questions freely
|
| 181 |
-
"""
|
| 182 |
-
)
|
| 183 |
-
|
| 184 |
-
# Event Handlers
|
| 185 |
-
msg.submit(chat_with_doctor, [msg, chatbot], [chatbot]).then(
|
| 186 |
-
lambda: gr.update(value=""), None, [msg]
|
| 187 |
-
)
|
| 188 |
-
submit_btn.click(chat_with_doctor, [msg, chatbot], [chatbot]).then(
|
| 189 |
-
lambda: gr.update(value=""), None, [msg]
|
| 190 |
-
)
|
| 191 |
-
clear_btn.click(reset_conversation, None, [chatbot]).then(
|
| 192 |
-
lambda: [[None, INITIAL_MESSAGE]], None, [chatbot]
|
| 193 |
-
)
|
| 194 |
-
|
| 195 |
-
# ==============================
|
| 196 |
-
# Launch app
|
| 197 |
-
# ==============================
|
| 198 |
-
if __name__ == "__main__":
|
| 199 |
-
demo.launch(
|
| 200 |
-
share=True,
|
| 201 |
-
show_error=True,
|
| 202 |
-
server_name="0.0.0.0",
|
| 203 |
-
server_port=7860
|
| 204 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|