| |
| """ |
| app.py (HF Space ยท ์
๋ก๋ํ ๋ฐ๋ชจ) |
| ์ฌ์ฉ์๊ฐ ํ ๋ฐฐํฐ๋ฆฌ์ CT ์ฌ๋ผ์ด์ค(์ด๋ฏธ ํฌ๋กญ๋ ํ์ ์ด๋ฏธ์ง)๋ฅผ ์
๋ก๋ โ ์ถ๋ก โ 3์กด ํ์ + ์ค๋ฒ๋ ์ด + ๋ณด๊ณ ์ + IATA ์ง์นจ. |
| |
| AI-Hub ์๋ณธ ๋ฐ์ดํฐ๋ ๋ฐฐํฌํ์ง ์๋๋ค(๋ผ์ด์ ์ค). ๋ฐํ/์์ฐ ๋ ์ง์ ์ฌ๋ผ์ด์ค๋ฅผ ์
๋ก๋ํด ์ฌ์ฉ. |
| |
| ํ๊ฒฝ๋ณ์: |
| HF_MODEL_REPO ๊ฐ์ค์น ๋ฐ์ HF ๋ชจ๋ธ ๋ ํฌ (์: user/battery-ct-defect-models) |
| ANTHROPIC_API_KEY (Space Secret) RAG ์ง์นจ ์์ฑ์ฉ. ์์ผ๋ฉด ๊ท์ ๋งคํ ํด๋ฐฑ. |
| GLUE_NO_SEG=1 seg ์ ๋ฐ ๋ง์คํฌ ๋๊ธฐ(CPU Space ์๋์ฉ; ๋ฐ์ค๋ง ํ์) |
| """ |
| import os |
|
|
| import gradio as gr |
|
|
| from load_models import load_all |
| from infer import infer_battery |
| from decide import decide |
| from render import make_overlay, make_report, pick_slice, recommend |
|
|
| MODELS = load_all() |
| USE_SEG = os.environ.get("GLUE_NO_SEG", "") == "" |
|
|
|
|
| def _paths(files): |
| out = [] |
| for f in files or []: |
| out.append(f if isinstance(f, str) else getattr(f, "name", None)) |
| return [p for p in out if p] |
|
|
|
|
| def screen(files, form, battery_id): |
| paths = _paths(files) |
| if not paths: |
| return "โ ๏ธ CT ์ฌ๋ผ์ด์ค ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์", None, {}, "" |
|
|
| slices = [{"idx": i, "name": os.path.basename(p), "img_path": p} |
| for i, p in enumerate(paths)] |
| bid = (battery_id or "uploaded").strip() |
| result = infer_battery(MODELS, slices, form, bid) |
| decision = decide(result) |
| report = make_report(result, decision) |
| guide = recommend(result, decision) |
|
|
| if decision["zone"].startswith("๐ข"): |
| return decision["zone"], None, report, guide |
| cur, crop = pick_slice(result, slices) |
| seg_model = MODELS["seg"] if USE_SEG else None |
| overlay = make_overlay(crop, result, cur, seg_model) if crop is not None else None |
| return decision["zone"], overlay, report, guide |
|
|
|
|
| with gr.Blocks(title="๋ฐฐํฐ๋ฆฌ CT ๊ฒฐํจ ์คํฌ๋ฆฌ๋") as demo: |
| gr.Markdown( |
| "## ๐ ๋ฐฐํฐ๋ฆฌ CT ๋ด๋ถ๊ฒฐํจ ์คํฌ๋ฆฌ๋\n" |
| "ํ ๋ฐฐํฐ๋ฆฌ์ **CT ์ฌ๋ผ์ด์ค(ํฌ๋กญ๋ ํ์ ์ด๋ฏธ์ง)** ๋ฅผ ์
๋ก๋ํ๋ฉด " |
| "๋ค๊ณต์ฑยท๋ ์งยทํฝ์ฐฝ์ ํ์งํด **๐ข ์ด์์์ ยท ๐ก ๊ฒํ ํ์ ยท ๐ด ์ด์ ์์** ์ผ๋ก ํ์ ํ๊ณ , " |
| "IATA ํญ๊ณต์ด์ก ๊ท์ ๊ธฐ๋ฐ ์กฐ์น ์ง์นจ์ ์์ฑํฉ๋๋ค.\n" |
| "> โ ๏ธ AI-Hub ์๋ณธ ๋ฐ์ดํฐ๋ ํฌํจํ์ง ์์ต๋๋ค. ๋ณธ์ธ ์ฌ๋ผ์ด์ค๋ฅผ ์
๋ก๋ํ์ธ์." |
| ) |
| with gr.Row(): |
| with gr.Column(): |
| files = gr.File(file_count="multiple", file_types=["image"], |
| label="CT ์ฌ๋ผ์ด์ค ์
๋ก๋ (ํ ๋ฐฐํฐ๋ฆฌ)") |
| form = gr.Radio(["module", "cell"], value="module", |
| label="๋ฐฐํฐ๋ฆฌ ํํ (์ง์ ์ ํ)") |
| bid = gr.Textbox(label="๋ฐฐํฐ๋ฆฌ ID (์ ํ)", placeholder="์: module_402") |
| btn = gr.Button("๊ฒ์ฌ", variant="primary") |
| with gr.Column(): |
| verdict = gr.Label(label="ํ์ ") |
| image = gr.Image(label="์์น (๋ค๊ณต์ฑ=๋ง์คํฌ ยท ๋ ์ง=๋ฐ์ค)") |
| report = gr.JSON(label="๋ณด๊ณ ์") |
| guide = gr.Markdown(label="์กฐ์น ์ง์นจ (IATA ยท ํ/์)") |
|
|
| btn.click(screen, [files, form, bid], [verdict, image, report, guide]) |
|
|
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|