Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,220 @@
|
|
| 1 |
import gradio as gr
|
| 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 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
| 61 |
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration
|
| 4 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
+
import json
|
| 6 |
+
import re
|
| 7 |
+
from spaces import GPU
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# --- 1. Configurations and Constants ---
|
| 11 |
+
|
| 12 |
+
# Hugging Face model repository
|
| 13 |
+
MODEL_ID = "ChaseHan/Latex2Layout-2000-sync"
|
| 14 |
+
|
| 15 |
+
# BUG FIX: Use fixed-size scaling
|
| 16 |
+
TARGET_SIZE = (924, 1204)
|
| 17 |
+
|
| 18 |
+
# Visualization style constants
|
| 19 |
+
OUTLINE_WIDTH = 3
|
| 20 |
+
# RGBA colors for layout regions (with transparency)
|
| 21 |
+
LABEL_COLORS = {
|
| 22 |
+
"title": (255, 82, 82, 90), # Red
|
| 23 |
+
"abstract": (46, 204, 113, 90), # Green
|
| 24 |
+
"heading": (52, 152, 219, 90), # Blue
|
| 25 |
+
"footnote": (241, 196, 15, 90), # Yellow
|
| 26 |
+
"figure": (155, 89, 182, 90), # Purple
|
| 27 |
+
"figure caption": (26, 188, 156, 90), # Teal
|
| 28 |
+
"table": (230, 126, 34, 90), # Orange
|
| 29 |
+
"table caption": (44, 62, 80, 90), # Dark Blue/Gray
|
| 30 |
+
"math": (231, 76, 60, 90), # Pomegranate
|
| 31 |
+
"text": (149, 165, 166, 90), # Gray
|
| 32 |
+
"other": (127, 140, 141, 90) # Light Gray
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
# Default prompt for layout detection
|
| 36 |
+
PROMPT_GROUNDING = (
|
| 37 |
+
"""<image>Please carefully observe the document and detect the following regions: "title", "abstract", "heading", "footnote", "figure", "figure caption", "table", "table caption", "math", "text". Output each detected region's bbox coordinates in JSON format. The format of the output is: <answer>
|
| 38 |
+
json[{"bbox_2d": [x1, y1, x2, y2], "label": "region name", "order": "reading order"}]
|
| 39 |
+
</answer>."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
|
| 43 |
+
# --- 2. Load Model and Processor ---
|
| 44 |
+
|
| 45 |
+
print("Loading model and processor, this may take a moment...")
|
| 46 |
+
try:
|
| 47 |
+
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 48 |
+
MODEL_ID,
|
| 49 |
+
torch_dtype=torch.float16,
|
| 50 |
+
device_map="auto"
|
| 51 |
+
)
|
| 52 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 53 |
+
print("Model loaded successfully!")
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Error loading model: {e}")
|
| 56 |
+
exit()
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# --- 3. Core Inference and Visualization Function ---
|
| 60 |
+
|
| 61 |
+
@GPU
|
| 62 |
+
def analyze_and_visualize_layout(input_image: Image.Image, prompt: str, temperature: float, top_p: float, progress=gr.Progress(track_tqdm=True)):
|
| 63 |
+
"""
|
| 64 |
+
Resizes input image, runs model inference, visualizes layout, and returns results.
|
| 65 |
+
"""
|
| 66 |
+
if input_image is None:
|
| 67 |
+
return None, "Please upload an image first."
|
| 68 |
+
|
| 69 |
+
progress(0, desc="Resizing image...")
|
| 70 |
+
# BUG FIX: Use fixed-size scaling
|
| 71 |
+
image = input_image.resize(TARGET_SIZE)
|
| 72 |
+
image = image.convert("RGBA") # For transparent drawing
|
| 73 |
+
|
| 74 |
+
messages = [
|
| 75 |
+
{"role": "user", "content": [
|
| 76 |
+
{"type": "image", "image": image},
|
| 77 |
+
{"type": "text", "text": prompt}
|
| 78 |
+
]}
|
| 79 |
+
]
|
| 80 |
+
|
| 81 |
+
progress(0.2, desc="Preparing model inputs...")
|
| 82 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 83 |
+
inputs = processor(text=[text], images=[image], padding=True, return_tensors="pt").to(model.device)
|
| 84 |
+
|
| 85 |
+
progress(0.5, desc="Generating layout data...")
|
| 86 |
+
with torch.no_grad():
|
| 87 |
+
generate_kwargs = {
|
| 88 |
+
"max_new_tokens": 4096,
|
| 89 |
+
"do_sample": temperature > 0, # Enable sampling if temperature > 0
|
| 90 |
+
"temperature": temperature if temperature > 0 else None,
|
| 91 |
+
"top_p": top_p if temperature > 0 else None,
|
| 92 |
+
}
|
| 93 |
+
output_ids = model.generate(**inputs, **generate_kwargs)
|
| 94 |
+
|
| 95 |
+
output_text = processor.batch_decode(
|
| 96 |
+
output_ids[:, inputs.input_ids.shape[1]:], skip_special_tokens=True
|
| 97 |
+
)[0]
|
| 98 |
+
|
| 99 |
+
progress(0.8, desc="Parsing and visualizing results...")
|
| 100 |
+
try:
|
| 101 |
+
json_match = re.search(r"
|
| 102 |
+
json(.*?)
|
| 103 |
+
", output_text, re.DOTALL)
|
| 104 |
+
json_str = json_match.group(1).strip() if json_match else output_text.strip()
|
| 105 |
+
results = json.loads(json_str)
|
| 106 |
+
except (json.JSONDecodeError, AttributeError):
|
| 107 |
+
return image.convert("RGB"), f"Failed to parse JSON from model output:\n\n{output_text}"
|
| 108 |
+
|
| 109 |
+
overlay = Image.new('RGBA', image.size, (255, 255, 255, 0))
|
| 110 |
+
draw = ImageDraw.Draw(overlay)
|
| 111 |
+
|
| 112 |
+
try:
|
| 113 |
+
font = ImageFont.truetype("Arial.ttf", 15)
|
| 114 |
+
except IOError:
|
| 115 |
+
font = ImageFont.load_default()
|
| 116 |
+
|
| 117 |
+
for item in sorted(results, key=lambda x: x.get("order", 999)):
|
| 118 |
+
bbox = item.get("bbox_2d")
|
| 119 |
+
label = item.get("label", "other")
|
| 120 |
+
order = item.get("order", "")
|
| 121 |
+
|
| 122 |
+
if not bbox or len(bbox) != 4:
|
| 123 |
+
continue
|
| 124 |
+
|
| 125 |
+
fill_color_rgba = LABEL_COLORS.get(label, LABEL_COLORS["other"])
|
| 126 |
+
solid_color_rgb = fill_color_rgba[:3]
|
| 127 |
+
|
| 128 |
+
draw.rectangle(bbox, fill=fill_color_rgba, outline=solid_color_rgb, width=OUTLINE_WIDTH)
|
| 129 |
+
|
| 130 |
+
tag_text = f"{order}: {label}"
|
| 131 |
+
tag_bbox = draw.textbbox((0, 0), tag_text, font=font)
|
| 132 |
+
tag_w, tag_h = tag_bbox[2] - tag_bbox[0], tag_bbox[3] - tag_bbox[1]
|
| 133 |
+
|
| 134 |
+
tag_bg_box = [bbox[0], bbox[1], bbox[0] + tag_w + 10, bbox[1] + tag_h + 6]
|
| 135 |
+
draw.rectangle(tag_bg_box, fill=solid_color_rgb)
|
| 136 |
+
draw.text((bbox[0] + 5, bbox[1] + 3), tag_text, font=font, fill="white")
|
| 137 |
+
|
| 138 |
+
visualized_image = Image.alpha_composite(image, overlay).convert("RGB")
|
| 139 |
+
|
| 140 |
+
return visualized_image, output_text
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def clear_outputs():
|
| 144 |
+
"""Clears output fields."""
|
| 145 |
+
return None, None
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
# --- 4. Gradio User Interface ---
|
| 149 |
+
|
| 150 |
+
with gr.Blocks(theme=gr.themes.Glass(), title="Academic Paper Layout Detection") as demo:
|
| 151 |
+
gr.Markdown("# 📄 Academic Paper Layout Detection")
|
| 152 |
+
gr.Markdown(
|
| 153 |
+
"Welcome! This tool uses a Qwen2.5-VL-3B-Instruct model fine-tuned on our Latex2Layout annotated layout dataset to identify layout regions in academic papers. "
|
| 154 |
+
"Upload a document image to begin."
|
| 155 |
+
# BUG FIX: Updated description to reflect fixed-size scaling
|
| 156 |
+
"\n> **Please note:** All uploaded images are automatically resized to 924x1204 pixels to meet the model's input requirements."
|
| 157 |
+
)
|
| 158 |
+
gr.Markdown("<hr>")
|
| 159 |
+
|
| 160 |
+
with gr.Row():
|
| 161 |
+
with gr.Column(scale=4):
|
| 162 |
+
input_image = gr.Image(type="pil", label="Upload Document Image", height=700)
|
| 163 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 164 |
+
prompt_input = gr.Textbox(
|
| 165 |
+
value=PROMPT_GROUNDING,
|
| 166 |
+
label="Custom Prompt",
|
| 167 |
+
lines=5,
|
| 168 |
+
info="Edit the prompt sent to the model. Changes may affect output format."
|
| 169 |
+
)
|
| 170 |
+
temperature_input = gr.Slider(
|
| 171 |
+
minimum=0.0,
|
| 172 |
+
maximum=2.0,
|
| 173 |
+
value=1.0,
|
| 174 |
+
step=0.1,
|
| 175 |
+
label="Temperature",
|
| 176 |
+
info="Controls randomness (higher = more creative, 0 = deterministic)."
|
| 177 |
+
)
|
| 178 |
+
top_p_input = gr.Slider(
|
| 179 |
+
minimum=0.0,
|
| 180 |
+
maximum=1.0,
|
| 181 |
+
value=0.95,
|
| 182 |
+
step=0.05,
|
| 183 |
+
label="Top-P",
|
| 184 |
+
info="Nucleus sampling: considers the top p% probability mass."
|
| 185 |
+
)
|
| 186 |
+
with gr.Column(scale=5):
|
| 187 |
+
output_image = gr.Image(type="pil", label="Analyzed Layout", interactive=False, height=700)
|
| 188 |
+
|
| 189 |
+
with gr.Row():
|
| 190 |
+
analyze_btn = gr.Button("✨ Analyze Layout", variant="primary", scale=1)
|
| 191 |
+
|
| 192 |
+
output_text = gr.Textbox(label="Model Raw Output", lines=8, interactive=False, visible=True)
|
| 193 |
+
|
| 194 |
+
# fix
|
| 195 |
+
gr.Examples(
|
| 196 |
+
examples=[["page_2.png"], ["page_3.png"], ["page_5.png"], ["page_13.png"]],
|
| 197 |
+
inputs=[input_image, prompt_input, temperature_input, top_p_input],
|
| 198 |
+
outputs=[output_image, output_text],
|
| 199 |
+
fn=analyze_and_visualize_layout,
|
| 200 |
+
label="Examples (Click to Run)",
|
| 201 |
+
cache_examples=True
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
gr.Markdown("<p style='text-align:center; color:grey;'>Powered by the Latex2Layout dataset generated by Feijiang Han</p>")
|
| 205 |
+
|
| 206 |
+
# --- Event Handlers ---
|
| 207 |
+
analyze_btn.click(
|
| 208 |
+
fn=analyze_and_visualize_layout,
|
| 209 |
+
inputs=[input_image, prompt_input, temperature_input, top_p_input],
|
| 210 |
+
outputs=[output_image, output_text]
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
input_image.upload(fn=clear_outputs, inputs=None, outputs=[output_image, output_text])
|
| 214 |
+
input_image.clear(fn=clear_outputs, inputs=None, outputs=[output_image, output_text])
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
# --- 5. Launch the Application ---
|
| 218 |
+
|
| 219 |
if __name__ == "__main__":
|
| 220 |
+
demo.launch()
|