Spaces:
Runtime error
Runtime error
File size: 1,860 Bytes
a140940 12bc6a8 a140940 12bc6a8 a140940 12bc6a8 a140940 12bc6a8 a140940 12bc6a8 a140940 | 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 | import io
import json
import requests
from PIL import Image
import gradio as gr
# Your Cloud Run backend endpoint
BACKEND_URL = "https://materi-backend-359263001449.europe-west1.run.app/predict"
def call_backend(image: Image.Image):
"""Send the uploaded image to the Cloud Run backend."""
if image is None:
return "No image provided."
# Convert PIL image to bytes
buf = io.BytesIO()
image.save(buf, format="PNG")
buf.seek(0)
files = {"file": ("input.png", buf, "image/png")}
try:
resp = requests.post(BACKEND_URL, files=files, timeout=60)
except Exception as e:
return f"Error contacting backend: {e}"
if not resp.ok:
return f"Backend error {resp.status_code}: {resp.text}"
try:
return json.dumps(resp.json(), indent=2)
except Exception:
return f"Non-JSON response:\n{resp.text}"
def build_ui():
"""Construct the Gradio UI."""
with gr.Blocks(title="MateriAI Defect Analysis") as demo:
gr.Markdown(
"""
# MateriAI – Public Defect Analysis
Upload an image and the request will be forwarded to a secure Cloud Run backend.
"""
)
with gr.Row():
with gr.Column():
input_image = gr.Image(type="pil", label="Upload image")
submit = gr.Button("Analyze", variant="primary")
with gr.Column():
output = gr.Textbox(
label="Backend response (JSON)",
lines=20,
show_copy_button=True,
)
submit.click(call_backend, inputs=input_image, outputs=output)
return demo
demo = build_ui()
# Required for Docker Spaces: must bind to 0.0.0.0:7860
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860) |