Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
9c6a17f
1
Parent(s):
14a25f4
first init
Browse files
app.py
CHANGED
|
@@ -18,10 +18,9 @@ import spaces
|
|
| 18 |
# ==================== DEEPSEEK OCR SETUP ====================
|
| 19 |
OCR_MODEL_NAME = 'deepseek-ai/DeepSeek-OCR'
|
| 20 |
|
| 21 |
-
print("🔄 Loading OCR
|
| 22 |
ocr_tokenizer = AutoTokenizer.from_pretrained(OCR_MODEL_NAME, trust_remote_code=True)
|
| 23 |
|
| 24 |
-
print("🔄 Loading OCR model...")
|
| 25 |
try:
|
| 26 |
ocr_model = AutoModel.from_pretrained(
|
| 27 |
OCR_MODEL_NAME,
|
|
@@ -41,6 +40,7 @@ except (ImportError, ValueError):
|
|
| 41 |
use_safetensors=True
|
| 42 |
)
|
| 43 |
|
|
|
|
| 44 |
ocr_model = ocr_model.eval()
|
| 45 |
|
| 46 |
MODEL_CONFIGS = {
|
|
@@ -124,12 +124,12 @@ def clean_output(text, include_images=False, remove_labels=False):
|
|
| 124 |
return text.strip()
|
| 125 |
|
| 126 |
# ==================== OCR FUNCTIONS ====================
|
| 127 |
-
@spaces.GPU
|
| 128 |
def ocr_process_image(image, mode="Crab"):
|
| 129 |
if image is None:
|
| 130 |
return "Error: Upload image"
|
| 131 |
|
| 132 |
-
# Move model to GPU inside @spaces.GPU decorated function
|
| 133 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 134 |
ocr_model.to(device)
|
| 135 |
|
|
@@ -281,9 +281,10 @@ def split_by_sentences(text: str, max_words: int = 100):
|
|
| 281 |
|
| 282 |
return chunks
|
| 283 |
|
| 284 |
-
@spaces.GPU
|
| 285 |
def translate_chunk(chunk_text):
|
| 286 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 287 |
if hasattr(translator, 'model') and hasattr(translator.model, 'to'):
|
| 288 |
translator.model.to(device)
|
| 289 |
return translator.translate(chunk_text, max_new_tokens=2048).strip()
|
|
@@ -417,12 +418,12 @@ footer { visibility: hidden }
|
|
| 417 |
}
|
| 418 |
"""
|
| 419 |
|
| 420 |
-
with gr.Blocks(theme=gr.themes.Soft(), css=css, title="
|
| 421 |
|
| 422 |
gr.Markdown("""
|
| 423 |
<div class="main-title">
|
| 424 |
<h1>🦀 MedCrab Translation</h1>
|
| 425 |
-
<p><b>Quét PDF Y khoa → Dịch trực tiếp sang tiếng Việt</b></p>
|
| 426 |
</div>
|
| 427 |
""")
|
| 428 |
|
|
@@ -456,7 +457,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, title="🦀 MedCrab Translation"
|
|
| 456 |
- **Crab**: 1024 base + 640 tiles (Tốt nhất, cân bằng)
|
| 457 |
- **Base**: 1024×1024 (Nhanh hơn)
|
| 458 |
|
| 459 |
-
**Lưu ý:** Space này sử dụng GPU miễn phí của Hugging Face
|
| 460 |
""")
|
| 461 |
|
| 462 |
file_in.change(load_image, [file_in, page_input], [input_img])
|
|
|
|
| 18 |
# ==================== DEEPSEEK OCR SETUP ====================
|
| 19 |
OCR_MODEL_NAME = 'deepseek-ai/DeepSeek-OCR'
|
| 20 |
|
| 21 |
+
print("🔄 Loading OCR model...")
|
| 22 |
ocr_tokenizer = AutoTokenizer.from_pretrained(OCR_MODEL_NAME, trust_remote_code=True)
|
| 23 |
|
|
|
|
| 24 |
try:
|
| 25 |
ocr_model = AutoModel.from_pretrained(
|
| 26 |
OCR_MODEL_NAME,
|
|
|
|
| 40 |
use_safetensors=True
|
| 41 |
)
|
| 42 |
|
| 43 |
+
# Don't move model to GPU here - let @spaces.GPU handle it
|
| 44 |
ocr_model = ocr_model.eval()
|
| 45 |
|
| 46 |
MODEL_CONFIGS = {
|
|
|
|
| 124 |
return text.strip()
|
| 125 |
|
| 126 |
# ==================== OCR FUNCTIONS ====================
|
| 127 |
+
@spaces.GPU
|
| 128 |
def ocr_process_image(image, mode="Crab"):
|
| 129 |
if image is None:
|
| 130 |
return "Error: Upload image"
|
| 131 |
|
| 132 |
+
# Move model to GPU inside the @spaces.GPU decorated function
|
| 133 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 134 |
ocr_model.to(device)
|
| 135 |
|
|
|
|
| 281 |
|
| 282 |
return chunks
|
| 283 |
|
| 284 |
+
@spaces.GPU
|
| 285 |
def translate_chunk(chunk_text):
|
| 286 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 287 |
+
# Ensure translator is on correct device
|
| 288 |
if hasattr(translator, 'model') and hasattr(translator.model, 'to'):
|
| 289 |
translator.model.to(device)
|
| 290 |
return translator.translate(chunk_text, max_new_tokens=2048).strip()
|
|
|
|
| 418 |
}
|
| 419 |
"""
|
| 420 |
|
| 421 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=css, title="OCR + Translation") as demo:
|
| 422 |
|
| 423 |
gr.Markdown("""
|
| 424 |
<div class="main-title">
|
| 425 |
<h1>🦀 MedCrab Translation</h1>
|
| 426 |
+
<p><b>Quét PDF Y khoa → Dịch trực tiếp sang tiếng Việt (Streaming)</b></p>
|
| 427 |
</div>
|
| 428 |
""")
|
| 429 |
|
|
|
|
| 457 |
- **Crab**: 1024 base + 640 tiles (Tốt nhất, cân bằng)
|
| 458 |
- **Base**: 1024×1024 (Nhanh hơn)
|
| 459 |
|
| 460 |
+
**Lưu ý:** Space này sử dụng GPU miễn phí của Hugging Face, có thể mất vài giây để khởi động.
|
| 461 |
""")
|
| 462 |
|
| 463 |
file_in.change(load_image, [file_in, page_input], [input_img])
|