hb-setosys
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,56 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
-
from tensorflow.keras.applications.resnet import ResNet152, preprocess_input, decode_predictions
|
4 |
-
from tensorflow.keras.preprocessing.image import img_to_array
|
5 |
-
from PIL import Image
|
6 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
# Decode the base64 string to bytes
|
18 |
-
image_data = base64.b64decode(base64_str)
|
19 |
-
# Convert the bytes into a PIL image
|
20 |
-
image = Image.open(BytesIO(image_data))
|
21 |
return image
|
22 |
-
|
23 |
-
|
|
|
24 |
"""
|
25 |
-
|
26 |
"""
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
|
34 |
-
# Get predictions
|
35 |
-
predictions = model.predict(image_array)
|
36 |
-
decoded_predictions = decode_predictions(predictions, top=3)[0]
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
return dict(results)
|
41 |
|
42 |
-
except Exception as e:
|
43 |
-
return {"Error": str(e)}
|
44 |
|
45 |
-
# Create
|
46 |
interface = gr.Interface(
|
47 |
-
fn=
|
48 |
-
inputs=gr.Image(type="pil"), #
|
49 |
-
outputs=gr.
|
50 |
-
title="
|
51 |
-
description="Upload an image
|
52 |
-
examples=[
|
|
|
|
|
|
|
53 |
)
|
54 |
|
55 |
-
|
|
|
56 |
if __name__ == "__main__":
|
57 |
-
interface.launch()
|
|
|
1 |
+
#denis_mnist_cnn_model_resnet50_v1.h5") # Ensure you upload this file to Hugging Face Spaces
|
2 |
+
|
3 |
import gradio as gr
|
4 |
import tensorflow as tf
|
|
|
|
|
|
|
5 |
import numpy as np
|
6 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input
|
7 |
+
from tensorflow.keras.utils import load_img, img_to_array
|
8 |
+
|
9 |
+
# Load your trained model
|
10 |
+
model = tf.keras.models.load_model("denis_mnist_cnn_model_resnet50_v1.h5") # Ensure you upload this file to Hugging Face Spaces
|
11 |
|
12 |
+
# Define a function to preprocess the image
|
13 |
+
def preprocess_image(image):
|
14 |
+
"""
|
15 |
+
Preprocesses the uploaded image for prediction.
|
16 |
+
"""
|
17 |
+
image = image.resize((128, 128)) # Resize to match the model input size
|
18 |
+
image = img_to_array(image) # Convert PIL image to NumPy array
|
19 |
+
image = preprocess_input(image) # Normalize for ResNet50
|
20 |
+
image = np.expand_dims(image, axis=0) # Add batch dimension
|
|
|
|
|
|
|
|
|
21 |
return image
|
22 |
+
|
23 |
+
# Define the prediction function
|
24 |
+
def predict(image):
|
25 |
"""
|
26 |
+
Accepts an image, preprocesses it, and returns the predicted label.
|
27 |
"""
|
28 |
+
processed_image = preprocess_image(image)
|
29 |
+
predictions = model.predict(processed_image)
|
30 |
+
predicted_class = np.argmax(predictions, axis=-1)[0] # Get the class index
|
31 |
+
confidence = np.max(predictions) # Get confidence score
|
32 |
+
#return f"Predicted Class: {predicted_class}, Confidence: {confidence:.2f}"
|
33 |
+
return {"prediction": int(predicted_class)}
|
34 |
|
|
|
|
|
|
|
35 |
|
36 |
+
# Create a Gradio interface
|
37 |
+
#interface = gr.Interface(fn=predict, inputs="image", outputs="json")
|
|
|
38 |
|
|
|
|
|
39 |
|
40 |
+
# Create a Gradio interface
|
41 |
interface = gr.Interface(
|
42 |
+
fn=predict, # The prediction function
|
43 |
+
inputs=gr.Image(type="pil", label="Upload an Image"), # Input: Image
|
44 |
+
outputs=gr.Textbox(label="Prediction"), # Output: Textbox
|
45 |
+
title="MNIST ResNet50 Classifier",
|
46 |
+
description="Upload an image to classify it using the trained ResNet50 model.",
|
47 |
+
examples=[
|
48 |
+
["example_images/example1.png"], # Add paths to example images in your Hugging Face repository
|
49 |
+
["example_images/example2.png"]
|
50 |
+
],
|
51 |
)
|
52 |
|
53 |
+
|
54 |
+
# Launch the app
|
55 |
if __name__ == "__main__":
|
56 |
+
interface.launch(share=True)
|