Ethan MacCumber commited on
Commit
ddfba2f
·
1 Parent(s): 527a2b6

trying altair

Browse files
Files changed (1) hide show
  1. app.py +53 -37
app.py CHANGED
@@ -1,47 +1,63 @@
1
  import gradio as gr
2
  from fastai.vision.all import *
3
- import matplotlib.pyplot as plt
4
- import numpy as np
5
 
6
- learn = load_learner('export.pkl')
 
7
 
8
  def predict_and_plot(img):
9
-
10
  pred, pred_idx, probs = learn.predict(img)
11
-
12
- class_names = ['Normal',
13
- 'Hollenhorst Emboli',
14
- 'Hypertensive Retinopathy',
15
- 'Coat\'s',
16
- 'Macroaneurism',
17
- 'Choroidal Neovascularization',
18
- 'Other',
19
- 'Branch Retinal Artery Occlusion',
20
- 'Cilio-Retinal Artery Occlusion',
21
- 'Branch Retinal Vein Occlusion',
22
- 'Central Retinal Vein Occlusion',
23
- 'Hemi-Central Retinal Vein Occlusion',
24
- 'Background Diabetic Retinopathy',
25
- 'Proliferative Diabetic Retinopathy',
26
- 'Arteriosclerotic Retinopathy']
 
 
 
 
 
27
  probs_np = probs.numpy()
28
-
29
  threshold = 0.5
30
-
31
  present = probs_np > threshold
32
- fig, ax = plt.subplots(figsize=(10, 6))
33
- y_pos = np.arange(len(class_names))
34
-
35
- colors = ['green' if is_present else 'red' for is_present in present]
36
- ax.barh(y_pos, probs_np, color=colors)
37
- ax.set_yticks(y_pos)
38
- ax.set_yticklabels(class_names)
39
- ax.invert_yaxis()
40
- ax.set_xlabel('Probability')
41
- ax.set_xlim(0, 1)
42
- ax.set_title('Predicted Probabilities for Each Condition')
43
- plt.tight_layout()
44
- return fig
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  # Create the Gradio interface
47
  interface = gr.Interface(
@@ -53,6 +69,6 @@ interface = gr.Interface(
53
  )
54
 
55
  # Launch the app
56
- interface.launch()
57
-
58
 
 
1
  import gradio as gr
2
  from fastai.vision.all import *
3
+ import altair as alt
4
+ import pandas as pd
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
+
13
+ # Define class names
14
+ class_names = [
15
+ 'Normal',
16
+ 'Hollenhorst Emboli',
17
+ 'Hypertensive Retinopathy',
18
+ "Coat's",
19
+ 'Macroaneurism',
20
+ 'Choroidal Neovascularization',
21
+ 'Other',
22
+ 'Branch Retinal Artery Occlusion',
23
+ 'Cilio-Retinal Artery Occlusion',
24
+ 'Branch Retinal Vein Occlusion',
25
+ 'Central Retinal Vein Occlusion',
26
+ 'Hemi-Central Retinal Vein Occlusion',
27
+ 'Background Diabetic Retinopathy',
28
+ 'Proliferative Diabetic Retinopathy',
29
+ 'Arteriosclerotic Retinopathy'
30
+ ]
31
+
32
+ # Convert probabilities to numpy array
33
  probs_np = probs.numpy()
 
34
  threshold = 0.5
 
35
  present = probs_np > threshold
36
+
37
+ # Create a DataFrame with the results
38
+ df = pd.DataFrame({
39
+ 'Condition': class_names,
40
+ 'Probability': probs_np,
41
+ 'Present': present
42
+ })
43
+
44
+ # Create an Altair bar chart
45
+ chart = alt.Chart(df).mark_bar().encode(
46
+ x=alt.X('Probability:Q', scale=alt.Scale(domain=(0, 1))),
47
+ y=alt.Y('Condition:N', sort='-x'),
48
+ color=alt.condition(
49
+ alt.datum.Present,
50
+ alt.value('green'), # Color for conditions present
51
+ alt.value('red') # Color for conditions not present
52
+ )
53
+ ).properties(
54
+ title='Predicted Probabilities for Each Condition',
55
+ width=600,
56
+ height=400
57
+ )
58
+
59
+ # Return the chart
60
+ return chart
61
 
62
  # Create the Gradio interface
63
  interface = gr.Interface(
 
69
  )
70
 
71
  # Launch the app
72
+ if __name__ == "__main__":
73
+ interface.launch()
74