-
Notifications
You must be signed in to change notification settings - Fork 10
/
global_visualization.py
367 lines (325 loc) · 17.3 KB
/
global_visualization.py
1
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import os
import pathlib
import random
import pandas as pd
import streamlit as st
from datasets import load_dataset
from jinja2 import Template
from obelics.processors import (
DOMTreeSimplificator,
PreExtractionSimplificator,
TextMediaPairsExtractor,
)
from obelics.utils import make_selectolax_tree
class Visualization:
def __init__(self, num_docs, dom_viz_template_path):
self.num_docs = num_docs
@st.experimental_memo # it is caching but is incredibly slow when N is big.
def load_examples(num_docs):
try:
dataset = load_dataset(
"bs-modeling-metadata/c4-en-html-with-metadata",
streaming=True,
split="train",
use_auth_token=True,
)
except FileNotFoundError:
# This is how the DOM DOM Spaces should get access to the data.
dataset = load_dataset( # Use any dataset of html files containing columns "html" and "url"
"bs-modeling-metadata/c4-en-html-with-metadata",
streaming=True,
split="train",
use_auth_token=st.secrets["DOMDOM_READ_TOKEN"],
)
return list(dataset.take(num_docs))
self.examples = load_examples(num_docs)
def load_dom_viz_template(dom_viz_template_path):
with open(dom_viz_template_path, "r") as file:
template_string = file.read()
return Template(template_string)
self.dom_viz_template = load_dom_viz_template(dom_viz_template_path)
self.dom_tree_simplificator = DOMTreeSimplificator(
strip_multiple_linebreaks=True,
strip_multiple_spaces=True,
remove_html_comments=True,
replace_line_break_tags=True,
unwrap_tags=True,
strip_tags=True,
strip_special_divs=True,
remove_dates=True,
remove_empty_leaves=True,
unnest_nodes=True,
remake_tree=True,
)
self.pre_extraction_simplificator_not_merge_texts = PreExtractionSimplificator(
only_text_image_nodes=True,
format_texts=True,
merge_consecutive_text_nodes=False,
)
self.pre_extraction_simplificator_merge_texts = PreExtractionSimplificator(
only_text_image_nodes=True,
format_texts=True,
merge_consecutive_text_nodes=True,
)
self.extractor = TextMediaPairsExtractor(
dom_tree_simplificator=self.dom_tree_simplificator,
pre_extraction_simplificator=self.pre_extraction_simplificator_merge_texts,
also_extract_images_not_in_simplified_dom_tree=True,
extract_clip_scores=True,
)
def visualization(self):
st.title(
"Visualization of DOM tree simplification strategies, "
"web document rendering, and text-image pair extractions"
)
self.choose_mode()
self.choose_example()
self.simplification_mode()
self.extraction_mode()
def choose_mode(self):
st.header("Mode")
self.mode = st.selectbox(
label="Choose a mode",
options=["Simplification", "Extraction"],
index=1,
)
def choose_example(self):
st.header("Document")
if st.button("Select a random document"):
dct_idx = random.randint(a=0, b=self.num_docs - 1)
else:
dct_idx = 0
idx = st.number_input(
f"Select a document among the first {self.num_docs} ones",
min_value=0,
max_value=self.num_docs - 1,
value=dct_idx,
step=1,
help=f"Index between 0 and {self.num_docs-1}",
)
self.current_example = self.examples[idx]
def get_dom_viz_html(self, html):
def get_body_html_string(html):
tree = make_selectolax_tree(html)
tree.strip_tags(["script"])
return tree.body.html
body_html = get_body_html_string(html)
rendered_dom = self.dom_viz_template.render(body_html=body_html)
return rendered_dom
def simplification_mode(self):
if self.mode == "Simplification":
current_html = self.current_example["html"]
current_url = self.current_example["url"]
simplified_current_html = self.dom_tree_simplificator(current_html, type_return="str")
def display_rendered_webpages():
st.header("Rendered webpage")
st.markdown(f"Webpage url: {current_url}")
col1, col2 = st.columns(2)
with col1:
st.subheader("Raw html rendering")
st.components.v1.html(current_html, height=450, scrolling=True)
with col2:
st.subheader("Simplified html rendering")
st.components.v1.html(simplified_current_html, height=450, scrolling=True)
def display_dom_trees():
st.header("DOM trees")
col1, col2 = st.columns(2)
with col1:
st.subheader("Raw DOM tree")
rendered_dom = self.get_dom_viz_html(current_html)
st.components.v1.html(rendered_dom, height=600, scrolling=True)
with col2:
st.subheader("Simplified DOM tree")
simplified_rendered_dom = self.get_dom_viz_html(simplified_current_html)
st.components.v1.html(simplified_rendered_dom, height=600, scrolling=True)
def display_html_codes():
st.header("HTML codes")
col1, col2 = st.columns(2)
with col1:
st.subheader("Raw HTML code")
st.components.v1.html("<xmp>" + current_html + "</xmp>", height=450, scrolling=True)
with col2:
st.subheader("Simplified HTML code")
st.components.v1.html("<xmp>" + simplified_current_html + "</xmp>", height=450, scrolling=True)
display_rendered_webpages()
display_dom_trees()
display_html_codes()
def extraction_mode(self):
if self.mode == "Extraction":
current_html = self.current_example["html"]
current_url = self.current_example["url"]
simplified_current_html_tree = self.dom_tree_simplificator(current_html, type_return="selectolax_tree")
simplified_current_html = simplified_current_html_tree.html
current_list_nodes_not_merge_texts = self.pre_extraction_simplificator_not_merge_texts(
simplified_current_html_tree, page_url=current_url
)
current_list_nodes_merge_texts = self.pre_extraction_simplificator_merge_texts(
simplified_current_html_tree, page_url=current_url
)
extracted_images = self.extractor(html_str=current_html, page_url=current_url)
# For simplicity, only doing this replacement on the extracted images.
# Doing that before the extraction (i.e. in the DOM simplification) would be possible be would require
# more significant changes
replacement_dict = {
elem["unformatted_src"]: elem["src"]
for elem in extracted_images
if elem["src"] != elem["unformatted_src"]
}
def replace_relative_paths(html_string):
if replacement_dict:
for k, v in replacement_dict.items():
html_string = html_string.replace(k, v)
return html_string
def display_rendered_webpages():
st.header("Rendered webpage")
st.markdown(f"Webpage url: {current_url}")
display_raw_html_rendering = st.checkbox("Raw html rendering", value=True)
display_simplified_html_rendering = st.checkbox("Simplified html rendering", value=True)
col1, col2 = st.columns(2)
with col1:
display_pre_extraction_visualization = st.checkbox(
"Web document rendering (pre-extraction visualization)", value=True
)
with col2:
if display_pre_extraction_visualization:
merge_text_nodes = st.checkbox("Merge text nodes", value=True)
list_display_pages = [
[display_raw_html_rendering, "raw_html_rendering"],
[display_simplified_html_rendering, "simplified_html_rendering"],
[display_pre_extraction_visualization, "pre_extraction_visualization"],
]
list_display_pages = [
page_to_display
for should_display_page, page_to_display in list_display_pages
if should_display_page
]
def display_specific_rendered_webpage(page_to_display, col):
with col:
if page_to_display == "raw_html_rendering":
st.subheader("Raw html rendering")
st.components.v1.html(replace_relative_paths(current_html), height=800, scrolling=True)
elif page_to_display == "simplified_html_rendering":
st.subheader("Simplified html rendering")
st.components.v1.html(
replace_relative_paths(simplified_current_html), height=800, scrolling=True
)
elif page_to_display == "pre_extraction_visualization":
st.subheader("Web document rendering (pre-extraction visualization)")
def list_nodes_to_visu():
if not merge_text_nodes:
list_nodes = current_list_nodes_not_merge_texts
reduce_levels = {
v: i + 1
for i, v in enumerate(sorted(list(set([node.level for node in list_nodes]))))
}
last_level = None
markdown = ""
for node in list_nodes:
if node.tag in ["-text", "img"]:
current_level = reduce_levels[node.level]
if last_level != current_level:
markdown += (
"#" * min(current_level, 6) + f" Level: {current_level}\n\n"
)
last_level = current_level
path_in_tree_str = [tag for tag, _ in node.path_in_tree]
markdown += f"**{'/'.join(path_in_tree_str)}**\n\n"
if node.tag == "-text":
markdown += f"{node.text}\n\n"
elif node.tag == "img":
markdown += f"![img]({node.media_info['src']})\n\n"
st.markdown(markdown)
else:
list_nodes = current_list_nodes_merge_texts
for node in list_nodes:
if node.tag == "-text":
st.text(f"{node.text}\n\n")
elif node.tag == "img":
st.markdown(f"![img]({node.media_info['src']})\n\n")
list_nodes_to_visu()
if list_display_pages:
columns = st.columns(len(list_display_pages))
for page_to_display, col in zip(list_display_pages, columns):
display_specific_rendered_webpage(page_to_display, col)
def display_extraction():
st.header("Extracted content")
if not extracted_images:
st.write("No extracted content")
else:
df = pd.DataFrame(
extracted_images,
columns=[
"src",
"unformatted_src",
"format",
"rendered_width",
"rendered_height",
"original_width",
"original_height",
"formatted_filename",
"alt_text",
"extracted_text",
"clip_score_image_formatted_filename",
"clip_score_image_alt_text",
"clip_score_image_extracted_text",
"image_in_simplified_dom_tree",
],
)
for i, link in enumerate(df["src"]):
col1, col2 = st.columns(2)
with col1:
st.image(link, width=500, use_column_width=True)
with col2:
src = f'<a target="_blank" href="{link}">{link.split("/")[-1]}</a>'
unformatted_src = df["unformatted_src"][i]
format = df["format"][i]
rendered_width = df["rendered_width"][i]
rendered_height = df["rendered_height"][i]
original_width = df["original_width"][i]
original_height = df["original_height"][i]
formatted_filename = df["formatted_filename"][i]
alt_text = df["alt_text"][i]
extracted_text = df["extracted_text"][i]
clip_score_image_formatted_filename = df["clip_score_image_formatted_filename"][i]
clip_score_image_alt_text = df["clip_score_image_alt_text"][i]
clip_score_image_extracted_text = df["clip_score_image_extracted_text"][i]
image_in_simplified_dom_tree = df["image_in_simplified_dom_tree"][i]
st.components.v1.html(
(
f"<p><strong>Source</strong>: {src}</p><p><strong>Unformated source</strong>:"
f" {unformatted_src}</p><p><strong>Format</strong>:"
f" {format}</p><p><strong>Rendered width</strong>:"
f" {rendered_width}</p><p><strong>Rendered height</strong>:"
f" {rendered_height}</p><p><strong>Original width</strong>:"
f" {original_width}</p><p><strong>Original height</strong>:"
f" {original_height}</p><p><strong>Formatted filename</strong>:"
f" {formatted_filename}</p><p><strong>Alt-text</strong>:"
f" {alt_text}</p><p><strong>Extracted text</strong>:"
f" {extracted_text}</p><p><strong>Clip score image/formatted filename</strong>:"
f" {clip_score_image_formatted_filename:.4f}</p><p><strong>Clip score"
f" image/alt-text</strong>: {clip_score_image_alt_text:.4f}</p><p><strong>Clip"
" score image/extracted text</strong>:"
f" {clip_score_image_extracted_text:.4f}</p><p><strong>Image in simplified DOM"
f" tree</strong>: {image_in_simplified_dom_tree}</p>"
),
height=500,
scrolling=True,
)
st.write("-----")
def display_dom_tree():
st.header("Simplified DOM tree")
simplified_rendered_dom = self.get_dom_viz_html(simplified_current_html)
st.components.v1.html(simplified_rendered_dom, height=600, scrolling=True)
def display_html_code():
st.header("Simplified HTML code")
st.components.v1.html("<xmp>" + simplified_current_html + "</xmp>", height=450, scrolling=True)
display_rendered_webpages()
display_extraction()
display_dom_tree()
display_html_code()
if __name__ == "__main__":
st.set_page_config(layout="wide")
num_docs = 1_000
dom_viz_template_path = os.path.join(pathlib.Path(__file__).parent.resolve(), "assets/DOM_tree_viz.html")
visualization = Visualization(num_docs=num_docs, dom_viz_template_path=dom_viz_template_path)
visualization.visualization()