Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +60 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
import os
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
# Load API key (from .env or Colab environment)
|
| 8 |
+
load_dotenv()
|
| 9 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 10 |
+
|
| 11 |
+
SYSTEM_PROMPT = (
|
| 12 |
+
"You are a kind and professional healthcare assistant. "
|
| 13 |
+
"Provide general medical information, healthy lifestyle advice, "
|
| 14 |
+
"and explanations for symptoms. "
|
| 15 |
+
"Do NOT give strict diagnoses or prescribe medications. "
|
| 16 |
+
"Always remind users to consult a doctor for confirmation."
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
def chat_with_bot(history, user_message):
|
| 20 |
+
"""Chatbot logic for Gradio"""
|
| 21 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 22 |
+
for h in history:
|
| 23 |
+
messages.append({"role": "user", "content": h[0]})
|
| 24 |
+
messages.append({"role": "assistant", "content": h[1]})
|
| 25 |
+
messages.append({"role": "user", "content": user_message})
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
response = client.chat.completions.create(
|
| 29 |
+
model="gpt-4o-mini",
|
| 30 |
+
messages=messages
|
| 31 |
+
)
|
| 32 |
+
reply = response.choices[0].message.content
|
| 33 |
+
except Exception as e:
|
| 34 |
+
reply = f"⚠️ Error: {str(e)}"
|
| 35 |
+
history.append((user_message, reply))
|
| 36 |
+
return history, ""
|
| 37 |
+
|
| 38 |
+
# Gradio Chat Interface
|
| 39 |
+
chatbot = gr.Chatbot(
|
| 40 |
+
label="🩺 Healthcare Assistant",
|
| 41 |
+
avatar_images=["https://cdn-icons-png.flaticon.com/512/3774/3774299.png", None]
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
demo = gr.Blocks()
|
| 45 |
+
with demo:
|
| 46 |
+
gr.Markdown("# 🩺 AI Healthcare Chatbot")
|
| 47 |
+
gr.Markdown("💬 I'm here to provide **general health information** — not a replacement for a doctor.")
|
| 48 |
+
state = gr.State([])
|
| 49 |
+
with gr.Row():
|
| 50 |
+
chatbot.render()
|
| 51 |
+
with gr.Row():
|
| 52 |
+
msg = gr.Textbox(placeholder="Describe your symptoms or ask a health question...", show_label=False)
|
| 53 |
+
send = gr.Button("Send", variant="primary")
|
| 54 |
+
send.click(chat_with_bot, [state, msg], [state, msg])
|
| 55 |
+
msg.submit(chat_with_bot, [state, msg], [state, msg])
|
| 56 |
+
gr.Markdown("⚠️ **Disclaimer:** This chatbot provides general information and is **not** a substitute for professional medical advice.")
|
| 57 |
+
|
| 58 |
+
# Run
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
openai
|
| 3 |
+
python-dotenv
|