Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -64,85 +64,35 @@
|
|
64 |
# demo.launch()
|
65 |
|
66 |
import gradio as gr
|
67 |
-
from transformers import AutoProcessor,
|
68 |
-
from
|
69 |
-
from threading import Thread
|
70 |
-
import torch
|
71 |
-
import html
|
72 |
-
import re
|
73 |
-
from PIL import Image, ImageOps
|
74 |
|
75 |
# Load model & processor once at startup
|
76 |
processor = AutoProcessor.from_pretrained("ds4sd/SmolDocling-256M-preview")
|
77 |
-
model =
|
78 |
-
|
79 |
-
def
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
def extract_table(image_file):
|
91 |
-
# Load image
|
92 |
-
image = load_image(image_file)
|
93 |
-
|
94 |
-
# Optionally add padding if needed for model robustness (optional)
|
95 |
-
image = add_random_padding(image)
|
96 |
-
|
97 |
-
# Fixed prompt to extract table only (modify if needed)
|
98 |
-
text = "Convert this table to OTSL."
|
99 |
-
|
100 |
-
# Build the message structure for processor
|
101 |
-
resulting_messages = [{
|
102 |
-
"role": "user",
|
103 |
-
"content": [{"type": "image"}] + [{"type": "text", "text": text}]
|
104 |
-
}]
|
105 |
-
|
106 |
-
prompt = processor.apply_chat_template(resulting_messages, add_generation_prompt=True)
|
107 |
-
inputs = processor(text=prompt, images=[image], return_tensors="pt").to('cuda')
|
108 |
-
|
109 |
-
generation_args = {
|
110 |
-
"input_ids": inputs.input_ids,
|
111 |
-
"pixel_values": inputs.pixel_values,
|
112 |
-
"attention_mask": inputs.attention_mask,
|
113 |
-
"max_new_tokens": 8192,
|
114 |
-
"num_return_sequences": 1,
|
115 |
-
}
|
116 |
-
|
117 |
-
streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
|
118 |
-
generation_args["streamer"] = streamer
|
119 |
-
|
120 |
-
thread = Thread(target=model.generate, kwargs=generation_args)
|
121 |
-
thread.start()
|
122 |
-
|
123 |
-
output_text = ""
|
124 |
-
for new_text in streamer:
|
125 |
-
output_text += new_text
|
126 |
-
|
127 |
-
# Clean and return output
|
128 |
-
cleaned_output = output_text.replace("<end_of_utterance>", "").strip()
|
129 |
-
|
130 |
-
# Optionally convert <chart> tags to <otsl> if present
|
131 |
-
if "<chart>" in cleaned_output:
|
132 |
-
cleaned_output = cleaned_output.replace("<chart>", "<otsl>").replace("</chart>", "</otsl>")
|
133 |
-
cleaned_output = re.sub(r'(<loc_500>)(?!.*<loc_500>)<[^>]+>', r'\1', cleaned_output)
|
134 |
-
|
135 |
-
return cleaned_output or "No table found or unable to extract."
|
136 |
|
137 |
# Gradio UI
|
138 |
demo = gr.Interface(
|
139 |
-
fn=
|
140 |
-
inputs=
|
141 |
-
|
142 |
-
|
143 |
-
|
|
|
|
|
|
|
144 |
)
|
145 |
|
146 |
-
demo.launch(
|
147 |
-
|
148 |
-
|
|
|
64 |
# demo.launch()
|
65 |
|
66 |
import gradio as gr
|
67 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
68 |
+
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
# Load model & processor once at startup
|
71 |
processor = AutoProcessor.from_pretrained("ds4sd/SmolDocling-256M-preview")
|
72 |
+
model = AutoModelForImageTextToText.from_pretrained("ds4sd/SmolDocling-256M-preview")
|
73 |
+
|
74 |
+
def smoldocling_readimage(image, prompt_text):
|
75 |
+
messages = [
|
76 |
+
{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": prompt_text}]}
|
77 |
+
]
|
78 |
+
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
79 |
+
inputs = processor(text=prompt, images=[image], return_tensors="pt")
|
80 |
+
outputs = model.generate(**inputs, max_new_tokens=1024)
|
81 |
+
prompt_length = inputs.input_ids.shape[1]
|
82 |
+
generated = outputs[:, prompt_length:]
|
83 |
+
result = processor.batch_decode(generated, skip_special_tokens=False)[0]
|
84 |
+
return result.replace("<end_of_utterance>", "").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
# Gradio UI
|
87 |
demo = gr.Interface(
|
88 |
+
fn=smoldocling_readimage,
|
89 |
+
inputs=[
|
90 |
+
gr.Image(type="pil", label="Upload Image"),
|
91 |
+
gr.Textbox(lines=1, placeholder="Enter prompt (e.g. Convert to docling)", label="Prompt"),
|
92 |
+
],
|
93 |
+
outputs="ostl",
|
94 |
+
title="SmolDocling Web App",
|
95 |
+
description="Upload a document image and convert it to structured docling format."
|
96 |
)
|
97 |
|
98 |
+
demo.launch()
|
|
|
|