Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
# Load Hugging Face token (paste yours below if needed)
|
| 9 |
+
os.environ['HF_TOKEN'] = 'HF_TOKEN'
|
| 10 |
+
|
| 11 |
+
model_id = "ibm-granite/granite-3.3-2b-instruct"
|
| 12 |
+
token = os.getenv("HF_TOKEN")
|
| 13 |
+
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, token=token)
|
| 15 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32, token=token)
|
| 16 |
+
|
| 17 |
+
# Core generation function
|
| 18 |
+
def generate_response(prompt):
|
| 19 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 20 |
+
outputs = model.generate(**inputs, max_new_tokens=200, do_sample=True)
|
| 21 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 22 |
+
|
| 23 |
+
# Four feature functions
|
| 24 |
+
def disease_prediction(symptoms):
|
| 25 |
+
prompt = f"Patient has symptoms: {symptoms}. What could be the possible conditions?"
|
| 26 |
+
return generate_response(prompt)
|
| 27 |
+
|
| 28 |
+
def treatment_plan(condition):
|
| 29 |
+
prompt = f"What is the treatment plan for {condition}? Include medications, lifestyle changes, and follow-up."
|
| 30 |
+
return generate_response(prompt)
|
| 31 |
+
|
| 32 |
+
def health_analytics(vitals):
|
| 33 |
+
prompt = f"Analyze this health data and give insights: {vitals}"
|
| 34 |
+
return generate_response(prompt)
|
| 35 |
+
|
| 36 |
+
def patient_chat(query):
|
| 37 |
+
prompt = f"Medical Question: {query}"
|
| 38 |
+
return generate_response(prompt)
|
| 39 |
+
|
| 40 |
+
# Custom CSS
|
| 41 |
+
custom_css = """
|
| 42 |
+
body {
|
| 43 |
+
font-family: 'Segoe UI', sans-serif;
|
| 44 |
+
background-color: #f8f9fa;
|
| 45 |
+
}
|
| 46 |
+
h1, h2 {
|
| 47 |
+
color: #114B5F;
|
| 48 |
+
font-weight: bold;
|
| 49 |
+
}
|
| 50 |
+
.gradio-container {
|
| 51 |
+
padding: 20px !important;
|
| 52 |
+
}
|
| 53 |
+
textarea {
|
| 54 |
+
border-radius: 10px !important;
|
| 55 |
+
border: 1px solid #ccc !important;
|
| 56 |
+
}
|
| 57 |
+
button {
|
| 58 |
+
background-color: #114B5F !important;
|
| 59 |
+
color: white !important;
|
| 60 |
+
border-radius: 8px !important;
|
| 61 |
+
padding: 10px 16px !important;
|
| 62 |
+
}
|
| 63 |
+
.tabitem {
|
| 64 |
+
background-color: #d6ecf3 !important;
|
| 65 |
+
padding: 10px;
|
| 66 |
+
border-radius: 10px;
|
| 67 |
+
}
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
# Gradio Interface
|
| 71 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 72 |
+
gr.Markdown("# π₯ HealthAI - Generative Healthcare Assistant")
|
| 73 |
+
|
| 74 |
+
with gr.Tab("π§ Disease Prediction"):
|
| 75 |
+
with gr.Column():
|
| 76 |
+
symptom_input = gr.Textbox(label="Enter your symptoms")
|
| 77 |
+
disease_output = gr.Textbox(label="Predicted Conditions")
|
| 78 |
+
predict_btn = gr.Button("Predict")
|
| 79 |
+
predict_btn.click(disease_prediction, inputs=symptom_input, outputs=disease_output)
|
| 80 |
+
|
| 81 |
+
with gr.Tab("π Treatment Plans"):
|
| 82 |
+
with gr.Column():
|
| 83 |
+
condition_input = gr.Textbox(label="Enter diagnosed condition")
|
| 84 |
+
treatment_output = gr.Textbox(label="Recommended Treatment")
|
| 85 |
+
treatment_btn = gr.Button("Get Treatment Plan")
|
| 86 |
+
treatment_btn.click(treatment_plan, inputs=condition_input, outputs=treatment_output)
|
| 87 |
+
|
| 88 |
+
with gr.Tab("π Health Analytics"):
|
| 89 |
+
with gr.Column():
|
| 90 |
+
vitals_input = gr.Textbox(label="Enter vitals (e.g., heart rate: 80, BP: 120/80...)")
|
| 91 |
+
analytics_output = gr.Textbox(label="AI Insights")
|
| 92 |
+
analytics_btn = gr.Button("Analyze")
|
| 93 |
+
analytics_btn.click(health_analytics, inputs=vitals_input, outputs=analytics_output)
|
| 94 |
+
|
| 95 |
+
with gr.Tab("π¬ Patient Chat"):
|
| 96 |
+
with gr.Column():
|
| 97 |
+
query_input = gr.Textbox(label="Ask a health-related question")
|
| 98 |
+
chat_output = gr.Textbox(label="Response")
|
| 99 |
+
chat_btn = gr.Button("Ask")
|
| 100 |
+
chat_btn.click(patient_chat, inputs=query_input, outputs=chat_output)
|
| 101 |
+
|
| 102 |
+
demo.launch()
|