Spaces:
Sleeping
Sleeping
Commit ·
f63e98a
1
Parent(s): 1a98f91
Fix Gradio schema bug: Interface+filepath, gradio-client>=1.4
Browse files
README.md
CHANGED
|
@@ -4,7 +4,7 @@ emoji: 🔬
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: "4.44.
|
| 8 |
python_version: "3.10"
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
|
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: "4.44.1"
|
| 8 |
python_version: "3.10"
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
app.py
CHANGED
|
@@ -3,7 +3,10 @@
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import os
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
import numpy as np
|
|
@@ -11,33 +14,18 @@ from PIL import Image
|
|
| 11 |
|
| 12 |
from prebackbone_infer import enrich_pair, get_enricher
|
| 13 |
|
| 14 |
-
EXAMPLES_DIR = Path(__file__).resolve().parent / "examples"
|
| 15 |
TITLE = "RefDiffNet — PCB Reference–Defect Enrichment"
|
| 16 |
DESCRIPTION = """
|
| 17 |
Upload a **defect PCB image** and its **golden reference** (same board, no defect).
|
| 18 |
-
The
|
| 19 |
-
|
| 20 |
-
Formula: `enriched = defect + α · gate · delta`
|
| 21 |
"""
|
| 22 |
|
| 23 |
|
| 24 |
-
def
|
| 25 |
-
if not
|
| 26 |
-
return []
|
| 27 |
-
inputs = sorted(EXAMPLES_DIR.glob("*_input.jpg"))
|
| 28 |
-
out: list[list[str]] = []
|
| 29 |
-
for inp in inputs:
|
| 30 |
-
ref = EXAMPLES_DIR / inp.name.replace("_input.", "_reference.")
|
| 31 |
-
if ref.exists():
|
| 32 |
-
out.append([str(inp), str(ref)])
|
| 33 |
-
return out
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
def run_demo(defect_img, reference_img):
|
| 37 |
-
if defect_img is None or reference_img is None:
|
| 38 |
raise gr.Error("Please upload both defect and reference images.")
|
| 39 |
|
| 40 |
-
defect_rgb, ref_rgb, enriched_rgb = enrich_pair(
|
| 41 |
|
| 42 |
h = max(defect_rgb.shape[0], ref_rgb.shape[0], enriched_rgb.shape[0])
|
| 43 |
|
|
@@ -54,33 +42,29 @@ def run_demo(defect_img, reference_img):
|
|
| 54 |
def _preload_model():
|
| 55 |
try:
|
| 56 |
get_enricher()
|
|
|
|
| 57 |
except FileNotFoundError as e:
|
| 58 |
-
print(f"[
|
| 59 |
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
| 62 |
_preload_model()
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
ex = _examples()
|
| 80 |
-
if ex:
|
| 81 |
-
gr.Examples(examples=ex, inputs=[defect_in, ref_in], label="Example pairs")
|
| 82 |
|
| 83 |
-
# HF Spaces: bind 0.0.0.0, never use share=True
|
| 84 |
demo.launch(
|
| 85 |
server_name="0.0.0.0",
|
| 86 |
server_port=int(os.environ.get("PORT", "7860")),
|
|
|
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import os
|
| 6 |
+
|
| 7 |
+
# Hugging Face Spaces: required before importing gradio
|
| 8 |
+
os.environ.setdefault("GRADIO_SERVER_NAME", "0.0.0.0")
|
| 9 |
+
os.environ.setdefault("GRADIO_SERVER_PORT", os.environ.get("PORT", "7860"))
|
| 10 |
|
| 11 |
import gradio as gr
|
| 12 |
import numpy as np
|
|
|
|
| 14 |
|
| 15 |
from prebackbone_infer import enrich_pair, get_enricher
|
| 16 |
|
|
|
|
| 17 |
TITLE = "RefDiffNet — PCB Reference–Defect Enrichment"
|
| 18 |
DESCRIPTION = """
|
| 19 |
Upload a **defect PCB image** and its **golden reference** (same board, no defect).
|
| 20 |
+
The A11_CA prebackbone outputs an **enriched** image: `enriched = defect + α · gate · delta`
|
|
|
|
|
|
|
| 21 |
"""
|
| 22 |
|
| 23 |
|
| 24 |
+
def run_demo(defect_path: str | None, reference_path: str | None):
|
| 25 |
+
if not defect_path or not reference_path:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
raise gr.Error("Please upload both defect and reference images.")
|
| 27 |
|
| 28 |
+
defect_rgb, ref_rgb, enriched_rgb = enrich_pair(defect_path, reference_path)
|
| 29 |
|
| 30 |
h = max(defect_rgb.shape[0], ref_rgb.shape[0], enriched_rgb.shape[0])
|
| 31 |
|
|
|
|
| 42 |
def _preload_model():
|
| 43 |
try:
|
| 44 |
get_enricher()
|
| 45 |
+
print("[RefDiffNet] Prebackbone loaded.")
|
| 46 |
except FileNotFoundError as e:
|
| 47 |
+
print(f"[RefDiffNet] WARN: {e}")
|
| 48 |
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
_preload_model()
|
| 52 |
|
| 53 |
+
demo = gr.Interface(
|
| 54 |
+
fn=run_demo,
|
| 55 |
+
inputs=[
|
| 56 |
+
gr.Image(type="filepath", label="Defect image (input)"),
|
| 57 |
+
gr.Image(type="filepath", label="Golden reference"),
|
| 58 |
+
],
|
| 59 |
+
outputs=[
|
| 60 |
+
gr.Image(type="pil", label="Enriched output"),
|
| 61 |
+
gr.Image(type="pil", label="Defect | Reference | Enriched"),
|
| 62 |
+
],
|
| 63 |
+
title=TITLE,
|
| 64 |
+
description=DESCRIPTION,
|
| 65 |
+
allow_flagging="never",
|
| 66 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
| 68 |
demo.launch(
|
| 69 |
server_name="0.0.0.0",
|
| 70 |
server_port=int(os.environ.get("PORT", "7860")),
|
setup.sh
CHANGED
|
@@ -4,7 +4,10 @@ set -euo pipefail
|
|
| 4 |
cd "$(dirname "$0")"
|
| 5 |
# HF base image may install huggingface_hub 1.x; Gradio 4.x needs <1.0 (HfFolder)
|
| 6 |
pip install -q "huggingface_hub>=0.23.0,<1.0"
|
|
|
|
|
|
|
| 7 |
export YOLO_CONFIG_DIR="${YOLO_CONFIG_DIR:-/tmp/Ultralytics}"
|
|
|
|
| 8 |
mkdir -p "${YOLO_CONFIG_DIR}"
|
| 9 |
|
| 10 |
if [[ -d vendor ]]; then
|
|
|
|
| 4 |
cd "$(dirname "$0")"
|
| 5 |
# HF base image may install huggingface_hub 1.x; Gradio 4.x needs <1.0 (HfFolder)
|
| 6 |
pip install -q "huggingface_hub>=0.23.0,<1.0"
|
| 7 |
+
# Fix Gradio 4.44.0 + pydantic schema bug (TypeError: bool is not iterable)
|
| 8 |
+
pip install -q "gradio-client>=1.4.0,<2.0.0"
|
| 9 |
export YOLO_CONFIG_DIR="${YOLO_CONFIG_DIR:-/tmp/Ultralytics}"
|
| 10 |
+
export GRADIO_SERVER_NAME="${GRADIO_SERVER_NAME:-0.0.0.0}"
|
| 11 |
mkdir -p "${YOLO_CONFIG_DIR}"
|
| 12 |
|
| 13 |
if [[ -d vendor ]]; then
|