ImanAmran commited on
Commit
f12fd4b
·
1 Parent(s): f79bc54

Upload 101234444_aml_assignment_1.py

Browse files
Files changed (1) hide show
  1. 101234444_aml_assignment_1.py +70 -0
101234444_aml_assignment_1.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """101234444_aml_assignment_1.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1GBU5kKqfnliMP-lElZZ4VgVgcsyqy1wQ
8
+ """
9
+
10
+ !pip install --upgrade typing_extensions
11
+ !pip install --upgrade fastapi
12
+ !pip uninstall -y gradio
13
+ !pip install gradio
14
+
15
+ import requests
16
+ import tensorflow as tf
17
+ import PIL.Image
18
+ import numpy as np
19
+ import json
20
+ import gradio as gr
21
+
22
+ # Download the final_model.h5 file
23
+ url_model = "https://huggingface.co/ImanAmran/ml_assignment_1/resolve/main/final_model.h5"
24
+ response_model = requests.get(url_model)
25
+ with open("final_model.h5", "wb") as f_model:
26
+ f_model.write(response_model.content)
27
+
28
+ # Download the class_indices.json file
29
+ url_indices = "https://huggingface.co/ImanAmran/ml_assignment_1/resolve/main/class_indices.json"
30
+ response_indices = requests.get(url_indices)
31
+ class_indices = response_indices.json() # Parse the JSON response
32
+
33
+ # Load the model
34
+ model = tf.keras.models.load_model("final_model.h5")
35
+
36
+ # Reverse the key-value pairs in the class_indices dictionary
37
+ index_to_class = {v: k for k, v in class_indices.items()}
38
+
39
+ def classify_image(image: PIL.Image.Image):
40
+ try:
41
+ # Ensure the input is a PIL Image, resize it, and then convert it to a NumPy array
42
+ if not isinstance(image, PIL.Image.Image):
43
+ image = PIL.Image.fromarray(image)
44
+ image_resized = image.resize((375, 375))
45
+ image_array = np.array(image_resized)
46
+ image_array = np.expand_dims(image_array, axis=0) # Add a batch dimension
47
+
48
+ # Preprocess the image array in the same way as your manual prediction function
49
+ img_preprocessed = tf.keras.applications.resnet50.preprocess_input(image_array)
50
+
51
+ # Perform inference
52
+ predictions = model.predict(img_preprocessed)
53
+ predicted_class_idx = np.argmax(predictions) # Get the predicted class index
54
+
55
+ # Map index to label using index_to_class
56
+ predicted_class_label = index_to_class[predicted_class_idx]
57
+ return predicted_class_label
58
+
59
+ except Exception as e:
60
+ return str(e) # Return the exception message to help identify the issue
61
+
62
+ # Create a Gradio Interface
63
+ iface = gr.Interface(
64
+ fn=classify_image,
65
+ inputs=gr.components.Image(),
66
+ outputs=gr.components.Textbox(),
67
+ live=True, # This line is optional, it enables real-time feedback but may slow down performance
68
+ share=True # This line allows Gradio to be run in this Colab notebook
69
+ )
70
+ #iface.launch()