drkareemkamal commited on
Commit
4bef3fc
·
verified ·
1 Parent(s): 3467094

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from timeit import default_timer as timer
4
+ from typing import Tuple , Dict
5
+ import tensorflow as tf
6
+ import numpy as np
7
+
8
+ from PIL import Image
9
+ import os
10
+
11
+ # 1.Import and class names setup
12
+ class_names = ['CNV','DME','DRUSEN','NORMAL']
13
+
14
+
15
+ # 2. Model annd transforms prepration
16
+ # model = tf.keras.models.load_model(
17
+ # 'oct_classification_final_model_lg.keras', custom_objects=None, compile=True, safe_mode=True
18
+ # )
19
+ model = tf.keras.models.load_model(
20
+ 'oct_classification_final_model_lg.keras', custom_objects=None, compile=True, safe_mode=False
21
+ )
22
+
23
+
24
+ # Load save weights
25
+
26
+ # 3.prediction function (predict())
27
+
28
+ def load_and_prep_imgg(filename, img_shape=224, scale=True):
29
+ img = tf.io.read_file(filename)
30
+ img = tf.io.decode_image(img, channels=3)
31
+ img = tf.image.resize(img, size=[img_shape, img_shape])
32
+ if scale:
33
+ return img / 255
34
+ else:
35
+ return img
36
+
37
+ def predict(img) -> Tuple[Dict,float] :
38
+
39
+ start_time = timer()
40
+
41
+ image = load_and_prep_imgg(img)
42
+ #image = Image.open(image)
43
+
44
+ pred_img = model.predict(tf.expand_dims(image, axis=0))
45
+ pred_class = class_names[pred_img.argmax()]
46
+ st.write(f"Predicted brain tumor is: {pred_class} with probability: {pred_img.max():.2f}")
47
+
48
+
49
+
50
+
51
+ end_time = timer()
52
+ pred_time = round(end_time - start_time , 4)
53
+
54
+ return pred_class , pred_time
55
+
56
+ ### 4. Gradio app - our Gradio interface + launch command
57
+
58
+ title = 'FoodVision Big'
59
+ description = 'Feature Extraxtion VGG model to classifiy Macular Diseases by OCT '
60
+ article = 'created at Tensorflow Model Deployment'
61
+
62
+ # Create example list
63
+
64
+ example_list = [['examples/'+ example] for example in os.listdir('examples')]
65
+ example_list
66
+
67
+ # create a gradio demo
68
+ demo = gr.Interface(fn=predict ,
69
+ inputs=gr.Image(type='pil'),
70
+ outputs=[gr.Label(num_top_classes = 3 , label= 'prediction'),
71
+ gr.Number(label= 'Prediction time (s)')],
72
+ examples = example_list,
73
+ title = title,
74
+ description = description,
75
+ article= article)
76
+
77
+ # Launch the demo
78
+ demo.launch(debug= False)