Health_AI / app.py
Pranith06's picture
Update app.py
f89dbac verified
raw
history blame
3.35 kB
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}")