drkareemkamal commited on
Commit
c998166
·
verified ·
1 Parent(s): f50970f

Create app.py

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