File size: 3,350 Bytes
f89dbac
 
9a243e1
 
f89dbac
 
 
 
98e6dc9
f89dbac
 
 
 
 
98e6dc9
f89dbac
 
 
 
 
 
05e9d12
f89dbac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a243e1
f89dbac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a243e1
f89dbac
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import requests
from transformers import pipeline

# πŸ” Securely get your Hugging Face API token from environment variables
HF_API_TOKEN = os.environ.get("HF_API_TOKEN")
API_URL = "https://api-inference.huggingface.co/models/your-medical-model"  # Replace with actual model URL
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}

# Optionally load a local model (useful for fallback/testing)
try:
    classifier = pipeline("text-classification", model="bert-base-cased")
except:
    classifier = None

# Disease-to-symptoms mapping
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 advice & precautions
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):
    """Identifies disease based on symptoms using Hugging Face Inference API."""
    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]:
            predicted_disease = results[0]["label"].lower()
            return predicted_disease
        else:
            return "unknown"
    except Exception as e:
        print("API Error:", e)
        return "unknown"

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.")

if __name__ == "__main__":
    print("πŸ€– Welcome to the AI Health Assistant!")
    
    while True:
        user_input = input("\nEnter your symptoms or a disease name (or type 'exit' to quit): ").strip()
        
        if user_input.lower() == "exit":
            print("πŸ‘‹ Thank you for using the AI Health Assistant. Stay safe!")
            break

        if user_input.lower() in disease_symptoms:
            disease = user_input.lower()
            symptoms_list = get_symptoms(disease)
            advice = provide_assistance(disease)
            print(f"\n🦠 Disease: {disease.capitalize()}")
            print(f"πŸ“‹ Symptoms: {', '.join(symptoms_list)}")
            print(f"πŸ’‘ Advice: {advice}")
        else:
            print("\nπŸ” Analyzing symptoms, please wait...")
            predicted_disease = identify_disease(user_input)

            if predicted_disease == "unknown":
                print("❌ Sorry, we couldn't identify the disease. Please consult a healthcare provider.")
            else:
                symptoms_list = get_symptoms(predicted_disease)
                advice = provide_assistance(predicted_disease)
                print(f"\n🧠 Predicted Disease: {predicted_disease.capitalize()}")
                print(f"πŸ“‹ Associated Symptoms: {', '.join(symptoms_list) if isinstance(symptoms_list, list) else symptoms_list}")
                print(f"πŸ’‘ Medical Assistance: {advice}")