Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,80 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
num_return_sequences=1,
|
| 18 |
-
temperature=0.7
|
| 19 |
-
)[0]['generated_text']
|
| 20 |
-
|
| 21 |
-
# Extract just the diagnosis part
|
| 22 |
-
return diagnosis.split("1.", 1)[-1].strip()
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# π Securely get your Hugging Face API token from environment variables
|
| 6 |
+
HF_API_TOKEN = os.environ.get("HF_API_TOKEN")
|
| 7 |
+
API_URL = "https://api-inference.huggingface.co/models/your-medical-model" # Replace with actual model URL
|
| 8 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 9 |
|
| 10 |
+
# Optionally load a local model (useful for fallback/testing)
|
| 11 |
+
try:
|
| 12 |
+
classifier = pipeline("text-classification", model="bert-base-cased")
|
| 13 |
+
except:
|
| 14 |
+
classifier = None
|
| 15 |
|
| 16 |
+
# Disease-to-symptoms mapping
|
| 17 |
+
disease_symptoms = {
|
| 18 |
+
"flu": ["fever", "cough", "sore throat", "fatigue"],
|
| 19 |
+
"diabetes": ["increased thirst", "frequent urination", "weight loss", "fatigue"],
|
| 20 |
+
"covid-19": ["fever", "dry cough", "loss of taste or smell", "difficulty breathing"]
|
| 21 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Medical advice & precautions
|
| 24 |
+
medical_guidelines = {
|
| 25 |
+
"flu": "Drink warm fluids, rest, and take antiviral medication if severe.",
|
| 26 |
+
"diabetes": "Monitor blood sugar, maintain a healthy diet, and exercise regularly.",
|
| 27 |
+
"covid-19": "Isolate, monitor oxygen levels, and seek medical attention if necessary."
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
def identify_disease(symptoms):
|
| 31 |
+
"""Identifies disease based on symptoms using Hugging Face Inference API."""
|
| 32 |
+
payload = {"inputs": symptoms}
|
| 33 |
+
try:
|
| 34 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=10)
|
| 35 |
+
results = response.json()
|
| 36 |
+
|
| 37 |
+
if isinstance(results, list) and "label" in results[0]:
|
| 38 |
+
predicted_disease = results[0]["label"].lower()
|
| 39 |
+
return predicted_disease
|
| 40 |
+
else:
|
| 41 |
+
return "unknown"
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print("API Error:", e)
|
| 44 |
+
return "unknown"
|
| 45 |
+
|
| 46 |
+
def get_symptoms(disease):
|
| 47 |
+
return disease_symptoms.get(disease.lower(), "No data available.")
|
| 48 |
+
|
| 49 |
+
def provide_assistance(disease):
|
| 50 |
+
return medical_guidelines.get(disease.lower(), "Consult a healthcare professional.")
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
print("π€ Welcome to the AI Health Assistant!")
|
| 54 |
|
| 55 |
+
while True:
|
| 56 |
+
user_input = input("\nEnter your symptoms or a disease name (or type 'exit' to quit): ").strip()
|
| 57 |
+
|
| 58 |
+
if user_input.lower() == "exit":
|
| 59 |
+
print("π Thank you for using the AI Health Assistant. Stay safe!")
|
| 60 |
+
break
|
| 61 |
+
|
| 62 |
+
if user_input.lower() in disease_symptoms:
|
| 63 |
+
disease = user_input.lower()
|
| 64 |
+
symptoms_list = get_symptoms(disease)
|
| 65 |
+
advice = provide_assistance(disease)
|
| 66 |
+
print(f"\nπ¦ Disease: {disease.capitalize()}")
|
| 67 |
+
print(f"π Symptoms: {', '.join(symptoms_list)}")
|
| 68 |
+
print(f"π‘ Advice: {advice}")
|
| 69 |
+
else:
|
| 70 |
+
print("\nπ Analyzing symptoms, please wait...")
|
| 71 |
+
predicted_disease = identify_disease(user_input)
|
| 72 |
|
| 73 |
+
if predicted_disease == "unknown":
|
| 74 |
+
print("β Sorry, we couldn't identify the disease. Please consult a healthcare provider.")
|
| 75 |
+
else:
|
| 76 |
+
symptoms_list = get_symptoms(predicted_disease)
|
| 77 |
+
advice = provide_assistance(predicted_disease)
|
| 78 |
+
print(f"\nπ§ Predicted Disease: {predicted_disease.capitalize()}")
|
| 79 |
+
print(f"π Associated Symptoms: {', '.join(symptoms_list) if isinstance(symptoms_list, list) else symptoms_list}")
|
| 80 |
+
print(f"π‘ Medical Assistance: {advice}")
|