Upload 2 files
Browse files- app.py +92 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py — HealthSense AI
|
| 2 |
+
# Deployed on Hugging Face Spaces (Gradio SDK)
|
| 3 |
+
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import joblib
|
| 9 |
+
from groq import Groq
|
| 10 |
+
|
| 11 |
+
# --- Load trained ML artifacts ---
|
| 12 |
+
MODEL_DIR = "models"
|
| 13 |
+
model = joblib.load(os.path.join(MODEL_DIR, "disease_model.joblib"))
|
| 14 |
+
encoder = joblib.load(os.path.join(MODEL_DIR, "label_encoder.joblib"))
|
| 15 |
+
|
| 16 |
+
with open(os.path.join(MODEL_DIR, "symptoms.json")) as f:
|
| 17 |
+
symptoms_list = json.load(f)
|
| 18 |
+
|
| 19 |
+
# --- Groq client (reads GROQ_API_KEY from Space secrets) ---
|
| 20 |
+
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def predict_disease(selected_symptoms):
|
| 24 |
+
"""Run the Random Forest classifier on the selected symptoms."""
|
| 25 |
+
input_vec = [[1 if s in selected_symptoms else 0 for s in symptoms_list]]
|
| 26 |
+
proba = model.predict_proba(input_vec)[0]
|
| 27 |
+
top_idx = int(proba.argmax())
|
| 28 |
+
disease = encoder.inverse_transform([top_idx])[0]
|
| 29 |
+
confidence = round(float(proba[top_idx]) * 100, 1)
|
| 30 |
+
return disease, confidence
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def get_ai_guidance(disease, symptoms, confidence):
|
| 34 |
+
"""Ask the LLM for a structured explanation of the predicted disease."""
|
| 35 |
+
prompt = f"""Patient symptoms: {', '.join(symptoms)}.
|
| 36 |
+
ML model predicted: {disease} ({confidence}% confidence).
|
| 37 |
+
|
| 38 |
+
Provide:
|
| 39 |
+
1) What this disease is
|
| 40 |
+
2) Common causes
|
| 41 |
+
3) Severity level
|
| 42 |
+
4) Home care steps
|
| 43 |
+
5) When to see a doctor
|
| 44 |
+
|
| 45 |
+
Keep it clear, empathetic, and under 300 words."""
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
response = groq_client.chat.completions.create(
|
| 49 |
+
model="llama3-8b-8192",
|
| 50 |
+
messages=[{"role": "user", "content": prompt}],
|
| 51 |
+
)
|
| 52 |
+
return response.choices[0].message.content
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return (
|
| 55 |
+
"⚠️ Couldn't reach the AI guidance service right now "
|
| 56 |
+
f"({e}). The ML prediction above is still valid."
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def analyse(selected_symptoms):
|
| 61 |
+
if not selected_symptoms:
|
| 62 |
+
return "⚠️ Please select at least one symptom.", ""
|
| 63 |
+
|
| 64 |
+
disease, confidence = predict_disease(selected_symptoms)
|
| 65 |
+
guidance = get_ai_guidance(disease, selected_symptoms, confidence)
|
| 66 |
+
result = f"### 🩺 Predicted: **{disease}** — {confidence}% confidence"
|
| 67 |
+
return result, guidance
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
DISCLAIMER = (
|
| 71 |
+
"⚠️ **Disclaimer:** HealthSense AI is an educational demo, not a substitute "
|
| 72 |
+
"for professional medical advice. Always consult a qualified doctor for "
|
| 73 |
+
"diagnosis and treatment."
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
with gr.Blocks(title="HealthSense AI", theme=gr.themes.Soft()) as app:
|
| 77 |
+
gr.Markdown("# 🏥 HealthSense AI\nSelect your symptoms for AI-powered disease prediction.")
|
| 78 |
+
|
| 79 |
+
symptom_input = gr.CheckboxGroup(
|
| 80 |
+
choices=symptoms_list, label="Select Symptoms"
|
| 81 |
+
)
|
| 82 |
+
btn = gr.Button("🔍 Analyse Symptoms", variant="primary")
|
| 83 |
+
|
| 84 |
+
ml_output = gr.Markdown(label="ML Prediction")
|
| 85 |
+
ai_output = gr.Markdown(label="AI Medical Guidance")
|
| 86 |
+
|
| 87 |
+
gr.Markdown(DISCLAIMER)
|
| 88 |
+
|
| 89 |
+
btn.click(fn=analyse, inputs=symptom_input, outputs=[ml_output, ai_output])
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
app.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
scikit-learn>=1.3.0
|
| 3 |
+
joblib>=1.3.0
|
| 4 |
+
numpy>=1.24.0
|
| 5 |
+
pandas>=2.0.0
|
| 6 |
+
groq>=0.9.0
|