fix: move EasyOCR initialization inside GPU decorator
Browse files- Initialize EasyOCR inside @spaces.GPU decorated function
- Fixes CUDA initialization error in Spaces Stateless GPU environment
- Reader is now created per inference call
๐ค Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
app.py
CHANGED
|
@@ -8,10 +8,7 @@ import spaces
|
|
| 8 |
from PIL import Image
|
| 9 |
import easyocr
|
| 10 |
|
| 11 |
-
# EasyOCR ์ด๊ธฐํ (
|
| 12 |
-
print("๐ Loading EasyOCR (Korean + English)...")
|
| 13 |
-
OCR_MODEL = easyocr.Reader(['ko', 'en'], gpu=True)
|
| 14 |
-
print("โ
EasyOCR loaded!")
|
| 15 |
|
| 16 |
|
| 17 |
def _extract_assistant_content(decoded: str) -> str:
|
|
@@ -31,14 +28,18 @@ def _extract_json_block(text: str) -> Optional[str]:
|
|
| 31 |
return match.group(0)
|
| 32 |
|
| 33 |
|
|
|
|
| 34 |
def extract_text_from_image(image: Image.Image) -> str:
|
| 35 |
"""EasyOCR๋ก ์ด๋ฏธ์ง์์ ํ
์คํธ ์ถ์ถ"""
|
| 36 |
try:
|
|
|
|
|
|
|
|
|
|
| 37 |
# PIL Image๋ฅผ numpy array๋ก ๋ณํ
|
| 38 |
img_array = np.array(image)
|
| 39 |
|
| 40 |
# EasyOCR ์คํ
|
| 41 |
-
result =
|
| 42 |
|
| 43 |
# ๊ฒฐ๊ณผ์์ ํ
์คํธ๋ง ์ถ์ถ
|
| 44 |
if result:
|
|
|
|
| 8 |
from PIL import Image
|
| 9 |
import easyocr
|
| 10 |
|
| 11 |
+
# EasyOCR์ ํจ์ ๋ด๋ถ์์ ์ด๊ธฐํ๋ฉ๋๋ค (Spaces GPU ํ๊ฒฝ ํธํ)
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
def _extract_assistant_content(decoded: str) -> str:
|
|
|
|
| 28 |
return match.group(0)
|
| 29 |
|
| 30 |
|
| 31 |
+
@spaces.GPU
|
| 32 |
def extract_text_from_image(image: Image.Image) -> str:
|
| 33 |
"""EasyOCR๋ก ์ด๋ฏธ์ง์์ ํ
์คํธ ์ถ์ถ"""
|
| 34 |
try:
|
| 35 |
+
# GPU ํ๊ฒฝ์์ EasyOCR ์ด๊ธฐํ
|
| 36 |
+
reader = easyocr.Reader(['ko', 'en'], gpu=True)
|
| 37 |
+
|
| 38 |
# PIL Image๋ฅผ numpy array๋ก ๋ณํ
|
| 39 |
img_array = np.array(image)
|
| 40 |
|
| 41 |
# EasyOCR ์คํ
|
| 42 |
+
result = reader.readtext(img_array)
|
| 43 |
|
| 44 |
# ๊ฒฐ๊ณผ์์ ํ
์คํธ๋ง ์ถ์ถ
|
| 45 |
if result:
|