Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
import os
|
2 |
import requests
|
3 |
-
import gradio as gr
|
4 |
|
|
|
5 |
API_URL = "https://api-inference.huggingface.co/models/sunnwei/ClinicalBERT-finetuned-disease"
|
6 |
-
|
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"],
|
@@ -14,48 +14,49 @@ disease_symptoms = {
|
|
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
|
18 |
"covid-19": "Isolate, monitor oxygen levels, and seek medical attention if necessary."
|
19 |
}
|
20 |
|
|
|
21 |
def identify_disease(symptoms):
|
22 |
payload = {"inputs": symptoms}
|
23 |
try:
|
24 |
-
response = requests.post(API_URL, headers=headers, json=payload
|
25 |
results = response.json()
|
26 |
-
|
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.")
|
35 |
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import requests
|
|
|
3 |
|
4 |
+
# Hugging Face Inference API URL for Disease Classification
|
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 |
+
# Symptom mappings and medical advice
|
9 |
disease_symptoms = {
|
10 |
"flu": ["fever", "cough", "sore throat", "fatigue"],
|
11 |
"diabetes": ["increased thirst", "frequent urination", "weight loss", "fatigue"],
|
|
|
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.",
|
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=payload)
|
26 |
results = response.json()
|
27 |
+
return results[0]["label"]
|
|
|
|
|
|
|
28 |
except Exception as e:
|
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 |
if __name__ == "__main__":
|
40 |
+
print("🤖 Welcome to the AI Health Assistant!")
|
41 |
+
|
42 |
+
while True:
|
43 |
+
try:
|
44 |
+
user_input = input("\nEnter your symptoms or a disease name (or type 'exit' to quit): ").strip()
|
45 |
+
if user_input.lower() == "exit":
|
46 |
+
break
|
47 |
+
|
48 |
+
if user_input.lower() in disease_symptoms:
|
49 |
+
symptoms = get_symptoms(user_input)
|
50 |
+
advice = provide_assistance(user_input)
|
51 |
+
print(f"\nSymptoms of {user_input.title()}: {symptoms}")
|
52 |
+
print(f"Medical Assistance: {advice}")
|
53 |
+
else:
|
54 |
+
predicted_disease = identify_disease(user_input)
|
55 |
+
symptoms = get_symptoms(predicted_disease)
|
56 |
+
advice = provide_assistance(predicted_disease)
|
57 |
+
print(f"\nPredicted Disease: {predicted_disease}")
|
58 |
+
print(f"Associated Symptoms: {symptoms}")
|
59 |
+
print(f"Medical Assistance: {advice}")
|
60 |
+
except EOFError:
|
61 |
+
print("\nSession ended.")
|
62 |
+
break
|