Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
from PIL import Image, ImageDraw
|
| 6 |
from transformers import AutoModel, AutoProcessor, AutoTokenizer, GenerationConfig
|
|
|
|
| 7 |
import spaces
|
|
|
|
| 8 |
from gradio.themes import Soft
|
| 9 |
from gradio.themes.utils import colors, fonts, sizes
|
| 10 |
|
| 11 |
-
# ---
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
f.write(response.content)
|
| 20 |
-
print("Download complete.")
|
| 21 |
|
| 22 |
try:
|
| 23 |
from postprocessing import extract_classes_bboxes, transform_bbox_to_original, postprocess_text
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
def transform_bbox_to_original(bbox, w, h): return bbox
|
| 29 |
-
def postprocess_text(text, **kwargs): return text
|
| 30 |
|
| 31 |
# --- Device Setup ---
|
| 32 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
@@ -100,28 +98,26 @@ css = """
|
|
| 100 |
"""
|
| 101 |
|
| 102 |
# --- Model Loading ---
|
| 103 |
-
print("Loading
|
| 104 |
-
model_path = "nvidia/NVIDIA-Nemotron-Parse-v1.1"
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
processor = AutoProcessor.from_pretrained(
|
| 108 |
model = AutoModel.from_pretrained(
|
| 109 |
-
|
| 110 |
trust_remote_code=True,
|
| 111 |
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 112 |
).to(device).eval()
|
| 113 |
|
| 114 |
-
# Generation Config
|
| 115 |
try:
|
| 116 |
-
generation_config = GenerationConfig.from_pretrained(
|
| 117 |
-
except:
|
| 118 |
-
|
| 119 |
generation_config = GenerationConfig(max_new_tokens=4096)
|
| 120 |
|
| 121 |
print("✅ Model loaded successfully.")
|
| 122 |
|
| 123 |
@spaces.GPU
|
| 124 |
-
def process_ocr_task(image
|
| 125 |
"""
|
| 126 |
Processes an image with NVIDIA-Nemotron-Parse-v1.1.
|
| 127 |
"""
|
|
@@ -129,7 +125,6 @@ def process_ocr_task(image, output_format="markdown"):
|
|
| 129 |
return "Please upload an image first.", None
|
| 130 |
|
| 131 |
# 1. Prepare Input
|
| 132 |
-
# The prompt triggers: Bounding Boxes -> Classes -> Markdown Output
|
| 133 |
task_prompt = "</s><s><predict_bbox><predict_classes><output_markdown>"
|
| 134 |
|
| 135 |
inputs = processor(images=[image], text=task_prompt, return_tensors="pt").to(device)
|
|
@@ -159,8 +154,8 @@ def process_ocr_task(image, output_format="markdown"):
|
|
| 159 |
bboxes = [transform_bbox_to_original(bbox, image.width, image.height) for bbox in bboxes]
|
| 160 |
|
| 161 |
# Configure text formatting
|
| 162 |
-
table_format = 'latex'
|
| 163 |
-
text_format = 'markdown'
|
| 164 |
blank_text_in_figures = False
|
| 165 |
|
| 166 |
# Process text segments
|
|
@@ -179,7 +174,6 @@ def process_ocr_task(image, output_format="markdown"):
|
|
| 179 |
result_image = image.copy()
|
| 180 |
draw = ImageDraw.Draw(result_image)
|
| 181 |
|
| 182 |
-
# Color map for different classes (optional)
|
| 183 |
color_map = {
|
| 184 |
"Table": "red",
|
| 185 |
"Figure": "blue",
|
|
@@ -195,14 +189,15 @@ def process_ocr_task(image, output_format="markdown"):
|
|
| 195 |
draw.rectangle([bbox[0], bbox[1], bbox[2], bbox[3]], outline=color, width=3)
|
| 196 |
|
| 197 |
# Append to text output
|
| 198 |
-
# We structure the text output to resemble the document flow
|
| 199 |
if cls == "Table":
|
| 200 |
final_output_text += f"\n\n--- [Table] ---\n{txt}\n-----------------\n"
|
|
|
|
|
|
|
| 201 |
else:
|
| 202 |
final_output_text += f"{txt}\n"
|
| 203 |
|
| 204 |
-
#
|
| 205 |
-
if not final_output_text.strip():
|
| 206 |
final_output_text = generated_text
|
| 207 |
|
| 208 |
return final_output_text, result_image
|
|
@@ -215,15 +210,11 @@ with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
|
|
| 215 |
with gr.Row():
|
| 216 |
with gr.Column(scale=1):
|
| 217 |
image_input = gr.Image(type="pil", label="Upload Image", sources=["upload", "clipboard"])
|
| 218 |
-
|
| 219 |
-
# Removing task selector as Nemotron Parse is an end-to-end parser
|
| 220 |
-
# output_format = gr.Radio(["markdown", "plain"], label="Text Output Format", value="markdown")
|
| 221 |
-
|
| 222 |
submit_btn = gr.Button("Process Document", variant="primary")
|
| 223 |
|
| 224 |
-
#
|
| 225 |
# examples = gr.Examples(
|
| 226 |
-
# examples=["examples/1.jpg"
|
| 227 |
# inputs=image_input,
|
| 228 |
# label="Examples"
|
| 229 |
# )
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import os
|
| 4 |
+
import sys
|
| 5 |
from PIL import Image, ImageDraw
|
| 6 |
from transformers import AutoModel, AutoProcessor, AutoTokenizer, GenerationConfig
|
| 7 |
+
from huggingface_hub import snapshot_download
|
| 8 |
import spaces
|
| 9 |
+
from typing import Optional, Tuple, Dict, Any, Iterable
|
| 10 |
from gradio.themes import Soft
|
| 11 |
from gradio.themes.utils import colors, fonts, sizes
|
| 12 |
|
| 13 |
+
# --- Model & Script Download ---
|
| 14 |
+
print("Downloading model snapshot to ensure all scripts are present...")
|
| 15 |
+
# Download the full model repo to ensure postprocessing.py is available locally
|
| 16 |
+
model_dir = snapshot_download(repo_id="nvidia/NVIDIA-Nemotron-Parse-v1.1")
|
| 17 |
+
print(f"Model downloaded to: {model_dir}")
|
| 18 |
+
|
| 19 |
+
# Add the model directory to sys.path so we can import postprocessing
|
| 20 |
+
sys.path.append(model_dir)
|
|
|
|
|
|
|
| 21 |
|
| 22 |
try:
|
| 23 |
from postprocessing import extract_classes_bboxes, transform_bbox_to_original, postprocess_text
|
| 24 |
+
print("✅ Successfully imported postprocessing functions.")
|
| 25 |
+
except ImportError as e:
|
| 26 |
+
print(f"❌ Error importing postprocessing: {e}")
|
| 27 |
+
raise e
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# --- Device Setup ---
|
| 30 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 98 |
"""
|
| 99 |
|
| 100 |
# --- Model Loading ---
|
| 101 |
+
print("Loading Model components...")
|
|
|
|
| 102 |
|
| 103 |
+
# Use the local directory path from snapshot_download
|
| 104 |
+
processor = AutoProcessor.from_pretrained(model_dir, trust_remote_code=True)
|
| 105 |
model = AutoModel.from_pretrained(
|
| 106 |
+
model_dir,
|
| 107 |
trust_remote_code=True,
|
| 108 |
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 109 |
).to(device).eval()
|
| 110 |
|
|
|
|
| 111 |
try:
|
| 112 |
+
generation_config = GenerationConfig.from_pretrained(model_dir, trust_remote_code=True)
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print(f"Warning: Could not load GenerationConfig: {e}. Using default.")
|
| 115 |
generation_config = GenerationConfig(max_new_tokens=4096)
|
| 116 |
|
| 117 |
print("✅ Model loaded successfully.")
|
| 118 |
|
| 119 |
@spaces.GPU
|
| 120 |
+
def process_ocr_task(image):
|
| 121 |
"""
|
| 122 |
Processes an image with NVIDIA-Nemotron-Parse-v1.1.
|
| 123 |
"""
|
|
|
|
| 125 |
return "Please upload an image first.", None
|
| 126 |
|
| 127 |
# 1. Prepare Input
|
|
|
|
| 128 |
task_prompt = "</s><s><predict_bbox><predict_classes><output_markdown>"
|
| 129 |
|
| 130 |
inputs = processor(images=[image], text=task_prompt, return_tensors="pt").to(device)
|
|
|
|
| 154 |
bboxes = [transform_bbox_to_original(bbox, image.width, image.height) for bbox in bboxes]
|
| 155 |
|
| 156 |
# Configure text formatting
|
| 157 |
+
table_format = 'latex'
|
| 158 |
+
text_format = 'markdown'
|
| 159 |
blank_text_in_figures = False
|
| 160 |
|
| 161 |
# Process text segments
|
|
|
|
| 174 |
result_image = image.copy()
|
| 175 |
draw = ImageDraw.Draw(result_image)
|
| 176 |
|
|
|
|
| 177 |
color_map = {
|
| 178 |
"Table": "red",
|
| 179 |
"Figure": "blue",
|
|
|
|
| 189 |
draw.rectangle([bbox[0], bbox[1], bbox[2], bbox[3]], outline=color, width=3)
|
| 190 |
|
| 191 |
# Append to text output
|
|
|
|
| 192 |
if cls == "Table":
|
| 193 |
final_output_text += f"\n\n--- [Table] ---\n{txt}\n-----------------\n"
|
| 194 |
+
elif cls == "Figure":
|
| 195 |
+
final_output_text += f"\n\n--- [Figure] ---\n(Figure Detected)\n-----------------\n"
|
| 196 |
else:
|
| 197 |
final_output_text += f"{txt}\n"
|
| 198 |
|
| 199 |
+
# Fallback if parsing returned nothing but text exists
|
| 200 |
+
if not final_output_text.strip() and generated_text:
|
| 201 |
final_output_text = generated_text
|
| 202 |
|
| 203 |
return final_output_text, result_image
|
|
|
|
| 210 |
with gr.Row():
|
| 211 |
with gr.Column(scale=1):
|
| 212 |
image_input = gr.Image(type="pil", label="Upload Image", sources=["upload", "clipboard"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
submit_btn = gr.Button("Process Document", variant="primary")
|
| 214 |
|
| 215 |
+
# Optional: Add examples if you have them in your repo
|
| 216 |
# examples = gr.Examples(
|
| 217 |
+
# examples=["examples/1.jpg"],
|
| 218 |
# inputs=image_input,
|
| 219 |
# label="Examples"
|
| 220 |
# )
|