Nina-HK commited on
Commit
ab0c74d
1 Parent(s): 67e1d45

two interfaces

Browse files
Files changed (1) hide show
  1. app.py +29 -18
app.py CHANGED
@@ -15,7 +15,9 @@ 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
 
20
 
21
  #model = tf.keras.models.load_model('/content/drive/MyDrive/project_image_2023_NO/saved_models/saved_model/densenet')
@@ -26,29 +28,38 @@ def classify_image(inp):
26
  inp = inp.reshape((-1, 224, 224, 3))
27
  inp = tf.keras.applications.densenet.preprocess_input(inp)
28
  prediction = model.predict(inp)
29
- confidences = {labels[i]: float(prediction[0][i]) for i in range(2)}
30
  return confidences
31
 
32
 
33
- image_interface = gr.Interface(fn=classify_image,
34
- inputs=gr.inputs.Image(shape=(224, 224)),
35
- outputs=gr.outputs.Label(num_top_classes = 2),
36
- title="Demo",
37
  description="Here's a sample image classification. Enjoy!",
38
  examples=[['300104.png']])
39
 
40
- image_interface.launch()
41
 
42
- text_interface = pipeline("text-generation", model="densenet")
 
43
 
44
- def generate_text(prompt):
45
- text = text_interface(prompt, max_length=50)[0]['generated_text']
46
- return text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- text_generator_interface = gr.Interface(fn=generate_text,
49
- inputs="text",
50
- outputs="text",
51
- title="Text Generator",
52
- description="Generate text using GPT-2",
53
- examples=[["300104.png"]])
54
- text_generator_interface.launch()
 
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
 
23
  #model = tf.keras.models.load_model('/content/drive/MyDrive/project_image_2023_NO/saved_models/saved_model/densenet')
 
28
  inp = inp.reshape((-1, 224, 224, 3))
29
  inp = tf.keras.applications.densenet.preprocess_input(inp)
30
  prediction = model.predict(inp)
31
+ confidences = {binary_labels[i]: float(prediction[0][i]) for i in range(2)}
32
  return confidences
33
 
34
 
35
+ binary_interface = gr.Interface(fn=classify_image,
36
+ inputs=gr.Image(shape=(224, 224)),
37
+ outputs=gr.Label(num_top_classes = 2),
38
+ title="Binary Image Classification",
39
  description="Here's a sample image classification. Enjoy!",
40
  examples=[['300104.png']])
41
 
42
+ # Multi-Class Image Classification
43
 
44
+ multi_model = pipeline('image-classification', model='densenet')
45
+ multi_labels = multi_model.model.config.id2label
46
 
47
+ def classify_multi_image(inp):
48
+ prediction = multi_model(inp)
49
+ confidences = {multi_labels[i]: float(prediction[0][i]) for i in range(len(multi_labels))}
50
+ return confidences
51
+
52
+ multi_interface = gr.Interface(fn=classify_multi_image,
53
+ inputs=gr.Image(shape=(224, 224)),
54
+ outputs=gr.Label(num_top_classes=len(multi_labels)),
55
+ title="Multi-Class Image Classification",
56
+ description="Classify an image into multiple classes",
57
+ examples=[['300104.png']]
58
+ )
59
+ iface = gr.Interface([binary_interface, multi_interface],
60
+ title="Image Classification App",
61
+ layout="vertical",
62
+ description="Demo for binary and multi-class image classification"
63
+ )
64
 
65
+ iface.launch()