Spaces:
Runtime error
Runtime error
File size: 13,679 Bytes
50060d5 e3a2d49 50060d5 717f406 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 6698955 e3a2d49 b62226e e3a2d49 6698955 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 e7411a5 e3a2d49 e7411a5 e3a2d49 e7411a5 e3a2d49 e7411a5 50060d5 e7411a5 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 50060d5 e7411a5 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 50060d5 e3a2d49 50060d5 6698955 e3a2d49 50060d5 e3a2d49 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
import gradio as gr
from PIL import Image
from transformers import AutoModelForImageTextToText, AutoProcessor, AutoTokenizer, TextIteratorStreamer
import spaces
from threading import Thread
from pdf2image import convert_from_path
import os
import tempfile
import base64
from io import BytesIO
import time
# --- Model Loading ---
# Load the model, processor, and tokenizer once when the app starts.
model_path = "nanonets/Nanonets-OCR-s"
print("Loading Nanonets OCR model...")
model = AutoModelForImageTextToText.from_pretrained(
model_path,
torch_dtype="auto",
device_map="auto",
attn_implementation="flash_attention_2"
)
model.eval()
processor = AutoProcessor.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path)
print("Model loaded successfully!")
# --- Helper Functions ---
def process_tags(content: str) -> str:
"""Replaces special tags with HTML entities to prevent them from being rendered as HTML."""
content = content.replace("<img>", "<img>")
content = content.replace("</img>", "</img>")
content = content.replace("<watermark>", "<watermark>")
content = content.replace("</watermark>", "</watermark>")
content = content.replace("<page_number>", "<page_number>")
content = content.replace("</page_number>", "</page_number>")
content = content.replace("<signature>", "<signature>")
content = content.replace("</signature>", "</signature>")
return content
def encode_image(image: Image) -> str:
"""Encodes an image to a base64 string."""
buffered = BytesIO()
image.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
return img_str
@spaces.GPU
def stream_request(
messages: list[dict],
model_name: str,
max_tokens: int = 8000,
temperature: float = 0.0,
):
"""
Stream text generation from the OCR model given messages with images and text.
Args:
messages: List of message dictionaries with role and content
model_name: Name of the model (unused but kept for compatibility)
max_tokens: Maximum number of tokens to generate
temperature: Temperature for generation (unused, model runs deterministically)
Yields:
str: Generated text chunks
"""
# Extract the image and text from messages
for message in messages:
if message["role"] == "user":
content = message["content"]
image_data = None
text_prompt = ""
for item in content:
if item["type"] == "image_url":
# Decode base64 image
image_url = item["image_url"]["url"]
if image_url.startswith("data:image/jpeg;base64,"):
image_base64 = image_url.split(",")[1]
image_bytes = base64.b64decode(image_base64)
image_data = Image.open(BytesIO(image_bytes))
elif item["type"] == "text":
text_prompt = item["text"]
if image_data is not None:
# Format messages in the expected format for the model
formatted_messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": [
{"type": "image", "image": image_data},
{"type": "text", "text": text_prompt},
]},
]
# Apply chat template to format the input properly
text = processor.apply_chat_template(
formatted_messages,
tokenize=False,
add_generation_prompt=True
)
# Process the formatted text and image
inputs = processor(
text=[text],
images=[image_data],
padding=True,
return_tensors="pt"
)
# Move inputs to the same device as the model
inputs = {k: v.to(model.device) if hasattr(v, 'to') else v for k, v in inputs.items()}
# Set up streaming
streamer = TextIteratorStreamer(
tokenizer,
timeout=60.0,
skip_prompt=True,
skip_special_tokens=True
)
generation_kwargs = {
**inputs,
"streamer": streamer,
"max_new_tokens": max_tokens,
"do_sample": False, # Deterministic generation
"pad_token_id": tokenizer.eos_token_id,
}
# Start generation in a separate thread
thread = Thread(target=model.generate, kwargs=generation_kwargs)
thread.start()
# Yield generated tokens as they come
for new_text in streamer:
yield new_text
thread.join()
return
# If no valid image/text pair found, return empty
yield ""
def convert_to_markdown_stream(
images: Image, model_name, max_gen_tokens, with_img_desc: bool = False
):
"""
Generator function that yields streaming markdown conversion results
Processes images one by one and concatenates results
"""
images = [images]
# validate_file_paths(file_paths)
# file_paths = convert_files_to_images(file_paths)
# resize_images(file_paths, max_img_size)
# Create system prompt for PDF to markdown conversion
if with_img_desc:
user_prompt = """Extract the text from the above document as if you were reading it naturally. Return the tables in html format. Return the equations in LaTeX representation. If there is an image in the document and image caption is not present, add a small description of the image inside the <img></img> tag; otherwise, add the image caption inside <img></img>. Watermarks should be wrapped in brackets. Ex: <watermark>OFFICIAL COPY</watermark>. Page numbers should be wrapped in brackets. Ex: <page_number>14</page_number> or <page_number>9/22</page_number>. Prefer using β and β for check boxes."""
else:
user_prompt = """Extract the text from the above document as if you were reading it naturally. Return the tables in html format. Watermarks should be wrapped in brackets. Ex: <watermark>OFFICIAL COPY</watermark>. Page numbers should be wrapped in brackets. Ex: <page_number>14</page_number> or <page_number>9/22</page_number>. Prefer using β and β for check boxes."""
# Accumulate results from all pages
full_markdown_content = ""
# Process each image individually
for i, image in enumerate(images):
# Build messages for this single image
content = [
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{encode_image(image)}"
},
},
{"type": "text", "text": user_prompt},
]
messages = [{"role": "user", "content": content}]
# Stream this individual page
page_content = ""
try:
for chunk in stream_request(
messages=messages,
model_name=model_name,
max_tokens=max_gen_tokens,
):
page_content += chunk
# Yield accumulated content from all pages processed so far + current page
current_total = (
full_markdown_content
+ f"Page {i + 1} of {len(images)}\n"
+ page_content
)
time.sleep(0.05)
yield current_total
# Process the completed page content and add it to the full content
full_markdown_content += (
f"Page {i + 1} of {len(images)}\n" + page_content
)
except Exception as e:
return f"Error: {e}"
def process_document(image, max_tokens, with_img_desc: bool = False):
"""
Process uploaded document (PDF or image) and convert to markdown.
Args:
file_path: Path to uploaded file
max_tokens: Maximum tokens per page
Returns:
Generator yielding markdown content
"""
if image is None:
return "Please upload a file first."
try:
# Handle PDF files
# if file_path.name.lower().endswith('.pdf'):
# # Convert PDF to images
# with tempfile.TemporaryDirectory() as temp_dir:
# # Copy uploaded file to temp directory
# temp_pdf_path = os.path.join(temp_dir, "document.pdf")
# import shutil
# shutil.copy(file_path.name, temp_pdf_path)
# # Convert PDF pages to images
# images = convert_from_path(temp_pdf_path, dpi=150)
# images = [image.convert("RGB") for image in images]
# images = [image.resize((2048, 2048)) for image in images]
# # Process each page
# for result in convert_to_markdown_stream(
# images, "nanonets/Nanonets-OCR-s", max_tokens, with_img_desc
# ):
# yield process_tags(result)
# # Handle image files
# else:
# # Open image directly
# image = Image.open(file_path.name).convert("RGB")
# image = image.resize((2048, 2048))
image = Image.fromarray(image)
image = image.resize((2048, 2048))
# Process single image
for result in convert_to_markdown_stream(
image, "nanonets/Nanonets-OCR-s", max_tokens, with_img_desc
):
yield process_tags(result)
except Exception as e:
yield f"Error processing document: {str(e)}"
# --- Gradio Interface ---
with gr.Blocks(title="PDF to Markdown Converter", theme=gr.themes.Soft()) as demo:
gr.HTML("""
<div class="title" style="text-align: center">
<h1>π Nanonets-OCR-s: PDF & Image to Markdown Converter</h1>
<p style="font-size: 1.1em; color: #6b7280; margin-bottom: 0.6em;">
Powered by <strong>Nanonets-OCR-s</strong>, A model for transforming documents into structured markdown with intelligent content recognition and semantic tagging.
</p>
<div style="display: flex; justify-content: center; gap: 20px; margin: 15px 0;">
<a href="https://huggingface.co/nanonets/Nanonets-OCR-s" target="_blank" style="text-decoration: none; color: #2563eb; font-weight: 500;">
π Hugging Face Model
</a>
<a href="https://nanonets.com/research/nanonets-ocr-s/" target="_blank" style="text-decoration: none; color: #2563eb; font-weight: 500;">
π Release Blog
</a>
<a href="https://github.com/NanoNets/docext" target="_blank" style="text-decoration: none; color: #2563eb; font-weight: 500;">
π» GitHub Repository
</a>
</div>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
file_input = gr.Image(
label="Upload Image Document",
height=200
)
max_tokens_slider = gr.Slider(
minimum=1024,
maximum=8192,
value=4096,
step=512,
label="Max Tokens per Page",
info="Maximum number of new tokens to generate for each page."
)
with_img_desc_checkbox = gr.Checkbox(
label="Include Image Description",
value=False,
info="If enabled, the model will include a description of the image in the output. If no image is present, use with_img_desc=False."
)
extract_btn = gr.Button("Convert to Markdown", variant="primary", size="lg")
with gr.Column(scale=2):
output_text = gr.Markdown(
label="Formatted Model Prediction",
latex_delimiters=[{"left": "$$", "right": "$$", "display": True}, {"left": "$", "right": "$", "display": False}],
line_breaks=True,
show_copy_button=True,
height=600,
)
extract_btn.click(
fn=process_document,
inputs=[file_input, max_tokens_slider, with_img_desc_checkbox],
concurrency_limit=4,
outputs=output_text
)
with gr.Accordion("About the Model (Nanonets-OCR-s)", open=False):
gr.Markdown("""
### Key Features
- **LaTeX Equation Recognition**: Converts mathematical equations into properly formatted LaTeX.
- **Intelligent Image Description**: Describes images within documents using structured `<img>` tags.
- **Signature & Watermark Detection**: Identifies and isolates signatures and watermarks within `<signature>` and `<watermark>` tags.
- **Smart Checkbox Handling**: Converts form checkboxes into standardized Unicode symbols (β, β).
- **Complex Table Extraction**: Accurately converts tables into HTML format.
""")
if __name__ == "__main__":
demo.queue().launch(debug=True) |