Finding a Candidate List for a Website #26
-
I'm working on the Applications track for COMP 545 regarding implementation of Weblinx, and so far I understand the algorithm of how it works and what to feed in, the problem I'm encountering is finding a list of candidates for a website. I can find the dom tree while simulating a website using Playwright, but not how to get the list of candidates in order to feed the information into the model. Is there a function or method that I maybe missed that gets that list of candidates? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 19 replies
-
The function weblinx/modeling/dmr/processing.py Lines 302 to 364 in c8986ae You might want to simplify it, since you don't need the negatives in real-time use, and you also don't have a target_uid during inference (only for training/eval), so you might want something like this: def format_relevant_elements_for_single_turn(
turn, format_intent_input, uid_key="data-webtasks-id"
) -> List[dict]:
bboxes_filt = wh.filter_bboxes(
turn.bboxes,
viewport_height=turn.viewport_height,
viewport_width=turn.viewport_width,
)
root = lxml.html.fromstring(turn.html)
root_tree = root.getroottree()
elements = root.xpath(f"//*[@{uid_key}]")
elements_filt = [p for p in elements if p.attrib[uid_key] in bboxes_filt]
records = []
for elem in elements_filt:
bbox = turn.bboxes[elem.attrib[uid_key]]
elem_dict = represent_element_as_dict(elem, bbox, root_tree)
elem_str = convert_elem_dict_to_str_legacy(elem_dict)
record = {
"doc": elem_str,
"uid": elem.attrib[uid_key],
"turn_index": turn.index,
"elem_dict": elem_dict,
}
records.append(record)
return records
# Usage:
from modeling.dmr.processing import build_formatters # weblinx/modeling/dmr/processing.py
from sentence_transformers.util import cos_sim
uid_key = "what-you-injected-into-elements-on-javascript-side"
format_intent_input, _ = build_formatters()
query = format_turn_for_input(replay, turn, format_intent=format_intent_input)
elements_records = format_relevant_elements_for_single_turn(turn=turn, format_intent_input=format_intent_input, uid_key=uid_key)
# now, use dmr to select candidate elements from the list of relevant formatted elements
# for example (untested, just to illustrate):
docs = [r['doc'] for r in elements_records]
encoded = model.encode(
[query] + docs, batch_size=batch_size, show_progress_bar=False
)
query_vector, doc_vectors = encoded[0], encoded[1:]
scores = cos_sim(query_vector, doc_vectors).cpu().squeeze().tolist()
for i in range(len(records)):
records[i]['score'] = scores[i] This code snippet shows you how to do it: Lines 74 to 88 in 53b6a23 Note that you need to construct the import weblinx as wl
class ReplayDynamic(wl.Replay):
def __init__(self, ..., html=None):
super().__init__()
if html is not None:
self._html_in_memory = html
@cached_property
def html(self):
return self._html_in_memory
# modify any other method/property, or add new methods/properties
class DemonstrationDynamic(wl.Demonstration):
# also make necessary modifications here
class Turn(wl.Turn):
# also make necessary modifications here |
Beta Was this translation helpful? Give feedback.
The function
build_records_for_single_turn
used inmodeling.dmr.processing
shows how to take areplay
andturn
object to format the query and list of candidate elements:weblinx/modeling/dmr/processing.py
Lines 302 to 364 in c8986ae