Spaces:
Runtime error
Runtime error
a-guy-from-burma
commited on
Commit
•
018a570
1
Parent(s):
e0c3ab0
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
from tensorflow.keras.preprocessing.image import img_to_array, load_img
|
5 |
+
|
6 |
+
# Load the saved model
|
7 |
+
model = load_model('fruit_veg_classifier.h5')
|
8 |
+
|
9 |
+
# Create a class mapping (based on your dataset)
|
10 |
+
class_names = list(train_generator.class_indices.keys())
|
11 |
+
|
12 |
+
# Define the prediction function
|
13 |
+
def classify_image(image):
|
14 |
+
# Preprocess the image
|
15 |
+
image = image.resize((150, 150))
|
16 |
+
image = img_to_array(image) / 255.0
|
17 |
+
image = np.expand_dims(image, axis=0)
|
18 |
+
|
19 |
+
# Make prediction
|
20 |
+
predictions = model.predict(image)
|
21 |
+
predicted_class = np.argmax(predictions)
|
22 |
+
return class_names[predicted_class]
|
23 |
+
|
24 |
+
# Create the Gradio interface
|
25 |
+
interface = gr.Interface(
|
26 |
+
fn=classify_image,
|
27 |
+
inputs=gr.inputs.Image(shape=(150, 150)),
|
28 |
+
outputs=gr.outputs.Label(),
|
29 |
+
title="Fruit & Vegetable Classifier",
|
30 |
+
description="Upload an image of a fruit or vegetable, and the model will predict what it is!"
|
31 |
+
)
|
32 |
+
|
33 |
+
# Launch the app
|
34 |
+
interface.launch()
|