Health_AI / app.py
Pranith06's picture
Update app.py
2108881 verified
raw
history blame
2.36 kB
import os
import requests
import gradio as gr
API_URL = "https://api-inference.huggingface.co/models/your-medical-model"
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()