Spaces:
Running
on
Zero
Running
on
Zero
update app
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
from threading import Thread
|
| 4 |
from typing import Iterable
|
|
|
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import spaces
|
|
@@ -89,6 +90,48 @@ css = """
|
|
| 89 |
}
|
| 90 |
"""
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
# Constants for text generation
|
| 93 |
MAX_MAX_NEW_TOKENS = 5120
|
| 94 |
DEFAULT_MAX_NEW_TOKENS = 3072
|
|
@@ -105,8 +148,8 @@ model_m = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
|
| 105 |
torch_dtype=torch.float16
|
| 106 |
).to(device).eval()
|
| 107 |
|
| 108 |
-
# Load Dots.OCR
|
| 109 |
-
MODEL_PATH_D =
|
| 110 |
processor_d = AutoProcessor.from_pretrained(MODEL_PATH_D, trust_remote_code=True)
|
| 111 |
model_d = AutoModelForCausalLM.from_pretrained(
|
| 112 |
MODEL_PATH_D,
|
|
@@ -157,6 +200,7 @@ def generate_image(model_name: str, text: str, image: Image.Image,
|
|
| 157 |
"top_p": top_p,
|
| 158 |
"top_k": top_k,
|
| 159 |
"repetition_penalty": repetition_penalty,
|
|
|
|
| 160 |
}
|
| 161 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 162 |
thread.start()
|
|
@@ -182,26 +226,26 @@ with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
|
|
| 182 |
image_upload = gr.Image(type="pil", label="Upload Image", height=320)
|
| 183 |
image_submit = gr.Button("Submit", variant="primary")
|
| 184 |
gr.Examples(examples=image_examples, inputs=[image_query, image_upload])
|
| 185 |
-
|
| 186 |
with gr.Accordion("Advanced options", open=False):
|
| 187 |
max_new_tokens = gr.Slider(label="Max new tokens", minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS)
|
| 188 |
temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=4.0, step=0.1, value=0.6)
|
| 189 |
top_p = gr.Slider(label="Top-p (nucleus sampling)", minimum=0.05, maximum=1.0, step=0.05, value=0.9)
|
| 190 |
top_k = gr.Slider(label="Top-k", minimum=1, maximum=1000, step=1, value=50)
|
| 191 |
repetition_penalty = gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2)
|
| 192 |
-
|
| 193 |
with gr.Column(scale=3):
|
| 194 |
gr.Markdown("## Output", elem_id="output-title")
|
| 195 |
raw_output = gr.Textbox(label="Raw Output Stream", interactive=False, lines=13, show_copy_button=True)
|
| 196 |
with gr.Accordion("Formatted Result", open=True):
|
| 197 |
formatted_output = gr.Markdown(label="Formatted Result")
|
| 198 |
-
|
| 199 |
model_choice = gr.Radio(
|
| 200 |
choices=["Nanonets-OCR2-3B", "Dots.OCR"],
|
| 201 |
label="Select Model",
|
| 202 |
value="Nanonets-OCR2-3B"
|
| 203 |
)
|
| 204 |
-
|
| 205 |
image_submit.click(
|
| 206 |
fn=generate_image,
|
| 207 |
inputs=[model_choice, image_query, image_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sys
|
| 3 |
from threading import Thread
|
| 4 |
from typing import Iterable
|
| 5 |
+
from huggingface_hub import snapshot_download
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
import spaces
|
|
|
|
| 90 |
}
|
| 91 |
"""
|
| 92 |
|
| 93 |
+
# --- Fix for Dots.OCR Processor Loading ---
|
| 94 |
+
|
| 95 |
+
# Define a local directory to cache the model
|
| 96 |
+
CACHE_PATH = "./model_cache"
|
| 97 |
+
if not os.path.exists(CACHE_PATH):
|
| 98 |
+
os.makedirs(CACHE_PATH)
|
| 99 |
+
|
| 100 |
+
# Download the model files locally
|
| 101 |
+
model_path_d_local = snapshot_download(
|
| 102 |
+
repo_id='rednote-hilab/dots.ocr',
|
| 103 |
+
local_dir=CACHE_PATH,
|
| 104 |
+
max_workers=20,
|
| 105 |
+
local_dir_use_symlinks=False
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# Modify the configuration file to fix the processor loading issue
|
| 109 |
+
config_file_path = os.path.join(model_path_d_local, "configuration_dots.py")
|
| 110 |
+
|
| 111 |
+
if os.path.exists(config_file_path):
|
| 112 |
+
with open(config_file_path, 'r') as f:
|
| 113 |
+
input_code = f.read()
|
| 114 |
+
|
| 115 |
+
lines = input_code.splitlines()
|
| 116 |
+
if "class DotsVLProcessor" in input_code and not any("attributes = " in line for line in lines):
|
| 117 |
+
output_lines = []
|
| 118 |
+
for line in lines:
|
| 119 |
+
output_lines.append(line)
|
| 120 |
+
if line.strip().startswith("class DotsVLProcessor"):
|
| 121 |
+
# Insert the attributes line to specify which processors to load
|
| 122 |
+
output_lines.append(" attributes = [\"image_processor\", \"tokenizer\"]")
|
| 123 |
+
|
| 124 |
+
# Write the modified content back to the file
|
| 125 |
+
with open(config_file_path, 'w') as f:
|
| 126 |
+
f.write('\n'.join(output_lines))
|
| 127 |
+
print("Patched configuration_dots.py successfully.")
|
| 128 |
+
|
| 129 |
+
# Add the local model path to sys.path so transformers can use the modified code
|
| 130 |
+
sys.path.append(model_path_d_local)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
# --- Model Loading ---
|
| 134 |
+
|
| 135 |
# Constants for text generation
|
| 136 |
MAX_MAX_NEW_TOKENS = 5120
|
| 137 |
DEFAULT_MAX_NEW_TOKENS = 3072
|
|
|
|
| 148 |
torch_dtype=torch.float16
|
| 149 |
).to(device).eval()
|
| 150 |
|
| 151 |
+
# Load Dots.OCR from the local, patched directory
|
| 152 |
+
MODEL_PATH_D = model_path_d_local
|
| 153 |
processor_d = AutoProcessor.from_pretrained(MODEL_PATH_D, trust_remote_code=True)
|
| 154 |
model_d = AutoModelForCausalLM.from_pretrained(
|
| 155 |
MODEL_PATH_D,
|
|
|
|
| 200 |
"top_p": top_p,
|
| 201 |
"top_k": top_k,
|
| 202 |
"repetition_penalty": repetition_penalty,
|
| 203 |
+
"do_sample": True
|
| 204 |
}
|
| 205 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 206 |
thread.start()
|
|
|
|
| 226 |
image_upload = gr.Image(type="pil", label="Upload Image", height=320)
|
| 227 |
image_submit = gr.Button("Submit", variant="primary")
|
| 228 |
gr.Examples(examples=image_examples, inputs=[image_query, image_upload])
|
| 229 |
+
|
| 230 |
with gr.Accordion("Advanced options", open=False):
|
| 231 |
max_new_tokens = gr.Slider(label="Max new tokens", minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS)
|
| 232 |
temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=4.0, step=0.1, value=0.6)
|
| 233 |
top_p = gr.Slider(label="Top-p (nucleus sampling)", minimum=0.05, maximum=1.0, step=0.05, value=0.9)
|
| 234 |
top_k = gr.Slider(label="Top-k", minimum=1, maximum=1000, step=1, value=50)
|
| 235 |
repetition_penalty = gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2)
|
| 236 |
+
|
| 237 |
with gr.Column(scale=3):
|
| 238 |
gr.Markdown("## Output", elem_id="output-title")
|
| 239 |
raw_output = gr.Textbox(label="Raw Output Stream", interactive=False, lines=13, show_copy_button=True)
|
| 240 |
with gr.Accordion("Formatted Result", open=True):
|
| 241 |
formatted_output = gr.Markdown(label="Formatted Result")
|
| 242 |
+
|
| 243 |
model_choice = gr.Radio(
|
| 244 |
choices=["Nanonets-OCR2-3B", "Dots.OCR"],
|
| 245 |
label="Select Model",
|
| 246 |
value="Nanonets-OCR2-3B"
|
| 247 |
)
|
| 248 |
+
|
| 249 |
image_submit.click(
|
| 250 |
fn=generate_image,
|
| 251 |
inputs=[model_choice, image_query, image_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
|