perf: switch to Gemma-2-2B for faster inference
Browse files- Replace MedGemma-4B with Gemma-2-2B (2x smaller, much faster)
- Reduce max_new_tokens from 1536 to 768
- Add timing logs to track OCR and analysis performance
- Target: <30s total processing time
๐ค Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
app.py
CHANGED
|
@@ -17,8 +17,8 @@ HF_TOKEN = os.getenv("HF_TOKEN")
|
|
| 17 |
if HF_TOKEN:
|
| 18 |
login(token=HF_TOKEN.strip())
|
| 19 |
|
| 20 |
-
# ์ฝ ์ ๋ณด ๋ถ์ ๋ชจ๋ธ ID (
|
| 21 |
-
MED_MODEL_ID = "google/
|
| 22 |
|
| 23 |
# ์ ์ญ ๋ชจ๋ธ ๋ณ์ (ํ ๋ฒ๋ง ๋ก๋)
|
| 24 |
OCR_READER = None
|
|
@@ -35,7 +35,7 @@ def load_models():
|
|
| 35 |
print("โ
EasyOCR loaded!")
|
| 36 |
|
| 37 |
if MED_MODEL is None:
|
| 38 |
-
print("๐ Loading
|
| 39 |
MED_MODEL = AutoModelForCausalLM.from_pretrained(
|
| 40 |
MED_MODEL_ID,
|
| 41 |
torch_dtype=torch.bfloat16,
|
|
@@ -69,10 +69,14 @@ def _extract_json_block(text: str) -> Optional[str]:
|
|
| 69 |
@spaces.GPU(duration=120)
|
| 70 |
def analyze_medication_image(image: Image.Image) -> Tuple[str, str]:
|
| 71 |
"""์ด๋ฏธ์ง์์ OCR ์ถ์ถ ํ ์ฝ ์ ๋ณด ๋ถ์"""
|
|
|
|
| 72 |
try:
|
| 73 |
# Step 1: OCR - EasyOCR๋ก ๋น ๋ฅด๊ฒ ํ
์คํธ ์ถ์ถ
|
|
|
|
| 74 |
img_array = np.array(image)
|
| 75 |
ocr_results = OCR_READER.readtext(img_array)
|
|
|
|
|
|
|
| 76 |
|
| 77 |
if not ocr_results:
|
| 78 |
return "ํ
์คํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.", ""
|
|
@@ -82,6 +86,7 @@ def analyze_medication_image(image: Image.Image) -> Tuple[str, str]:
|
|
| 82 |
ocr_text = "\n".join([text for _, text, _ in ocr_results])
|
| 83 |
|
| 84 |
# Step 2: ์ฝ ์ ๋ณด ๋ถ์ - MedGemma๋ก ์๋ฃ ์ ๋ณด ์ ๊ณต
|
|
|
|
| 85 |
|
| 86 |
analysis_prompt = f"""๋ค์์ ์ฝ ๋ดํฌ๋ ์ฒ๋ฐฉ์ ์์ ์ถ์ถํ ํ
์คํธ์
๋๋ค:
|
| 87 |
|
|
@@ -116,7 +121,7 @@ def analyze_medication_image(image: Image.Image) -> Tuple[str, str]:
|
|
| 116 |
with torch.no_grad():
|
| 117 |
outputs = MED_MODEL.generate(
|
| 118 |
**inputs,
|
| 119 |
-
max_new_tokens=
|
| 120 |
temperature=0.7,
|
| 121 |
top_p=0.9,
|
| 122 |
do_sample=True
|
|
@@ -124,6 +129,11 @@ def analyze_medication_image(image: Image.Image) -> Tuple[str, str]:
|
|
| 124 |
|
| 125 |
analysis_text = MED_TOKENIZER.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
return ocr_text.strip(), analysis_text.strip()
|
| 128 |
|
| 129 |
except Exception as e:
|
|
@@ -363,8 +373,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
|
|
| 363 |
- AI๊ฐ ์์ฑํ ์ ๋ณด์ด๋ฏ๋ก ์ ํํ์ง ์์ ์ ์์ต๋๋ค
|
| 364 |
|
| 365 |
**๐ค ๊ธฐ์ ์คํ**
|
| 366 |
-
- EasyOCR (ํ๊ธ+์์ด, ์ด๊ณ ์ OCR
|
| 367 |
-
- Google
|
| 368 |
|
| 369 |
**๐ ์ค์ ๋ฐฉ๋ฒ**
|
| 370 |
- Hugging Face Spaces์ Settings โ Repository secrets์์ `HF_TOKEN` ์ถ๊ฐ ํ์
|
|
|
|
| 17 |
if HF_TOKEN:
|
| 18 |
login(token=HF_TOKEN.strip())
|
| 19 |
|
| 20 |
+
# ์ฝ ์ ๋ณด ๋ถ์ ๋ชจ๋ธ ID (๋น ๋ฅธ ์ถ๋ก ์ ์ํด ๊ฒฝ๋ ๋ชจ๋ธ ์ฌ์ฉ)
|
| 21 |
+
MED_MODEL_ID = "google/gemma-2-2b-it"
|
| 22 |
|
| 23 |
# ์ ์ญ ๋ชจ๋ธ ๋ณ์ (ํ ๋ฒ๋ง ๋ก๋)
|
| 24 |
OCR_READER = None
|
|
|
|
| 35 |
print("โ
EasyOCR loaded!")
|
| 36 |
|
| 37 |
if MED_MODEL is None:
|
| 38 |
+
print("๐ Loading Gemma-2-2B for medical analysis (8bit quantization)...")
|
| 39 |
MED_MODEL = AutoModelForCausalLM.from_pretrained(
|
| 40 |
MED_MODEL_ID,
|
| 41 |
torch_dtype=torch.bfloat16,
|
|
|
|
| 69 |
@spaces.GPU(duration=120)
|
| 70 |
def analyze_medication_image(image: Image.Image) -> Tuple[str, str]:
|
| 71 |
"""์ด๋ฏธ์ง์์ OCR ์ถ์ถ ํ ์ฝ ์ ๋ณด ๋ถ์"""
|
| 72 |
+
import time
|
| 73 |
try:
|
| 74 |
# Step 1: OCR - EasyOCR๋ก ๋น ๋ฅด๊ฒ ํ
์คํธ ์ถ์ถ
|
| 75 |
+
start_time = time.time()
|
| 76 |
img_array = np.array(image)
|
| 77 |
ocr_results = OCR_READER.readtext(img_array)
|
| 78 |
+
ocr_time = time.time() - start_time
|
| 79 |
+
print(f"โฑ๏ธ OCR took {ocr_time:.2f}s")
|
| 80 |
|
| 81 |
if not ocr_results:
|
| 82 |
return "ํ
์คํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.", ""
|
|
|
|
| 86 |
ocr_text = "\n".join([text for _, text, _ in ocr_results])
|
| 87 |
|
| 88 |
# Step 2: ์ฝ ์ ๋ณด ๋ถ์ - MedGemma๋ก ์๋ฃ ์ ๋ณด ์ ๊ณต
|
| 89 |
+
analysis_start = time.time()
|
| 90 |
|
| 91 |
analysis_prompt = f"""๋ค์์ ์ฝ ๋ดํฌ๋ ์ฒ๋ฐฉ์ ์์ ์ถ์ถํ ํ
์คํธ์
๋๋ค:
|
| 92 |
|
|
|
|
| 121 |
with torch.no_grad():
|
| 122 |
outputs = MED_MODEL.generate(
|
| 123 |
**inputs,
|
| 124 |
+
max_new_tokens=768,
|
| 125 |
temperature=0.7,
|
| 126 |
top_p=0.9,
|
| 127 |
do_sample=True
|
|
|
|
| 129 |
|
| 130 |
analysis_text = MED_TOKENIZER.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 131 |
|
| 132 |
+
analysis_time = time.time() - analysis_start
|
| 133 |
+
total_time = time.time() - start_time
|
| 134 |
+
print(f"โฑ๏ธ Medical analysis took {analysis_time:.2f}s")
|
| 135 |
+
print(f"โฑ๏ธ Total processing time: {total_time:.2f}s")
|
| 136 |
+
|
| 137 |
return ocr_text.strip(), analysis_text.strip()
|
| 138 |
|
| 139 |
except Exception as e:
|
|
|
|
| 373 |
- AI๊ฐ ์์ฑํ ์ ๋ณด์ด๋ฏ๋ก ์ ํํ์ง ์์ ์ ์์ต๋๋ค
|
| 374 |
|
| 375 |
**๐ค ๊ธฐ์ ์คํ**
|
| 376 |
+
- EasyOCR (ํ๊ธ+์์ด, ์ด๊ณ ์ OCR)
|
| 377 |
+
- Google Gemma-2-2B-IT (8bit ์์ํ, ๋น ๋ฅธ ์๋ฃ ์ ๋ณด ๋ถ์)
|
| 378 |
|
| 379 |
**๐ ์ค์ ๋ฐฉ๋ฒ**
|
| 380 |
- Hugging Face Spaces์ Settings โ Repository secrets์์ `HF_TOKEN` ์ถ๊ฐ ํ์
|