feat: use Gemma-2-2B for medical analysis
Browse files- Separate OCR (Qwen2.5-VL-7B) and medical analysis (Gemma-2-2B)
- Add comprehensive medication info: name, effects, side effects, usage, precautions
- Enhanced prompt for easy-to-understand explanations
- User-friendly format for elderly and children
๐ค Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
app.py
CHANGED
|
@@ -7,11 +7,14 @@ import gradio as gr
|
|
| 7 |
import spaces
|
| 8 |
import torch
|
| 9 |
from PIL import Image
|
| 10 |
-
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
|
| 11 |
from qwen_vl_utils import process_vision_info
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
def _extract_assistant_content(decoded: str) -> str:
|
|
@@ -35,15 +38,14 @@ def _extract_json_block(text: str) -> Optional[str]:
|
|
| 35 |
def analyze_medication_image(image: Image.Image) -> Tuple[str, str]:
|
| 36 |
"""์ด๋ฏธ์ง์์ OCR ์ถ์ถ ํ ์ฝ ์ ๋ณด ๋ถ์"""
|
| 37 |
try:
|
| 38 |
-
# Qwen2.5-VL
|
| 39 |
-
|
| 40 |
-
|
| 41 |
torch_dtype="auto",
|
| 42 |
device_map="auto"
|
| 43 |
)
|
| 44 |
-
|
| 45 |
|
| 46 |
-
# Step 1: OCR - ์ด๋ฏธ์ง์์ ํ
์คํธ ์ถ์ถ
|
| 47 |
ocr_messages = [
|
| 48 |
{
|
| 49 |
"role": "user",
|
|
@@ -54,72 +56,79 @@ def analyze_medication_image(image: Image.Image) -> Tuple[str, str]:
|
|
| 54 |
}
|
| 55 |
]
|
| 56 |
|
| 57 |
-
text =
|
| 58 |
image_inputs, video_inputs = process_vision_info(ocr_messages)
|
| 59 |
-
inputs =
|
| 60 |
text=[text],
|
| 61 |
images=image_inputs,
|
| 62 |
videos=video_inputs,
|
| 63 |
padding=True,
|
| 64 |
return_tensors="pt",
|
| 65 |
)
|
| 66 |
-
inputs = inputs.to(
|
| 67 |
|
| 68 |
with torch.no_grad():
|
| 69 |
-
generated_ids =
|
| 70 |
|
| 71 |
generated_ids_trimmed = [
|
| 72 |
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 73 |
]
|
| 74 |
|
| 75 |
-
ocr_text =
|
| 76 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 77 |
)[0]
|
| 78 |
|
| 79 |
if not ocr_text or ocr_text.strip() == "":
|
| 80 |
return "ํ
์คํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.", ""
|
| 81 |
|
| 82 |
-
# Step 2: ์ฝ ์ ๋ณด ๋ถ์ -
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
{ocr_text}
|
| 90 |
|
| 91 |
-
์ ํ
์คํธ์์ ์ฝ ์ด๋ฆ์ ์ฐพ์์, ๊ฐ ์ฝ์ ๋ํด
|
| 92 |
|
| 93 |
-
|
| 94 |
-
2. **ํจ๋ฅ**: ์ด ์ฝ์ด ๋ฌด์์ ์น๋ฃํ๊ณ ์ด๋ป๊ฒ ๋์์ด ๋๋์ง
|
| 95 |
-
3. **๋ถ์์ฉ**: ์ฃผ์ํด์ผ ํ ๋ถ์์ฉ๋ค
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
return_tensors="pt",
|
| 110 |
-
)
|
| 111 |
-
inputs = inputs.to(model.device)
|
| 112 |
|
| 113 |
-
|
| 114 |
-
generated_ids = model.generate(**inputs, max_new_tokens=3072, temperature=0.7)
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
]
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
return ocr_text.strip(), analysis_text.strip()
|
| 125 |
|
|
@@ -360,7 +369,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
|
|
| 360 |
- AI๊ฐ ์์ฑํ ์ ๋ณด์ด๋ฏ๋ก ์ ํํ์ง ์์ ์ ์์ต๋๋ค
|
| 361 |
|
| 362 |
**๐ค ๊ธฐ์ ์คํ**
|
| 363 |
-
- Qwen2.5-VL-7B-Instruct (OCR
|
|
|
|
| 364 |
""")
|
| 365 |
|
| 366 |
if __name__ == "__main__":
|
|
|
|
| 7 |
import spaces
|
| 8 |
import torch
|
| 9 |
from PIL import Image
|
| 10 |
+
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor, AutoTokenizer, AutoModelForCausalLM
|
| 11 |
from qwen_vl_utils import process_vision_info
|
| 12 |
|
| 13 |
+
# OCR ๋ชจ๋ธ ID
|
| 14 |
+
OCR_MODEL_ID = "Qwen/Qwen2.5-VL-7B-Instruct"
|
| 15 |
+
|
| 16 |
+
# ์ฝ ์ ๋ณด ๋ถ์ ๋ชจ๋ธ ID (์๋ฃ ์ ๋ฌธ)
|
| 17 |
+
MED_MODEL_ID = "google/gemma-2-2b-it"
|
| 18 |
|
| 19 |
|
| 20 |
def _extract_assistant_content(decoded: str) -> str:
|
|
|
|
| 38 |
def analyze_medication_image(image: Image.Image) -> Tuple[str, str]:
|
| 39 |
"""์ด๋ฏธ์ง์์ OCR ์ถ์ถ ํ ์ฝ ์ ๋ณด ๋ถ์"""
|
| 40 |
try:
|
| 41 |
+
# Step 1: OCR - Qwen2.5-VL๋ก ์ด๋ฏธ์ง์์ ํ
์คํธ ์ถ์ถ
|
| 42 |
+
ocr_model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 43 |
+
OCR_MODEL_ID,
|
| 44 |
torch_dtype="auto",
|
| 45 |
device_map="auto"
|
| 46 |
)
|
| 47 |
+
ocr_processor = AutoProcessor.from_pretrained(OCR_MODEL_ID)
|
| 48 |
|
|
|
|
| 49 |
ocr_messages = [
|
| 50 |
{
|
| 51 |
"role": "user",
|
|
|
|
| 56 |
}
|
| 57 |
]
|
| 58 |
|
| 59 |
+
text = ocr_processor.apply_chat_template(ocr_messages, tokenize=False, add_generation_prompt=True)
|
| 60 |
image_inputs, video_inputs = process_vision_info(ocr_messages)
|
| 61 |
+
inputs = ocr_processor(
|
| 62 |
text=[text],
|
| 63 |
images=image_inputs,
|
| 64 |
videos=video_inputs,
|
| 65 |
padding=True,
|
| 66 |
return_tensors="pt",
|
| 67 |
)
|
| 68 |
+
inputs = inputs.to(ocr_model.device)
|
| 69 |
|
| 70 |
with torch.no_grad():
|
| 71 |
+
generated_ids = ocr_model.generate(**inputs, max_new_tokens=2048)
|
| 72 |
|
| 73 |
generated_ids_trimmed = [
|
| 74 |
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 75 |
]
|
| 76 |
|
| 77 |
+
ocr_text = ocr_processor.batch_decode(
|
| 78 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 79 |
)[0]
|
| 80 |
|
| 81 |
if not ocr_text or ocr_text.strip() == "":
|
| 82 |
return "ํ
์คํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.", ""
|
| 83 |
|
| 84 |
+
# Step 2: ์ฝ ์ ๋ณด ๋ถ์ - Gemma-2๋ก ์๋ฃ ์ ๋ณด ์ ๊ณต
|
| 85 |
+
med_model = AutoModelForCausalLM.from_pretrained(
|
| 86 |
+
MED_MODEL_ID,
|
| 87 |
+
torch_dtype=torch.bfloat16,
|
| 88 |
+
device_map="auto"
|
| 89 |
+
)
|
| 90 |
+
med_tokenizer = AutoTokenizer.from_pretrained(MED_MODEL_ID)
|
| 91 |
+
|
| 92 |
+
analysis_prompt = f"""๋ค์์ ์ฝ ๋ดํฌ๋ ์ฒ๋ฐฉ์ ์์ ์ถ์ถํ ํ
์คํธ์
๋๋ค:
|
| 93 |
|
| 94 |
{ocr_text}
|
| 95 |
|
| 96 |
+
์ ํ
์คํธ์์ ์ฝ ์ด๋ฆ์ ์ฐพ์์, ๊ฐ ์ฝ์ ๋ํด **๋
ธ์ธ๊ณผ ์ด๋ฆฐ์ด ๋ชจ๋ ์ฝ๊ฒ ์ดํดํ ์ ์๋๋ก** ์ฌ๋ฏธ์๊ณ ์น๊ทผํ๊ฒ ์ค๋ช
ํด์ฃผ์ธ์:
|
| 97 |
|
| 98 |
+
๐ **๊ฐ ์ฝ๋ง๋ค ๋ค์ ์ ๋ณด๋ฅผ ํฌํจํด์ฃผ์ธ์:**
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
1. ๐ **์ฝ ์ด๋ฆ**: ์ ํํ ์ฝ ์ด๋ฆ
|
| 101 |
+
2. ๐ฏ **ํจ๋ฅ**: ์ด ์ฝ์ด ๋ฌด์์ ์น๋ฃํ๊ณ ์ด๋ป๊ฒ ๋์์ด ๋๋์ง
|
| 102 |
+
3. โ ๏ธ **๋ถ์์ฉ**: ์ฃผ์ํด์ผ ํ ๋ถ์์ฉ๋ค
|
| 103 |
+
4. ๐ก **๋ณต์ฉ ๋ฐฉ๋ฒ**: ์ธ์ , ์ด๋ป๊ฒ ๋จน์ด์ผ ํ๋์ง (์์ /์ํ, ํ๋ฃจ ๋ช ๋ฒ ๋ฑ)
|
| 104 |
+
5. ๐ซ **์ฃผ์์ฌํญ**: ์ด ์ฝ๊ณผ ํจ๊ป ๋จน์ผ๋ฉด ์ ๋๋ ๊ฒ๋ค (์์, ๋ค๋ฅธ ์ฝ ๋ฑ)
|
| 105 |
|
| 106 |
+
**์คํ์ผ ๊ฐ์ด๋:**
|
| 107 |
+
- ์ด๋ชจ์ง๋ฅผ ์ ๊ทน ํ์ฉํ์ฌ ์ฌ๋ฏธ์๊ฒ ์์ฑ
|
| 108 |
+
- ํ ๋จธ๋ ํ ์๋ฒ์ง๋ ์ด๋ฑํ์๋ ์ดํดํ ์ ์๋ ์ฌ์ด ๋จ์ด ์ฌ์ฉ
|
| 109 |
+
- ๊ฐ ์ฝ๋ง๋ค ๊ตฌ๋ถ์ ์ผ๋ก ๊ตฌ๋ถ
|
| 110 |
+
- ์น๊ทผํ๊ณ ๋ฐ๋ปํ ๋งํฌ ์ฌ์ฉ
|
| 111 |
+
- ๋งํฌ๋ค์ด ํ์์ผ๋ก ์์ฑ
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
์์ํด์ฃผ์ธ์!"""
|
|
|
|
| 114 |
|
| 115 |
+
messages = [
|
| 116 |
+
{"role": "user", "content": analysis_prompt}
|
| 117 |
]
|
| 118 |
|
| 119 |
+
input_text = med_tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 120 |
+
inputs = med_tokenizer(input_text, return_tensors="pt").to(med_model.device)
|
| 121 |
+
|
| 122 |
+
with torch.no_grad():
|
| 123 |
+
outputs = med_model.generate(
|
| 124 |
+
**inputs,
|
| 125 |
+
max_new_tokens=3072,
|
| 126 |
+
temperature=0.7,
|
| 127 |
+
top_p=0.9,
|
| 128 |
+
do_sample=True
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
analysis_text = med_tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 132 |
|
| 133 |
return ocr_text.strip(), analysis_text.strip()
|
| 134 |
|
|
|
|
| 369 |
- AI๊ฐ ์์ฑํ ์ ๋ณด์ด๋ฏ๋ก ์ ํํ์ง ์์ ์ ์์ต๋๋ค
|
| 370 |
|
| 371 |
**๐ค ๊ธฐ์ ์คํ**
|
| 372 |
+
- Qwen2.5-VL-7B-Instruct (OCR ํ
์คํธ ์ถ์ถ)
|
| 373 |
+
- Google Gemma-2-2B-IT (์๋ฃ ์ ๋ณด ๋ถ์ ๋ฐ ์ค๋ช
)
|
| 374 |
""")
|
| 375 |
|
| 376 |
if __name__ == "__main__":
|