# 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()