refactor: simplify to medication name extractor only
Browse files**Complete Redesign**:
- Remove all complex features (explanations, cards, web search)
- Focus on ONE task: Extract medication names from image
- Ultra-simple UI (single upload โ list output)
**Changes**:
โ
Simplified OCR function: extract_medication_names()
โ
Clean JSON schema: just {"medications": ["name1", "name2"]}
โ
Minimalist UI: hero + upload + result (3 sections only)
โ
Removed: 600+ lines of unused code
โ
Inference optimized: temperature=0.1 for accuracy
โ
Beautiful gradient design maintained
**Result**:
- 730 lines โ 250 lines (66% reduction)
- Fast, focused, production-ready
- Perfect for simple medication list extraction
๐ค Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
|
@@ -1,78 +1,29 @@
|
|
| 1 |
import json
|
| 2 |
-
import os
|
| 3 |
import re
|
| 4 |
-
import
|
| 5 |
-
import urllib.parse
|
| 6 |
-
from typing import Any, Dict, List, Optional
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
-
import requests
|
| 10 |
import spaces
|
| 11 |
import torch
|
| 12 |
-
from PIL import Image
|
| 13 |
from transformers import (
|
| 14 |
Qwen2VLForConditionalGeneration,
|
| 15 |
AutoProcessor,
|
| 16 |
)
|
| 17 |
|
| 18 |
# ์ต๊ณ ํ์ง ๊ณต๊ฐ ๋ชจ๋ธ + 8๋นํธ ์์ํ (ZeroGPU ์ต์ ํ)
|
| 19 |
-
# Note: 32B/72B๋ gated model(์ธ์ฆ ํ์), 7B๊ฐ ์ต๋ ๊ณต๊ฐ ๋ชจ๋ธ
|
| 20 |
VL_MODEL_ID = "Qwen/Qwen2-VL-7B-Instruct"
|
| 21 |
|
| 22 |
|
| 23 |
-
def search_drug_web_simple(drug_name: str) -> str:
|
| 24 |
-
"""๊ฐ๋จํ ์น ๊ฒ์์ผ๋ก ์ฝ๋ฌผ ์ ๋ณด ๊ฒ์ฆ"""
|
| 25 |
-
try:
|
| 26 |
-
clean_name = re.sub(r'\(.*?\)|\d+mg|\d+mL|์ |ํฌ|์บก์', '', drug_name).strip()
|
| 27 |
-
sources = [
|
| 28 |
-
f"https://www.health.kr/searchIdentity/search_result_detail.asp?searchStr={urllib.parse.quote(clean_name)}",
|
| 29 |
-
f"https://terms.naver.com/search.naver?query={urllib.parse.quote(clean_name + ' ์ฝ')}"
|
| 30 |
-
]
|
| 31 |
-
|
| 32 |
-
for url in sources:
|
| 33 |
-
try:
|
| 34 |
-
response = requests.get(url, timeout=3, headers={'User-Agent': 'Mozilla/5.0'})
|
| 35 |
-
if response.status_code == 200 and len(response.text) > 1000:
|
| 36 |
-
text = response.text[:3000]
|
| 37 |
-
if any(kw in text for kw in ["ํจ๋ฅ", "ํจ๊ณผ", "๋ณต์ฉ", "์ฃผ์"]):
|
| 38 |
-
return f"โ ์น์์ {clean_name} ์ ๋ณด๋ฅผ ์ฐพ์์ต๋๋ค."
|
| 39 |
-
except:
|
| 40 |
-
continue
|
| 41 |
-
return ""
|
| 42 |
-
except:
|
| 43 |
-
return ""
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
def _load_font():
|
| 47 |
-
"""ํ๊ธ ํฐํธ ๋ก๋"""
|
| 48 |
-
font_path = "NotoSansKR-Regular.ttf"
|
| 49 |
-
if not os.path.exists(font_path):
|
| 50 |
-
try:
|
| 51 |
-
url = "https://github.com/notofonts/noto-cjk/raw/main/Sans/OTF/Korean/NotoSansKR-Regular.otf"
|
| 52 |
-
response = requests.get(url)
|
| 53 |
-
with open(font_path, "wb") as f:
|
| 54 |
-
f.write(response.content)
|
| 55 |
-
except:
|
| 56 |
-
return None
|
| 57 |
-
try:
|
| 58 |
-
return ImageFont.truetype(font_path, 16)
|
| 59 |
-
except:
|
| 60 |
-
return None
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
DEFAULT_FONT = _load_font()
|
| 64 |
-
|
| 65 |
-
|
| 66 |
def _load_vl_model():
|
| 67 |
-
"""
|
| 68 |
device_map = "auto" if torch.cuda.is_available() else None
|
| 69 |
|
| 70 |
-
# 8๋นํธ ์์ํ + FP16 ํผํฉ ์ ๋ฐ๋๋ก ์ต๊ณ ์ฑ๋ฅ
|
| 71 |
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 72 |
VL_MODEL_ID,
|
| 73 |
device_map=device_map,
|
| 74 |
-
load_in_8bit=True,
|
| 75 |
-
torch_dtype=torch.float16,
|
| 76 |
trust_remote_code=True,
|
| 77 |
)
|
| 78 |
|
|
@@ -80,9 +31,9 @@ def _load_vl_model():
|
|
| 80 |
return model, processor
|
| 81 |
|
| 82 |
|
| 83 |
-
print("๐ Loading Qwen2-VL-7B model
|
| 84 |
VL_MODEL, VL_PROCESSOR = _load_vl_model()
|
| 85 |
-
print("โ
Model loaded successfully!
|
| 86 |
|
| 87 |
|
| 88 |
def _extract_assistant_content(decoded: str) -> str:
|
|
@@ -102,584 +53,171 @@ def _extract_json_block(text: str) -> Optional[str]:
|
|
| 102 |
return match.group(0)
|
| 103 |
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
return [str(v).strip() for v in value if str(v).strip()]
|
| 109 |
-
if isinstance(value, str):
|
| 110 |
-
return [v.strip() for v in re.split(r"[,;]", value) if v.strip()]
|
| 111 |
-
return []
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
def _sanitize_medication(item: Dict[str, Any]) -> Dict[str, Any]:
|
| 115 |
-
"""์ฝ๋ฌผ ์ ๋ณด ์ ์ """
|
| 116 |
-
def _to_str(val: Any) -> str:
|
| 117 |
-
return "" if val is None else str(val).strip()
|
| 118 |
-
|
| 119 |
-
times = item.get("times_per_day")
|
| 120 |
-
if isinstance(times, (int, float)):
|
| 121 |
-
times_str = str(int(times)) if float(times).is_integer() else str(times)
|
| 122 |
-
else:
|
| 123 |
-
times_str = _to_str(times)
|
| 124 |
-
|
| 125 |
-
return {
|
| 126 |
-
"name": _to_str(item.get("name")),
|
| 127 |
-
"dose_per_intake": _to_str(item.get("dose_per_intake")),
|
| 128 |
-
"times_per_day": times_str,
|
| 129 |
-
"time_slots": _sanitize_list(item.get("time_slots")),
|
| 130 |
-
"description": _to_str(item.get("description")),
|
| 131 |
-
"efficacy": _to_str(item.get("efficacy")),
|
| 132 |
-
"usage_precautions": _to_str(item.get("usage_precautions")),
|
| 133 |
-
"side_effects": _to_str(item.get("side_effects")),
|
| 134 |
-
"drug_interactions": _to_str(item.get("drug_interactions")),
|
| 135 |
-
"warnings": _to_str(item.get("warnings")),
|
| 136 |
-
}
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
def _parse_vl_response(text: str) -> Dict[str, Any]:
|
| 140 |
-
"""VL ๋ชจ๋ธ ์๋ต ํ์ฑ"""
|
| 141 |
-
json_block = _extract_json_block(text)
|
| 142 |
-
if not json_block:
|
| 143 |
-
return {
|
| 144 |
-
"raw_text": "",
|
| 145 |
-
"medications": [],
|
| 146 |
-
"warnings": ["๋ชจ๋ธ ์๋ต์์ JSON ํ์์ ์ฐพ์ง ๋ชปํ์ต๋๋ค."],
|
| 147 |
-
}
|
| 148 |
-
|
| 149 |
try:
|
| 150 |
-
|
| 151 |
-
except json.JSONDecodeError:
|
| 152 |
-
return {
|
| 153 |
-
"raw_text": "",
|
| 154 |
-
"medications": [],
|
| 155 |
-
"warnings": ["JSON ํ์ฑ ์คํจ"],
|
| 156 |
-
}
|
| 157 |
-
|
| 158 |
-
meds_raw = data.get("medications") or []
|
| 159 |
-
medications = []
|
| 160 |
-
if isinstance(meds_raw, list):
|
| 161 |
-
for item in meds_raw:
|
| 162 |
-
if isinstance(item, dict):
|
| 163 |
-
medications.append(_sanitize_medication(item))
|
| 164 |
-
|
| 165 |
-
warnings_raw = data.get("warnings")
|
| 166 |
-
if isinstance(warnings_raw, list):
|
| 167 |
-
warnings = [str(w).strip() for w in warnings_raw if str(w).strip()]
|
| 168 |
-
elif warnings_raw:
|
| 169 |
-
warnings = [str(warnings_raw).strip()]
|
| 170 |
-
else:
|
| 171 |
-
warnings = []
|
| 172 |
-
|
| 173 |
-
return {
|
| 174 |
-
"raw_text": str(data.get("raw_text", "")).strip(),
|
| 175 |
-
"medications": medications,
|
| 176 |
-
"warnings": warnings,
|
| 177 |
-
}
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
@spaces.GPU(duration=180) # ๊ณ ํ์ง ์ถ๋ก ์ ์ํ 3๋ถ ํ์ฉ
|
| 181 |
-
def analyze_with_vl_model(image: Image.Image, task: str = "ocr") -> Any:
|
| 182 |
-
"""
|
| 183 |
-
๋จ์ผ VL ๋ชจ๋ธ๋ก ๋ชจ๋ ์์
์ํ
|
| 184 |
-
task: "ocr" (์ฝ๋ดํฌ ๋ถ์) | "explain" (์ค๋ช
์์ฑ) | "image_prompt" (์ด๋ฏธ์ง ํ๋กฌํํธ)
|
| 185 |
-
"""
|
| 186 |
-
try:
|
| 187 |
-
if task == "ocr":
|
| 188 |
-
# ์ฝ๋ดํฌ OCR ๋ฐ ์ ๋ณด ์ถ์ถ
|
| 189 |
-
instructions = """์ฌ์ง ์ ์ฝ๋ดํฌ/์ฒ๋ฐฉ์ ์ ์ฝ๊ณ JSON ํ์์ผ๋ก ๋ต๋ณํ์ธ์."""
|
| 190 |
-
|
| 191 |
-
schema = """{
|
| 192 |
-
"raw_text": "OCR๋ก ์ฝ์ ์ ์ฒด ๋ฌธ์ฅ",
|
| 193 |
-
"medications": [
|
| 194 |
-
{
|
| 195 |
-
"name": "์ฝ ์ด๋ฆ (์ํ๋ช
๊ณผ ์ฑ๋ถ๋ช
)",
|
| 196 |
-
"dose_per_intake": "1ํ ์ฉ๋",
|
| 197 |
-
"times_per_day": "ํ๋ฃจ ๋ณต์ฉ ํ์",
|
| 198 |
-
"time_slots": ["๋ณต์ฉ ์๊ฐ๋"],
|
| 199 |
-
"description": "์ฝ ์ค๋ช
",
|
| 200 |
-
"efficacy": "์ด ์ฝ์ ๋ฌด์์
๋๊น? (์์ธํ ํจ๋ฅํจ๊ณผ)",
|
| 201 |
-
"usage_precautions": "์ด ์ฝ์ ์ด๋ป๊ฒ ๋ณต์ฉํฉ๋๊น? (์์ธํ ๋ณต์ฉ๋ฒ)",
|
| 202 |
-
"side_effects": "์ฃผ์ ๋ถ์์ฉ",
|
| 203 |
-
"drug_interactions": "์ฝ๋ฌผ ์ํธ์์ฉ",
|
| 204 |
-
"warnings": "ํน๋ณ ์ฃผ์์ฌํญ"
|
| 205 |
-
}
|
| 206 |
-
],
|
| 207 |
-
"warnings": ["์ ์ฒด ๊ฒฝ๊ณ "]
|
| 208 |
-
}"""
|
| 209 |
-
|
| 210 |
-
messages = [
|
| 211 |
-
{
|
| 212 |
-
"role": "system",
|
| 213 |
-
"content": "๋น์ ์ 20๋
๊ฒฝ๋ ฅ์ ๋ํ๋ฏผ๊ตญ ์์์ฝ์ฌ์
๋๋ค. ์ฝ๋ดํฌ๋ฅผ ์ ๋ฐํ๊ฒ ์ฝ๊ณ ์์ฝํ์ง(DUR) ์์ค์ ์ ๋ฌธ์ ์ด๊ณ ์์ธํ ์ ๋ณด๋ฅผ ์ ๊ณตํฉ๋๋ค. ๋ชจ๋ ํ๋๋ฅผ ์ต๋ํ ์์ธํ ์์ฑํ์ธ์.",
|
| 214 |
-
},
|
| 215 |
-
{
|
| 216 |
-
"role": "user",
|
| 217 |
-
"content": [
|
| 218 |
-
{"type": "text", "text": instructions},
|
| 219 |
-
{"type": "text", "text": schema},
|
| 220 |
-
{"type": "image"},
|
| 221 |
-
],
|
| 222 |
-
},
|
| 223 |
-
]
|
| 224 |
-
|
| 225 |
-
chat_text = VL_PROCESSOR.apply_chat_template(messages, add_generation_prompt=True)
|
| 226 |
-
inputs = VL_PROCESSOR(text=[chat_text], images=[image], return_tensors="pt").to(VL_MODEL.device)
|
| 227 |
-
|
| 228 |
-
output_ids = VL_MODEL.generate(
|
| 229 |
-
**inputs,
|
| 230 |
-
max_new_tokens=4096, # ๋ ๊ธด ์ถ๋ ฅ ํ์ฉ
|
| 231 |
-
temperature=0.2, # ๋ ๊ฒฐ์ ์ (์ ํ๋ ํฅ์)
|
| 232 |
-
top_p=0.9, # ๋ ์ง์ค๋ ์ํ๋ง
|
| 233 |
-
do_sample=True,
|
| 234 |
-
repetition_penalty=1.1, # ๋ฐ๋ณต ๋ฐฉ์ง
|
| 235 |
-
)
|
| 236 |
-
|
| 237 |
-
decoded = VL_PROCESSOR.batch_decode(output_ids, skip_special_tokens=False)[0]
|
| 238 |
-
assistant_text = _extract_assistant_content(decoded)
|
| 239 |
-
return _parse_vl_response(assistant_text)
|
| 240 |
-
|
| 241 |
-
elif task == "explain":
|
| 242 |
-
# ์ค๋ช
์์ฑ (image๋ None, text๋ง ์ฌ์ฉ)
|
| 243 |
-
return {"elderly_narrative": "", "child_narrative": "", "image_description": ""}
|
| 244 |
-
|
| 245 |
-
except Exception as e:
|
| 246 |
-
return {"error": str(e)}
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
def render_card(medications: List[Dict[str, Any]]) -> Image.Image:
|
| 250 |
-
"""ํ๋์ ์ธ ์ฝ๋ฌผ ์นด๋ ๋ ๋๋ง"""
|
| 251 |
-
try:
|
| 252 |
-
font_large = ImageFont.truetype("NotoSansKR-Regular.ttf", 28)
|
| 253 |
-
font_medium = ImageFont.truetype("NotoSansKR-Regular.ttf", 20)
|
| 254 |
-
font_small = ImageFont.truetype("NotoSansKR-Regular.ttf", 16)
|
| 255 |
-
except:
|
| 256 |
-
font_large = font_medium = font_small = None
|
| 257 |
-
|
| 258 |
-
if not medications:
|
| 259 |
-
canvas = Image.new("RGB", (900, 300), (255, 255, 255))
|
| 260 |
-
draw = ImageDraw.Draw(canvas)
|
| 261 |
-
draw.text((350, 130), "์ฝ ์ ๋ณด๊ฐ ์์ต๋๋ค", fill=(140, 140, 140), font=font_medium)
|
| 262 |
-
return canvas
|
| 263 |
-
|
| 264 |
-
card_height_per_med = 240
|
| 265 |
-
header_height = 120
|
| 266 |
-
footer_height = 80
|
| 267 |
-
total_height = header_height + (card_height_per_med * len(medications)) + footer_height
|
| 268 |
-
|
| 269 |
-
width = 900
|
| 270 |
-
canvas = Image.new("RGB", (width, total_height), (248, 250, 252))
|
| 271 |
-
draw = ImageDraw.Draw(canvas)
|
| 272 |
-
|
| 273 |
-
# ๋ชจ๋ ํค๋
|
| 274 |
-
for i in range(header_height):
|
| 275 |
-
alpha = i / header_height
|
| 276 |
-
color = (
|
| 277 |
-
int(99 + (248 - 99) * alpha),
|
| 278 |
-
int(102 + (250 - 102) * alpha),
|
| 279 |
-
int(241 + (252 - 241) * alpha),
|
| 280 |
-
)
|
| 281 |
-
draw.rectangle((0, i, width, i + 1), fill=color)
|
| 282 |
-
|
| 283 |
-
draw.text((40, 35), "๐ ๋ณต์ฉ ์๋ด", fill=(30, 41, 59), font=font_large)
|
| 284 |
-
draw.text((40, 75), f"{len(medications)}๊ฐ ์ฝํ", fill=(71, 85, 105), font=font_small)
|
| 285 |
-
|
| 286 |
-
y = header_height + 30
|
| 287 |
-
|
| 288 |
-
for idx, med in enumerate(medications):
|
| 289 |
-
card_y_start = y - 15
|
| 290 |
-
card_y_end = y + 200
|
| 291 |
-
|
| 292 |
-
# ์นด๋ ๊ทธ๋ฆผ์
|
| 293 |
-
draw.rounded_rectangle(
|
| 294 |
-
(35, card_y_start + 5, width - 35, card_y_end + 5),
|
| 295 |
-
radius=16,
|
| 296 |
-
fill=(203, 213, 225),
|
| 297 |
-
)
|
| 298 |
-
|
| 299 |
-
# ์นด๋ ๋ณธ์ฒด
|
| 300 |
-
draw.rounded_rectangle(
|
| 301 |
-
(30, card_y_start, width - 30, card_y_end),
|
| 302 |
-
radius=16,
|
| 303 |
-
fill=(255, 255, 255),
|
| 304 |
-
)
|
| 305 |
-
|
| 306 |
-
# ์ฝ ๋ฒํธ ๋ฐฐ์ง
|
| 307 |
-
badge_x, badge_y = 45, y
|
| 308 |
-
draw.ellipse(
|
| 309 |
-
(badge_x, badge_y, badge_x + 45, badge_y + 45),
|
| 310 |
-
fill=(99, 102, 241),
|
| 311 |
-
)
|
| 312 |
-
draw.text((badge_x + 12, badge_y + 8), str(idx + 1), fill=(255, 255, 255), font=font_medium)
|
| 313 |
-
|
| 314 |
-
# ์ฝ ์ด๋ฆ
|
| 315 |
-
name_text = med.get("name", "์ฝ ์ด๋ฆ ๋ฏธํ์ธ")
|
| 316 |
-
draw.text((105, y + 8), name_text, fill=(15, 23, 42), font=font_medium)
|
| 317 |
-
|
| 318 |
-
y += 60
|
| 319 |
-
|
| 320 |
-
# ์ ๋ณด ์น์
|
| 321 |
-
info_items = [
|
| 322 |
-
("๐ฆ", "์ฉ๋", med.get('dose_per_intake', '-')),
|
| 323 |
-
("๐ข", "ํ์", f"{med.get('times_per_day', '-')}ํ/์ผ"),
|
| 324 |
-
("๐", "์๊ฐ", ", ".join(med.get('time_slots') or ["-"])),
|
| 325 |
-
]
|
| 326 |
-
|
| 327 |
-
for icon, label, value in info_items:
|
| 328 |
-
draw.text((50, y), f"{icon} {label}", fill=(100, 116, 139), font=font_small)
|
| 329 |
-
draw.text((160, y), value, fill=(30, 41, 59), font=font_small)
|
| 330 |
-
y += 38
|
| 331 |
-
|
| 332 |
-
y += 30
|
| 333 |
-
|
| 334 |
-
# ํธํฐ
|
| 335 |
-
footer_y = total_height - footer_height + 25
|
| 336 |
-
draw.text((40, footer_y), "โป ๋ณธ ์ฑ์ ์ฐธ๊ณ ์ฉ์ด๋ฉฐ, ์ค์ ๋ณต์ฝ์ ์์ฌยท์ฝ์ฌ์ ์ง์๋ฅผ ๋ฐ๋ผ์ฃผ์ธ์.",
|
| 337 |
-
fill=(148, 163, 184), font=font_small)
|
| 338 |
-
|
| 339 |
-
return canvas
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
def medications_to_csv(medications: List[Dict[str, Any]]) -> str:
|
| 343 |
-
"""CSV ์์ฑ"""
|
| 344 |
-
if not medications:
|
| 345 |
-
return ""
|
| 346 |
-
|
| 347 |
-
rows = ["์ฝ๋ช
,1ํ์ฉ๋,1์ผํ์,์๊ฐ๋"]
|
| 348 |
-
for med in medications:
|
| 349 |
-
row = [
|
| 350 |
-
med.get("name", ""),
|
| 351 |
-
med.get("dose_per_intake", ""),
|
| 352 |
-
med.get("times_per_day", ""),
|
| 353 |
-
";".join(med.get("time_slots") or []),
|
| 354 |
-
]
|
| 355 |
-
rows.append(",".join(row))
|
| 356 |
-
|
| 357 |
-
return "\n".join(rows)
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
def format_warnings(warnings: List[str]) -> str:
|
| 361 |
-
"""๊ฒฝ๊ณ ๋ฉ์์ง ํฌ๋งท"""
|
| 362 |
-
if not warnings:
|
| 363 |
-
return "โ
์ธ์๋ ์ ๋ณด๊ฐ ์ถฉ๋ถํฉ๋๋ค."
|
| 364 |
-
|
| 365 |
-
lines = ["### โ ๏ธ ํ์ธ ํ์"]
|
| 366 |
-
for warn in warnings:
|
| 367 |
-
lines.append(f"- {warn}")
|
| 368 |
-
lines.append("\n> ์๋ฃ์ง์ ์ง์๊ฐ ๊ฐ์ฅ ์ ํํฉ๋๋ค.")
|
| 369 |
-
return "\n".join(lines)
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
@spaces.GPU(duration=120) # ๊ณ ํ์ง ์ค๋ช
์์ฑ
|
| 373 |
-
def generate_full_explanation(medications: List[Dict[str, Any]], raw_text: str, web_info: str = "") -> Dict[str, str]:
|
| 374 |
-
"""VL ๋ชจ๋ธ๋ก ์ค๋ช
์์ฑ"""
|
| 375 |
-
try:
|
| 376 |
-
med_summary = "\n".join([
|
| 377 |
-
f"- {med.get('name')} {med.get('dose_per_intake')} (ํ๋ฃจ {med.get('times_per_day')}ํ)"
|
| 378 |
-
for med in medications
|
| 379 |
-
])
|
| 380 |
-
|
| 381 |
-
web_context = f"\n\n์น ๊ฒ์ฆ: {web_info}" if web_info else ""
|
| 382 |
-
|
| 383 |
-
prompt = f"""๋ค์ ์ฝ๋ฌผ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์ด๋ฅด์ ๊ณผ ์ด๋ฆฐ์ด๋ฅผ ์ํ ์ค๋ช
์ ์์ฑํ์ธ์.
|
| 384 |
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
์๋ฌธ: {raw_text}{web_context}
|
| 389 |
-
|
| 390 |
-
JSON ํ์์ผ๋ก ๋ต๋ณ:
|
| 391 |
-
{{
|
| 392 |
-
"elderly": {{
|
| 393 |
-
"narrative": "์ด๋ฅด์ ์ ์ํ ์ค๋ช
(์กด๋๋ง, ๊ตฌ์ฒด์ , 5-7๋ฌธ์ฅ)",
|
| 394 |
-
"image_description": "์ฝ ๋ณต์ฉ ์ฅ๋ฉด ๋ฌ์ฌ (ํ๊ตญ์ด)"
|
| 395 |
-
}},
|
| 396 |
-
"child": {{
|
| 397 |
-
"narrative": "์ด๋ฆฐ์ด๋ฅผ ์ํ ์ค๋ช
(์ฌ์ด ๋ง, ์ฌ๋ฏธ์๊ฒ, 4-6๋ฌธ์ฅ)",
|
| 398 |
-
"image_description": "์ฝ ๋ณต์ฉ ์ฅ๋ฉด ๋ฌ์ฌ (ํ๊ตญ์ด)"
|
| 399 |
-
}}
|
| 400 |
-
}}"""
|
| 401 |
|
| 402 |
messages = [
|
| 403 |
{
|
| 404 |
"role": "system",
|
| 405 |
-
"content": "๋น์ ์
|
| 406 |
},
|
| 407 |
{
|
| 408 |
"role": "user",
|
| 409 |
-
"content":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
},
|
| 411 |
]
|
| 412 |
|
| 413 |
chat_text = VL_PROCESSOR.apply_chat_template(messages, add_generation_prompt=True)
|
| 414 |
-
inputs = VL_PROCESSOR(text=[chat_text], images=
|
| 415 |
|
| 416 |
output_ids = VL_MODEL.generate(
|
| 417 |
**inputs,
|
| 418 |
-
max_new_tokens=
|
| 419 |
-
temperature=0.
|
| 420 |
-
top_p=0.
|
| 421 |
do_sample=True,
|
| 422 |
-
repetition_penalty=1.15, # ๋ฐ๋ณต ๋ฐฉ์ง ๊ฐํ
|
| 423 |
)
|
| 424 |
|
| 425 |
decoded = VL_PROCESSOR.batch_decode(output_ids, skip_special_tokens=False)[0]
|
| 426 |
-
|
| 427 |
|
| 428 |
-
|
|
|
|
| 429 |
if json_block:
|
| 430 |
data = json.loads(json_block)
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
return {
|
| 435 |
-
"elderly_narrative": str(elderly.get("narrative", "")).strip(),
|
| 436 |
-
"child_narrative": str(child.get("narrative", "")).strip(),
|
| 437 |
-
}
|
| 438 |
|
| 439 |
-
return
|
| 440 |
-
"elderly_narrative": "์ค๋ช
์ ์์ฑํ์ง ๋ชปํ์ต๋๋ค.",
|
| 441 |
-
"child_narrative": "์ค๋ช
์ ์์ฑํ์ง ๋ชปํ์ต๋๋ค.",
|
| 442 |
-
}
|
| 443 |
|
| 444 |
except Exception as e:
|
| 445 |
-
return {
|
| 446 |
-
"elderly_narrative": "์ค๋ช
์์ฑ ์ค ์ค๋ฅ ๋ฐ์",
|
| 447 |
-
"child_narrative": "์ค๋ช
์์ฑ ์ค ์ค๋ฅ ๋ฐ์",
|
| 448 |
-
}
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
def run_pipeline(image: Optional[Image.Image], progress=gr.Progress()):
|
| 452 |
-
"""๋ฉ์ธ ํ์ดํ๋ผ์ธ"""
|
| 453 |
-
if image is None:
|
| 454 |
-
return (
|
| 455 |
-
"์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์.",
|
| 456 |
-
None,
|
| 457 |
-
None,
|
| 458 |
-
"์ด๋ฏธ์ง๋ฅผ ๋จผ์ ์
๋ก๋ํด ์ฃผ์ธ์.",
|
| 459 |
-
"๐ท ์ฝ ๋ดํฌ ์ฌ์ง์ ์ฌ๋ฆฌ๋ฉด ์ธ์์ด ์์๋ฉ๋๋ค.",
|
| 460 |
-
"",
|
| 461 |
-
"์ฝ๋ฌผ ์ ๋ณด๊ฐ ํ์๋ฉ๋๋ค.",
|
| 462 |
-
)
|
| 463 |
-
|
| 464 |
-
progress(0, desc="๐ ์ฝ๋ดํฌ ์ด๋ฏธ์ง ๋ถ์ ์ค...")
|
| 465 |
-
result = analyze_with_vl_model(image, task="ocr")
|
| 466 |
-
|
| 467 |
-
medications = result.get("medications") or []
|
| 468 |
-
|
| 469 |
-
# ์น ๊ฒ์
|
| 470 |
-
progress(0.25, desc="๐ ์น์์ ์ฝ๋ฌผ ์ ๋ณด ๊ฒ์ฆ ์ค...")
|
| 471 |
-
web_info_results = []
|
| 472 |
-
for med in medications[:3]:
|
| 473 |
-
drug_name = med.get("name", "")
|
| 474 |
-
if drug_name:
|
| 475 |
-
web_info = search_drug_web_simple(drug_name)
|
| 476 |
-
if web_info:
|
| 477 |
-
web_info_results.append(web_info)
|
| 478 |
-
med["web_verified"] = True
|
| 479 |
-
|
| 480 |
-
web_search_info = "\n".join(web_info_results) if web_info_results else ""
|
| 481 |
-
|
| 482 |
-
progress(0.5, desc="๐ฌ ์ค๋ช
์์ฑ ์ค...")
|
| 483 |
-
narratives = generate_full_explanation(medications, result.get("raw_text", ""), web_search_info)
|
| 484 |
|
| 485 |
-
progress(0.75, desc="๐จ ์นด๋ ๋ ๋๋ง ์ค...")
|
| 486 |
-
card_img = render_card(medications)
|
| 487 |
-
csv_row = medications_to_csv(medications)
|
| 488 |
|
| 489 |
-
|
| 490 |
-
|
|
|
|
|
|
|
| 491 |
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
|
| 496 |
-
|
| 497 |
-
web_badge = " ๐" if med.get("web_verified") else ""
|
| 498 |
-
detailed_info += f"## {idx + 1}. {med.get('name', '์ฝ ์ด๋ฆ ๋ฏธํ์ธ')}{web_badge}\n\n"
|
| 499 |
|
| 500 |
-
if med.get("efficacy"):
|
| 501 |
-
detailed_info += f"### ๐ ์ด ์ฝ์ ๋ฌด์์
๋๊น?\n{med.get('efficacy')}\n\n"
|
| 502 |
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
detailed_info += f"### โ ๏ธ ๋ถ์์ฉ\n{med.get('side_effects')}\n\n"
|
| 508 |
-
|
| 509 |
-
if med.get("drug_interactions"):
|
| 510 |
-
detailed_info += f"### ๐ ์ฝ๋ฌผ ์ํธ์์ฉ\n{med.get('drug_interactions')}\n\n"
|
| 511 |
-
|
| 512 |
-
if med.get("warnings"):
|
| 513 |
-
detailed_info += f"### โก ํน๋ณ ์ฃผ์์ฌํญ\n{med.get('warnings')}\n\n"
|
| 514 |
-
|
| 515 |
-
detailed_info += "---\n\n"
|
| 516 |
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
"## ๐ด ์ด๋ฅด์ ์ ์ํ ์ค๋ช
\n\n"
|
| 520 |
-
+ (narratives.get("elderly_narrative") or "์ค๋ช
์ ์ค๋นํ์ง ๋ชปํ์ต๋๋ค.")
|
| 521 |
-
+ "\n\n## ๐ถ ์ด๋ฆฐ์ด๋ฅผ ์ํ ์ค๋ช
\n\n"
|
| 522 |
-
+ (narratives.get("child_narrative") or "์ค๋ช
์ ์ค๋นํ์ง ๋ชปํ์ต๋๋ค.")
|
| 523 |
-
+ "\n\n> ๐ก ํญ์ ์๋ฃ์ง์ ์๋ด๋ฅผ ์ฐ์ ํ์ธ์."
|
| 524 |
-
)
|
| 525 |
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
json_text = json.dumps(result, ensure_ascii=False, indent=2)
|
| 529 |
|
| 530 |
progress(1.0, desc="โ
์๋ฃ!")
|
| 531 |
-
return
|
| 532 |
|
| 533 |
|
| 534 |
-
#
|
| 535 |
CUSTOM_CSS = """
|
| 536 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
|
| 537 |
|
| 538 |
:root {
|
| 539 |
--primary: #6366f1;
|
| 540 |
-
--primary-dark: #4f46e5;
|
| 541 |
--secondary: #8b5cf6;
|
| 542 |
-
--success: #10b981;
|
| 543 |
-
--warning: #f59e0b;
|
| 544 |
-
--danger: #ef4444;
|
| 545 |
-
--gray-50: #f9fafb;
|
| 546 |
-
--gray-100: #f3f4f6;
|
| 547 |
-
--gray-200: #e5e7eb;
|
| 548 |
-
--gray-300: #d1d5db;
|
| 549 |
-
--gray-600: #4b5563;
|
| 550 |
-
--gray-800: #1f2937;
|
| 551 |
-
--gray-900: #111827;
|
| 552 |
}
|
| 553 |
|
| 554 |
body {
|
| 555 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 556 |
-
font-family: 'Inter', -apple-system, BlinkMacSystemFont,
|
| 557 |
}
|
| 558 |
|
| 559 |
.gradio-container {
|
| 560 |
-
max-width:
|
| 561 |
margin: auto;
|
| 562 |
-
background: rgba(255, 255, 255, 0.
|
| 563 |
border-radius: 24px;
|
| 564 |
-
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.
|
| 565 |
padding: 40px;
|
| 566 |
}
|
| 567 |
|
| 568 |
-
.hero
|
|
|
|
|
|
|
| 569 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 570 |
border-radius: 20px;
|
| 571 |
-
padding: 50px 40px;
|
| 572 |
-
margin-bottom: 40px;
|
| 573 |
color: white;
|
| 574 |
-
|
| 575 |
}
|
| 576 |
|
| 577 |
-
.hero
|
| 578 |
font-size: 2.5rem;
|
| 579 |
font-weight: 700;
|
| 580 |
-
margin-bottom:
|
| 581 |
-
text-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
| 582 |
}
|
| 583 |
|
| 584 |
-
.hero
|
| 585 |
-
font-size: 1.
|
| 586 |
opacity: 0.95;
|
| 587 |
-
line-height: 1.6;
|
| 588 |
}
|
| 589 |
|
| 590 |
-
.
|
| 591 |
background: white;
|
| 592 |
border-radius: 16px;
|
| 593 |
-
padding:
|
| 594 |
-
box-shadow: 0 4px 6px
|
| 595 |
-
|
| 596 |
}
|
| 597 |
|
| 598 |
-
.
|
| 599 |
-
|
| 600 |
-
|
|
|
|
|
|
|
|
|
|
| 601 |
}
|
| 602 |
|
| 603 |
-
.
|
| 604 |
-
background: linear-gradient(135deg, var(--primary)
|
| 605 |
-
border: none !important;
|
| 606 |
color: white !important;
|
| 607 |
font-weight: 600 !important;
|
| 608 |
-
font-size: 1.
|
| 609 |
-
padding:
|
| 610 |
border-radius: 12px !important;
|
| 611 |
-
|
|
|
|
| 612 |
transition: all 0.3s ease !important;
|
| 613 |
}
|
| 614 |
|
| 615 |
-
.
|
| 616 |
transform: translateY(-2px) !important;
|
| 617 |
-
box-shadow: 0 15px 30px -5px rgba(99, 102, 241, 0.
|
| 618 |
-
}
|
| 619 |
-
|
| 620 |
-
.tab-nav button {
|
| 621 |
-
font-weight: 500 !important;
|
| 622 |
-
border-radius: 8px !important;
|
| 623 |
-
transition: all 0.2s ease !important;
|
| 624 |
-
}
|
| 625 |
-
|
| 626 |
-
.tab-nav button.selected {
|
| 627 |
-
background: var(--primary) !important;
|
| 628 |
-
color: white !important;
|
| 629 |
-
}
|
| 630 |
-
|
| 631 |
-
.notice {
|
| 632 |
-
background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
|
| 633 |
-
border-left: 4px solid var(--warning);
|
| 634 |
-
border-radius: 12px;
|
| 635 |
-
padding: 20px;
|
| 636 |
-
color: var(--gray-800);
|
| 637 |
-
}
|
| 638 |
-
|
| 639 |
-
.output-card {
|
| 640 |
-
background: var(--gray-50);
|
| 641 |
-
border-radius: 16px;
|
| 642 |
-
padding: 28px;
|
| 643 |
-
border: 1px solid var(--gray-200);
|
| 644 |
}
|
| 645 |
|
| 646 |
.gr-image {
|
| 647 |
-
border-radius: 16px !important;
|
| 648 |
-
box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.1) !important;
|
| 649 |
-
}
|
| 650 |
-
|
| 651 |
-
.csv-box textarea {
|
| 652 |
-
font-family: 'JetBrains Mono', 'Courier New', monospace !important;
|
| 653 |
-
font-size: 0.9rem !important;
|
| 654 |
-
background: var(--gray-900) !important;
|
| 655 |
-
color: #10b981 !important;
|
| 656 |
-
border-radius: 12px !important;
|
| 657 |
-
}
|
| 658 |
-
|
| 659 |
-
.accordion {
|
| 660 |
border-radius: 12px !important;
|
| 661 |
-
border: 1px solid var(--gray-200) !important;
|
| 662 |
-
}
|
| 663 |
-
|
| 664 |
-
h1, h2, h3 {
|
| 665 |
-
font-weight: 600;
|
| 666 |
-
color: var(--gray-900);
|
| 667 |
-
}
|
| 668 |
-
|
| 669 |
-
.markdown-text {
|
| 670 |
-
line-height: 1.8;
|
| 671 |
-
color: var(--gray-800);
|
| 672 |
}
|
| 673 |
"""
|
| 674 |
|
| 675 |
HERO_HTML = """
|
| 676 |
-
<div class="hero
|
| 677 |
-
<h1
|
| 678 |
-
<p>
|
| 679 |
-
<strong>AI ๊ธฐ๋ฐ ์ค๋งํธ ์ฝ๋ฌผ ๊ด๋ฆฌ ์์คํ
</strong><br>
|
| 680 |
-
Qwen2-VL-7B (8๋นํธ ์ต์ ํ + Ultra Quality ์ถ๋ก )๊ฐ ์ฝ๋ดํฌ๋ฅผ ์ ํํ๊ฒ ๋ถ์ํ๊ณ ,<br>
|
| 681 |
-
์น์์ ์ค์๊ฐ์ผ๋ก ์ ๋ณด๋ฅผ ๊ฒ์ฆํ์ฌ ํ๋กํ์
๋ํ ๋ณต์ฝ ์๋ด๋ฅผ ์ ๊ณตํฉ๋๋ค.
|
| 682 |
-
</p>
|
| 683 |
</div>
|
| 684 |
"""
|
| 685 |
|
|
@@ -687,48 +225,27 @@ HERO_HTML = """
|
|
| 687 |
with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
|
| 688 |
gr.HTML(HERO_HTML)
|
| 689 |
|
| 690 |
-
with gr.
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
warn_md = gr.Markdown("๐ก ์ฝ ๋ดํฌ ์ฌ์ง์ ์ฌ๋ ค์ฃผ์ธ์. AI๊ฐ ์๋์ผ๋ก ๋ถ์ํฉ๋๋ค.", elem_classes=["notice"])
|
| 695 |
-
btn = gr.Button("๐ ๋ถ์ ์์", elem_classes=["primary-btn"], size="lg")
|
| 696 |
-
|
| 697 |
-
with gr.Column(scale=7, elem_classes=["card"]):
|
| 698 |
-
gr.Markdown("### ๐ ๋ถ์ ๊ฒฐ๊ณผ")
|
| 699 |
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
card_out = gr.Image(type="pil", label="์ผ์ ์นด๋")
|
| 709 |
-
|
| 710 |
-
with gr.Accordion("๐ ์์ธ ๋ถ์ ๊ฒฐ๊ณผ", open=False):
|
| 711 |
-
raw_box = gr.Textbox(label="OCR ์๋ฌธ", lines=4, interactive=False)
|
| 712 |
-
csv_box = gr.Textbox(label="CSV ๋ฐ์ดํฐ", lines=3, elem_classes=["csv-box"])
|
| 713 |
-
json_out = gr.Code(label="JSON ๋ฐ์ดํฐ", language="json")
|
| 714 |
-
|
| 715 |
-
btn.click(
|
| 716 |
-
run_pipeline,
|
| 717 |
-
inputs=img_in,
|
| 718 |
-
outputs=[json_out, card_out, csv_box, explain_md, warn_md, raw_box, detailed_info_md],
|
| 719 |
)
|
| 720 |
|
| 721 |
-
gr.Markdown(
|
| 722 |
-
|
| 723 |
-
---
|
| 724 |
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
๐ ๊ฐ์ธ์ ๋ณด๋ ์ ์ฅ๋์ง ์์ผ๋ฉฐ, ๋ชจ๋ ์ฒ๋ฆฌ๋ ์ค์๊ฐ์ผ๋ก ์ด๋ฃจ์ด์ง๋๋ค.
|
| 730 |
-
"""
|
| 731 |
-
)
|
| 732 |
|
| 733 |
if __name__ == "__main__":
|
| 734 |
-
demo.queue().launch()
|
|
|
|
| 1 |
import json
|
|
|
|
| 2 |
import re
|
| 3 |
+
from typing import List, Optional
|
|
|
|
|
|
|
| 4 |
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
import spaces
|
| 7 |
import torch
|
| 8 |
+
from PIL import Image
|
| 9 |
from transformers import (
|
| 10 |
Qwen2VLForConditionalGeneration,
|
| 11 |
AutoProcessor,
|
| 12 |
)
|
| 13 |
|
| 14 |
# ์ต๊ณ ํ์ง ๊ณต๊ฐ ๋ชจ๋ธ + 8๋นํธ ์์ํ (ZeroGPU ์ต์ ํ)
|
|
|
|
| 15 |
VL_MODEL_ID = "Qwen/Qwen2-VL-7B-Instruct"
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def _load_vl_model():
|
| 19 |
+
"""VL ๋ชจ๋ธ ๋ก๋ - 8๋นํธ ์์ํ + FP16"""
|
| 20 |
device_map = "auto" if torch.cuda.is_available() else None
|
| 21 |
|
|
|
|
| 22 |
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 23 |
VL_MODEL_ID,
|
| 24 |
device_map=device_map,
|
| 25 |
+
load_in_8bit=True,
|
| 26 |
+
torch_dtype=torch.float16,
|
| 27 |
trust_remote_code=True,
|
| 28 |
)
|
| 29 |
|
|
|
|
| 31 |
return model, processor
|
| 32 |
|
| 33 |
|
| 34 |
+
print("๐ Loading Qwen2-VL-7B model...")
|
| 35 |
VL_MODEL, VL_PROCESSOR = _load_vl_model()
|
| 36 |
+
print("โ
Model loaded successfully!")
|
| 37 |
|
| 38 |
|
| 39 |
def _extract_assistant_content(decoded: str) -> str:
|
|
|
|
| 53 |
return match.group(0)
|
| 54 |
|
| 55 |
|
| 56 |
+
@spaces.GPU(duration=120)
|
| 57 |
+
def extract_medication_names(image: Image.Image) -> List[str]:
|
| 58 |
+
"""์ด๋ฏธ์ง์์ ์ฝ ์ด๋ฆ๋ง ์ถ์ถ"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
try:
|
| 60 |
+
instructions = """์ด ์ฌ์ง ์ ์ฝ๋ดํฌ/์ฒ๋ฐฉ์ ์์ ์ฝ ์ด๋ฆ๋ง ๋ชจ๋ ์ฐพ์์ JSON ํ์์ผ๋ก ๋ต๋ณํ์ธ์."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
schema = """{
|
| 63 |
+
"medications": ["์ฝ ์ด๋ฆ 1", "์ฝ ์ด๋ฆ 2", "์ฝ ์ด๋ฆ 3"]
|
| 64 |
+
}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
messages = [
|
| 67 |
{
|
| 68 |
"role": "system",
|
| 69 |
+
"content": "๋น์ ์ ์ฝ ์ด๋ฆ์ ์ ํํ ์ฝ๋ OCR ์ ๋ฌธ๊ฐ์
๋๋ค. ์ฝ๋ดํฌ๋ ์ฒ๋ฐฉ์ ์์ ์ฝ ์ด๋ฆ๋ง ์ถ์ถํฉ๋๋ค.",
|
| 70 |
},
|
| 71 |
{
|
| 72 |
"role": "user",
|
| 73 |
+
"content": [
|
| 74 |
+
{"type": "text", "text": instructions},
|
| 75 |
+
{"type": "text", "text": schema},
|
| 76 |
+
{"type": "image"},
|
| 77 |
+
],
|
| 78 |
},
|
| 79 |
]
|
| 80 |
|
| 81 |
chat_text = VL_PROCESSOR.apply_chat_template(messages, add_generation_prompt=True)
|
| 82 |
+
inputs = VL_PROCESSOR(text=[chat_text], images=[image], return_tensors="pt").to(VL_MODEL.device)
|
| 83 |
|
| 84 |
output_ids = VL_MODEL.generate(
|
| 85 |
**inputs,
|
| 86 |
+
max_new_tokens=1024,
|
| 87 |
+
temperature=0.1, # ๋งค์ฐ ์ ํํ๊ฒ
|
| 88 |
+
top_p=0.85,
|
| 89 |
do_sample=True,
|
|
|
|
| 90 |
)
|
| 91 |
|
| 92 |
decoded = VL_PROCESSOR.batch_decode(output_ids, skip_special_tokens=False)[0]
|
| 93 |
+
assistant_text = _extract_assistant_content(decoded)
|
| 94 |
|
| 95 |
+
# JSON ํ์ฑ
|
| 96 |
+
json_block = _extract_json_block(assistant_text)
|
| 97 |
if json_block:
|
| 98 |
data = json.loads(json_block)
|
| 99 |
+
meds = data.get("medications", [])
|
| 100 |
+
if isinstance(meds, list):
|
| 101 |
+
return [str(m).strip() for m in meds if str(m).strip()]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
return ["์ฝ ์ด๋ฆ์ ์ฐพ์ง ๋ชปํ์ต๋๋ค."]
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
except Exception as e:
|
| 106 |
+
return [f"์ค๋ฅ ๋ฐ์: {str(e)}"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
def format_medication_list(medications: List[str]) -> str:
|
| 110 |
+
"""์ฝ ์ด๋ฆ ๋ฆฌ์คํธ๋ฅผ ๋งํฌ๋ค์ด์ผ๋ก ํฌ๋งท"""
|
| 111 |
+
if not medications or medications[0].startswith("์ค๋ฅ") or medications[0].startswith("์ฝ ์ด๋ฆ์ ์ฐพ์ง"):
|
| 112 |
+
return f"### โ ๏ธ {medications[0] if medications else '์ฝ ์ด๋ฆ์ ์ฐพ์ง ๋ชปํ์ต๋๋ค.'}"
|
| 113 |
|
| 114 |
+
output = f"### ๐ ๊ฒ์ถ๋ ์ฝ๋ฌผ ({len(medications)}๊ฐ)\n\n"
|
| 115 |
+
for idx, med_name in enumerate(medications, 1):
|
| 116 |
+
output += f"{idx}. **{med_name}**\n"
|
| 117 |
|
| 118 |
+
return output
|
|
|
|
|
|
|
| 119 |
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
def run_analysis(image: Optional[Image.Image], progress=gr.Progress()):
|
| 122 |
+
"""๋ฉ์ธ ๋ถ์ ํ์ดํ๋ผ์ธ"""
|
| 123 |
+
if image is None:
|
| 124 |
+
return "๐ท ์ฝ ๋ดํฌ๋ ์ฒ๋ฐฉ์ ์ฌ์ง์ ์
๋ก๋ํด์ฃผ์ธ์."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
progress(0.3, desc="๐ ์ด๋ฏธ์ง ๋ถ์ ์ค...")
|
| 127 |
+
medications = extract_medication_names(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
progress(0.9, desc="๐ ๊ฒฐ๊ณผ ์ ๋ฆฌ ์ค...")
|
| 130 |
+
result_md = format_medication_list(medications)
|
|
|
|
| 131 |
|
| 132 |
progress(1.0, desc="โ
์๋ฃ!")
|
| 133 |
+
return result_md
|
| 134 |
|
| 135 |
|
| 136 |
+
# ์ฌํํ CSS
|
| 137 |
CUSTOM_CSS = """
|
| 138 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
|
| 139 |
|
| 140 |
:root {
|
| 141 |
--primary: #6366f1;
|
|
|
|
| 142 |
--secondary: #8b5cf6;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
}
|
| 144 |
|
| 145 |
body {
|
| 146 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 147 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
| 148 |
}
|
| 149 |
|
| 150 |
.gradio-container {
|
| 151 |
+
max-width: 900px !important;
|
| 152 |
margin: auto;
|
| 153 |
+
background: rgba(255, 255, 255, 0.98);
|
| 154 |
border-radius: 24px;
|
| 155 |
+
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.3);
|
| 156 |
padding: 40px;
|
| 157 |
}
|
| 158 |
|
| 159 |
+
.hero {
|
| 160 |
+
text-align: center;
|
| 161 |
+
padding: 30px 20px;
|
| 162 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 163 |
border-radius: 20px;
|
|
|
|
|
|
|
| 164 |
color: white;
|
| 165 |
+
margin-bottom: 30px;
|
| 166 |
}
|
| 167 |
|
| 168 |
+
.hero h1 {
|
| 169 |
font-size: 2.5rem;
|
| 170 |
font-weight: 700;
|
| 171 |
+
margin-bottom: 10px;
|
|
|
|
| 172 |
}
|
| 173 |
|
| 174 |
+
.hero p {
|
| 175 |
+
font-size: 1.1rem;
|
| 176 |
opacity: 0.95;
|
|
|
|
| 177 |
}
|
| 178 |
|
| 179 |
+
.upload-section {
|
| 180 |
background: white;
|
| 181 |
border-radius: 16px;
|
| 182 |
+
padding: 30px;
|
| 183 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.07);
|
| 184 |
+
margin-bottom: 20px;
|
| 185 |
}
|
| 186 |
|
| 187 |
+
.result-section {
|
| 188 |
+
background: white;
|
| 189 |
+
border-radius: 16px;
|
| 190 |
+
padding: 30px;
|
| 191 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.07);
|
| 192 |
+
min-height: 200px;
|
| 193 |
}
|
| 194 |
|
| 195 |
+
.analyze-btn button {
|
| 196 |
+
background: linear-gradient(135deg, var(--primary), var(--secondary)) !important;
|
|
|
|
| 197 |
color: white !important;
|
| 198 |
font-weight: 600 !important;
|
| 199 |
+
font-size: 1.1rem !important;
|
| 200 |
+
padding: 18px 40px !important;
|
| 201 |
border-radius: 12px !important;
|
| 202 |
+
border: none !important;
|
| 203 |
+
box-shadow: 0 10px 20px -5px rgba(99, 102, 241, 0.5) !important;
|
| 204 |
transition: all 0.3s ease !important;
|
| 205 |
}
|
| 206 |
|
| 207 |
+
.analyze-btn button:hover {
|
| 208 |
transform: translateY(-2px) !important;
|
| 209 |
+
box-shadow: 0 15px 30px -5px rgba(99, 102, 241, 0.6) !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
}
|
| 211 |
|
| 212 |
.gr-image {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
border-radius: 12px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
}
|
| 215 |
"""
|
| 216 |
|
| 217 |
HERO_HTML = """
|
| 218 |
+
<div class="hero">
|
| 219 |
+
<h1>๐ ์ฝ ์ด๋ฆ ์ถ์ถ๊ธฐ</h1>
|
| 220 |
+
<p>์ฝ๋ดํฌ/์ฒ๋ฐฉ์ ์ฌ์ง์์ ์ฝ ์ด๋ฆ์ ์๋์ผ๋ก ์ถ์ถํฉ๋๋ค</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
</div>
|
| 222 |
"""
|
| 223 |
|
|
|
|
| 225 |
with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
|
| 226 |
gr.HTML(HERO_HTML)
|
| 227 |
|
| 228 |
+
with gr.Column(elem_classes=["upload-section"]):
|
| 229 |
+
gr.Markdown("### ๐ธ ์ฌ์ง ์
๋ก๋")
|
| 230 |
+
image_input = gr.Image(type="pil", label="์ฝ๋ดํฌ ๋๋ ์ฒ๋ฐฉ์ ์ฌ์ง", height=350)
|
| 231 |
+
analyze_button = gr.Button("๐ ์ฝ ์ด๋ฆ ์ถ์ถ", elem_classes=["analyze-btn"], size="lg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
+
with gr.Column(elem_classes=["result-section"]):
|
| 234 |
+
gr.Markdown("### ๐ ์ถ์ถ ๊ฒฐ๊ณผ")
|
| 235 |
+
result_output = gr.Markdown("๋ถ์์ ์์ํ๋ฉด ์ฌ๊ธฐ์ ์ฝ ์ด๋ฆ ๋ฆฌ์คํธ๊ฐ ํ์๋ฉ๋๋ค.")
|
| 236 |
|
| 237 |
+
analyze_button.click(
|
| 238 |
+
run_analysis,
|
| 239 |
+
inputs=image_input,
|
| 240 |
+
outputs=result_output,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
)
|
| 242 |
|
| 243 |
+
gr.Markdown("""
|
| 244 |
+
---
|
|
|
|
| 245 |
|
| 246 |
+
**โน๏ธ ์ฐธ๊ณ ์ฌํญ**
|
| 247 |
+
์ด ๋๊ตฌ๋ OCR ๊ธฐ๋ฐ์ผ๋ก ์ฝ ์ด๋ฆ๋ง ์ถ์ถํฉ๋๋ค. ์ค์ ๋ณต์ฝ์ ์์ฌยท์ฝ์ฌ์ ์ง์๋ฅผ ๋ฐ๋ฅด์ธ์.
|
| 248 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
if __name__ == "__main__":
|
| 251 |
+
demo.queue().launch()
|