Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
-
from tensorflow.keras.models import load_model
|
| 4 |
-
from tensorflow.keras.preprocessing import image
|
| 5 |
-
import numpy as np
|
| 6 |
|
| 7 |
-
# Cargar el modelo
|
| 8 |
-
model = load_model(
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return predicted_class
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
iface = gr.Interface(
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
#
|
| 29 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Cargar el modelo desde Hugging Face
|
| 5 |
+
model = tf.keras.models.load_model("hf://imanolcb/basicFruitClassifier/modelo_frutas_final.keras")
|
| 6 |
|
| 7 |
+
# Preprocesamiento de las im谩genes de entrada
|
| 8 |
+
def preprocess_image(image):
|
| 9 |
+
image = image.resize((150, 150)) # Redimensionar la imagen a la entrada del modelo
|
| 10 |
+
image = tf.convert_to_tensor(image)
|
| 11 |
+
image = tf.cast(image, tf.float32) / 255.0 # Normalizaci贸n
|
| 12 |
+
return tf.expand_dims(image, axis=0) # A帽adir una dimensi贸n extra para el batch
|
| 13 |
+
|
| 14 |
+
# Funci贸n para hacer predicciones
|
| 15 |
+
def predict(image):
|
| 16 |
+
processed_image = preprocess_image(image) # Preprocesar la imagen
|
| 17 |
+
predictions = model.predict(processed_image)
|
| 18 |
+
class_names = ['apple', 'banana', 'orange'] # Las clases que usaste para el entrenamiento
|
| 19 |
+
predicted_class = class_names[tf.argmax(predictions, axis=1).numpy()[0]]
|
| 20 |
return predicted_class
|
| 21 |
|
| 22 |
+
# Crear la interfaz de usuario con Gradio
|
| 23 |
+
iface = gr.Interface(
|
| 24 |
+
fn=predict,
|
| 25 |
+
inputs=gr.inputs.Image(type="pil"), # Tipo de entrada: imagen
|
| 26 |
+
outputs="text", # Salida: texto (la clase predicha)
|
| 27 |
+
title="Fruit Classifier", # T铆tulo
|
| 28 |
+
description="Clasificador de frutas usando un modelo CNN entrenado desde cero.", # Descripci贸n
|
| 29 |
+
)
|
| 30 |
|
| 31 |
+
# Ejecutar la aplicaci贸n
|
| 32 |
iface.launch()
|