File size: 2,381 Bytes
f89dbac
 
2108881
9a243e1
cda88e2
f89dbac
 
98e6dc9
f89dbac
 
 
 
 
05e9d12
f89dbac
 
 
 
 
 
 
 
 
 
 
 
2108881
f89dbac
 
 
2108881
f89dbac
 
 
 
 
 
 
2108881
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f89dbac
2108881
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import requests
import gradio as gr

API_URL = "https://api-inference.huggingface.co/models/sunnwei/ClinicalBERT-finetuned-disease"
HF_API_TOKEN = os.environ.get("HF_API_TOKEN")
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}

disease_symptoms = {
    "flu": ["fever", "cough", "sore throat", "fatigue"],
    "diabetes": ["increased thirst", "frequent urination", "weight loss", "fatigue"],
    "covid-19": ["fever", "dry cough", "loss of taste or smell", "difficulty breathing"]
}

medical_guidelines = {
    "flu": "Drink warm fluids, rest, and take antiviral medication if severe.",
    "diabetes": "Monitor blood sugar, maintain a healthy diet, and exercise regularly.",
    "covid-19": "Isolate, monitor oxygen levels, and seek medical attention if necessary."
}

def identify_disease(symptoms):
    payload = {"inputs": symptoms}
    try:
        response = requests.post(API_URL, headers=headers, json=payload, timeout=10)
        results = response.json()
        if isinstance(results, list) and "label" in results[0]:
            return results[0]["label"].lower()
        else:
            return "unknown"
    except Exception as e:
        return "Error contacting API."

def get_symptoms(disease):
    return disease_symptoms.get(disease.lower(), "No data available.")

def provide_assistance(disease):
    return medical_guidelines.get(disease.lower(), "Consult a healthcare professional.")

def assistant(user_input):
    user_input = user_input.lower()
    if user_input in disease_symptoms:
        symptoms = get_symptoms(user_input)
        advice = provide_assistance(user_input)
        return f"Disease: {user_input.capitalize()}\nSymptoms: {', '.join(symptoms)}\nAdvice: {advice}"
    else:
        disease = identify_disease(user_input)
        if disease == "unknown":
            return "Sorry, could not identify the disease. Please consult a healthcare provider."
        symptoms = get_symptoms(disease)
        advice = provide_assistance(disease)
        return f"Predicted Disease: {disease.capitalize()}\nSymptoms: {', '.join(symptoms) if isinstance(symptoms, list) else symptoms}\nAdvice: {advice}"

demo = gr.Interface(
    fn=assistant,
    inputs=gr.Textbox(lines=2, placeholder="Enter your symptoms or disease name here..."),
    outputs="text",
    title="AI Health Assistant"
)

if __name__ == "__main__":
    demo.launch()