Spaces:
Sleeping
Sleeping
huggingface interface update
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ def solicitar_prediccion(image_path):
|
|
| 13 |
Envía la imagen al endpoint /predict
|
| 14 |
"""
|
| 15 |
if image_path is None:
|
| 16 |
-
return "
|
| 17 |
|
| 18 |
try:
|
| 19 |
# Abrimos la imagen en modo binario para enviarla
|
|
@@ -25,12 +25,12 @@ def solicitar_prediccion(image_path):
|
|
| 25 |
data = response.json()
|
| 26 |
|
| 27 |
# Devolvemos la predicción
|
| 28 |
-
return f"
|
| 29 |
|
| 30 |
except requests.exceptions.RequestException as e:
|
| 31 |
-
return f"Error
|
| 32 |
except Exception as e:
|
| 33 |
-
return f"
|
| 34 |
|
| 35 |
def solicitar_resize(image_path, width, height):
|
| 36 |
"""
|
|
@@ -42,7 +42,7 @@ def solicitar_resize(image_path, width, height):
|
|
| 42 |
try:
|
| 43 |
# Validar inputs
|
| 44 |
if width <= 0 or height <= 0:
|
| 45 |
-
print("
|
| 46 |
return None
|
| 47 |
|
| 48 |
payload = {"width": int(width), "height": int(height)}
|
|
@@ -66,39 +66,39 @@ def solicitar_resize(image_path, width, height):
|
|
| 66 |
|
| 67 |
# --- Construcción de la Interfaz con Blocks ---
|
| 68 |
with gr.Blocks(title="Predictor & Resizer API Client") as demo:
|
| 69 |
-
gr.Markdown("#
|
| 70 |
-
gr.Markdown("
|
| 71 |
|
| 72 |
with gr.Row():
|
| 73 |
-
#
|
| 74 |
with gr.Column():
|
| 75 |
-
gr.Markdown("### 1.
|
| 76 |
# Selector de imágenes. 'type="filepath"' guarda la imagen temporalmente y nos da la ruta
|
| 77 |
-
input_image = gr.Image(label="
|
| 78 |
|
| 79 |
-
#
|
| 80 |
with gr.Column():
|
| 81 |
|
| 82 |
-
# ---
|
| 83 |
-
gr.Markdown("### 2.
|
| 84 |
-
predict_btn = gr.Button("🔍
|
| 85 |
-
predict_output = gr.Textbox(label="
|
| 86 |
|
| 87 |
# CORRECCIÓN AQUÍ: gr.HTML en mayúsculas
|
| 88 |
gr.HTML("<hr>")
|
| 89 |
|
| 90 |
-
# ---
|
| 91 |
-
gr.Markdown("### 3.
|
| 92 |
with gr.Row():
|
| 93 |
-
w_input = gr.Number(label="
|
| 94 |
-
h_input = gr.Number(label="
|
| 95 |
|
| 96 |
-
resize_btn = gr.Button("🖼️
|
| 97 |
-
resize_output = gr.Image(label="
|
| 98 |
|
| 99 |
-
# ---
|
| 100 |
|
| 101 |
-
#
|
| 102 |
predict_btn.click(
|
| 103 |
fn=solicitar_prediccion,
|
| 104 |
inputs=[input_image],
|
|
@@ -112,6 +112,6 @@ with gr.Blocks(title="Predictor & Resizer API Client") as demo:
|
|
| 112 |
outputs=resize_output
|
| 113 |
)
|
| 114 |
|
| 115 |
-
#
|
| 116 |
if __name__ == "__main__":
|
| 117 |
demo.launch()
|
|
|
|
| 13 |
Envía la imagen al endpoint /predict
|
| 14 |
"""
|
| 15 |
if image_path is None:
|
| 16 |
+
return "You must upload an image first."
|
| 17 |
|
| 18 |
try:
|
| 19 |
# Abrimos la imagen en modo binario para enviarla
|
|
|
|
| 25 |
data = response.json()
|
| 26 |
|
| 27 |
# Devolvemos la predicción
|
| 28 |
+
return f"Prediction: {data.get('prediction')}"
|
| 29 |
|
| 30 |
except requests.exceptions.RequestException as e:
|
| 31 |
+
return f"Error connecting to the API: {str(e)}"
|
| 32 |
except Exception as e:
|
| 33 |
+
return f"Unknown error: {str(e)}"
|
| 34 |
|
| 35 |
def solicitar_resize(image_path, width, height):
|
| 36 |
"""
|
|
|
|
| 42 |
try:
|
| 43 |
# Validar inputs
|
| 44 |
if width <= 0 or height <= 0:
|
| 45 |
+
print("Width and height must be positive.")
|
| 46 |
return None
|
| 47 |
|
| 48 |
payload = {"width": int(width), "height": int(height)}
|
|
|
|
| 66 |
|
| 67 |
# --- Construcción de la Interfaz con Blocks ---
|
| 68 |
with gr.Blocks(title="Predictor & Resizer API Client") as demo:
|
| 69 |
+
gr.Markdown("# Image API Client")
|
| 70 |
+
gr.Markdown("Upload an image and choose whether you want to get a prediction or resize it.")
|
| 71 |
|
| 72 |
with gr.Row():
|
| 73 |
+
# Left Column: Input
|
| 74 |
with gr.Column():
|
| 75 |
+
gr.Markdown("### 1. Input Image")
|
| 76 |
# Selector de imágenes. 'type="filepath"' guarda la imagen temporalmente y nos da la ruta
|
| 77 |
+
input_image = gr.Image(label="Upload your image", type="filepath")
|
| 78 |
|
| 79 |
+
# Right Column: Actions
|
| 80 |
with gr.Column():
|
| 81 |
|
| 82 |
+
# --- Prediction Section ---
|
| 83 |
+
gr.Markdown("### 2. Prediction")
|
| 84 |
+
predict_btn = gr.Button("🔍 Get Prediction", variant="primary")
|
| 85 |
+
predict_output = gr.Textbox(label="API Result")
|
| 86 |
|
| 87 |
# CORRECCIÓN AQUÍ: gr.HTML en mayúsculas
|
| 88 |
gr.HTML("<hr>")
|
| 89 |
|
| 90 |
+
# --- Resize Section ---
|
| 91 |
+
gr.Markdown("### 3. Resize Image")
|
| 92 |
with gr.Row():
|
| 93 |
+
w_input = gr.Number(label="Width", value=200, precision=0)
|
| 94 |
+
h_input = gr.Number(label="Height", value=200, precision=0)
|
| 95 |
|
| 96 |
+
resize_btn = gr.Button("🖼️ Resize Image")
|
| 97 |
+
resize_output = gr.Image(label="Resized Image")
|
| 98 |
|
| 99 |
+
# --- Connect Logic ---
|
| 100 |
|
| 101 |
+
# Prediction Button
|
| 102 |
predict_btn.click(
|
| 103 |
fn=solicitar_prediccion,
|
| 104 |
inputs=[input_image],
|
|
|
|
| 112 |
outputs=resize_output
|
| 113 |
)
|
| 114 |
|
| 115 |
+
# Launch the app
|
| 116 |
if __name__ == "__main__":
|
| 117 |
demo.launch()
|