File size: 2,120 Bytes
3199293
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# app.py
import gradio as gr
from openai import OpenAI
import os
from dotenv import load_dotenv

# Load API key (from .env or Colab environment)
load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

SYSTEM_PROMPT = (
    "You are a kind and professional healthcare assistant. "
    "Provide general medical information, healthy lifestyle advice, "
    "and explanations for symptoms. "
    "Do NOT give strict diagnoses or prescribe medications. "
    "Always remind users to consult a doctor for confirmation."
)

def chat_with_bot(history, user_message):
    """Chatbot logic for Gradio"""
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]
    for h in history:
        messages.append({"role": "user", "content": h[0]})
        messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": user_message})

    try:
        response = client.chat.completions.create(
            model="gpt-4o-mini",
            messages=messages
        )
        reply = response.choices[0].message.content
    except Exception as e:
        reply = f"⚠️ Error: {str(e)}"
    history.append((user_message, reply))
    return history, ""

# Gradio Chat Interface
chatbot = gr.Chatbot(
    label="🩺 Healthcare Assistant",
    avatar_images=["https://cdn-icons-png.flaticon.com/512/3774/3774299.png", None]
)

demo = gr.Blocks()
with demo:
    gr.Markdown("# 🩺 AI Healthcare Chatbot")
    gr.Markdown("💬 I'm here to provide **general health information** — not a replacement for a doctor.")
    state = gr.State([])
    with gr.Row():
        chatbot.render()
    with gr.Row():
        msg = gr.Textbox(placeholder="Describe your symptoms or ask a health question...", show_label=False)
        send = gr.Button("Send", variant="primary")
    send.click(chat_with_bot, [state, msg], [state, msg])
    msg.submit(chat_with_bot, [state, msg], [state, msg])
    gr.Markdown("⚠️ **Disclaimer:** This chatbot provides general information and is **not** a substitute for professional medical advice.")

# Run
if __name__ == "__main__":
    demo.launch()