Pranith06 commited on
Commit
d1b1cc5
·
verified ·
1 Parent(s): 2d95008

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -34
app.py CHANGED
@@ -1,11 +1,12 @@
1
  import os
2
  import requests
 
3
 
4
- # Hugging Face Inference API URL for Disease Classification
5
  API_URL = "https://api-inference.huggingface.co/models/sunnwei/ClinicalBERT-finetuned-disease"
6
  headers = {"Authorization": f"Bearer {os.environ['HF_API_TOKEN']}"}
7
 
8
- # Symptom mappings and medical advice
9
  disease_symptoms = {
10
  "flu": ["fever", "cough", "sore throat", "fatigue"],
11
  "diabetes": ["increased thirst", "frequent urination", "weight loss", "fatigue"],
@@ -18,45 +19,39 @@ medical_guidelines = {
18
  "covid-19": "Isolate, monitor oxygen levels, and seek medical attention if necessary."
19
  }
20
 
21
- # Identify disease from symptoms using the Hugging Face API
22
  def identify_disease(symptoms):
23
- payload = {"inputs": symptoms}
24
  try:
25
- response = requests.post(API_URL, headers=headers, json=payload)
26
- results = response.json()
27
- return results[0]["label"]
28
- except Exception as e:
 
29
  return "Error contacting API."
30
 
31
- # Get symptoms for a known disease
32
  def get_symptoms(disease):
33
  return disease_symptoms.get(disease.lower(), "No data available.")
34
 
35
- # Get advice for a known disease
36
  def provide_assistance(disease):
37
  return medical_guidelines.get(disease.lower(), "Consult a healthcare professional.")
38
 
39
- if __name__ == "__main__":
40
- print("🤖 Welcome to the AI Health Assistant!")
41
-
42
- while True:
43
- try:
44
- user_input = input("\nEnter your symptoms or a disease name (or type 'exit' to quit): ").strip()
45
- if user_input.lower() == "exit":
46
- break
47
-
48
- if user_input.lower() in disease_symptoms:
49
- symptoms = get_symptoms(user_input)
50
- advice = provide_assistance(user_input)
51
- print(f"\nSymptoms of {user_input.title()}: {symptoms}")
52
- print(f"Medical Assistance: {advice}")
53
- else:
54
- predicted_disease = identify_disease(user_input)
55
- symptoms = get_symptoms(predicted_disease)
56
- advice = provide_assistance(predicted_disease)
57
- print(f"\nPredicted Disease: {predicted_disease}")
58
- print(f"Associated Symptoms: {symptoms}")
59
- print(f"Medical Assistance: {advice}")
60
- except EOFError:
61
- print("\nSession ended.")
62
- break
 
1
  import os
2
  import requests
3
+ import gradio as gr
4
 
5
+ # Hugging Face Inference API
6
  API_URL = "https://api-inference.huggingface.co/models/sunnwei/ClinicalBERT-finetuned-disease"
7
  headers = {"Authorization": f"Bearer {os.environ['HF_API_TOKEN']}"}
8
 
9
+ # Disease data
10
  disease_symptoms = {
11
  "flu": ["fever", "cough", "sore throat", "fatigue"],
12
  "diabetes": ["increased thirst", "frequent urination", "weight loss", "fatigue"],
 
19
  "covid-19": "Isolate, monitor oxygen levels, and seek medical attention if necessary."
20
  }
21
 
 
22
  def identify_disease(symptoms):
 
23
  try:
24
+ response = requests.post(API_URL, headers=headers, json={"inputs": symptoms})
25
+ response.raise_for_status()
26
+ prediction = response.json()
27
+ return prediction[0]["label"]
28
+ except Exception:
29
  return "Error contacting API."
30
 
 
31
  def get_symptoms(disease):
32
  return disease_symptoms.get(disease.lower(), "No data available.")
33
 
 
34
  def provide_assistance(disease):
35
  return medical_guidelines.get(disease.lower(), "Consult a healthcare professional.")
36
 
37
+ def ai_health_assistant(user_input):
38
+ if not user_input.strip():
39
+ return "Please enter your symptoms."
40
+
41
+ if user_input.lower() in disease_symptoms:
42
+ symptoms = get_symptoms(user_input)
43
+ advice = provide_assistance(user_input)
44
+ return f"✅ Known Disease: **{user_input.title()}**\n\nSymptoms: {symptoms}\nAdvice: {advice}"
45
+ else:
46
+ disease = identify_disease(user_input)
47
+ symptoms = get_symptoms(disease)
48
+ advice = provide_assistance(disease)
49
+ return f"🧠 Predicted Disease: **{disease}**\n\nSymptoms: {symptoms}\nAdvice: {advice}"
50
+
51
+ # Launch Gradio interface
52
+ gr.Interface(fn=ai_health_assistant,
53
+ inputs="text",
54
+ outputs="markdown",
55
+ title="🩺 AI Health Assistant",
56
+ description="Enter symptoms or disease name to get predictions and medical guidance.",
57
+ theme="soft").launch()