imanolcb commited on
Commit
f2fdad2
verified
1 Parent(s): a9cdd8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -42
app.py CHANGED
@@ -1,46 +1,50 @@
1
- import gradio as gr
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
- # Descargar el modelo .keras desde Hugging Face
11
- model_path = hf_hub_download(
12
- repo_id="imanolcb/basicFruitClassifier",
13
- filename="modelo_frutas_transfer.keras",
14
- repo_type="model"
15
- )
16
-
17
- # Cargar el modelo localmente
18
- model = tf.keras.models.load_model(model_path)
19
-
20
-
21
- # Preprocesamiento de las im谩genes de entrada
22
- def preprocess_image(image):
23
- image = image.resize((150, 150)) # Redimensionar la imagen a la entrada del modelo
24
- image = tf.convert_to_tensor(image)
25
- image = tf.cast(image, tf.float32) / 255.0 # Normalizaci贸n
26
- return tf.expand_dims(image, axis=0) # A帽adir una dimensi贸n extra para el batch
27
-
28
- # Funci贸n para hacer predicciones
29
- def predict(image):
30
- processed_image = preprocess_image(image) # Preprocesar la imagen
31
- predictions = model.predict(processed_image)
32
- class_names = ['apple', 'banana', 'orange'] # Las clases que usaste para el entrenamiento
33
- predicted_class = class_names[tf.argmax(predictions, axis=1).numpy()[0]]
 
 
 
 
 
 
 
34
  return predicted_class
35
 
36
- # Crear la interfaz de usuario con Gradio
37
- iface = gr.Interface(
38
- fn=predict,
39
- inputs=gr.inputs.Image(type="pil"), # Tipo de entrada: imagen
40
- outputs="text", # Salida: texto (la clase predicha)
41
- title="Fruit Classifier", # T铆tulo
42
- description="Clasificador de frutas usando un modelo CNN entrenado desde cero.", # Descripci贸n
43
- )
44
-
45
- # Ejecutar la aplicaci贸n
46
- iface.launch()
 
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
+