Nina-HK commited on
Commit
6ea82b1
1 Parent(s): bcaa4d3

two_interface_3

Browse files
Files changed (1) hide show
  1. app.py +33 -32
app.py CHANGED
@@ -2,6 +2,17 @@
2
 
3
  # %%capture
4
  # #Use capture to not show the output of installing the libraries!
 
 
 
 
 
 
 
 
 
 
 
5
  import gradio as gr
6
  import requests
7
  import torch
@@ -13,57 +24,47 @@ import numpy as np
13
  import tensorflow as tf
14
  from transformers import pipeline
15
 
16
- # load the model from the Hugging Face Model Hub
17
- #model = pipeline('image-classification', model='image_classification/densenet')
18
- #model = tf.keras.models.load_model("densenet")
19
- #binary_model = tf.keras.models.load_model("densenet")
20
- #binary_labels = {0: 'healthy', 1: 'patient'}
21
-
22
  # load the binary classification model
23
- #model_binary = tf.keras.models.load_model("densenet")
24
 
25
  # load the multi-label classification model
26
- #model_multi = tf.keras.models.load_model("densenet")
27
 
28
  # define the labels for the multi-label classification model
29
- #labels_multi = {0: 'healthy', 1: 'mild', 2: 'moderate'}
30
-
31
 
32
- #model = tf.keras.models.load_model('/content/drive/MyDrive/project_image_2023_NO/saved_models/saved_model/densenet')
33
- #labels = ['Healthy', 'Patient']
34
- #labels = {0: 'healthy', 1: 'patient'}
35
-
36
- # Load the model
37
- model = tf.keras.models.load_model("densenet")
38
-
39
- # Define the labels
40
- labels = {0: 'healthy', 1: 'patient'}
41
 
42
- def classify_image(inp):
43
  inp = inp.reshape((-1, 224, 224, 3))
44
  inp = tf.keras.applications.densenet.preprocess_input(inp)
45
- prediction = model.predict(inp)
46
- confidences = {labels[i]: float(prediction[0][i]) for i in range(2)}
47
  return confidences
48
 
49
- # Create the binary interface
50
- binary_interface = gr.Interface(fn=classify_image,
51
- inputs=gr.Image(shape=(224, 224)),
52
- outputs=gr.Label(num_top_classes=2),
53
  title="Binary Image Classification",
54
  description="Classify an image as healthy or patient.",
55
  examples=[['300104.png']]
56
  )
57
 
58
- # Create the multi-class interface
59
- multi_interface = gr.Interface(fn=classify_image,
60
- inputs=gr.Image(shape=(224, 224)),
61
- outputs=gr.Label(num_top_classes=3),
62
  title="Multi-class Image Classification",
63
  description="Classify an image as healthy, mild or moderate.",
64
  examples=[['300104.png']]
65
  )
66
 
67
- # Launch the app
68
- gr.Interface([binary_interface, multi_interface], "grid", title="My Image Classification App").launch()
 
69
 
 
2
 
3
  # %%capture
4
  # #Use capture to not show the output of installing the libraries!
5
+
6
+ #model_multi = tf.keras.models.load_model("densenet")
7
+
8
+ # define the labels for the multi-label classification model
9
+ #labels_multi = {0: 'healthy', 1: 'mild', 2: 'moderate'}
10
+
11
+
12
+ #model = tf.keras.models.load_model('/content/drive/MyDrive/project_image_2023_NO/saved_models/saved_model/densenet')
13
+ #labels = ['Healthy', 'Patient']
14
+ #labels = {0: 'healthy', 1: 'patient'}
15
+
16
  import gradio as gr
17
  import requests
18
  import torch
 
24
  import tensorflow as tf
25
  from transformers import pipeline
26
 
 
 
 
 
 
 
27
  # load the binary classification model
28
+ model_binary = tf.keras.models.load_model("densenet")
29
 
30
  # load the multi-label classification model
31
+ model_multi = tf.keras.models.load_model("densenet")
32
 
33
  # define the labels for the multi-label classification model
34
+ labels_multi = {0: 'healthy', 1: 'mild', 2: 'moderate'}
 
35
 
36
+ def classify_binary(inp):
37
+ inp = inp.reshape((-1, 224, 224, 3))
38
+ inp = tf.keras.applications.densenet.preprocess_input(inp)
39
+ prediction = model_binary.predict(inp)
40
+ confidence = float(prediction[0])
41
+ label = {0: 'healthy', 1: 'patient'}
42
+ return {label: confidence}
 
 
43
 
44
+ def classify_multi(inp):
45
  inp = inp.reshape((-1, 224, 224, 3))
46
  inp = tf.keras.applications.densenet.preprocess_input(inp)
47
+ prediction = model_multi.predict(inp)
48
+ confidences = {labels_multi[i]: float(prediction[0][i]) for i in range(len(labels_multi))}
49
  return confidences
50
 
51
+ binary_interface = gr.Interface(fn=classify_binary,
52
+ inputs=gr.inputs.Image(shape=(224, 224)),
53
+ outputs=gr.outputs.Label(num_top_classes=2),
 
54
  title="Binary Image Classification",
55
  description="Classify an image as healthy or patient.",
56
  examples=[['300104.png']]
57
  )
58
 
59
+ multi_interface = gr.Interface(fn=classify_multi,
60
+ inputs=gr.inputs.Image(shape=(224, 224)),
61
+ outputs=gr.outputs.Label(num_top_classes=3),
 
62
  title="Multi-class Image Classification",
63
  description="Classify an image as healthy, mild or moderate.",
64
  examples=[['300104.png']]
65
  )
66
 
67
+ demo = gr.TabbedInterface([binary_interface, multi_interface], ["Binary", "Multi-class"])
68
+
69
+ demo.launch()
70