Saintadlexx commited on
Commit
43c6f87
·
verified ·
1 Parent(s): bcc19d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -11
app.py CHANGED
@@ -3,7 +3,6 @@ import gradio as gr
3
  from transformers import pipeline
4
 
5
  # Step 2: Load our AI Model
6
- # This time, we are using a text-classification pipeline with our chosen health model.
7
  print("Loading the Health Analysis model...")
8
  health_classifier = pipeline(
9
  "text-classification",
@@ -11,23 +10,39 @@ health_classifier = pipeline(
11
  )
12
  print("Model loaded successfully!")
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  # Step 3: Define the main function for the app
15
- # This function takes the user's symptom text and returns a formatted prediction.
16
  def analyze_symptoms(symptoms):
17
- # Use the pipeline to get the prediction. It returns a list of dictionaries.
18
  predictions = health_classifier(symptoms)
19
- # For this model, the top prediction is the first item in the list.
20
  top_prediction = predictions[0]
21
 
22
- # Extract the predicted disease and the confidence score.
23
- disease = top_prediction['label']
 
24
  confidence_score = top_prediction['score']
25
 
26
- # Return the results in a clear, readable string.
27
  return f"Predicted Condition: {disease}\nConfidence: {confidence_score:.2f}"
28
 
29
- # Step 4: Define the content for our app
30
- # We'll create a title, a detailed description, and a very important disclaimer.
31
  app_title = "Symptom to Disease Classifier"
32
  app_description = """
33
  Enter a list of symptoms, and this AI will predict a possible related medical condition.
@@ -40,7 +55,7 @@ This is NOT a medical diagnostic tool. The predictions are generated by an AI mo
40
  This tool should not be used for any real-world medical decision-making.
41
  """
42
 
43
- # Step 5: Create and Launch the Gradio Web App Interface
44
  app = gr.Interface(
45
  fn=analyze_symptoms,
46
  inputs=gr.Textbox(
@@ -50,7 +65,7 @@ app = gr.Interface(
50
  outputs="text",
51
  title=app_title,
52
  description=app_description,
53
- article=disclaimer, # The 'article' parameter is great for adding extra text like our disclaimer.
54
  examples=[
55
  ["I have a persistent cough, high fever, and body aches."],
56
  ["Feeling very thirsty and needing to urinate frequently."],
 
3
  from transformers import pipeline
4
 
5
  # Step 2: Load our AI Model
 
6
  print("Loading the Health Analysis model...")
7
  health_classifier = pipeline(
8
  "text-classification",
 
10
  )
11
  print("Model loaded successfully!")
12
 
13
+ # NEW: Step 2.5 - Create the Label Dictionary
14
+ # The model outputs generic names like 'label_0'. This dictionary maps them to real disease names.
15
+ label_map = {
16
+ 'label_0': 'Fungal infection', 'label_1': 'Allergy', 'label_2': 'GERD',
17
+ 'label_3': 'Chronic cholestasis', 'label_4': 'Drug Reaction', 'label_5': 'Peptic ulcer disease',
18
+ 'label_6': 'AIDS', 'label_7': 'Diabetes', 'label_8': 'Gastroenteritis',
19
+ 'label_9': 'Bronchial Asthma', 'label_10': 'Hypertension', 'label_11': 'Migraine',
20
+ 'label_12': 'Cervical spondylosis', 'label_13': 'Paralysis (brain hemorrhage)',
21
+ 'label_14': 'Jaundice', 'label_15': 'Malaria', 'label_16': 'Chicken pox',
22
+ 'label_17': 'Dengue', 'label_18': 'Typhoid', 'label_19': 'Hepatitis A',
23
+ 'label_20': 'Hepatitis B', 'label_21': 'Hepatitis C', 'label_22': 'Hepatitis D',
24
+ 'label_23': 'Hepatitis E', 'label_24': 'Alcoholic hepatitis', 'label_25': 'Tuberculosis',
25
+ 'label_26': 'Common Cold', 'label_27': 'Pneumonia', 'label_28': 'Dimorphic hemmorhoids(piles)',
26
+ 'label_29': 'Heart attack', 'label_30': 'Varicose veins', 'label_31': 'Hypothyroidism',
27
+ 'label_32': 'Hyperthyroidism', 'label_33': 'Hypoglycemia', 'label_34': 'Osteoarthristis',
28
+ 'label_35': 'Arthritis', 'label_36': '(vertigo) Paroymsal Positional Vertigo',
29
+ 'label_37': 'Acne', 'label_38': 'Urinary tract infection', 'label_39': 'Psoriasis',
30
+ 'label_40': 'Impetigo'
31
+ }
32
+
33
  # Step 3: Define the main function for the app
 
34
  def analyze_symptoms(symptoms):
 
35
  predictions = health_classifier(symptoms)
 
36
  top_prediction = predictions[0]
37
 
38
+ # MODIFIED: Look up the generic label in our new dictionary
39
+ generic_label = top_prediction['label']
40
+ disease = label_map.get(generic_label, "Unknown Label") # .get() safely handles unknown labels
41
  confidence_score = top_prediction['score']
42
 
 
43
  return f"Predicted Condition: {disease}\nConfidence: {confidence_score:.2f}"
44
 
45
+ # Step 4: Define the content for our app (No changes here)
 
46
  app_title = "Symptom to Disease Classifier"
47
  app_description = """
48
  Enter a list of symptoms, and this AI will predict a possible related medical condition.
 
55
  This tool should not be used for any real-world medical decision-making.
56
  """
57
 
58
+ # Step 5: Create and Launch the Gradio Web App Interface (No changes here)
59
  app = gr.Interface(
60
  fn=analyze_symptoms,
61
  inputs=gr.Textbox(
 
65
  outputs="text",
66
  title=app_title,
67
  description=app_description,
68
+ article=disclaimer,
69
  examples=[
70
  ["I have a persistent cough, high fever, and body aches."],
71
  ["Feeling very thirsty and needing to urinate frequently."],