Zakia commited on
Commit
4281c7b
·
verified ·
1 Parent(s): 4cbdaca

Update app.py - set the categories using learn

Browse files
Files changed (1) hide show
  1. app.py +79 -79
app.py CHANGED
@@ -1,79 +1,79 @@
1
- # Bismillahir Rahmaanir Raheem
2
- # Almadadh Ya Gause Radi Allahu Ta'alah Anh - Ameen
3
-
4
- # Import necessary libraries from fastai and gradio
5
- from fastai.vision.all import *
6
- import gradio as gr
7
-
8
-
9
- # Function to determine if an image contains pneumonia
10
- # Checks if the filename contains 'virus' or 'bacteria'
11
- def is_pneumonia(x):
12
- return (x.find('virus')!=-1 or x.find('bacteria')!=-1)
13
-
14
-
15
- # Load the trained fastai model for predictions
16
- learn = load_learner('pneumonia_model.pkl')
17
-
18
-
19
- # Define the possible categories for prediction
20
- categories = ('Pneumonia', 'Normal')
21
-
22
-
23
- # Function to make a prediction on an input image
24
- def predict(img):
25
- pred, idx, probs = learn.predict(img) # Get the prediction, index, and probabilities
26
- return dict(zip(categories, map(float, probs))) # Return the probabilities mapped to categories
27
-
28
-
29
- # Title of the Gradio interface
30
- title = "Pediatric Pneumonia Chest X-Ray Predictor"
31
-
32
-
33
- # Description of the interface, including model and dataset information
34
- description = """
35
- A pediatric pneumonia chest x-ray predictor model trained on the chest-xray-pneumonia dataset using ResNet34 via
36
- <a href='http://www.fast.ai/' target='_blank'>fast.ai</a>. The dataset is from:
37
- <a href='http://www.kaggle.com/datasets/paultimothymooney/chest-xray-pneumonia' target='_blank'>Chest X-Ray Images (Pneumonia)</a>
38
- and the associated scientific journal paper is
39
- <a href='http://www.cell.com/cell/fulltext/S0092-8674(18)30154-5' target='_blank'>Identifying Medical Diagnoses and Treatable
40
- Diseases by Image-Based Deep Learning</a>. The accuracy of the model is: 87.50%
41
- """
42
-
43
-
44
- # Article or additional information to be displayed
45
- article = """
46
- <p style='text-align: center'><span style='font-size: 15pt;'>Pediatric Pneumonia Chest X-Ray Predictor. Dr Zakia Salod. 2024. </span></p>
47
- """
48
-
49
-
50
- # Gradio input component for image upload
51
- image = gr.Image(height=512, width=512)
52
-
53
- # Gradio output component for displaying the label
54
- label = gr.Label()
55
-
56
-
57
- # Example images to demonstrate the model's predictions
58
- examples = [
59
- ['NORMAL2-IM-0222-0001.jpeg'],
60
- ['person159_bacteria_747.jpeg'],
61
- ['person1618_virus_2805.jpeg'],
62
- ]
63
-
64
-
65
- # Create the Gradio interface
66
- iface = gr.Interface(
67
- fn=predict, # Function to call for predictions
68
- title=title, # Title of the interface
69
- description=description, # Description of the interface
70
- article=article, # Additional article or information
71
- inputs=image, # Input component
72
- outputs=label, # Output component
73
- theme="default", # Theme of the interface
74
- examples=examples # Example images
75
- )
76
-
77
-
78
- # Launch the Gradio interface
79
- iface.launch(inline=False)
 
1
+ # Bismillahir Rahmaanir Raheem
2
+ # Almadadh Ya Gause Radi Allahu Ta'alah Anh - Ameen
3
+
4
+ # Import necessary libraries from fastai and gradio
5
+ from fastai.vision.all import *
6
+ import gradio as gr
7
+
8
+
9
+ # Function to determine if an image contains pneumonia
10
+ # Checks if the filename contains 'virus' or 'bacteria'
11
+ def is_pneumonia(x):
12
+ return (x.find('virus')!=-1 or x.find('bacteria')!=-1)
13
+
14
+
15
+ # Load the trained fastai model for predictions
16
+ learn = load_learner('pneumonia_model.pkl')
17
+
18
+
19
+ # Define the possible categories for prediction
20
+ categories = learn.dls.vocab
21
+
22
+
23
+ # Function to make a prediction on an input image
24
+ def predict(img):
25
+ pred, idx, probs = learn.predict(img) # Get the prediction, index, and probabilities
26
+ return dict(zip(categories, map(float, probs))) # Return the probabilities mapped to categories
27
+
28
+
29
+ # Title of the Gradio interface
30
+ title = "Pediatric Pneumonia Chest X-Ray Predictor"
31
+
32
+
33
+ # Description of the interface, including model and dataset information
34
+ description = """
35
+ A pediatric pneumonia chest x-ray predictor model trained on the chest-xray-pneumonia dataset using ResNet34 via
36
+ <a href='http://www.fast.ai/' target='_blank'>fast.ai</a>. The dataset is from:
37
+ <a href='http://www.kaggle.com/datasets/paultimothymooney/chest-xray-pneumonia' target='_blank'>Chest X-Ray Images (Pneumonia)</a>
38
+ and the associated scientific journal paper is
39
+ <a href='http://www.cell.com/cell/fulltext/S0092-8674(18)30154-5' target='_blank'>Identifying Medical Diagnoses and Treatable
40
+ Diseases by Image-Based Deep Learning</a>. The accuracy of the model is: 87.50%
41
+ """
42
+
43
+
44
+ # Article or additional information to be displayed
45
+ article = """
46
+ <p style='text-align: center'><span style='font-size: 15pt;'>Pediatric Pneumonia Chest X-Ray Predictor. Dr Zakia Salod. 2024. </span></p>
47
+ """
48
+
49
+
50
+ # Gradio input component for image upload
51
+ image = gr.Image(height=512, width=512)
52
+
53
+ # Gradio output component for displaying the label
54
+ label = gr.Label()
55
+
56
+
57
+ # Example images to demonstrate the model's predictions
58
+ examples = [
59
+ ['NORMAL2-IM-0222-0001.jpeg'],
60
+ ['person159_bacteria_747.jpeg'],
61
+ ['person1618_virus_2805.jpeg'],
62
+ ]
63
+
64
+
65
+ # Create the Gradio interface
66
+ iface = gr.Interface(
67
+ fn=predict, # Function to call for predictions
68
+ title=title, # Title of the interface
69
+ description=description, # Description of the interface
70
+ article=article, # Additional article or information
71
+ inputs=image, # Input component
72
+ outputs=label, # Output component
73
+ theme="default", # Theme of the interface
74
+ examples=examples # Example images
75
+ )
76
+
77
+
78
+ # Launch the Gradio interface
79
+ iface.launch(inline=False)