Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,17 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
HF_API_TOKEN = os.environ.get("HF_API_TOKEN")
|
| 7 |
-
API_URL = "https://api-inference.huggingface.co/models/your-medical-model" # Replace with actual model URL
|
| 8 |
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 9 |
|
| 10 |
-
# Optionally load a local model (useful for fallback/testing)
|
| 11 |
-
try:
|
| 12 |
-
classifier = pipeline("text-classification", model="bert-base-cased")
|
| 13 |
-
except:
|
| 14 |
-
classifier = None
|
| 15 |
-
|
| 16 |
-
# Disease-to-symptoms mapping
|
| 17 |
disease_symptoms = {
|
| 18 |
"flu": ["fever", "cough", "sore throat", "fatigue"],
|
| 19 |
"diabetes": ["increased thirst", "frequent urination", "weight loss", "fatigue"],
|
| 20 |
"covid-19": ["fever", "dry cough", "loss of taste or smell", "difficulty breathing"]
|
| 21 |
}
|
| 22 |
|
| 23 |
-
# Medical advice & precautions
|
| 24 |
medical_guidelines = {
|
| 25 |
"flu": "Drink warm fluids, rest, and take antiviral medication if severe.",
|
| 26 |
"diabetes": "Monitor blood sugar, maintain a healthy diet, and exercise regularly.",
|
|
@@ -28,20 +19,16 @@ medical_guidelines = {
|
|
| 28 |
}
|
| 29 |
|
| 30 |
def identify_disease(symptoms):
|
| 31 |
-
"""Identifies disease based on symptoms using Hugging Face Inference API."""
|
| 32 |
payload = {"inputs": symptoms}
|
| 33 |
try:
|
| 34 |
response = requests.post(API_URL, headers=headers, json=payload, timeout=10)
|
| 35 |
results = response.json()
|
| 36 |
-
|
| 37 |
if isinstance(results, list) and "label" in results[0]:
|
| 38 |
-
|
| 39 |
-
return predicted_disease
|
| 40 |
else:
|
| 41 |
return "unknown"
|
| 42 |
except Exception as e:
|
| 43 |
-
|
| 44 |
-
return "unknown"
|
| 45 |
|
| 46 |
def get_symptoms(disease):
|
| 47 |
return disease_symptoms.get(disease.lower(), "No data available.")
|
|
@@ -49,32 +36,26 @@ def get_symptoms(disease):
|
|
| 49 |
def provide_assistance(disease):
|
| 50 |
return medical_guidelines.get(disease.lower(), "Consult a healthcare professional.")
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
symptoms_list = get_symptoms(disease)
|
| 65 |
-
advice = provide_assistance(disease)
|
| 66 |
-
print(f"\nπ¦ Disease: {disease.capitalize()}")
|
| 67 |
-
print(f"π Symptoms: {', '.join(symptoms_list)}")
|
| 68 |
-
print(f"π‘ Advice: {advice}")
|
| 69 |
-
else:
|
| 70 |
-
print("\nπ Analyzing symptoms, please wait...")
|
| 71 |
-
predicted_disease = identify_disease(user_input)
|
| 72 |
-
|
| 73 |
-
if predicted_disease == "unknown":
|
| 74 |
-
print("β Sorry, we couldn't identify the disease. Please consult a healthcare provider.")
|
| 75 |
-
else:
|
| 76 |
-
symptoms_list = get_symptoms(predicted_disease)
|
| 77 |
-
advice = provide_assistance(predicted_disease)
|
| 78 |
-
print(f"\nπ§ Predicted Disease: {predicted_disease.capitalize()}")
|
| 79 |
-
print(f"π Associated Symptoms: {', '.join(symptoms_list) if isinstance(symptoms_list, list) else symptoms_list}")
|
| 80 |
-
print(f"π‘ Medical Assistance: {advice}")
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
+
API_URL = "https://api-inference.huggingface.co/models/your-medical-model"
|
| 6 |
HF_API_TOKEN = os.environ.get("HF_API_TOKEN")
|
|
|
|
| 7 |
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
disease_symptoms = {
|
| 10 |
"flu": ["fever", "cough", "sore throat", "fatigue"],
|
| 11 |
"diabetes": ["increased thirst", "frequent urination", "weight loss", "fatigue"],
|
| 12 |
"covid-19": ["fever", "dry cough", "loss of taste or smell", "difficulty breathing"]
|
| 13 |
}
|
| 14 |
|
|
|
|
| 15 |
medical_guidelines = {
|
| 16 |
"flu": "Drink warm fluids, rest, and take antiviral medication if severe.",
|
| 17 |
"diabetes": "Monitor blood sugar, maintain a healthy diet, and exercise regularly.",
|
|
|
|
| 19 |
}
|
| 20 |
|
| 21 |
def identify_disease(symptoms):
|
|
|
|
| 22 |
payload = {"inputs": symptoms}
|
| 23 |
try:
|
| 24 |
response = requests.post(API_URL, headers=headers, json=payload, timeout=10)
|
| 25 |
results = response.json()
|
|
|
|
| 26 |
if isinstance(results, list) and "label" in results[0]:
|
| 27 |
+
return results[0]["label"].lower()
|
|
|
|
| 28 |
else:
|
| 29 |
return "unknown"
|
| 30 |
except Exception as e:
|
| 31 |
+
return "Error contacting API."
|
|
|
|
| 32 |
|
| 33 |
def get_symptoms(disease):
|
| 34 |
return disease_symptoms.get(disease.lower(), "No data available.")
|
|
|
|
| 36 |
def provide_assistance(disease):
|
| 37 |
return medical_guidelines.get(disease.lower(), "Consult a healthcare professional.")
|
| 38 |
|
| 39 |
+
def assistant(user_input):
|
| 40 |
+
user_input = user_input.lower()
|
| 41 |
+
if user_input in disease_symptoms:
|
| 42 |
+
symptoms = get_symptoms(user_input)
|
| 43 |
+
advice = provide_assistance(user_input)
|
| 44 |
+
return f"Disease: {user_input.capitalize()}\nSymptoms: {', '.join(symptoms)}\nAdvice: {advice}"
|
| 45 |
+
else:
|
| 46 |
+
disease = identify_disease(user_input)
|
| 47 |
+
if disease == "unknown":
|
| 48 |
+
return "Sorry, could not identify the disease. Please consult a healthcare provider."
|
| 49 |
+
symptoms = get_symptoms(disease)
|
| 50 |
+
advice = provide_assistance(disease)
|
| 51 |
+
return f"Predicted Disease: {disease.capitalize()}\nSymptoms: {', '.join(symptoms) if isinstance(symptoms, list) else symptoms}\nAdvice: {advice}"
|
| 52 |
+
|
| 53 |
+
demo = gr.Interface(
|
| 54 |
+
fn=assistant,
|
| 55 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your symptoms or disease name here..."),
|
| 56 |
+
outputs="text",
|
| 57 |
+
title="AI Health Assistant"
|
| 58 |
+
)
|
| 59 |
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|