Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,76 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from fastai.vision.all import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
learner = from_pretrained_fastai(repo_id)
|
| 8 |
labels = learner.dls.vocab
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
def predict(img):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from PIL import Image as PILImage
|
| 3 |
import gradio as gr
|
| 4 |
from fastai.vision.all import *
|
| 5 |
+
from huggingface_hub import from_pretrained_fastai
|
| 6 |
+
|
| 7 |
+
# Funci贸n auxiliar para convertir las selecciones del men煤 en una imagen
|
| 8 |
+
def menu_to_image(selected_cells, selected_year, selected_inclinacion):
|
| 9 |
+
# Dimensiones de la matriz de la imagen
|
| 10 |
+
m, n = 20, 18
|
| 11 |
+
image_matrix = np.zeros((n, m, 3), dtype=np.uint8)
|
| 12 |
+
|
| 13 |
+
# Asignar valores a las celdas seleccionadas
|
| 14 |
+
for col, row in selected_cells:
|
| 15 |
+
image_matrix[n - row, ord(col) - ord("A")] = [255, 255, 255] # Blanco
|
| 16 |
+
|
| 17 |
+
# Escalar la columna "A脩O" y asignar valores de gris
|
| 18 |
+
ano_scaled = (selected_year - 2016) / (2019 - 2016)
|
| 19 |
+
grey_intensity_ano = int(ano_scaled * (255 - 50) + 50)
|
| 20 |
+
image_matrix[:, -7:-5] = [grey_intensity_ano, grey_intensity_ano, grey_intensity_ano]
|
| 21 |
|
| 22 |
+
# Escalar la columna "INCLINACION" y asignar valores de gris
|
| 23 |
+
inclinacion_scaled = (selected_inclinacion - 25) / (40 - 25)
|
| 24 |
+
grey_intensity_inclinacion = int(inclinacion_scaled * (100) + 100)
|
| 25 |
+
image_matrix[:, -3:-1] = [grey_intensity_inclinacion, grey_intensity_inclinacion, grey_intensity_inclinacion]
|
| 26 |
|
| 27 |
+
# Convertir la matriz numpy a imagen PIL
|
| 28 |
+
image_pil = PILImage.fromarray(image_matrix, 'RGB')
|
| 29 |
+
|
| 30 |
+
# Redimensionar al triple del tama帽o resultante sin suavizar
|
| 31 |
+
new_size = (image_pil.width * 3, image_pil.height * 3)
|
| 32 |
+
resized_image_pil = image_pil.resize(new_size, resample=PILImage.NEAREST)
|
| 33 |
+
|
| 34 |
+
return resized_image_pil
|
| 35 |
+
|
| 36 |
+
# Cargar el modelo preentrenado
|
| 37 |
+
repo_id = "ignaciobfp/moonboard"
|
| 38 |
learner = from_pretrained_fastai(repo_id)
|
| 39 |
labels = learner.dls.vocab
|
| 40 |
|
| 41 |
+
# Funci贸n para realizar predicciones
|
| 42 |
+
def predict(img, selected_cells, selected_year, selected_inclinacion):
|
| 43 |
+
if isinstance(img, gr.Image):
|
| 44 |
+
img_array = np.array(img)
|
| 45 |
+
else:
|
| 46 |
+
img_array = np.array(gr.Image.from_file(img))
|
| 47 |
+
|
| 48 |
+
# Generar imagen a partir de las selecciones del men煤
|
| 49 |
+
menu_image = menu_to_image(selected_cells, selected_year, selected_inclinacion)
|
| 50 |
+
|
| 51 |
+
# Realizar la predicci贸n en la imagen generada por el men煤
|
| 52 |
+
pred, pred_idx, probs = learner.predict(np.array(menu_image))
|
| 53 |
+
|
| 54 |
+
return {
|
| 55 |
+
"Predictions": {labels[i]: float(probs[i]) for i in range(len(labels))},
|
| 56 |
+
"Menu Image": gr.Image(menu_image)
|
| 57 |
+
}
|
| 58 |
|
| 59 |
+
# Crear las opciones para el men煤 emergente
|
| 60 |
+
columns = list("ABCDEFGHIJK")
|
| 61 |
+
rows = list(range(1, 19))
|
| 62 |
+
cell_options = [(col, row) for col in columns for row in rows]
|
| 63 |
|
| 64 |
+
# Crear la interfaz y lanzarla
|
| 65 |
+
iface = gr.Interface(
|
| 66 |
+
fn=predict,
|
| 67 |
+
inputs=[
|
| 68 |
+
gr.Image(shape=(128, 128)), # Permitir subir una imagen o usar ejemplos
|
| 69 |
+
gr.MultiCheckbox(cell_options, label="Selecciona las celdas:"),
|
| 70 |
+
gr.Dropdown(list(range(2016, 2020)), label="Selecciona el a帽o:"),
|
| 71 |
+
gr.Dropdown(list(range(25, 41)), label="Selecciona la inclinaci贸n:")
|
| 72 |
+
],
|
| 73 |
+
outputs=[gr.Label(num_top_classes=3), gr.Image()],
|
| 74 |
+
live=True
|
| 75 |
+
)
|
| 76 |
+
iface.launch(share=False)
|