Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,45 +2,32 @@ import gradio as gr
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from tensorflow.keras.preprocessing import image
|
| 5 |
-
from huggingface_hub import hf_hub_download
|
| 6 |
|
| 7 |
-
# Cargar el modelo
|
| 8 |
def load_model():
|
| 9 |
return tf.keras.models.load_model("modelo_frutas_transfer.keras")
|
| 10 |
|
| 11 |
-
# Cargar el modelo
|
| 12 |
model = load_model()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
class_names = ['Manzana', 'Banana', 'Naranja', 'Pera', 'Uva']
|
| 16 |
|
| 17 |
-
# Funci贸n
|
| 18 |
def predict_image(image_input):
|
| 19 |
-
# Cargar y redimensionar la imagen
|
| 20 |
img = image.load_img(image_input, target_size=(150, 150))
|
| 21 |
-
|
| 22 |
-
# Convertir la imagen a un array y normalizar
|
| 23 |
img_array = image.img_to_array(img) / 255.0
|
| 24 |
img_array = np.expand_dims(img_array, axis=0)
|
| 25 |
-
|
| 26 |
-
# Hacer la predicci贸n
|
| 27 |
pred = model.predict(img_array)
|
| 28 |
-
|
| 29 |
-
# Obtener la clase predicha
|
| 30 |
predicted_class = np.argmax(pred, axis=1)
|
| 31 |
-
|
| 32 |
-
# Mapear la clase al nombre
|
| 33 |
-
predicted_class_name = class_names[predicted_class[0]]
|
| 34 |
-
return predicted_class_name
|
| 35 |
|
| 36 |
-
# Interfaz Gradio
|
| 37 |
iface = gr.Interface(
|
| 38 |
-
fn=predict_image,
|
| 39 |
-
inputs=gr.Image(type="file", label="Cargar imagen de fruta"),
|
| 40 |
-
outputs=gr.Textbox(label="Predicci贸n de la clase"),
|
| 41 |
title="Clasificador de Frutas",
|
| 42 |
description="Sube una imagen de una fruta y el modelo predecir谩 qu茅 fruta es."
|
| 43 |
)
|
| 44 |
|
| 45 |
-
# Iniciar la interfaz
|
| 46 |
iface.launch()
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from tensorflow.keras.preprocessing import image
|
|
|
|
| 5 |
|
| 6 |
+
# Cargar el modelo (se asume que el archivo .keras est谩 en el repositorio del Space)
|
| 7 |
def load_model():
|
| 8 |
return tf.keras.models.load_model("modelo_frutas_transfer.keras")
|
| 9 |
|
|
|
|
| 10 |
model = load_model()
|
| 11 |
|
| 12 |
+
# Lista de clases (aj煤stala si es necesario)
|
| 13 |
+
class_names = ['Manzana', 'Banana', 'Naranja', 'Pera', 'Uva']
|
| 14 |
|
| 15 |
+
# Funci贸n de predicci贸n
|
| 16 |
def predict_image(image_input):
|
|
|
|
| 17 |
img = image.load_img(image_input, target_size=(150, 150))
|
|
|
|
|
|
|
| 18 |
img_array = image.img_to_array(img) / 255.0
|
| 19 |
img_array = np.expand_dims(img_array, axis=0)
|
|
|
|
|
|
|
| 20 |
pred = model.predict(img_array)
|
|
|
|
|
|
|
| 21 |
predicted_class = np.argmax(pred, axis=1)
|
| 22 |
+
return class_names[predicted_class[0]]
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Interfaz con Gradio
|
| 25 |
iface = gr.Interface(
|
| 26 |
+
fn=predict_image,
|
| 27 |
+
inputs=gr.Image(type="file", label="Cargar imagen de fruta"),
|
| 28 |
+
outputs=gr.Textbox(label="Predicci贸n de la clase"),
|
| 29 |
title="Clasificador de Frutas",
|
| 30 |
description="Sube una imagen de una fruta y el modelo predecir谩 qu茅 fruta es."
|
| 31 |
)
|
| 32 |
|
|
|
|
| 33 |
iface.launch()
|