Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Configurar variables de entorno para reducir advertencias
|
| 8 |
+
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
|
| 9 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
| 10 |
+
|
| 11 |
+
# Configuraci贸n inicial
|
| 12 |
+
cifar10_labels = [
|
| 13 |
+
'avi贸n', 'autom贸vil', 'p谩jaro', 'gato', 'venado',
|
| 14 |
+
'perro', 'rana', 'caballo', 'barco', 'cami贸n'
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
model = tf.keras.models.load_model('my_model.keras')
|
| 18 |
+
|
| 19 |
+
def preprocess_image(image):
|
| 20 |
+
"""Preprocesado de imagen para el modelo"""
|
| 21 |
+
img = image.resize((32, 32)).convert('RGB')
|
| 22 |
+
return np.array(img).astype('float32') / 255
|
| 23 |
+
|
| 24 |
+
def predict(image):
|
| 25 |
+
"""Realizar predicci贸n y formatear resultados"""
|
| 26 |
+
if image is None:
|
| 27 |
+
raise gr.Error("隆Por favor sube una imagen o toma una foto!")
|
| 28 |
+
|
| 29 |
+
processed_img = preprocess_image(image)
|
| 30 |
+
preds = model.predict(np.expand_dims(processed_img, axis=0))[0]
|
| 31 |
+
return {label: float(preds[i]) for i, label in enumerate(cifar10_labels)}
|
| 32 |
+
|
| 33 |
+
# Configurar ejemplos
|
| 34 |
+
examples = [
|
| 35 |
+
["ejemplos/avion.jpg"],
|
| 36 |
+
["ejemplos/automovil.jpg"],
|
| 37 |
+
["ejemplos/pajaro.jpg"],
|
| 38 |
+
["ejemplos/gato.jpg"],
|
| 39 |
+
["ejemplos/venado.jpg"],
|
| 40 |
+
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
# Construir interfaz
|
| 44 |
+
with gr.Blocks(theme=gr.themes.Soft(), css="""
|
| 45 |
+
.examples-grid {display: flex !important; flex-direction: column; gap: 1rem}
|
| 46 |
+
.examples-row {display: flex !important; gap: 1rem; justify-content: center}
|
| 47 |
+
""") as app:
|
| 48 |
+
|
| 49 |
+
gr.Markdown("# 馃摲 Clasificador CIFAR-10 by Aryy :3")
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
with gr.Column():
|
| 53 |
+
input_image = gr.Image(
|
| 54 |
+
sources=["upload", "webcam", "clipboard"],
|
| 55 |
+
type="pil",
|
| 56 |
+
label="Entrada de imagen",
|
| 57 |
+
height=250
|
| 58 |
+
)
|
| 59 |
+
with gr.Row():
|
| 60 |
+
submit_btn = gr.Button("Predecir", variant="primary")
|
| 61 |
+
clear_btn = gr.Button("Limpiar")
|
| 62 |
+
|
| 63 |
+
with gr.Column():
|
| 64 |
+
output_label = gr.Label(
|
| 65 |
+
label="Resultados",
|
| 66 |
+
num_top_classes=3,
|
| 67 |
+
show_label=True
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Secci贸n de ejemplos con interacci贸n
|
| 71 |
+
gr.Markdown("## Ejemplos de categor铆as")
|
| 72 |
+
with gr.Column(elem_classes=["examples-grid"]):
|
| 73 |
+
# Primera fila
|
| 74 |
+
with gr.Row(elem_classes=["examples-row"]):
|
| 75 |
+
for example, label in zip(examples[:5], cifar10_labels[:5]):
|
| 76 |
+
gr.Examples(
|
| 77 |
+
examples=example,
|
| 78 |
+
inputs=[input_image],
|
| 79 |
+
label=label.capitalize(),
|
| 80 |
+
examples_per_page=1,
|
| 81 |
+
fn=predict,
|
| 82 |
+
outputs=[output_label],
|
| 83 |
+
)
|
| 84 |
+
# Segunda fila
|
| 85 |
+
with gr.Row(elem_classes=["examples-row"]):
|
| 86 |
+
for example, label in zip(examples[5:], cifar10_labels[5:]):
|
| 87 |
+
gr.Examples(
|
| 88 |
+
examples=example,
|
| 89 |
+
inputs=[input_image],
|
| 90 |
+
label=label.capitalize(),
|
| 91 |
+
examples_per_page=1,
|
| 92 |
+
fn=predict,
|
| 93 |
+
outputs=[output_label],
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Conectar eventos
|
| 97 |
+
submit_btn.click(
|
| 98 |
+
fn=predict,
|
| 99 |
+
inputs=input_image,
|
| 100 |
+
outputs=output_label,
|
| 101 |
+
api_name="predict"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
clear_btn.click(
|
| 105 |
+
fn=lambda: [None, None],
|
| 106 |
+
inputs=None,
|
| 107 |
+
outputs=[input_image, output_label]
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
app.launch()
|