Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,86 +1,230 @@
|
|
| 1 |
-
import os
|
| 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 |
if __name__ == "__main__":
|
| 85 |
-
demo.
|
| 86 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import zipfile
|
| 4 |
+
import tempfile
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# --- Imports do seu projeto (devem existir em src/...) ---
|
| 9 |
+
from src.io.load_images import load_png_group
|
| 10 |
+
from src.io.load_dicom import load_dicom_zip_group
|
| 11 |
+
from src.io.load_nifti import load_nifti_group
|
| 12 |
+
from src.io.load_mesh import load_mesh_group
|
| 13 |
+
|
| 14 |
+
from src.fuse.resample import resample_to_isotropic
|
| 15 |
+
from src.seg.seg_client_2d import segment_2d_batch
|
| 16 |
+
from src.seg.seg_client_3d import segment_3d_batch
|
| 17 |
+
from src.fuse.fusion import fuse_modalities
|
| 18 |
+
|
| 19 |
+
from src.design.socket_gen import build_socket_mask
|
| 20 |
+
from src.design.forearm_shell import build_forearm_shell_mask
|
| 21 |
+
from src.geom.mcubes import mask_to_mesh
|
| 22 |
+
from src.design.wrist_adapter import build_wrist_and_adapters
|
| 23 |
+
from src.design.hardware import apply_hardware
|
| 24 |
+
from src.design.assembly import save_parts_zip
|
| 25 |
+
|
| 26 |
+
from src.export.bom import make_bom
|
| 27 |
+
from src.export.docs import make_report
|
| 28 |
+
|
| 29 |
+
# --- Limites e constantes ---
|
| 30 |
+
MAX_BYTES = 500 * 1024 * 1024 # 500 MB por arquivo
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# =========================
|
| 34 |
+
# PIPELINE CORE
|
| 35 |
+
# =========================
|
| 36 |
+
def run_pipeline(
|
| 37 |
+
pngs, # lista de arquivos (gr.File com file_count="multiple")
|
| 38 |
+
dicoms, # lista de arquivos .zip
|
| 39 |
+
niftis, # lista de arquivos .nii / .nii.gz
|
| 40 |
+
meshes, # lista de arquivos .stl/.obj/.ply
|
| 41 |
+
wrist_type,
|
| 42 |
+
wall_mm,
|
| 43 |
+
off_in,
|
| 44 |
+
off_out,
|
| 45 |
+
screw_d,
|
| 46 |
+
screw_len,
|
| 47 |
+
left_right,
|
| 48 |
+
):
|
| 49 |
+
# 0) Sanidade básica
|
| 50 |
+
# Gradio v5 retorna [] quando não há arquivos; as funções de load já lidam com isso.
|
| 51 |
+
# Apenas garantimos mensagens amigáveis se absolutamente nada foi enviado:
|
| 52 |
+
if not any([pngs, dicoms, niftis, meshes]):
|
| 53 |
+
raise gr.Error("Envie ao menos um arquivo: PNG/JPG, DICOM ZIP, NIfTI ou STL/OBJ/PLY.")
|
| 54 |
+
|
| 55 |
+
# 1) LOAD – múltiplos por tipo
|
| 56 |
+
imgs2d = load_png_group(pngs, max_bytes=MAX_BYTES) # list[np.ndarray 2D]
|
| 57 |
+
vols_dcm = load_dicom_zip_group(dicoms, max_bytes=MAX_BYTES) # list[(vol3d, spacing)]
|
| 58 |
+
vols_nii = load_nifti_group(niftis, max_bytes=MAX_BYTES) # list[(vol3d, spacing)]
|
| 59 |
+
meshes_in = load_mesh_group(meshes, max_bytes=MAX_BYTES) # list[trimesh.Trimesh]
|
| 60 |
+
|
| 61 |
+
# 2) NORMALIZE/RESAMPLE – volumes para isotrópico 1.0 mm
|
| 62 |
+
vols_iso = []
|
| 63 |
+
for vol, sp in (vols_dcm + vols_nii):
|
| 64 |
+
vol_iso, sp_iso = resample_to_isotropic(vol, sp, iso=1.0)
|
| 65 |
+
vols_iso.append((vol_iso, sp_iso))
|
| 66 |
+
|
| 67 |
+
# 3) SEGMENTAÇÃO – Providers + fallback interno
|
| 68 |
+
seg2d = segment_2d_batch(imgs2d) # list[mask2D]
|
| 69 |
+
seg3d = segment_3d_batch(vols_iso) # list[mask3D]
|
| 70 |
+
|
| 71 |
+
# 4) FUSÃO – combina 2D/3D/meshes em um volume base + spacing
|
| 72 |
+
fused = fuse_modalities(seg2d, seg3d, meshes_in) # SimpleNamespace(mask, spacing)
|
| 73 |
+
|
| 74 |
+
# 5) DESIGN (máscaras -> soquete + casca)
|
| 75 |
+
socket_mask = build_socket_mask(
|
| 76 |
+
fused.mask, fused.spacing,
|
| 77 |
+
wall=wall_mm, off_in=off_in, off_out=off_out
|
| 78 |
+
)
|
| 79 |
+
shell_mask = build_forearm_shell_mask(fused.mask, fused.spacing)
|
| 80 |
+
|
| 81 |
+
# 6) MESHING – marching cubes
|
| 82 |
+
socket_mesh = mask_to_mesh(socket_mask, fused.spacing)
|
| 83 |
+
shell_mesh = mask_to_mesh(shell_mask, fused.spacing)
|
| 84 |
+
|
| 85 |
+
# 7) PUNHO/ADAPTADORES + HARDWARE
|
| 86 |
+
wrist_meshes = build_wrist_and_adapters(
|
| 87 |
+
socket_mesh, shell_mesh,
|
| 88 |
+
wrist_type=wrist_type, hand_side=left_right
|
| 89 |
+
)
|
| 90 |
+
parts = {
|
| 91 |
+
"socket": socket_mesh,
|
| 92 |
+
"forearm_shell": shell_mesh,
|
| 93 |
+
**wrist_meshes
|
| 94 |
+
}
|
| 95 |
+
parts_hw = apply_hardware(parts, screw_d=screw_d, screw_len=screw_len)
|
| 96 |
+
|
| 97 |
+
# 8) EXPORT – ZIP com peças + BOM + PDF
|
| 98 |
+
td = Path(tempfile.mkdtemp())
|
| 99 |
+
out_dir = td / "out"
|
| 100 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 101 |
+
|
| 102 |
+
zip_path = save_parts_zip(parts_hw, out_dir=out_dir) # retorna caminho do ZIP
|
| 103 |
+
bom_csv = make_bom(parts_hw, out_dir=out_dir) # retorna caminho do CSV
|
| 104 |
+
report_pdf = make_report(
|
| 105 |
+
parts_hw,
|
| 106 |
+
out_dir=out_dir,
|
| 107 |
+
params={
|
| 108 |
+
"wrist_type": wrist_type,
|
| 109 |
+
"wall": wall_mm,
|
| 110 |
+
"off_in": off_in,
|
| 111 |
+
"off_out": off_out,
|
| 112 |
+
"side": left_right,
|
| 113 |
+
},
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# 9) PREVIEW – exibe o soquete no Model3D (caminho .stl)
|
| 117 |
+
preview = str(out_dir / "socket.stl")
|
| 118 |
+
|
| 119 |
+
# 10) LER ZIP COMO BYTES (para gr.File output)
|
| 120 |
+
with open(zip_path, "rb") as f:
|
| 121 |
+
zip_bytes = f.read()
|
| 122 |
+
|
| 123 |
+
# 11) RETORNOS (ordem deve bater com outputs do .click)
|
| 124 |
+
# - preview: caminho .stl (Model3D)
|
| 125 |
+
# - zip_out: (filename, bytes)
|
| 126 |
+
# - bom_out: caminho .csv
|
| 127 |
+
# - pdf_out: caminho .pdf
|
| 128 |
+
return preview, ("pro_t_transradial.zip", zip_bytes), str(bom_csv), str(report_pdf)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
# =========================
|
| 132 |
+
# UI
|
| 133 |
+
# =========================
|
| 134 |
+
with gr.Blocks(title="Pro T – Transradial (Providers, Multimodal)") as demo:
|
| 135 |
+
gr.Markdown(
|
| 136 |
+
"# 🦾 Pro T – Transradial (Providers, Multimodal)\n"
|
| 137 |
+
"Envie **PNG/JPG**, **DICOM (ZIP)**, **NIfTI (.nii/.nii.gz)** e/ou **STL/OBJ/PLY**.\n"
|
| 138 |
+
"O sistema funde as fontes e gera as **peças separadas** (STL/OBJ) + **BOM** e **Relatório**.\n\n"
|
| 139 |
+
"> **Aviso**: Uso para P&D. Validação clínica e regulatória obrigatórias antes de uso em pacientes."
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
with gr.Row():
|
| 143 |
+
with gr.Column():
|
| 144 |
+
# Gradio 5.x: File com múltiplos
|
| 145 |
+
pngs = gr.File(
|
| 146 |
+
label="PNG/JPG (flatbed)",
|
| 147 |
+
file_count="multiple",
|
| 148 |
+
file_types=["image"]
|
| 149 |
+
)
|
| 150 |
+
dicoms = gr.File(
|
| 151 |
+
label="DICOM (.zip)",
|
| 152 |
+
file_count="multiple",
|
| 153 |
+
file_types=[".zip"]
|
| 154 |
+
)
|
| 155 |
+
niftis = gr.File(
|
| 156 |
+
label="NIfTI (.nii/.nii.gz)",
|
| 157 |
+
file_count="multiple",
|
| 158 |
+
file_types=[".nii", ".nii.gz"]
|
| 159 |
+
)
|
| 160 |
+
meshes = gr.File(
|
| 161 |
+
label="Meshes (STL/OBJ/PLY)",
|
| 162 |
+
file_count="multiple",
|
| 163 |
+
file_types=[".stl", ".obj", ".ply"]
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
wrist = gr.Dropdown(
|
| 167 |
+
["passivo", "articulado"],
|
| 168 |
+
value="passivo",
|
| 169 |
+
label="Tipo de punho/junta"
|
| 170 |
+
)
|
| 171 |
+
side = gr.Radio(
|
| 172 |
+
["esquerdo", "direito"],
|
| 173 |
+
value="direito",
|
| 174 |
+
label="Lado"
|
| 175 |
+
)
|
| 176 |
+
wall = gr.Slider(
|
| 177 |
+
minimum=2.0,
|
| 178 |
+
maximum=5.0,
|
| 179 |
+
value=3.0,
|
| 180 |
+
step=0.1,
|
| 181 |
+
label="Espessura do soquete (mm)"
|
| 182 |
+
)
|
| 183 |
+
offin = gr.Slider(
|
| 184 |
+
minimum=0.5,
|
| 185 |
+
maximum=2.0,
|
| 186 |
+
value=1.0,
|
| 187 |
+
step=0.1,
|
| 188 |
+
label="Offset interno (mm)"
|
| 189 |
+
)
|
| 190 |
+
offout = gr.Slider(
|
| 191 |
+
minimum=2.0,
|
| 192 |
+
maximum=6.0,
|
| 193 |
+
value=3.0,
|
| 194 |
+
step=0.1,
|
| 195 |
+
label="Offset externo (mm)"
|
| 196 |
+
)
|
| 197 |
+
screwD = gr.Dropdown(
|
| 198 |
+
["M3", "M4"],
|
| 199 |
+
value="M3",
|
| 200 |
+
label="Parafuso (diâmetro)"
|
| 201 |
+
)
|
| 202 |
+
screwL = gr.Dropdown(
|
| 203 |
+
["8", "10", "12", "16"],
|
| 204 |
+
value="10",
|
| 205 |
+
label="Parafuso (comprimento, mm)"
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
run = gr.Button("Gerar conjunto (STL/OBJ + BOM + PDF)")
|
| 209 |
+
|
| 210 |
+
with gr.Column():
|
| 211 |
+
preview = gr.Model3D(label="Prévia do soquete")
|
| 212 |
+
zip_out = gr.File(label="Download ZIP (peças)")
|
| 213 |
+
bom_out = gr.File(label="BOM.csv")
|
| 214 |
+
pdf_out = gr.File(label="Relatório PDF")
|
| 215 |
+
|
| 216 |
+
run.click(
|
| 217 |
+
run_pipeline,
|
| 218 |
+
inputs=[
|
| 219 |
+
pngs, dicoms, niftis, meshes,
|
| 220 |
+
wrist, wall, offin, offout,
|
| 221 |
+
screwD, screwL, side
|
| 222 |
+
],
|
| 223 |
+
outputs=[preview, zip_out, bom_out, pdf_out],
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
# Gradio 5.x: fila sem argumentos extras
|
| 227 |
+
demo.queue()
|
| 228 |
|
| 229 |
if __name__ == "__main__":
|
| 230 |
+
demo.launch()
|
|
|