sdfdsfads23333
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ from transformers import AutoModel, AutoProcessor, AutoTokenizer, AutoModelForCa
|
|
| 12 |
OCR_MODEL_ID = "ibm-granite/granite-docling-258M"
|
| 13 |
|
| 14 |
# Stage 2: LLM ๋ชจ๋ธ (ํ
์คํธ์์ ์ฝ ์ด๋ฆ ์ถ์ถ)
|
| 15 |
-
LLM_MODEL_ID = "
|
| 16 |
|
| 17 |
|
| 18 |
def _load_ocr_model():
|
|
@@ -50,7 +50,7 @@ print("๐ Loading Granite Docling OCR model...")
|
|
| 50 |
OCR_MODEL, OCR_PROCESSOR = _load_ocr_model()
|
| 51 |
print("โ
OCR model loaded!")
|
| 52 |
|
| 53 |
-
print("๐ Loading
|
| 54 |
LLM_MODEL, LLM_TOKENIZER = _load_llm_model()
|
| 55 |
print("โ
LLM model loaded!")
|
| 56 |
|
|
@@ -88,21 +88,24 @@ def extract_text_from_image(image: Image.Image) -> str:
|
|
| 88 |
|
| 89 |
|
| 90 |
def extract_medications_from_text(text: str) -> List[str]:
|
| 91 |
-
"""Stage 2:
|
| 92 |
try:
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
Return ONLY valid JSON format
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
inputs = LLM_TOKENIZER(prompt, return_tensors="pt").to(LLM_MODEL.device)
|
| 108 |
|
|
@@ -118,9 +121,10 @@ Return only the JSON array of medication names.<|eot_id|><|start_header_id|>assi
|
|
| 118 |
|
| 119 |
response = LLM_TOKENIZER.decode(outputs[0], skip_special_tokens=True)
|
| 120 |
|
| 121 |
-
# Extract assistant response
|
| 122 |
-
if "<|
|
| 123 |
-
response = response.split("<|
|
|
|
|
| 124 |
|
| 125 |
# Parse JSON
|
| 126 |
json_match = re.search(r'\{.*?\}', response, re.DOTALL)
|
|
@@ -305,7 +309,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
|
|
| 305 |
|
| 306 |
**โน๏ธ 2๋จ๊ณ ํ์ดํ๋ผ์ธ**
|
| 307 |
- **Stage 1**: Granite Docling (OCR) - ์ด๋ฏธ์ง์์ ๋ชจ๋ ํ
์คํธ ์ถ์ถ
|
| 308 |
-
- **Stage 2**:
|
| 309 |
|
| 310 |
์ค์ ๋ณต์ฝ์ ์์ฌยท์ฝ์ฌ์ ์ง์๋ฅผ ๋ฐ๋ฅด์ธ์.
|
| 311 |
""")
|
|
|
|
| 12 |
OCR_MODEL_ID = "ibm-granite/granite-docling-258M"
|
| 13 |
|
| 14 |
# Stage 2: LLM ๋ชจ๋ธ (ํ
์คํธ์์ ์ฝ ์ด๋ฆ ์ถ์ถ)
|
| 15 |
+
LLM_MODEL_ID = "Qwen/Qwen2.5-7B-Instruct"
|
| 16 |
|
| 17 |
|
| 18 |
def _load_ocr_model():
|
|
|
|
| 50 |
OCR_MODEL, OCR_PROCESSOR = _load_ocr_model()
|
| 51 |
print("โ
OCR model loaded!")
|
| 52 |
|
| 53 |
+
print("๐ Loading Qwen2.5-7B-Instruct...")
|
| 54 |
LLM_MODEL, LLM_TOKENIZER = _load_llm_model()
|
| 55 |
print("โ
LLM model loaded!")
|
| 56 |
|
|
|
|
| 88 |
|
| 89 |
|
| 90 |
def extract_medications_from_text(text: str) -> List[str]:
|
| 91 |
+
"""Stage 2: Qwen2.5๋ก ํ
์คํธ์์ ์ฝ ์ด๋ฆ๋ง ์ถ์ถ"""
|
| 92 |
try:
|
| 93 |
+
messages = [
|
| 94 |
+
{
|
| 95 |
+
"role": "system",
|
| 96 |
+
"content": "You are a medical text analyzer. Extract only medication names from the given text and return them as a JSON array. Return ONLY valid JSON format."
|
| 97 |
+
},
|
| 98 |
+
{
|
| 99 |
+
"role": "user",
|
| 100 |
+
"content": f"Extract all medication names from this text:\n\n{text}\n\nReturn format: {{\"medications\": [\"name1\", \"name2\"]}}"
|
| 101 |
+
}
|
| 102 |
+
]
|
| 103 |
+
|
| 104 |
+
prompt = LLM_TOKENIZER.apply_chat_template(
|
| 105 |
+
messages,
|
| 106 |
+
tokenize=False,
|
| 107 |
+
add_generation_prompt=True
|
| 108 |
+
)
|
| 109 |
|
| 110 |
inputs = LLM_TOKENIZER(prompt, return_tensors="pt").to(LLM_MODEL.device)
|
| 111 |
|
|
|
|
| 121 |
|
| 122 |
response = LLM_TOKENIZER.decode(outputs[0], skip_special_tokens=True)
|
| 123 |
|
| 124 |
+
# Extract assistant response (Qwen format)
|
| 125 |
+
if "<|im_start|>assistant" in response:
|
| 126 |
+
response = response.split("<|im_start|>assistant")[-1]
|
| 127 |
+
response = response.replace("<|im_end|>", "").strip()
|
| 128 |
|
| 129 |
# Parse JSON
|
| 130 |
json_match = re.search(r'\{.*?\}', response, re.DOTALL)
|
|
|
|
| 309 |
|
| 310 |
**โน๏ธ 2๋จ๊ณ ํ์ดํ๋ผ์ธ**
|
| 311 |
- **Stage 1**: Granite Docling (OCR) - ์ด๋ฏธ์ง์์ ๋ชจ๋ ํ
์คํธ ์ถ์ถ
|
| 312 |
+
- **Stage 2**: Qwen2.5 7B (LLM) - ์ถ์ถ๋ ํ
์คํธ์์ ์ฝ ์ด๋ฆ๋ง ์๋ณ
|
| 313 |
|
| 314 |
์ค์ ๋ณต์ฝ์ ์์ฌยท์ฝ์ฌ์ ์ง์๋ฅผ ๋ฐ๋ฅด์ธ์.
|
| 315 |
""")
|