Spaces:
Running
Running
import gradio as gr | |
import torch | |
import base64 | |
import fitz # PyMuPDF | |
import tempfile | |
from io import BytesIO | |
from PIL import Image | |
from pathlib import Path | |
from transformers import AutoProcessor, Qwen2VLForConditionalGeneration | |
from olmocr.data.renderpdf import render_pdf_to_base64png | |
from olmocr.prompts.anchor import get_anchor_text | |
from ebooklib import epub | |
import json | |
import html | |
# Load model and processor | |
model = Qwen2VLForConditionalGeneration.from_pretrained( | |
"allenai/olmOCR-7B-0225-preview", torch_dtype=torch.bfloat16 | |
).eval() | |
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct") | |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
model.to(device) | |
def process_pdf_to_epub(pdf_file, title, author): | |
pdf_path = pdf_file.name | |
doc = fitz.open(pdf_path) | |
num_pages = len(doc) | |
book = epub.EpubBook() | |
book.set_identifier("id123456") | |
book.set_title(title) | |
book.add_author(author) | |
all_text = "" | |
for i in range(num_pages): | |
page_num = i + 1 | |
print(f"Processing page {page_num}...") | |
try: | |
image_base64 = render_pdf_to_base64png(pdf_path, page_num, target_longest_image_dim=1024) | |
anchor_text = get_anchor_text(pdf_path, page_num, pdf_engine="pdfreport", target_length=4000) | |
prompt = ( | |
"Below is the image of one page of a document, as well as some raw textual content that was previously " | |
"extracted for it. Just return the plain text representation of this document as if you were reading it naturally.\n" | |
"Do not hallucinate.\n" | |
"RAW_TEXT_START\n" | |
f"{anchor_text}\n" | |
"RAW_TEXT_END" | |
) | |
messages = [ | |
{ | |
"role": "user", | |
"content": [ | |
{"type": "text", "text": prompt}, | |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image_base64}"}}, | |
], | |
} | |
] | |
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) | |
image = Image.open(BytesIO(base64.b64decode(image_base64))) | |
inputs = processor( | |
text=[text], | |
images=[image], | |
padding=True, | |
return_tensors="pt", | |
) | |
inputs = {k: v.to(device) for k, v in inputs.items()} | |
output = model.generate( | |
**inputs, | |
temperature=0.8, | |
max_new_tokens=5096, | |
num_return_sequences=1, | |
do_sample=True, | |
) | |
prompt_length = inputs["input_ids"].shape[1] | |
new_tokens = output[:, prompt_length:].detach().cpu() | |
decoded = "[No output generated]" | |
if new_tokens is not None and new_tokens.shape[1] > 0: | |
try: | |
decoded_list = processor.tokenizer.batch_decode(new_tokens, skip_special_tokens=True) | |
raw_output = decoded_list[0].strip() if decoded_list else "[No output generated]" | |
try: | |
parsed = json.loads(raw_output) | |
# Only include `natural_text`, drop undesired metadata | |
decoded = parsed.get("natural_text", raw_output) | |
except json.JSONDecodeError: | |
decoded = raw_output | |
except Exception as decode_error: | |
decoded = f"[Decoding error on page {page_num}: {str(decode_error)}]" | |
else: | |
decoded = "[Model returned no new tokens]" | |
except Exception as processing_error: | |
decoded = f"[Processing error on page {page_num}: {str(processing_error)}]" | |
print(f"Decoded content for page {page_num}: {decoded}") | |
# Escape HTML and preserve spacing and math expressions (basic TeX formatting support) | |
escaped_text = html.escape(decoded) | |
# Restore math delimiters after escaping, and preserve line breaks | |
escaped_text = ( | |
escaped_text | |
.replace(r'\[', '<div class="math">\\[') | |
.replace(r'\]', '\\]</div>') | |
.replace(r'\(', '<span class="math">\\(') | |
.replace(r'\)', '\\)</span>') | |
.replace("\n", "<br>") | |
) | |
all_text += f"<div>{escaped_text}</div>" | |
if page_num == 1: | |
cover_image = Image.open(BytesIO(base64.b64decode(image_base64))) | |
cover_io = BytesIO() | |
cover_image.save(cover_io, format='PNG') | |
book.set_cover("cover.png", cover_io.getvalue()) | |
single_chapter = epub.EpubHtml(title="Full Document", file_name="full_document.xhtml", lang="en") | |
mathjax_script = """ | |
<script type="text/javascript" id="MathJax-script" async | |
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"> | |
</script> | |
""" | |
single_chapter.content = f"""<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>{html.escape(title)}</title> | |
{mathjax_script} | |
</head> | |
<body> | |
<h1>{html.escape(title)}</h1> | |
{all_text} | |
</body> | |
</html> | |
""" | |
book.add_item(single_chapter) | |
book.toc = (single_chapter,) | |
book.spine = ['nav', single_chapter] | |
book.add_item(epub.EpubNcx()) | |
book.add_item(epub.EpubNav()) | |
with tempfile.NamedTemporaryFile(delete=False, suffix=".epub", dir="/tmp") as tmp: | |
epub.write_epub(tmp.name, book) | |
return tmp.name | |
# Gradio Interface | |
iface = gr.Interface( | |
fn=process_pdf_to_epub, | |
inputs=[ | |
gr.File(label="Upload PDF", file_types=[".pdf"]), | |
gr.Textbox(label="EPUB Title"), | |
gr.Textbox(label="Author(s)") | |
], | |
outputs=gr.File(label="Download EPUB"), | |
title="PDF to EPUB Converter (with olmOCR)", | |
description="Uploads a PDF, extracts text from each page with vision + prompt, and builds an EPUB using the outputs. Sets the first page as cover.", | |
allow_flagging="never" | |
) | |
if __name__ == "__main__": | |
iface.launch( | |
server_name="0.0.0.0", | |
server_port=7860, | |
share=True, | |
debug=True, | |
allowed_paths=["/tmp"] | |
) | |