| import json |
| import re |
| from typing import Any, Dict, List, Optional, Sequence |
|
|
| import gradio as gr |
| import numpy as np |
| import paddle |
| import torch |
| from PIL import Image, ImageDraw |
| from paddleocr import PaddleOCR |
| from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline |
|
|
| |
| |
| OCR_LANGS = ["korean", "en"] |
| LLM_MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct" |
|
|
|
|
| def _load_ocr(): |
| use_gpu = torch.cuda.is_available() |
| device = "gpu" if use_gpu else "cpu" |
| paddle.device.set_device(device) |
| return PaddleOCR( |
| lang=OCR_LANGS[0], |
| use_textline_orientation=True, |
| text_det_limit_side_len=2048, |
| text_det_box_thresh=0.5, |
| det_model_dir=None, |
| rec_model_dir=None, |
| ) |
|
|
|
|
| ocr_reader = _load_ocr() |
|
|
|
|
| def _load_llm(): |
| device_map = "auto" if torch.cuda.is_available() else None |
| dtype = torch.float16 if torch.cuda.is_available() else torch.float32 |
| model = AutoModelForCausalLM.from_pretrained( |
| LLM_MODEL_ID, |
| device_map=device_map, |
| torch_dtype=dtype, |
| trust_remote_code=True, |
| ) |
| if device_map is None: |
| model = model.to(torch.device("cpu")) |
| tokenizer = AutoTokenizer.from_pretrained(LLM_MODEL_ID, trust_remote_code=True) |
| return model, tokenizer |
|
|
|
|
| LLM_MODEL, LLM_TOKENIZER = _load_llm() |
|
|
| |
| TIME_KEYWORDS = [ |
| "์์นจ", |
| "์ ์ฌ", |
| "์ ๋
", |
| "์ทจ์นจ", |
| "์๊ธฐ", |
| "์์ ", |
| "์ํ", |
| "์๊ฐ", |
| "๊ธฐ์", |
| ] |
|
|
| |
| MED_KNOWLEDGE: Sequence[Dict[str, Any]] = [ |
| { |
| "keywords": ["ํ์ด๋ ๋", "์์ธํธ์๋ฏธ๋
ธํ", "acetaminophen"], |
| "category": "์งํตยทํด์ด์ ", |
| "what_it_does": "๋ชธ์ด์ด๋ ๊ฐ๊ธฐ๋ก ์ด์ด ๋๊ฑฐ๋ ๋จธ๋ฆฌ๊ฐ ์ํ ๋ ํต์ฆ๊ณผ ์ด์ ๋ฎ์ถฐ ์ค๋๋ค.", |
| "example": "์: ์ํ์ํ ์ค๋น๋ก ๊ธด์ฅํ๋๋ฐ ๋จธ๋ฆฌ๊ฐ ์ง๋๊ฑฐ๋ฆด ๋, ํ ์ ๋ณต์ฉํ๋ฉด ํต์ฆ์ด ์ค์ด๋ญ๋๋ค.", |
| "tip": "์์ ๋ถ๋ด์ ์ค์ด๊ธฐ ์ํด ๊ฐ๋จํ ๊ฐ์๊ณผ ํจ๊ป ๋ฌผ๊ณผ ๋ณต์ฉํ๊ณ , ํ๋ฃจ ์ด ๋ณต์ฉ ํ์(์ผ๋ฐ์ ์ผ๋ก 4ํ ์ดํ)๋ฅผ ๋๊ธฐ์ง ๋ง์ธ์.", |
| }, |
| { |
| "keywords": ["์ด๋ถํ๋กํ", "๋ถ๋ฃจํ", "ibuprofen"], |
| "category": "์งํตยท์์ผ์ ", |
| "what_it_does": "๋ชธ์ ์ผ์ฆ์ ๊ฐ๋ผ์ํ๊ณ ํต์ฆ์ ์ํํด์ ๊ทผ์กํต์ด๋ ์นํต์ ์์ฃผ ์ฌ์ฉ๋ฉ๋๋ค.", |
| "example": "์: ์ฒด์ก ์๊ฐ์ ๋ฌด๋ฆ์ ์ด์ง ์์์ ๋ ๋ถ๊ธฐ์ ์ํ์ ์ค์ฌ ์ค๋๋ค.", |
| "tip": "์ํ์ ๋ณต์ฉํ๋ฉด ์ ์ฐ๋ฆผ์ ์ค์ผ ์ ์๊ณ , ๋ค๋ฅธ ์์ผ์งํต์ ์๋ ์๊ฐ ๊ฐ๊ฒฉ์ ๋์ธ์.", |
| }, |
| { |
| "keywords": ["์์", "์ธํฐ๋ฆฌ์ง", "cetirizine", "์ง๋ฅดํ
"], |
| "category": "์๋ ๋ฅด๊ธฐ ์ํ์ ", |
| "what_it_does": "์ฝ๊ฐ ๊ฐ์ง๊ฑฐ๋ฆฌ๊ฑฐ๋ ํผ๋ถ๊ฐ ๊ฐ๋ ค์ธ ๋ ์๋ ๋ฅด๊ธฐ ๋ฐ์์ ๊ฐ๋ผ์ํ ์ค๋๋ค.", |
| "example": "์: ๋ด์ฒ ๊ฝ๊ฐ๋ฃจ ๋๋ฌธ์ ๊ธฐ์นจ๊ณผ ์ฝง๋ฌผ์ด ๋์ฌ ๋ ์ฆ์์ ์ค์ฌ ์ค๋๋ค.", |
| "tip": "์กธ๋ฆด ์ ์์ผ๋ ์ฒซ ๋ณต์ฉ ํ์๋ ์ด์ ์ด๋ ์ง์ค์ด ํ์ํ ํ๋์ ํผํ์ธ์.", |
| }, |
| { |
| "keywords": ["ํผ์คํ", "pancreatin", "์์ฅ", "์ํ์ "], |
| "category": "์ํ์ ", |
| "what_it_does": "๊ธฐ๋ฆ์ง ์์์ ๋จน๊ณ ๋ฐฐ๊ฐ ๋๋ถ๋ฃฉํ ๋ ์ํ๋ฅผ ๋์ ์์ ํธํ๊ฒ ํด ์ค๋๋ค.", |
| "example": "์: ์นํจ์ ๋ง์ด ๋จน์ด ์์ด ๋๋ถ๋ฃฉํ ๋ ์์ ๊ฐ๋ณ๊ฒ ํด ์ค๋๋ค.", |
| "tip": "์ํ์ ๋ณต์ฉํ๋ฉด ํจ๊ณผ๊ฐ ์ข์ผ๋ฉฐ, ๋ณตํต์ด ๊ณ์๋๋ฉด ๋ณ์์ ๋ฐฉ๋ฌธํ์ธ์.", |
| }, |
| { |
| "keywords": ["๋นํ๋ฏผ", "multivitamin", "vitamin"], |
| "category": "์์์ ", |
| "what_it_does": "๋ชธ์ ํ์ํ ๋นํ๋ฏผ์ ์ฑ์ ํผ๊ณคํจ์ ์ค์ด๊ณ ๋ฉด์ญ๋ ฅ์ ๋์ต๋๋ค.", |
| "example": "์: ์ํ ์ค๋น๋ก ์ ์ ์ค์์ ๋ ๋ชธ์ด ์ง์น์ง ์๋๋ก ๋์์ค๋๋ค.", |
| "tip": "ํ๋ฃจ ๊ถ์ฅ๋์ ์ง์ผ ๊พธ์คํ ๋ณต์ฉํ๋ฉด ๋ ํจ๊ณผ์ ์ด๋ฉฐ, ๋ฌผ๊ณผ ํจ๊ป ์ผํค์ธ์.", |
| }, |
| ] |
|
|
|
|
| def _extract_time_slots(text: str) -> List[str]: |
| slots = [] |
| for kw in TIME_KEYWORDS: |
| if kw in text: |
| slots.append(kw) |
| |
| for match in re.findall(r"(\d{1,2}[:์]\d{0,2})", text): |
| norm = match.replace("์", ":") |
| if norm.endswith(":"): |
| norm += "00" |
| if norm not in slots: |
| slots.append(norm) |
| return slots |
|
|
|
|
| STOPWORDS = {"์ฉ๋ฒ", "์ฉ๋", "๋ณต์ฉ", "๋ฐฉ๋ฒ", "์ฝ", "์ "} |
|
|
|
|
| def _extract_medications(text: str) -> List[Dict[str, Optional[str]]]: |
| meds: List[Dict[str, Optional[str]]] = [] |
| pattern = re.compile( |
| r"([๊ฐ-ํฃA-Za-z]{2,})[\sยท]*(\d+[\./]?\d*\s*(?:mg|mL|ML|ml|์ |์บก์))?" |
| ) |
| seen: set[str] = set() |
| for match in pattern.finditer(text): |
| name = match.group(1) |
| if name in STOPWORDS or len(name) <= 1: |
| continue |
| if any(sw in name for sw in STOPWORDS): |
| continue |
| name_norm = name.strip() |
| if name_norm in seen: |
| continue |
| seen.add(name_norm) |
| dose = match.group(2).strip() if match.group(2) else None |
| meds.append({"name": name_norm, "dose": dose}) |
| return meds |
|
|
|
|
| def parse_fields(raw: str) -> Dict[str, Any]: |
| """Extract drug name and dosage information from OCR text.""" |
| collapsed = raw.replace("\n", " ") |
| collapsed = re.sub(r"\s+", " ", collapsed) |
|
|
| medications = _extract_medications(collapsed) |
|
|
| first = medications[0] if medications else {"name": None, "dose": None} |
| drug_name = first.get("name") |
| dose_per_intake = first.get("dose") |
|
|
| times_per_day: Optional[int] = None |
| times_match = re.search(r"(?:1์ผ|ํ๋ฃจ)\s*(\d+)\s*ํ", collapsed) |
| if times_match: |
| times_per_day = int(times_match.group(1)) |
|
|
| time_slots = _extract_time_slots(collapsed) |
|
|
| return { |
| "drug_name": drug_name, |
| "dose_per_intake": dose_per_intake, |
| "times_per_day": times_per_day, |
| "time_slots": time_slots or None, |
| "medications": medications, |
| } |
|
|
|
|
| def ocr_and_parse(image: Image.Image) -> Dict[str, Any]: |
| np_img = np.array(image.convert("RGB")) |
| ocr_results = ocr_reader.ocr(np_img) |
|
|
| segments: List[Dict[str, Any]] = [] |
| lines: List[str] = [] |
| for result in ocr_results: |
| if not result: |
| continue |
| for entry in result: |
| if not entry: |
| continue |
| bbox = entry[0] |
| text = "" |
| confidence = 1.0 |
| if len(entry) == 2: |
| text_info = entry[1] |
| if isinstance(text_info, (list, tuple)) and text_info: |
| text = text_info[0] or "" |
| if len(text_info) > 1 and text_info[1] is not None: |
| confidence = float(text_info[1]) |
| else: |
| text = str(text_info) |
| elif len(entry) >= 3: |
| text = entry[1] or "" |
| raw_conf = entry[2] |
| try: |
| if raw_conf is not None: |
| confidence = float(raw_conf) |
| except (TypeError, ValueError): |
| confidence = 1.0 |
|
|
| cleaned = text.strip() |
| if not cleaned: |
| continue |
| lines.append(cleaned) |
| try: |
| box_arr = np.asarray(bbox, dtype=float) |
| box_serializable = box_arr.tolist() |
| except (TypeError, ValueError): |
| box_serializable = None |
| segments.append({ |
| "text": cleaned, |
| "confidence": float(confidence), |
| "bbox": box_serializable, |
| }) |
|
|
| raw_text = "\n".join(lines) |
| fields = parse_fields(raw_text) |
|
|
| warnings: List[str] = [] |
| if not fields["drug_name"]: |
| warnings.append("์ฝ ์ด๋ฆ ์ธ์์ด ๋ถํ์คํฉ๋๋ค.") |
| if not fields["times_per_day"]: |
| warnings.append("1์ผ ํ์๋ฅผ ์ฐพ์ง ๋ชปํ์ต๋๋ค (์: 1์ผ 3ํ).") |
|
|
| return { |
| "raw_text": raw_text, |
| "fields": fields, |
| "warnings": warnings, |
| "segments": segments, |
| } |
|
|
|
|
| def render_card(fields: Dict[str, Any]) -> Image.Image: |
| width, height = 720, 400 |
| img = Image.new("RGB", (width, height), "white") |
| draw = ImageDraw.Draw(img) |
|
|
| header_text = "์ค๋ ๋ณต์ฉ ์ผ์ " |
| draw.rectangle((0, 0, width, 60), fill=(230, 240, 255)) |
| draw.text((24, 18), header_text, fill=(0, 0, 0)) |
|
|
| y = 90 |
|
|
| def add_line(label: str, value: Optional[str]): |
| nonlocal y |
| draw.text((24, y), label, fill=(60, 60, 60)) |
| display = value if value else "-" |
| draw.text((180, y), f": {display}", fill=(0, 0, 0)) |
| y += 34 |
|
|
| add_line("์ฝ ์ด๋ฆ", fields.get("drug_name")) |
| add_line("1ํ ์ฉ๋", fields.get("dose_per_intake")) |
| add_line("1์ผ ํ์", str(fields.get("times_per_day") or "")) |
|
|
| slots = fields.get("time_slots") or [] |
| add_line("์๊ฐ๋", ", ".join(slots) if slots else None) |
|
|
| footer = "โป ์๋ฃ์ง ์ฒ๋ฐฉ์ด ์ฐ์ ์ด๋ฉฐ, ๋ณธ ์ฑ์ ์ฐธ๊ณ ์ฉ์
๋๋ค." |
| draw.text((24, height - 60), footer, fill=(120, 120, 120)) |
| return img |
|
|
|
|
| def to_csv_row(output: Dict[str, Any]) -> str: |
| fields = output["fields"] |
| row = [ |
| fields.get("drug_name") or "", |
| fields.get("dose_per_intake") or "", |
| str(fields.get("times_per_day") or ""), |
| ";".join(fields.get("time_slots") or []), |
| ] |
| return ",".join(row) |
|
|
|
|
| def _match_knowledge(name: str) -> Optional[Dict[str, Any]]: |
| lowered = name.lower() |
| for info in MED_KNOWLEDGE: |
| for kw in info["keywords"]: |
| if kw.lower() in lowered or lowered in kw.lower(): |
| return info |
| return None |
|
|
|
|
| def build_kb_explanations(output: Dict[str, Any]) -> str: |
| meds = output["fields"].get("medications") or [] |
| if not meds: |
| return ( |
| "### ์ฝ ์ค๋ช
\n" |
| "- ์ฝ ์ด๋ฆ์ ์ ํํ ์ธ์ํ์ง ๋ชปํ์ด์. ์ฌ์ง์ ๋ค์ ์ฐ๊ฑฐ๋ ์ฝ์ฌ์๊ฒ ์ง์ ํ์ธํด ์ฃผ์ธ์.\n" |
| "\n> โ ๏ธ ์๋ฃ์ง ์ฒ๋ฐฉ๊ณผ ๋ณต์ฝ ์ง์๊ฐ ๊ฐ์ฅ ์ฐ์ ์
๋๋ค." |
| ) |
|
|
| lines = ["### ์ฝ๊ฒ ์์๋ณด๋ ์ฝ ์ค๋ช
"] |
| for med in meds: |
| name = med.get("name") or "์ด๋ฆ ๋ฏธํ์ธ" |
| info = _match_knowledge(name) if name else None |
| dose = med.get("dose") |
| if info: |
| lines.append( |
| f"- **{name}** ({info['category']})" |
| ) |
| if dose: |
| lines.append(f" - ์ฝ ๋ดํฌ์ ์ ํ ์ฉ๋: `{dose}`") |
| lines.append(f" - ํ๋ ์ผ: {info['what_it_does']}") |
| lines.append(f" - ์คํ์ ์์: {info['example']}") |
| lines.append(f" - ๋ณต์ฉ ํ: {info['tip']}") |
| else: |
| lines.append(f"- **{name}**") |
| if dose: |
| lines.append(f" - ์ฝ ๋ดํฌ ์ฉ๋: `{dose}`") |
| lines.append( |
| " - ์์ง ๋ฐ์ดํฐ๊ฐ ์์ด์. ์ฝ ์ด๋ฆ์ ๋ค์ ํ์ธํ๊ฑฐ๋ ์ฝ์ฌ์๊ฒ ๋ฌผ์ด๋ณด์ธ์." |
| ) |
|
|
| lines.append("\n> โ ๏ธ ์ค์ ๋ณต์ฝ์ ์์ฌยท์ฝ์ฌ์ ์ง์์ ๋ฐ๋์ ๋ฐ๋ฅด์ธ์.") |
| return "\n".join(lines) |
|
|
|
|
| def generate_llm_explanations(output: Dict[str, Any]) -> str: |
| meds = output["fields"].get("medications") or [] |
| if not meds: |
| return ( |
| "์ฝ ์ด๋ฆ์ ์ ๋๋ก ์ธ์ํ์ง ๋ชปํ์ด์. ์ฌ์ง์ ๋ค์ ์ฐ๊ฑฐ๋ ์ฝ์ฌ์๊ฒ ์ง์ ํ์ธํด ์ฃผ์ธ์." |
| ) |
|
|
| med_lines = [] |
| for idx, med in enumerate(meds, 1): |
| name = med.get("name") or "์ด๋ฆ ๋ฏธํ์ธ" |
| dose = med.get("dose") or "์ฉ๋ ์ ๋ณด ์์" |
| med_lines.append(f"{idx}. {name} โ {dose}") |
|
|
| context = "\n".join(med_lines) |
| raw_text = output.get("raw_text", "") |
|
|
| system_prompt = ( |
| "๋น์ ์ ์ฝ์ฌ ์ ์๋์
๋๋ค. ์ด๋ ค์ด ์ํ ์ฉ์ด๋ฅผ ์ฐ์ง ๋ง๊ณ , ์คํ์๋ ์ดํดํ ์ ์๋ ๋งํฌ๋ก ์น์ ํ๊ฒ ์ค๋ช
ํ์ธ์." |
| ) |
| user_prompt = ( |
| "๋ค์์ ์ฝ๋ดํฌ์์ OCR๋ก ์ถ์ถํ ์ ์ฒด ํ
์คํธ์
๋๋ค. ์ฝ ์ด๋ฆ๊ณผ ๋ณต์ฉ ์ง์๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ ์ฝ์ ์ ๋ณด๋ฅผ ์์ฃผ ์ฝ๊ฒ ์ ๋ฆฌํด ์ฃผ์ธ์.\n" |
| "์๊ตฌ ์ฌํญ:\n" |
| "1. ๊ฐ ์ฝ๋ง๋ค ์๋ ํญ๋ชฉ์ bullet ํ์์ผ๋ก ์์ฑํฉ๋๋ค.\n" |
| " - ์ฝ ์ด๋ฆ: (๊ฐ๋ฅํ๋ฉด ํ๊ธ/์๋ฌธ ๋ณ๊ธฐ)\n" |
| " - ์ด๋ค ์ฝ์ธ์ง ํ ์ค ์ค๋ช
\n" |
| " - ๋ณต์ฉ ์์: ์ธ์ , ์ด๋ค ์ํฉ์์ ๋ณต์ฉํ๋ฉด ์ข์์ง ์์\n" |
| " - ๋ณต์ฉ ๋ฐฉ๋ฒ ์์: 1ํ ์ฉ๋/ํ๋ฃจ ํ์๊ฐ ์๋ค๋ฉด ์ธ๊ธ\n" |
| " - ๋ถ์์ฉ ๋๋ ์ฃผ์์ฌํญ: ํํ ๋ถ์์ฉ, ํผํด์ผ ํ ํ๋\n" |
| "2. ์ด๋ ค์ด ์ํ ์ฉ์ด๋ ํผํ๊ณ , ์คํ์๋ ์ดํดํ ์ ์๋ ๋งํฌ๋ก ์์ฑํฉ๋๋ค.\n" |
| "3. ์ฝ ์ด๋ฆ์ ํ์คํ ๋ชจ๋ฅด๋ฉด โ์ด๋ฆ ๋ฏธํ์ธโ์ด๋ผ๊ณ ์ฐ๊ณ , ์ฝ์ฌ์๊ฒ ํ์ธํ๋ผ๊ณ ์๋ดํฉ๋๋ค.\n" |
| "4. ๋ง์ง๋ง ๋ฌธ๋จ์ ๋ฐ๋์ โ์ค์ ๋ณต์ฝ์ ์์ฌยท์ฝ์ฌ์ ์ง์๋ฅผ ๋ฐ๋ฅด์ธ์โ ๋ฌธ์ฅ์ ํฌํจํ์ธ์.\n" |
| f"\n์ฝ ๋ชฉ๋ก(์ถ์ถ ์์ฝ):\n{context}\n\nOCR ์๋ฌธ ์ ์ฒด:\n{raw_text}\n" |
| ) |
|
|
| messages = [ |
| {"role": "system", "content": system_prompt}, |
| {"role": "user", "content": user_prompt}, |
| ] |
|
|
| input_ids = LLM_TOKENIZER.apply_chat_template( |
| messages, |
| add_generation_prompt=True, |
| return_tensors="pt", |
| ) |
| input_ids = input_ids.to(LLM_MODEL.device) |
|
|
| with torch.no_grad(): |
| output_ids = LLM_MODEL.generate( |
| input_ids, |
| max_new_tokens=480, |
| temperature=0.7, |
| top_p=0.9, |
| do_sample=True, |
| eos_token_id=LLM_TOKENIZER.eos_token_id, |
| ) |
|
|
| generated_ids = output_ids[0][input_ids.shape[1]:] |
| text = LLM_TOKENIZER.decode(generated_ids, skip_special_tokens=True).strip() |
| return text |
|
|
|
|
| def build_explanations(output: Dict[str, Any]) -> str: |
| try: |
| llm_text = generate_llm_explanations(output) |
| if llm_text: |
| return llm_text |
| except Exception as err: |
| print(f"[WARN] LLM generation failed: {err}", flush=True) |
| return build_kb_explanations(output) |
|
|
|
|
| def format_warnings(warnings: List[str]) -> str: |
| if not warnings: |
| return "โ
์ธ์๋ ์ ๋ณด๊ฐ ์ถฉ๋ถํด์. ๋ณต์ฝ ์๊ฐ๋ง ์ ์ง์ผ ์ฃผ์ธ์." |
| lines = ["### ํ์ธํด ์ฃผ์ธ์"] |
| for warn in warnings: |
| lines.append(f"- {warn}") |
| lines.append("\n> ์๋ฃ์ง์ ์ง์๊ฐ ๊ฐ์ฅ ์ ํํฉ๋๋ค.") |
| return "\n".join(lines) |
|
|
|
|
| def run_pipeline(image: Optional[Image.Image]): |
| if image is None: |
| return ( |
| "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์.", |
| None, |
| None, |
| "์ด๋ฏธ์ง๋ฅผ ๋จผ์ ์
๋ก๋ํด ์ฃผ์ธ์.", |
| "๐ท ์ฝ ๋ดํฌ ์ฌ์ง์ ์ฌ๋ฆฌ๋ฉด ์ธ์์ด ์์๋ผ์.", |
| "", |
| ) |
|
|
| output = ocr_and_parse(image) |
| card = render_card(output["fields"]) |
| csv_row = to_csv_row(output) |
| json_text = json.dumps(output, ensure_ascii=False, indent=2) |
| explanations = build_explanations(output) |
| warnings_md = format_warnings(output.get("warnings", [])) |
| return json_text, card, csv_row, explanations, warnings_md, output.get("raw_text", "") |
|
|
|
|
| CUSTOM_CSS = """ |
| body {background: radial-gradient(circle at top left, #f5f0ff 0%, #fff7ec 60%, #ffffff 100%);} |
| .gradio-container {max-width: 1180px !important; margin: auto; font-family: 'Noto Sans KR', sans-serif;} |
| .hero { |
| background: linear-gradient(120deg, rgba(123, 97, 255, 0.12), rgba(255, 207, 117, 0.18)); |
| border-radius: 28px; |
| padding: 36px 44px; |
| box-shadow: 0 20px 40px rgba(66, 46, 138, 0.08); |
| margin-bottom: 32px; |
| } |
| .hero h1 {font-size: 2.4rem; font-weight: 700; color: #1f1c3b; margin-bottom: 12px;} |
| .hero p {color: #514c7b; font-size: 1.05rem; line-height: 1.6; max-width: 640px;} |
| .glass-panel {background: rgba(255, 255, 255, 0.72); backdrop-filter: blur(18px); border-radius: 26px; padding: 28px; box-shadow: 0 12px 32px rgba(80, 60, 160, 0.12);} |
| .panel-title {font-weight: 700; font-size: 1.2rem; margin-bottom: 18px; color: #2f2355;} |
| .primary-btn button {background: linear-gradient(120deg, #7c62ff, #ffa74d); border: none; color: white; font-weight: 600; border-radius: 999px; padding: 12px 22px; box-shadow: 0 12px 24px rgba(124, 98, 255, 0.25);} |
| .primary-btn button:hover {opacity: 0.95; transform: translateY(-1px);} |
| .output-card {background: rgba(255, 255, 255, 0.88); border-radius: 22px; padding: 24px; box-shadow: inset 0 0 0 1px rgba(124, 98, 255, 0.08), 0 14px 30px rgba(49, 32, 114, 0.12);} |
| .notice {background: rgba(255, 247, 226, 0.9); border-radius: 18px; padding: 18px; color: #7a4b00; box-shadow: inset 0 0 0 1px rgba(255, 193, 96, 0.3);} |
| .csv-box textarea {font-family: 'JetBrains Mono', monospace;} |
| .gr-image {border-radius: 20px !important; box-shadow: 0 10px 20px rgba(60, 40, 120, 0.15);} |
| .accordion {border-radius: 20px !important;} |
| """ |
|
|
| HERO_HTML = """ |
| <div class="hero"> |
| <h1>MedCard-KR ยท ์ฝ๋ดํฌ ํ ์ปท์ผ๋ก ์ดํดํ๋ ๋ณต์ฉ ์๋ด</h1> |
| <p>์ฌ์ง ์ ์ฝ ์ด๋ฆ์ OCR๋ก ์ฝ์ด ๋ค์ด๊ณ , Qwen LLM์ด ์คํ์๋ ์ดํดํ ์ ์๋ ๋งํฌ๋ก ์ฝ์ ์ค๋ช
ํด ๋๋ฆฝ๋๋ค. |
| ๋ณต์ฉ ์ผ์ ์นด๋์ CSV๊น์ง ํ ๋ฒ์ ๋ฐ์ ๋ณด์ธ์.</p> |
| </div> |
| """ |
|
|
|
|
| with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo: |
| gr.HTML(HERO_HTML) |
| with gr.Row(): |
| with gr.Column(scale=4, elem_classes=["glass-panel"]): |
| gr.Markdown("### 1. ์ฝ ๋ดํฌ ์ฌ์ง์ ์
๋ก๋ํ์ธ์") |
| img_in = gr.Image(type="pil", label="์ฝ ๋ดํฌ/๋ผ๋ฒจ ์ฌ์ง", height=360) |
| warn_md = gr.Markdown("๐ท ์ฝ ๋ดํฌ ์ฌ์ง์ ์ฌ๋ฆฌ๋ฉด ์ธ์์ด ์์๋ผ์.", elem_classes=["notice"]) |
| btn = gr.Button("์ธ์ & ์ค๋ช
์์ฑ", elem_classes=["primary-btn"]) |
| with gr.Column(scale=6, elem_classes=["glass-panel"]): |
| gr.Markdown("### 2. ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ์ธ์") |
| explain_md = gr.Markdown("์ฌ๊ธฐ์ ์ฝ ์ค๋ช
์ด ํ์๋ฉ๋๋ค.", elem_classes=["output-card"]) |
| raw_box = gr.Textbox(label="OCR ์๋ฌธ ํ
์คํธ", lines=5, interactive=False) |
| card_out = gr.Image(type="pil", label="์ผ์ ์นด๋(๋ฏธ๋ฆฌ๋ณด๊ธฐ)") |
| csv_box = gr.Textbox(label="CSV(์ฝ๋ช
,1ํ์ฉ๋,1์ผํ์,์๊ฐ๋)", lines=2, elem_classes=["csv-box"]) |
| with gr.Accordion("์ธ๋ถ JSON ๊ฒฐ๊ณผ", open=False, elem_classes=["accordion"]): |
| json_out = gr.Code(label="์ธ์ ๊ฒฐ๊ณผ(JSON)") |
|
|
| btn.click( |
| run_pipeline, |
| inputs=img_in, |
| outputs=[json_out, card_out, csv_box, explain_md, warn_md, raw_box], |
| ) |
|
|
| gr.Markdown( |
| """ |
| > โน๏ธ **์ฃผ์**: ์ด ์๋น์ค๋ ์ฐธ๊ณ ์ฉ ๋๊ตฌ์ด๋ฉฐ, ์ค์ ๋ณต์ฝ์ ๋ฐ๋์ ์์ฌยท์ฝ์ฌ์ ์ง์์ ๋ฐ๋ผ ์ฃผ์ธ์. |
| """ |
| ) |
|
|
|
|
| if __name__ == "__main__": |
| demo.queue().launch() |
|
|