Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcguire12 committed Oct 4, 2024
1 parent 4c5edf4 commit 0e56205
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
13 changes: 6 additions & 7 deletions tarsier/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from tarsier.ocr.types import ImageAnnotation
from tarsier.text_format import format_text


class TagMetadata(TypedDict):
tarsier_id: int
element_name: str
Expand Down Expand Up @@ -124,9 +125,11 @@ async def page_to_text_colour_tag(
adapter = adapter_factory(driver)
stored_dom = await self.store_dom(driver)

coloured_elems, tag_to_xpath, inserted_id_strings = await self._colour_based_tagify(
adapter, tag_text_elements, tagless
)
(
coloured_elems,
tag_to_xpath,
inserted_id_strings,
) = await self._colour_based_tagify(adapter, tag_text_elements, tagless)

coloured_image = await self._take_coloured_screenshot(adapter)

Expand Down Expand Up @@ -186,7 +189,6 @@ async def _take_screenshot(adapter: BrowserAdapter) -> bytes:

return screenshot


async def _take_coloured_screenshot(self, adapter: BrowserAdapter) -> bytes:
await self._disable_transitions(adapter)
coloured_image = await self._take_screenshot(adapter)
Expand Down Expand Up @@ -253,7 +255,6 @@ async def _colour_based_tagify(
await self._hide_non_coloured_elements(adapter)
return colour_mapping, tag_metadata_dict, inserted_id_strings


async def _detect_colours_in_image(self, image_bytes: bytes) -> list[str]:
detected_colours = await self.check_colors_brute_force(image_bytes)
return detected_colours
Expand Down Expand Up @@ -380,7 +381,6 @@ async def _create_annotations(

return combined_annotations


async def _store_dom(self, adapter: BrowserAdapter) -> str:
await self._load_tarsier_utils(adapter)
stored_dom = await adapter.run_js("return window.storeDOM();")
Expand All @@ -399,7 +399,6 @@ async def restore_dom(self, driver: AnyDriver, stored_dom: str) -> None:
adapter = adapter_factory(driver)
await self._restore_dom(adapter, stored_dom)


async def _remove_tags(self, adapter: BrowserAdapter) -> None:
await self._load_tarsier_utils(adapter)
script = "return window.removeTags();"
Expand Down
45 changes: 22 additions & 23 deletions tarsier/tag_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ window.storeDOM = () => {
return document.body.outerHTML;
};


window.restoreDOM = (storedDOM) => {
console.log("Restoring DOM");
if (storedDOM) {
Expand Down Expand Up @@ -1042,9 +1041,9 @@ function insertIdStringsIntoTextNodes(
return insertedIdStrings;
}

function collectElementsToColor(
tagMappingWithTagMeta: { [key: number]: TagMetadata },
): HTMLElement[] {
function collectElementsToColor(tagMappingWithTagMeta: {
[key: number]: TagMetadata;
}): HTMLElement[] {
const elements: HTMLElement[] = [];
const viewportWidth = window.innerWidth;
Object.values(tagMappingWithTagMeta).forEach((meta) => {
Expand Down Expand Up @@ -1095,7 +1094,7 @@ function createColorMappingAndApplyStyles(
const bodyRect = document.body.getBoundingClientRect();
const attribute = "data-colored";
const taggedElements = new Set(
Object.values(tagMappingWithTagMeta).map((meta) => meta.xpath)
Object.values(tagMappingWithTagMeta).map((meta) => meta.xpath),
);

elements.forEach((element) => {
Expand Down Expand Up @@ -1198,22 +1197,18 @@ function applyStylesToCheckbox(
// Add event listener for checkbox state change
checkboxElement.addEventListener("change", function () {
if (checkboxElement.checked) {
checkboxElement.style.setProperty(
"background-color",
color,
"important",
);
checkboxElement.style.setProperty("background-color", color, "important");
} else {
checkboxElement.style.setProperty(
"background-color",
color,
"important",
);
checkboxElement.style.setProperty("background-color", color, "important");
}
});
}

function applyStylesToImage(element: HTMLImageElement, color: string, attribute: string) {
function applyStylesToImage(
element: HTMLImageElement,
color: string,
attribute: string,
) {
const imageWidth = element.offsetWidth;
const imageHeight = element.offsetHeight;

Expand Down Expand Up @@ -1384,7 +1379,6 @@ window.createTextBoundingBoxes = () => {
});
};


window.documentDimensions = () => {
return {
width: document.documentElement.scrollWidth,
Expand All @@ -1393,7 +1387,7 @@ window.documentDimensions = () => {
};

window.getElementBoundingBoxes = (xpath: string) => {
const element = document.evaluate(
const element = document.evaluate(
xpath,
document,
null,
Expand Down Expand Up @@ -1438,9 +1432,11 @@ window.getElementBoundingBoxes = (xpath: string) => {
placeholderText = (element as HTMLImageElement).alt || " ";
}

const words = element.querySelectorAll(":scope > .tarsier-highlighted-word") as NodeListOf<HTMLElement>;
const words = element.querySelectorAll(
":scope > .tarsier-highlighted-word",
) as NodeListOf<HTMLElement>;
const boundingBoxes = Array.from(words)
.map((word ) => {
.map((word) => {
const rect = (word as HTMLElement).getBoundingClientRect();
return {
text: word.innerText || "",
Expand All @@ -1452,11 +1448,14 @@ window.getElementBoundingBoxes = (xpath: string) => {
})
.filter(
(box) =>
box.width > 0 && box.height > 0 && box.top >= 0 && box.left >= 0 && isValidText(box.text),
box.width > 0 &&
box.height > 0 &&
box.top >= 0 &&
box.left >= 0 &&
isValidText(box.text),
);

if (
words.length === 0) {
if (words.length === 0) {
const elementRect = element.getBoundingClientRect();
return [
{
Expand Down

0 comments on commit 0e56205

Please sign in to comment.