Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +133 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""heai.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1CPgKNfxzP9sPf9nsHmsct1wlUuZL3XpL
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import torch
|
| 12 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 13 |
+
|
| 14 |
+
# Load model and tokenizer
|
| 15 |
+
model_name = "ibm-granite/granite-3.2-2b-instruct"
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 17 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
+
model_name,
|
| 19 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 20 |
+
device_map="auto" if torch.cuda.is_available() else None
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
if tokenizer.pad_token is None:
|
| 24 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 25 |
+
|
| 26 |
+
# Function to generate LLM response
|
| 27 |
+
def generate_response(prompt, max_length=1024):
|
| 28 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
| 29 |
+
if torch.cuda.is_available():
|
| 30 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
outputs = model.generate(
|
| 33 |
+
**inputs,
|
| 34 |
+
max_length=max_length,
|
| 35 |
+
temperature=0.7,
|
| 36 |
+
do_sample=True,
|
| 37 |
+
pad_token_id=tokenizer.eos_token_id
|
| 38 |
+
)
|
| 39 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 40 |
+
response = response.replace(prompt, "").strip()
|
| 41 |
+
return response
|
| 42 |
+
|
| 43 |
+
# Function for disease prediction
|
| 44 |
+
def disease_prediction(symptoms):
|
| 45 |
+
prompt = f"""Based on the following symptoms, provide possible medical conditions and general medication suggestions.
|
| 46 |
+
Always emphasize the importance of consulting a doctor for proper diagnosis.
|
| 47 |
+
|
| 48 |
+
Symptoms: {symptoms}
|
| 49 |
+
|
| 50 |
+
Possible conditions and recommendations:
|
| 51 |
+
|
| 52 |
+
**IMPORTANT: This is for informational purposes only. Please consult a healthcare professional for proper diagnosis and treatment.**
|
| 53 |
+
|
| 54 |
+
Analysis:"""
|
| 55 |
+
return generate_response(prompt, max_length=1200)
|
| 56 |
+
|
| 57 |
+
# Function for treatment plan
|
| 58 |
+
def treatment_plan(condition, age, gender, medical_history):
|
| 59 |
+
prompt = f"""Generate personalized treatment suggestions for the following patient information. Include home remedies and general medication guidelines.
|
| 60 |
+
|
| 61 |
+
Medical Condition: {condition}
|
| 62 |
+
Age: {age}
|
| 63 |
+
Gender: {gender}
|
| 64 |
+
Medical History: {medical_history}
|
| 65 |
+
|
| 66 |
+
Personalized treatment plan including home remedies and medication guidelines:
|
| 67 |
+
|
| 68 |
+
**IMPORTANT: This is for informational purposes only. Please consult a healthcare professional for proper treatment.**
|
| 69 |
+
|
| 70 |
+
Treatment Plan:"""
|
| 71 |
+
return generate_response(prompt, max_length=1200)
|
| 72 |
+
|
| 73 |
+
# Function for chat with patient
|
| 74 |
+
def patient_chat(chat_history, user_input):
|
| 75 |
+
conversation = chat_history + f"\nPatient: {user_input}\nAI:"
|
| 76 |
+
response = generate_response(conversation, max_length=800)
|
| 77 |
+
chat_history += f"\nPatient: {user_input}\nAI: {response}"
|
| 78 |
+
return chat_history, chat_history
|
| 79 |
+
|
| 80 |
+
# Build Gradio app
|
| 81 |
+
with gr.Blocks() as app:
|
| 82 |
+
gr.Markdown("# Medical AI Assistant")
|
| 83 |
+
gr.Markdown("**Disclaimer: This is for informational purposes only. Always consult healthcare professionals for medical advice.**")
|
| 84 |
+
|
| 85 |
+
with gr.Tabs():
|
| 86 |
+
with gr.TabItem("Patient Chat"):
|
| 87 |
+
chat_history = gr.Textbox(label="Conversation", lines=15, value="", interactive=False)
|
| 88 |
+
user_input = gr.Textbox(label="Your Message", placeholder="Describe your symptoms or ask questions...", lines=2)
|
| 89 |
+
send_btn = gr.Button("Send")
|
| 90 |
+
send_btn.click(patient_chat, inputs=[chat_history, user_input], outputs=[chat_history, chat_history])
|
| 91 |
+
|
| 92 |
+
with gr.TabItem("Disease Prediction"):
|
| 93 |
+
with gr.Row():
|
| 94 |
+
with gr.Column():
|
| 95 |
+
symptoms_input = gr.Textbox(
|
| 96 |
+
label="Enter Symptoms",
|
| 97 |
+
placeholder="e.g., fever, headache, cough, fatigue...",
|
| 98 |
+
lines=4
|
| 99 |
+
)
|
| 100 |
+
predict_btn = gr.Button("Analyze Symptoms")
|
| 101 |
+
with gr.Column():
|
| 102 |
+
prediction_output = gr.Textbox(label="Possible Conditions & Recommendations", lines=20)
|
| 103 |
+
predict_btn.click(disease_prediction, inputs=symptoms_input, outputs=prediction_output)
|
| 104 |
+
|
| 105 |
+
with gr.TabItem("Treatment Plans"):
|
| 106 |
+
with gr.Row():
|
| 107 |
+
with gr.Column():
|
| 108 |
+
condition_input = gr.Textbox(
|
| 109 |
+
label="Medical Condition",
|
| 110 |
+
placeholder="e.g., diabetes, hypertension, migraine...",
|
| 111 |
+
lines=2
|
| 112 |
+
)
|
| 113 |
+
age_input = gr.Number(label="Age", value=30)
|
| 114 |
+
gender_input = gr.Dropdown(
|
| 115 |
+
choices=["Male", "Female", "Other"],
|
| 116 |
+
label="Gender",
|
| 117 |
+
value="Male"
|
| 118 |
+
)
|
| 119 |
+
history_input = gr.Textbox(
|
| 120 |
+
label="Medical History",
|
| 121 |
+
placeholder="Previous conditions, allergies, medications or None",
|
| 122 |
+
lines=3
|
| 123 |
+
)
|
| 124 |
+
plan_btn = gr.Button("Generate Treatment Plan")
|
| 125 |
+
with gr.Column():
|
| 126 |
+
plan_output = gr.Textbox(label="Personalized Treatment Plan", lines=20)
|
| 127 |
+
plan_btn.click(
|
| 128 |
+
treatment_plan,
|
| 129 |
+
inputs=[condition_input, age_input, gender_input, history_input],
|
| 130 |
+
outputs=plan_output
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
app.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
transformers
|
| 4 |
+
numpy
|
| 5 |
+
sentencepiece
|
| 6 |
+
|