perf: preload models at startup for faster inference
Browse files- Load models once at startup instead of per request
- Use global model variables to avoid repeated loading
- Reduces inference time from 160s+ to ~10s
- Models are loaded when app starts, not during inference
๐ค Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
app.py
CHANGED
|
@@ -23,6 +23,39 @@ OCR_MODEL_ID = "Qwen/Qwen2.5-VL-7B-Instruct"
|
|
| 23 |
# ์ฝ ์ ๋ณด ๋ถ์ ๋ชจ๋ธ ID (์๋ฃ ์ ๋ฌธ)
|
| 24 |
MED_MODEL_ID = "google/medgemma-4b-it"
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def _extract_assistant_content(decoded: str) -> str:
|
| 28 |
"""์ด์์คํดํธ ์๋ต ์ถ์ถ"""
|
|
@@ -46,13 +79,6 @@ def analyze_medication_image(image: Image.Image) -> Tuple[str, str]:
|
|
| 46 |
"""์ด๋ฏธ์ง์์ OCR ์ถ์ถ ํ ์ฝ ์ ๋ณด ๋ถ์"""
|
| 47 |
try:
|
| 48 |
# Step 1: OCR - Qwen2.5-VL๋ก ์ด๋ฏธ์ง์์ ํ
์คํธ ์ถ์ถ
|
| 49 |
-
ocr_model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 50 |
-
OCR_MODEL_ID,
|
| 51 |
-
torch_dtype="auto",
|
| 52 |
-
device_map="auto"
|
| 53 |
-
)
|
| 54 |
-
ocr_processor = AutoProcessor.from_pretrained(OCR_MODEL_ID)
|
| 55 |
-
|
| 56 |
ocr_messages = [
|
| 57 |
{
|
| 58 |
"role": "user",
|
|
@@ -63,38 +89,32 @@ def analyze_medication_image(image: Image.Image) -> Tuple[str, str]:
|
|
| 63 |
}
|
| 64 |
]
|
| 65 |
|
| 66 |
-
text =
|
| 67 |
image_inputs, video_inputs = process_vision_info(ocr_messages)
|
| 68 |
-
inputs =
|
| 69 |
text=[text],
|
| 70 |
images=image_inputs,
|
| 71 |
videos=video_inputs,
|
| 72 |
padding=True,
|
| 73 |
return_tensors="pt",
|
| 74 |
)
|
| 75 |
-
inputs = inputs.to(
|
| 76 |
|
| 77 |
with torch.no_grad():
|
| 78 |
-
generated_ids =
|
| 79 |
|
| 80 |
generated_ids_trimmed = [
|
| 81 |
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 82 |
]
|
| 83 |
|
| 84 |
-
ocr_text =
|
| 85 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 86 |
)[0]
|
| 87 |
|
| 88 |
if not ocr_text or ocr_text.strip() == "":
|
| 89 |
return "ํ
์คํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.", ""
|
| 90 |
|
| 91 |
-
# Step 2: ์ฝ ์ ๋ณด ๋ถ์ -
|
| 92 |
-
med_model = AutoModelForCausalLM.from_pretrained(
|
| 93 |
-
MED_MODEL_ID,
|
| 94 |
-
torch_dtype=torch.bfloat16,
|
| 95 |
-
device_map="auto"
|
| 96 |
-
)
|
| 97 |
-
med_tokenizer = AutoTokenizer.from_pretrained(MED_MODEL_ID)
|
| 98 |
|
| 99 |
analysis_prompt = f"""๋ค์์ ์ฝ ๋ดํฌ๋ ์ฒ๋ฐฉ์ ์์ ์ถ์ถํ ํ
์คํธ์
๋๋ค:
|
| 100 |
|
|
@@ -123,11 +143,11 @@ def analyze_medication_image(image: Image.Image) -> Tuple[str, str]:
|
|
| 123 |
{"role": "user", "content": analysis_prompt}
|
| 124 |
]
|
| 125 |
|
| 126 |
-
input_text =
|
| 127 |
-
inputs =
|
| 128 |
|
| 129 |
with torch.no_grad():
|
| 130 |
-
outputs =
|
| 131 |
**inputs,
|
| 132 |
max_new_tokens=3072,
|
| 133 |
temperature=0.7,
|
|
@@ -135,7 +155,7 @@ def analyze_medication_image(image: Image.Image) -> Tuple[str, str]:
|
|
| 135 |
do_sample=True
|
| 136 |
)
|
| 137 |
|
| 138 |
-
analysis_text =
|
| 139 |
|
| 140 |
return ocr_text.strip(), analysis_text.strip()
|
| 141 |
|
|
|
|
| 23 |
# ์ฝ ์ ๋ณด ๋ถ์ ๋ชจ๋ธ ID (์๋ฃ ์ ๋ฌธ)
|
| 24 |
MED_MODEL_ID = "google/medgemma-4b-it"
|
| 25 |
|
| 26 |
+
# ์ ์ญ ๋ชจ๋ธ ๋ณ์ (ํ ๋ฒ๋ง ๋ก๋)
|
| 27 |
+
OCR_MODEL = None
|
| 28 |
+
OCR_PROCESSOR = None
|
| 29 |
+
MED_MODEL = None
|
| 30 |
+
MED_TOKENIZER = None
|
| 31 |
+
|
| 32 |
+
def load_models():
|
| 33 |
+
"""๋ชจ๋ธ๋ค์ ํ ๋ฒ๋ง ๋ก๋"""
|
| 34 |
+
global OCR_MODEL, OCR_PROCESSOR, MED_MODEL, MED_TOKENIZER
|
| 35 |
+
|
| 36 |
+
if OCR_MODEL is None:
|
| 37 |
+
print("๐ Loading Qwen2.5-VL-7B for OCR...")
|
| 38 |
+
OCR_MODEL = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 39 |
+
OCR_MODEL_ID,
|
| 40 |
+
torch_dtype="auto",
|
| 41 |
+
device_map="auto"
|
| 42 |
+
)
|
| 43 |
+
OCR_PROCESSOR = AutoProcessor.from_pretrained(OCR_MODEL_ID)
|
| 44 |
+
print("โ
OCR model loaded!")
|
| 45 |
+
|
| 46 |
+
if MED_MODEL is None:
|
| 47 |
+
print("๐ Loading MedGemma-4B for medical analysis...")
|
| 48 |
+
MED_MODEL = AutoModelForCausalLM.from_pretrained(
|
| 49 |
+
MED_MODEL_ID,
|
| 50 |
+
torch_dtype=torch.bfloat16,
|
| 51 |
+
device_map="auto"
|
| 52 |
+
)
|
| 53 |
+
MED_TOKENIZER = AutoTokenizer.from_pretrained(MED_MODEL_ID)
|
| 54 |
+
print("โ
Medical model loaded!")
|
| 55 |
+
|
| 56 |
+
# ์ฑ ์์ ์ ๋ชจ๋ธ ๋ก๋
|
| 57 |
+
load_models()
|
| 58 |
+
|
| 59 |
|
| 60 |
def _extract_assistant_content(decoded: str) -> str:
|
| 61 |
"""์ด์์คํดํธ ์๋ต ์ถ์ถ"""
|
|
|
|
| 79 |
"""์ด๋ฏธ์ง์์ OCR ์ถ์ถ ํ ์ฝ ์ ๋ณด ๋ถ์"""
|
| 80 |
try:
|
| 81 |
# Step 1: OCR - Qwen2.5-VL๋ก ์ด๋ฏธ์ง์์ ํ
์คํธ ์ถ์ถ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
ocr_messages = [
|
| 83 |
{
|
| 84 |
"role": "user",
|
|
|
|
| 89 |
}
|
| 90 |
]
|
| 91 |
|
| 92 |
+
text = OCR_PROCESSOR.apply_chat_template(ocr_messages, tokenize=False, add_generation_prompt=True)
|
| 93 |
image_inputs, video_inputs = process_vision_info(ocr_messages)
|
| 94 |
+
inputs = OCR_PROCESSOR(
|
| 95 |
text=[text],
|
| 96 |
images=image_inputs,
|
| 97 |
videos=video_inputs,
|
| 98 |
padding=True,
|
| 99 |
return_tensors="pt",
|
| 100 |
)
|
| 101 |
+
inputs = inputs.to(OCR_MODEL.device)
|
| 102 |
|
| 103 |
with torch.no_grad():
|
| 104 |
+
generated_ids = OCR_MODEL.generate(**inputs, max_new_tokens=2048)
|
| 105 |
|
| 106 |
generated_ids_trimmed = [
|
| 107 |
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 108 |
]
|
| 109 |
|
| 110 |
+
ocr_text = OCR_PROCESSOR.batch_decode(
|
| 111 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 112 |
)[0]
|
| 113 |
|
| 114 |
if not ocr_text or ocr_text.strip() == "":
|
| 115 |
return "ํ
์คํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.", ""
|
| 116 |
|
| 117 |
+
# Step 2: ์ฝ ์ ๋ณด ๋ถ์ - MedGemma๋ก ์๋ฃ ์ ๋ณด ์ ๊ณต
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
analysis_prompt = f"""๋ค์์ ์ฝ ๋ดํฌ๋ ์ฒ๋ฐฉ์ ์์ ์ถ์ถํ ํ
์คํธ์
๋๋ค:
|
| 120 |
|
|
|
|
| 143 |
{"role": "user", "content": analysis_prompt}
|
| 144 |
]
|
| 145 |
|
| 146 |
+
input_text = MED_TOKENIZER.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 147 |
+
inputs = MED_TOKENIZER(input_text, return_tensors="pt").to(MED_MODEL.device)
|
| 148 |
|
| 149 |
with torch.no_grad():
|
| 150 |
+
outputs = MED_MODEL.generate(
|
| 151 |
**inputs,
|
| 152 |
max_new_tokens=3072,
|
| 153 |
temperature=0.7,
|
|
|
|
| 155 |
do_sample=True
|
| 156 |
)
|
| 157 |
|
| 158 |
+
analysis_text = MED_TOKENIZER.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 159 |
|
| 160 |
return ocr_text.strip(), analysis_text.strip()
|
| 161 |
|