Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from tensorflow.keras.models import load_model
|
| 3 |
-
from tensorflow.keras.utils import img_to_array
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
# Load the model
|
| 8 |
model = load_model("model.h5")
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
| 11 |
def predict_image(img):
|
| 12 |
-
img = img.
|
| 13 |
-
|
| 14 |
-
img_array =
|
| 15 |
-
img_array = img_array
|
|
|
|
| 16 |
|
| 17 |
prediction = model.predict(img_array)
|
| 18 |
class_index = np.argmax(prediction)
|
| 19 |
-
|
| 20 |
-
return f"Predicted Class: {class_index}"
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
interface = gr.Interface(
|
| 24 |
fn=predict_image,
|
| 25 |
inputs=gr.Image(type="pil"),
|
| 26 |
outputs="text",
|
| 27 |
-
title="Image
|
| 28 |
)
|
| 29 |
|
| 30 |
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from tensorflow.keras.models import load_model
|
| 3 |
+
from tensorflow.keras.utils import img_to_array
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
# Load the model
|
| 8 |
model = load_model("model.h5")
|
| 9 |
|
| 10 |
+
# Label list (edit this according to your model output)
|
| 11 |
+
class_labels = ['Cat', 'Dog', 'Panda'] # Change these
|
| 12 |
+
|
| 13 |
+
# Prediction function
|
| 14 |
def predict_image(img):
|
| 15 |
+
img = img.convert("RGB") # Ensure RGB format
|
| 16 |
+
img = img.resize((128, 128))
|
| 17 |
+
img_array = img_to_array(img)
|
| 18 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 19 |
+
img_array = img_array / 255.0
|
| 20 |
|
| 21 |
prediction = model.predict(img_array)
|
| 22 |
class_index = np.argmax(prediction)
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
return f"Predicted Class: {class_labels[class_index]}"
|
| 25 |
+
|
| 26 |
+
# Gradio UI
|
| 27 |
interface = gr.Interface(
|
| 28 |
fn=predict_image,
|
| 29 |
inputs=gr.Image(type="pil"),
|
| 30 |
outputs="text",
|
| 31 |
+
title="Image Classifier"
|
| 32 |
)
|
| 33 |
|
| 34 |
interface.launch()
|