Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,50 @@
|
|
| 1 |
-
import
|
| 2 |
-
import os
|
| 3 |
-
os.environ["KERAS_BACKEND"] = "jax"
|
| 4 |
-
|
| 5 |
-
import keras
|
| 6 |
-
|
| 7 |
-
from huggingface_hub import hf_hub_download
|
| 8 |
import tensorflow as tf
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
filename="modelo_frutas_transfer.keras"
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
# Cargar el modelo
|
| 18 |
-
model =
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
return predicted_class
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 1 |
+
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 desde Hugging Face
|
| 8 |
+
@st.cache_resource # Usamos cache para no cargar el modelo en cada interacci贸n
|
| 9 |
+
def load_model():
|
| 10 |
+
model_path = hf_hub_download(repo_id="imanolcb/basicFruitClassifier", filename="modelo_frutas_transfer.keras")
|
| 11 |
+
model = tf.keras.models.load_model(model_path)
|
| 12 |
+
return model
|
| 13 |
+
|
| 14 |
+
# Cargar el modelo
|
| 15 |
+
model = load_model()
|
| 16 |
+
|
| 17 |
+
# T铆tulo de la app
|
| 18 |
+
st.title('Clasificador de Frutas')
|
| 19 |
+
st.write('Sube una imagen de una fruta y el modelo predecir谩 qu茅 fruta es.')
|
| 20 |
+
|
| 21 |
+
# Cargar imagen
|
| 22 |
+
uploaded_image = st.file_uploader("Elige una imagen", type=['jpg', 'png', 'jpeg'])
|
| 23 |
+
|
| 24 |
+
# Funci贸n para preprocesar la imagen y hacer la predicci贸n
|
| 25 |
+
def predict_image(image_input):
|
| 26 |
+
# Cargar y redimensionar la imagen
|
| 27 |
+
img = image.load_img(image_input, target_size=(150, 150))
|
| 28 |
+
|
| 29 |
+
# Convertir la imagen a un array y normalizar
|
| 30 |
+
img_array = image.img_to_array(img) / 255.0
|
| 31 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 32 |
+
|
| 33 |
+
# Hacer la predicci贸n
|
| 34 |
+
pred = model.predict(img_array)
|
| 35 |
+
|
| 36 |
+
# Obtener la clase predicha
|
| 37 |
+
predicted_class = np.argmax(pred, axis=1)
|
| 38 |
return predicted_class
|
| 39 |
|
| 40 |
+
# Si se carga una imagen
|
| 41 |
+
if uploaded_image is not None:
|
| 42 |
+
st.image(uploaded_image, caption='Imagen cargada', use_column_width=True)
|
| 43 |
+
st.write("")
|
| 44 |
+
|
| 45 |
+
# Predecir la clase de la imagen
|
| 46 |
+
predicted_class = predict_image(uploaded_image)
|
| 47 |
+
|
| 48 |
+
# Mostrar el resultado
|
| 49 |
+
st.write(f'Predicci贸n de la clase: {predicted_class}')
|
| 50 |
+
|