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()
|