Spaces:
Running
on
Zero
Running
on
Zero
remove task type extract text
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ model = AutoModel.from_pretrained(
|
|
| 14 |
trust_remote_code=True,
|
| 15 |
use_safetensors=True,
|
| 16 |
)
|
| 17 |
-
model = model.eval()
|
| 18 |
|
| 19 |
|
| 20 |
@spaces.GPU
|
|
@@ -27,6 +27,9 @@ def process_image(image, model_size, task_type):
|
|
| 27 |
model_size: Model size configuration
|
| 28 |
task_type: OCR task type
|
| 29 |
"""
|
|
|
|
|
|
|
|
|
|
| 30 |
# Create temporary directory for output
|
| 31 |
with tempfile.TemporaryDirectory() as output_path:
|
| 32 |
# Set prompt based on task type
|
|
@@ -34,8 +37,6 @@ def process_image(image, model_size, task_type):
|
|
| 34 |
prompt = "<image>\nFree OCR. "
|
| 35 |
elif task_type == "Convert to Markdown":
|
| 36 |
prompt = "<image>\n<|grounding|>Convert the document to markdown. "
|
| 37 |
-
elif task_type == "Extract Text":
|
| 38 |
-
prompt = "<image>\nExtract all text from the image. "
|
| 39 |
else:
|
| 40 |
prompt = "<image>\nFree OCR. "
|
| 41 |
|
|
@@ -59,7 +60,7 @@ def process_image(image, model_size, task_type):
|
|
| 59 |
config = size_configs.get(model_size, size_configs["Gundam (Recommended)"])
|
| 60 |
|
| 61 |
# Run inference
|
| 62 |
-
result =
|
| 63 |
tokenizer,
|
| 64 |
prompt=prompt,
|
| 65 |
image_file=temp_image_path,
|
|
@@ -71,6 +72,7 @@ def process_image(image, model_size, task_type):
|
|
| 71 |
test_compress=True,
|
| 72 |
)
|
| 73 |
|
|
|
|
| 74 |
return result
|
| 75 |
|
| 76 |
|
|
@@ -105,7 +107,7 @@ with gr.Blocks(title="DeepSeek-OCR") as demo:
|
|
| 105 |
)
|
| 106 |
|
| 107 |
task_type = gr.Dropdown(
|
| 108 |
-
choices=["Free OCR", "Convert to Markdown"
|
| 109 |
value="Convert to Markdown",
|
| 110 |
label="Task Type",
|
| 111 |
)
|
|
|
|
| 14 |
trust_remote_code=True,
|
| 15 |
use_safetensors=True,
|
| 16 |
)
|
| 17 |
+
model = model.eval()
|
| 18 |
|
| 19 |
|
| 20 |
@spaces.GPU
|
|
|
|
| 27 |
model_size: Model size configuration
|
| 28 |
task_type: OCR task type
|
| 29 |
"""
|
| 30 |
+
# 在 GPU 函数内部移动模型到 GPU
|
| 31 |
+
model_gpu = model.cuda().to(torch.bfloat16)
|
| 32 |
+
|
| 33 |
# Create temporary directory for output
|
| 34 |
with tempfile.TemporaryDirectory() as output_path:
|
| 35 |
# Set prompt based on task type
|
|
|
|
| 37 |
prompt = "<image>\nFree OCR. "
|
| 38 |
elif task_type == "Convert to Markdown":
|
| 39 |
prompt = "<image>\n<|grounding|>Convert the document to markdown. "
|
|
|
|
|
|
|
| 40 |
else:
|
| 41 |
prompt = "<image>\nFree OCR. "
|
| 42 |
|
|
|
|
| 60 |
config = size_configs.get(model_size, size_configs["Gundam (Recommended)"])
|
| 61 |
|
| 62 |
# Run inference
|
| 63 |
+
result = model_gpu.infer(
|
| 64 |
tokenizer,
|
| 65 |
prompt=prompt,
|
| 66 |
image_file=temp_image_path,
|
|
|
|
| 72 |
test_compress=True,
|
| 73 |
)
|
| 74 |
|
| 75 |
+
print(f"====\nresult: {result}\n====\n")
|
| 76 |
return result
|
| 77 |
|
| 78 |
|
|
|
|
| 107 |
)
|
| 108 |
|
| 109 |
task_type = gr.Dropdown(
|
| 110 |
+
choices=["Free OCR", "Convert to Markdown"],
|
| 111 |
value="Convert to Markdown",
|
| 112 |
label="Task Type",
|
| 113 |
)
|