|
import os |
|
import requests |
|
from transformers import pipeline |
|
|
|
|
|
HF_API_TOKEN = os.environ.get("HF_API_TOKEN") |
|
API_URL = "https://api-inference.huggingface.co/models/your-medical-model" |
|
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"} |
|
|
|
|
|
try: |
|
classifier = pipeline("text-classification", model="bert-base-cased") |
|
except: |
|
classifier = None |
|
|
|
|
|
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): |
|
"""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}") |
|
|