Spaces:
Sleeping
Sleeping
File size: 3,889 Bytes
2cccbaa b0a9e25 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa 0f8398d 2cccbaa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | import gradio as gr
import requests
from PIL import Image
import io
# URL de tu API
# Si estás ejecutando esto en local, suele ser http://127.0.0.1:8000
# Si la API está en Render, usa la URL de Render (ej: https://tuproyecto.onrender.com)
API_URL = "https://predictor-api-lab3.onrender.com"
def solicitar_prediccion(image_path):
"""
Envía la imagen al endpoint /predict
"""
if image_path is None:
return "You must upload an image first."
try:
# Abrimos la imagen en modo binario para enviarla
with open(image_path, "rb") as f:
files = {"file": f}
response = requests.post(f"{API_URL}/predict", files=files, timeout=10)
response.raise_for_status()
data = response.json()
# Devolvemos la predicción
return f"Prediction: {data.get('prediction')}"
except requests.exceptions.RequestException as e:
return f"Error connecting to the API: {str(e)}"
except Exception as e:
return f"Unknown error: {str(e)}"
def solicitar_resize(image_path, width, height):
"""
Envía la imagen y dimensiones al endpoint /resize
"""
if image_path is None:
return None
try:
# Validar inputs
if width <= 0 or height <= 0:
print("Width and height must be positive.")
return None
payload = {"width": int(width), "height": int(height)}
with open(image_path, "rb") as f:
files = {"file": f}
# Nota: 'data' se usa para los campos del Form (width, height)
# y 'files' para el archivo
response = requests.post(f"{API_URL}/resize", data=payload, files=files, timeout=10)
response.raise_for_status()
# La API devuelve una imagen en bytes (StreamingResponse)
# La convertimos a objeto PIL Image para que Gradio la pueda mostrar
image_stream = io.BytesIO(response.content)
return Image.open(image_stream)
except requests.exceptions.RequestException as e:
print(f"Error API: {e}")
return None
# --- Construcción de la Interfaz con Blocks ---
with gr.Blocks(title="Predictor & Resizer API Client") as demo:
gr.Markdown("# Image API Client")
gr.Markdown("Upload an image and choose whether you want to get a prediction or resize it.")
with gr.Row():
# Left Column: Input
with gr.Column():
gr.Markdown("### 1. Input Image")
# Selector de imágenes. 'type="filepath"' guarda la imagen temporalmente y nos da la ruta
input_image = gr.Image(label="Upload your image", type="filepath")
# Right Column: Actions
with gr.Column():
# --- Prediction Section ---
gr.Markdown("### 2. Prediction")
predict_btn = gr.Button("🔍 Get Prediction", variant="primary")
predict_output = gr.Textbox(label="API Result")
# CORRECCIÓN AQUÍ: gr.HTML en mayúsculas
gr.HTML("<hr>")
# --- Resize Section ---
gr.Markdown("### 3. Resize Image")
with gr.Row():
w_input = gr.Number(label="Width", value=200, precision=0)
h_input = gr.Number(label="Height", value=200, precision=0)
resize_btn = gr.Button("🖼️ Resize Image")
resize_output = gr.Image(label="Resized Image")
# --- Connect Logic ---
# Prediction Button
predict_btn.click(
fn=solicitar_prediccion,
inputs=[input_image],
outputs=predict_output
)
# Botón Resize
resize_btn.click(
fn=solicitar_resize,
inputs=[input_image, w_input, h_input],
outputs=resize_output
)
# Launch the app
if __name__ == "__main__":
demo.launch()
|