Spaces:
Runtime error
Runtime error
Download app.py from Materiai/AM-Defects-Google: direct link, hf CLI and curl.
- Browser
- Download file 1.86 kB
-
https://huggingface.co/spaces/Materiai/AM-Defects-Google/resolve/main/app.py
- Command line
-
hf download hf://spaces/Materiai/AM-Defects-Google/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Materiai/AM-Defects-Google/resolve/main/app.py
1.86 kB
| 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) |