Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
import os
|
2 |
import requests
|
|
|
3 |
|
4 |
-
# Hugging Face Inference API
|
5 |
API_URL = "https://api-inference.huggingface.co/models/sunnwei/ClinicalBERT-finetuned-disease"
|
6 |
headers = {"Authorization": f"Bearer {os.environ['HF_API_TOKEN']}"}
|
7 |
|
8 |
-
#
|
9 |
disease_symptoms = {
|
10 |
"flu": ["fever", "cough", "sore throat", "fatigue"],
|
11 |
"diabetes": ["increased thirst", "frequent urination", "weight loss", "fatigue"],
|
@@ -18,45 +19,39 @@ medical_guidelines = {
|
|
18 |
"covid-19": "Isolate, monitor oxygen levels, and seek medical attention if necessary."
|
19 |
}
|
20 |
|
21 |
-
# Identify disease from symptoms using the Hugging Face API
|
22 |
def identify_disease(symptoms):
|
23 |
-
payload = {"inputs": symptoms}
|
24 |
try:
|
25 |
-
response = requests.post(API_URL, headers=headers, json=
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
29 |
return "Error contacting API."
|
30 |
|
31 |
-
# Get symptoms for a known disease
|
32 |
def get_symptoms(disease):
|
33 |
return disease_symptoms.get(disease.lower(), "No data available.")
|
34 |
|
35 |
-
# Get advice for a known disease
|
36 |
def provide_assistance(disease):
|
37 |
return medical_guidelines.get(disease.lower(), "Consult a healthcare professional.")
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
except EOFError:
|
61 |
-
print("\nSession ended.")
|
62 |
-
break
|
|
|
1 |
import os
|
2 |
import requests
|
3 |
+
import gradio as gr
|
4 |
|
5 |
+
# Hugging Face Inference API
|
6 |
API_URL = "https://api-inference.huggingface.co/models/sunnwei/ClinicalBERT-finetuned-disease"
|
7 |
headers = {"Authorization": f"Bearer {os.environ['HF_API_TOKEN']}"}
|
8 |
|
9 |
+
# Disease data
|
10 |
disease_symptoms = {
|
11 |
"flu": ["fever", "cough", "sore throat", "fatigue"],
|
12 |
"diabetes": ["increased thirst", "frequent urination", "weight loss", "fatigue"],
|
|
|
19 |
"covid-19": "Isolate, monitor oxygen levels, and seek medical attention if necessary."
|
20 |
}
|
21 |
|
|
|
22 |
def identify_disease(symptoms):
|
|
|
23 |
try:
|
24 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": symptoms})
|
25 |
+
response.raise_for_status()
|
26 |
+
prediction = response.json()
|
27 |
+
return prediction[0]["label"]
|
28 |
+
except Exception:
|
29 |
return "Error contacting API."
|
30 |
|
|
|
31 |
def get_symptoms(disease):
|
32 |
return disease_symptoms.get(disease.lower(), "No data available.")
|
33 |
|
|
|
34 |
def provide_assistance(disease):
|
35 |
return medical_guidelines.get(disease.lower(), "Consult a healthcare professional.")
|
36 |
|
37 |
+
def ai_health_assistant(user_input):
|
38 |
+
if not user_input.strip():
|
39 |
+
return "Please enter your symptoms."
|
40 |
+
|
41 |
+
if user_input.lower() in disease_symptoms:
|
42 |
+
symptoms = get_symptoms(user_input)
|
43 |
+
advice = provide_assistance(user_input)
|
44 |
+
return f"✅ Known Disease: **{user_input.title()}**\n\nSymptoms: {symptoms}\nAdvice: {advice}"
|
45 |
+
else:
|
46 |
+
disease = identify_disease(user_input)
|
47 |
+
symptoms = get_symptoms(disease)
|
48 |
+
advice = provide_assistance(disease)
|
49 |
+
return f"🧠 Predicted Disease: **{disease}**\n\nSymptoms: {symptoms}\nAdvice: {advice}"
|
50 |
+
|
51 |
+
# Launch Gradio interface
|
52 |
+
gr.Interface(fn=ai_health_assistant,
|
53 |
+
inputs="text",
|
54 |
+
outputs="markdown",
|
55 |
+
title="🩺 AI Health Assistant",
|
56 |
+
description="Enter symptoms or disease name to get predictions and medical guidance.",
|
57 |
+
theme="soft").launch()
|
|
|
|
|
|