Spaces:
Running
Running
import os | |
import torch | |
import gradio as gr | |
from transformers import AutoTokenizer, AutoModelForCausalLM | |
# β Load Model | |
MODEL_NAME = "microsoft/BioGPT" | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
model = AutoModelForCausalLM.from_pretrained( | |
MODEL_NAME, | |
torch_dtype=torch.float16 if device == "cuda" else torch.float32, | |
device_map=device | |
) | |
# β Define chatbot function | |
def chat_with_gpt(user_query): | |
if not user_query.strip(): | |
return "β οΈ Please enter a valid medical question." | |
inputs = tokenizer(user_query, return_tensors="pt").to(device) | |
outputs = model.generate(**inputs, max_new_tokens=100) | |
return tokenizer.decode(outputs[0], skip_special_tokens=True) | |
# β Define Gradio UI | |
with gr.Blocks(theme=gr.themes.Soft()) as app: | |
with gr.Tab("π₯ Medical Chatbot"): | |
gr.Markdown("<h1 style='text-align: center;'>π₯ AI Health Assistant</h1>") | |
user_input = gr.Textbox(label="Enter your medical query:") | |
submit_button = gr.Button("π Get Answer", variant="primary") | |
chatbot_response = gr.Textbox(label="Chatbot Response", interactive=False) | |
submit_button.click(chat_with_gpt, inputs=user_input, outputs=chatbot_response) | |
with gr.Tab("βΉοΈ About"): | |
gr.Markdown("### βΉοΈ About AI Health Assistant") | |
gr.Markdown(""" | |
- This AI chatbot answers **medical-related questions**. | |
- **Not a substitute for professional medical advice**. | |
- It does **not** provide **medications or treatments**. | |
""") | |
with gr.Tab("β FAQ"): | |
gr.Markdown("### β Frequently Asked Questions") | |
gr.Markdown("**1οΈβ£ Can I use this for medical diagnosis?**\n- β No, this is for **informational purposes only**.") | |
gr.Markdown("**2οΈβ£ How accurate are the responses?**\n- π The AI provides answers based on trained medical data but should be cross-checked.") | |
gr.Markdown("**3οΈβ£ Is my data safe?**\n- π Yes, your input is **not stored**.") | |
gr.Markdown("**4οΈβ£ What types of medical questions can I ask?**\n- π‘ You can ask about **symptoms, diseases, and treatments**.") | |
gr.Markdown("**5οΈβ£ Does this AI prescribe medicine?**\n- β No, this chatbot does **not prescribe medications**.") | |
gr.Markdown("**6οΈβ£ Can it provide emergency medical advice?**\n- β οΈ No, always contact a doctor in emergencies.") | |
gr.Markdown("**7οΈβ£ Is it suitable for mental health support?**\n- π§ It provides basic guidance but is **not a replacement for therapy**.") | |
gr.Markdown("**8οΈβ£ How often is the AI updated?**\n- π The AI is updated periodically.") | |
gr.Markdown("**9οΈβ£ Can I trust the medical advice given?**\n- π₯ Always verify with professionals.") | |
gr.Markdown("**π Can I use this chatbot for educational purposes?**\n- π Yes! Great for learning.") | |
# β Launch the app | |
app.launch() |