Spaces:
Sleeping
Sleeping
Ethan MacCumber
commited on
Commit
·
722fcb5
1
Parent(s):
339d435
add application file
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from fastai.vision.all import *
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Load your trained FastAI model
|
7 |
+
learn = load_learner('export.pkl')
|
8 |
+
|
9 |
+
def predict_and_plot(img):
|
10 |
+
# Get predictions
|
11 |
+
pred, pred_idx, probs = learn.predict(img)
|
12 |
+
# Get class names
|
13 |
+
class_names = ['Normal',
|
14 |
+
'Hollenhorst Emboli',
|
15 |
+
'Hypertensive Retinopathy',
|
16 |
+
'Coat\'s',
|
17 |
+
'Macroaneurism',
|
18 |
+
'Choroidal Neovascularization',
|
19 |
+
'Other',
|
20 |
+
'Branch Retinal Artery Occlusion',
|
21 |
+
'Cilio-Retinal Artery Occlusion',
|
22 |
+
'Branch Retinal Vein Occlusion',
|
23 |
+
'Central Retinal Vein Occlusion',
|
24 |
+
'Hemi-Central Retinal Vein Occlusion',
|
25 |
+
'Background Diabetic Retinopathy',
|
26 |
+
'Proliferative Diabetic Retinopathy',
|
27 |
+
'Arteriosclerotic Retinopathy']
|
28 |
+
probs_np = probs.numpy()
|
29 |
+
|
30 |
+
threshold = 0.5
|
31 |
+
|
32 |
+
present = probs_np > threshold
|
33 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
34 |
+
y_pos = np.arange(len(class_names))
|
35 |
+
|
36 |
+
colors = ['green' if is_present else 'red' for is_present in present]
|
37 |
+
ax.barh(y_pos, probs_np, color=colors)
|
38 |
+
ax.set_yticks(y_pos)
|
39 |
+
ax.set_yticklabels(class_names)
|
40 |
+
ax.invert_yaxis()
|
41 |
+
ax.set_xlabel('Probability')
|
42 |
+
ax.set_xlim(0, 1)
|
43 |
+
ax.set_title('Predicted Probabilities for Each Condition')
|
44 |
+
plt.tight_layout()
|
45 |
+
return fig
|
46 |
+
|
47 |
+
# Create the Gradio interface
|
48 |
+
interface = gr.Interface(
|
49 |
+
fn=predict_and_plot,
|
50 |
+
inputs=gr.Image(type='pil'),
|
51 |
+
outputs=gr.Plot(),
|
52 |
+
title="Dr. Macloomber",
|
53 |
+
description="Upload an image of a retina to predict the probabilities of various eye conditions."
|
54 |
+
)
|
55 |
+
|
56 |
+
# Launch the app
|
57 |
+
interface.launch()
|
58 |
+
|
59 |
+
|