Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,44 @@
|
|
| 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/
|
| 38 |
learner = from_pretrained_fastai(repo_id)
|
| 39 |
labels = learner.dls.vocab
|
| 40 |
|
| 41 |
# Funci贸n para realizar predicciones
|
| 42 |
-
def predict(img
|
| 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 |
-
#
|
| 49 |
-
|
| 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 |
-
"
|
| 57 |
}
|
| 58 |
|
| 59 |
-
# Crear
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
)
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from fastai.vision.all import *
|
| 3 |
from huggingface_hub import from_pretrained_fastai
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Cargar el modelo preentrenado
|
| 6 |
+
repo_id = "ignaciobfp/moonboard_difficulty"
|
| 7 |
learner = from_pretrained_fastai(repo_id)
|
| 8 |
labels = learner.dls.vocab
|
| 9 |
|
| 10 |
# Funci贸n para realizar predicciones
|
| 11 |
+
def predict(img):
|
| 12 |
if isinstance(img, gr.Image):
|
| 13 |
img_array = np.array(img)
|
| 14 |
else:
|
| 15 |
img_array = np.array(gr.Image.from_file(img))
|
| 16 |
|
| 17 |
+
# Realizar la predicci贸n en la imagen proporcionada
|
| 18 |
+
pred, pred_idx, probs = learner.predict(img_array)
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
return {
|
| 21 |
"Predictions": {labels[i]: float(probs[i]) for i in range(len(labels))},
|
| 22 |
+
"Input Image": img
|
| 23 |
}
|
| 24 |
|
| 25 |
+
# Crear ejemplos
|
| 26 |
+
examples = [
|
| 27 |
+
["easy1.jpg"],
|
| 28 |
+
["easy2.jpg"],
|
| 29 |
+
["easy3.jpg"],
|
| 30 |
+
["med1.jpg"],
|
| 31 |
+
["med2.jpg"],
|
| 32 |
+
["med3.jpg"],
|
| 33 |
+
["hard1.jpg"],
|
| 34 |
+
["hard2.jpg"],
|
| 35 |
+
["hard3.jpg"]
|
| 36 |
+
]
|
| 37 |
|
| 38 |
# Crear la interfaz y lanzarla
|
| 39 |
iface = gr.Interface(
|
| 40 |
fn=predict,
|
| 41 |
+
inputs=gr.Image(shape=(128, 128), examples=examples),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
outputs=[gr.Label(num_top_classes=3), gr.Image()],
|
| 43 |
live=True
|
| 44 |
)
|