Pranith06 commited on
Commit
2108881
Β·
verified Β·
1 Parent(s): 180d87d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -45
app.py CHANGED
@@ -1,26 +1,17 @@
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.",
@@ -28,20 +19,16 @@ medical_guidelines = {
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.")
@@ -49,32 +36,26 @@ def get_symptoms(disease):
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}")
 
1
  import os
2
  import requests
3
+ import gradio as gr
4
 
5
+ API_URL = "https://api-inference.huggingface.co/models/your-medical-model"
6
  HF_API_TOKEN = os.environ.get("HF_API_TOKEN")
 
7
  headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
8
 
 
 
 
 
 
 
 
9
  disease_symptoms = {
10
  "flu": ["fever", "cough", "sore throat", "fatigue"],
11
  "diabetes": ["increased thirst", "frequent urination", "weight loss", "fatigue"],
12
  "covid-19": ["fever", "dry cough", "loss of taste or smell", "difficulty breathing"]
13
  }
14
 
 
15
  medical_guidelines = {
16
  "flu": "Drink warm fluids, rest, and take antiviral medication if severe.",
17
  "diabetes": "Monitor blood sugar, maintain a healthy diet, and exercise regularly.",
 
19
  }
20
 
21
  def identify_disease(symptoms):
 
22
  payload = {"inputs": symptoms}
23
  try:
24
  response = requests.post(API_URL, headers=headers, json=payload, timeout=10)
25
  results = response.json()
 
26
  if isinstance(results, list) and "label" in results[0]:
27
+ return results[0]["label"].lower()
 
28
  else:
29
  return "unknown"
30
  except Exception as e:
31
+ return "Error contacting API."
 
32
 
33
  def get_symptoms(disease):
34
  return disease_symptoms.get(disease.lower(), "No data available.")
 
36
  def provide_assistance(disease):
37
  return medical_guidelines.get(disease.lower(), "Consult a healthcare professional.")
38
 
39
+ def assistant(user_input):
40
+ user_input = user_input.lower()
41
+ if user_input in disease_symptoms:
42
+ symptoms = get_symptoms(user_input)
43
+ advice = provide_assistance(user_input)
44
+ return f"Disease: {user_input.capitalize()}\nSymptoms: {', '.join(symptoms)}\nAdvice: {advice}"
45
+ else:
46
+ disease = identify_disease(user_input)
47
+ if disease == "unknown":
48
+ return "Sorry, could not identify the disease. Please consult a healthcare provider."
49
+ symptoms = get_symptoms(disease)
50
+ advice = provide_assistance(disease)
51
+ return f"Predicted Disease: {disease.capitalize()}\nSymptoms: {', '.join(symptoms) if isinstance(symptoms, list) else symptoms}\nAdvice: {advice}"
52
+
53
+ demo = gr.Interface(
54
+ fn=assistant,
55
+ inputs=gr.Textbox(lines=2, placeholder="Enter your symptoms or disease name here..."),
56
+ outputs="text",
57
+ title="AI Health Assistant"
58
+ )
59
 
60
+ if __name__ == "__main__":
61
+ demo.launch()