File size: 17,721 Bytes
76e08e0 149f48c 76e08e0 c103d7c 76e08e0 c103d7c 72114b8 76e08e0 6d9d526 76e08e0 72114b8 c103d7c 6d9d526 72114b8 c103d7c 72114b8 c103d7c 76e08e0 6d9d526 76e08e0 5f1a404 149f48c 5f1a404 76e08e0 149f48c 76e08e0 149f48c 76e08e0 149f48c 76e08e0 149f48c 76e08e0 149f48c 76e08e0 c103d7c 76e08e0 c103d7c 5f1a404 76e08e0 5f1a404 76e08e0 5f1a404 76e08e0 5f1a404 76e08e0 5f1a404 76e08e0 5f1a404 76e08e0 5f1a404 76e08e0 5f1a404 76e08e0 5f1a404 149f48c 6d9d526 149f48c 6d9d526 c103d7c 6d9d526 76e08e0 6d9d526 0e757ea 6d9d526 5f1a404 76e08e0 149f48c 6d9d526 0e757ea 6d9d526 76e08e0 6d9d526 0e757ea 76e08e0 6d9d526 0e757ea 6d9d526 5f1a404 76e08e0 |
1 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
import json
import re
from typing import Any, Dict, List, Optional, Sequence
import easyocr
import gradio as gr
import numpy as np
import torch
from PIL import Image, ImageDraw
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
# --- OCR pipeline ---------------------------------------------------------
# Use a high-capacity OCR model for better accuracy on prescription labels.
OCR_LANGS = ["ko", "en"]
LLM_MODEL_ID = "Qwen/Qwen2.5-0.5B-Instruct"
def _load_ocr():
use_gpu = torch.cuda.is_available()
return easyocr.Reader(OCR_LANGS, gpu=use_gpu)
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)
if device_map is None:
model = model.to(torch.device("cpu"))
tokenizer = AutoTokenizer.from_pretrained(LLM_MODEL_ID)
return model, tokenizer
LLM_MODEL, LLM_TOKENIZER = _load_llm()
# Korean keywords describing time slots on prescription labels.
TIME_KEYWORDS = [
"์์นจ",
"์ ์ฌ",
"์ ๋
",
"์ทจ์นจ",
"์๊ธฐ",
"์์ ",
"์ํ",
"์๊ฐ",
"๊ธฐ์",
]
# Very small knowledge base for common Korean OTC medications.
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)
# Also capture explicit times like 08:00 ํน์ 8์
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"))
results = ocr_reader.readtext(np_img, detail=1, paragraph=False)
segments: List[Dict[str, Any]] = []
lines: List[str] = []
for bbox, text, confidence in results:
cleaned = text.strip()
if not cleaned:
continue
lines.append(cleaned)
segments.append({
"text": cleaned,
"confidence": float(confidence),
"bbox": bbox,
})
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: # pragma: no cover - safe fallback
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()
|