Document Conversion with POINTS Reader ๐
Using tencent/POINTS-Reader Multimodal for Image Content Extraction
""")
with gr.Row():
# Left Column (Inputs)
with gr.Column(scale=1):
gr.Textbox(label="Model in Use โก", value="tencent/POINTS-Reader", interactive=False)
prompt_input = gr.Textbox(label="Query Input", placeholder="โฆ๏ธ Enter the prompt", value="Perform OCR on the image precisely.")
image_input = gr.Image(label="Upload Image", type="pil", sources=['upload'])
with gr.Accordion("Advanced Settings", open=False):
image_scale_factor = gr.Slider(minimum=1.0, maximum=3.0, value=1.0, step=0.1, label="Image Upscale Factor", info="Increases image size before processing. Can improve OCR on small text.")
max_new_tokens = gr.Slider(minimum=512, maximum=8192, value=2048, step=256, label="Max New Tokens")
temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=1.0, step=0.05, value=0.7)
top_p = gr.Slider(label="Top-p (nucleus sampling)", minimum=0.05, maximum=1.0, step=0.05, value=0.8)
top_k = gr.Slider(label="Top-k", minimum=1, maximum=100, step=1, value=20)
repetition_penalty = gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.05)
gr.Markdown("### PDF Export Settings")
font_size = gr.Dropdown(choices=["8", "10", "12", "14", "16", "18"], value="12", label="Font Size")
line_spacing = gr.Dropdown(choices=[1.0, 1.15, 1.5, 2.0], value=1.15, label="Line Spacing")
alignment = gr.Dropdown(choices=["Left", "Center", "Right", "Justified"], value="Justified", label="Text Alignment")
image_size = gr.Dropdown(choices=["Small", "Medium", "Large"], value="Medium", label="Image Size in PDF")
process_btn = gr.Button("๐ Process Image", variant="primary", elem_classes=["process-button"], size="lg")
clear_btn = gr.Button("๐๏ธ Clear All", variant="secondary")
# Right Column (Outputs)
with gr.Column(scale=2):
with gr.Tabs() as tabs:
with gr.Tab("๐ Extracted Content"):
raw_output_stream = gr.Textbox(label="Raw Model Output (max T โค 120s)", interactive=False, lines=15, show_copy_button=True)
with gr.Row():
examples = gr.Examples(examples=["examples/1.jpeg", "examples/2.jpeg", "examples/3.jpeg", "examples/4.jpeg", "examples/5.jpeg"], inputs=image_input, label="Examples")
gr.Markdown("[Report-Bug๐ป](https://huggingface.co/spaces/prithivMLmods/POINTS-Reader-OCR/discussions) | [prithivMLmods๐ค](https://huggingface.co/prithivMLmods)")
with gr.Tab("๐ฐ README.md"):
with gr.Accordion("(Result.md)", open=True):
markdown_output = gr.Markdown()
with gr.Tab("Bounding Boxes"):
gr.Markdown("Click the button to extract text and visualize its location on the image. This uses a specialized prompt to get coordinates from the model.")
with gr.Row():
with gr.Column(scale=1):
ocr_button = gr.Button("๐ Extract Text with Coordinates", variant="primary")
ocr_text = gr.Textbox(label="Extracted Text with Coordinates", info="Format: x1,y1,x2,y2,x3,y3,x4,y4,text", lines=15, show_copy_button=True)
with gr.Column(scale=1):
ocr_vis = gr.Image(label="Visualization (Red boxes show detected text)")
with gr.Tab("๐ PDF Preview"):
generate_pdf_btn = gr.Button("๐ Generate PDF & Render", variant="primary")
pdf_output_file = gr.File(label="Download Generated PDF", interactive=False)
pdf_preview_gallery = gr.Gallery(label="PDF Page Preview", show_label=True, elem_id="gallery", columns=2, object_fit="contain", height="auto")
# Event Handlers
def clear_all_outputs():
# Clear all input and output fields across all tabs
return None, "", "Raw output will appear here.", "", None, None, "", None
process_btn.click(
fn=process_document_stream,
inputs=[image_input, prompt_input, image_scale_factor, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
outputs=[raw_output_stream, markdown_output]
)
ocr_button.click(
fn=extract_text_with_coordinates,
inputs=[image_input],
outputs=[ocr_text, ocr_vis]
)
generate_pdf_btn.click(
fn=generate_and_preview_pdf,
inputs=[image_input, raw_output_stream, font_size, line_spacing, alignment, image_size],
outputs=[pdf_output_file, pdf_preview_gallery]
)
clear_btn.click(
clear_all_outputs,
outputs=[image_input, prompt_input, raw_output_stream, markdown_output, pdf_output_file, pdf_preview_gallery, ocr_text, ocr_vis]
)
return demo
if __name__ == "__main__":
demo = create_gradio_interface()
demo.queue(max_size=50).launch(share=True, show_error=True)