Spaces:
Sleeping
Sleeping
Update app.py
Browse filestry addition viewport width & height data again
app.py
CHANGED
@@ -40,99 +40,102 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
40 |
|
41 |
|
42 |
def load_image_sources():
|
43 |
-
with open("
|
|
|
44 |
return json.load(file)
|
45 |
|
46 |
image_sources = load_image_sources()
|
47 |
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
# def get_image_url(image_type: str):
|
50 |
# """Finds the best match for the given image type using fuzzy matching."""
|
51 |
-
#
|
52 |
-
# choices = list(expanded.keys()) # Get all available keys
|
53 |
# best_match, score, *rest = process.extractOne(image_type, choices)
|
54 |
-
|
55 |
# if score > 90: # Set a threshold to ensure a reasonable match
|
56 |
-
# print(
|
57 |
-
# return
|
58 |
-
# "url": image_sources[best_match]["url"],
|
59 |
-
# "width": image_sources[best_match]["width"],
|
60 |
-
# "height": image_sources[best_match]["height"]
|
61 |
-
# }
|
62 |
# else:
|
63 |
-
#
|
64 |
-
# return
|
65 |
-
# "url": "https://editor.p5js.org/kfahn/full/2XD5Y8MiV",
|
66 |
-
# "width": 800,
|
67 |
-
# "height": 800
|
68 |
-
# }
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
async def capture_screenshot(image_type: str):
|
84 |
-
"""
|
85 |
print("Launching Playwright...")
|
86 |
async with async_playwright() as p:
|
87 |
browser = await p.chromium.launch(headless=True)
|
88 |
page = await browser.new_page()
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
-
|
93 |
-
if image_type:
|
94 |
-
image_url = get_image_url(image_type)
|
95 |
-
else:
|
96 |
-
image_url = url
|
97 |
-
|
98 |
-
print(f"Opening image from p5 sketch: {image_url}")
|
99 |
-
await page.goto(image_url, timeout=120000) # Wait for the image page to load
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
print("Capturing screenshot...")
|
105 |
-
await page.set_viewport_size({"width": 800, "height": 800})
|
106 |
-
await page.locator("iframe").screenshot(path="img.png")
|
107 |
-
await browser.close()
|
108 |
-
print("Screenshot saved!")
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
# print("Launching Playwright...")
|
113 |
-
# async with async_playwright() as p:
|
114 |
-
# browser = await p.chromium.launch(headless=True)
|
115 |
-
# page = await browser.new_page()
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
# width = image_data["width"]
|
120 |
-
# height = image_data["height"]
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
# print("Waiting for sketch to render...")
|
126 |
-
# await page.wait_for_timeout(5000) # Allow sketch to fully render
|
127 |
-
|
128 |
-
# print(f"Setting viewport to {width}x{height}...")
|
129 |
-
# await page.set_viewport_size({"width": width, "height": height})
|
130 |
|
131 |
-
|
132 |
-
|
133 |
|
134 |
-
|
135 |
-
|
136 |
|
137 |
|
138 |
|
|
|
40 |
|
41 |
|
42 |
def load_image_sources():
|
43 |
+
with open("extended.json", "r") as file:
|
44 |
+
# with open("image_sources.json", "r") as file:
|
45 |
return json.load(file)
|
46 |
|
47 |
image_sources = load_image_sources()
|
48 |
|
49 |
|
50 |
+
def get_image_url(image_type: str, image_sources: dict):
|
51 |
+
"""Finds the best match for the given image type in a nested JSON structure."""
|
52 |
+
|
53 |
+
choices = list(image_sources.keys()) # Get all available keys
|
54 |
+
|
55 |
+
# Find the best match using `rapidfuzz`
|
56 |
+
best_match, score, *_ = process.extractOne(image_type, choices, score_cutoff=60)
|
57 |
+
|
58 |
+
if best_match:
|
59 |
+
# Access the image_sources dictionary directly using the best_match string as the key
|
60 |
+
print(f"Best match found: {best_match}") # Removed category access as it's not needed
|
61 |
+
return image_sources[best_match] # Returns {'url', 'width', 'height', etc.}
|
62 |
+
|
63 |
+
else:
|
64 |
+
# Default return if no good match is found
|
65 |
+
return {
|
66 |
+
"url": "https://editor.p5js.org/kfahn/full/2XD5Y8MiV",
|
67 |
+
"width": 800,
|
68 |
+
"height": 800
|
69 |
+
}
|
70 |
+
|
71 |
# def get_image_url(image_type: str):
|
72 |
# """Finds the best match for the given image type using fuzzy matching."""
|
73 |
+
# choices = list(image_sources.keys()) # Get all available keys
|
|
|
74 |
# best_match, score, *rest = process.extractOne(image_type, choices)
|
75 |
+
|
76 |
# if score > 90: # Set a threshold to ensure a reasonable match
|
77 |
+
# print(best_match)
|
78 |
+
# return image_sources[best_match]
|
|
|
|
|
|
|
|
|
79 |
# else:
|
80 |
+
# #return None # No good match found
|
81 |
+
# return "https://editor.p5js.org/kfahn/full/2XD5Y8MiV"
|
|
|
|
|
|
|
|
|
82 |
|
83 |
+
# async def capture_screenshot(image_type: str):
|
84 |
+
# """Launches Playwright and uses user input, if any, to captures a screenshot of an image from p5.js."""
|
85 |
+
# print("Launching Playwright...")
|
86 |
+
# async with async_playwright() as p:
|
87 |
+
# browser = await p.chromium.launch(headless=True)
|
88 |
+
# page = await browser.new_page()
|
89 |
|
90 |
+
# #url = "https://openprocessing.org/sketch/2539973"
|
91 |
+
# url = "https://editor.p5js.org/kfahn/full/2XD5Y8MiV"
|
92 |
+
|
93 |
+
# if image_type:
|
94 |
+
# image_url = get_image_url(image_type)
|
95 |
+
# else:
|
96 |
+
# image_url = url
|
97 |
+
|
98 |
+
# print(f"Opening image from p5 sketch: {image_url}")
|
99 |
+
# await page.goto(image_url, timeout=120000) # Wait for the image page to load
|
100 |
+
|
101 |
+
# print("Waiting for image element...")
|
102 |
+
# # await page.wait_for_selector("img", timeout=120000) # Wait for the <img> to be visible
|
103 |
+
# await page.wait_for_timeout(5000) # Allow sketch to fully render
|
104 |
+
# print("Capturing screenshot...")
|
105 |
+
# await page.set_viewport_size({"width": 800, "height": 800})
|
106 |
+
# await page.locator("iframe").screenshot(path="img.png")
|
107 |
+
# await browser.close()
|
108 |
+
# print("Screenshot saved!")
|
109 |
|
110 |
async def capture_screenshot(image_type: str):
|
111 |
+
"""Captures a screenshot of an image from p5.js."""
|
112 |
print("Launching Playwright...")
|
113 |
async with async_playwright() as p:
|
114 |
browser = await p.chromium.launch(headless=True)
|
115 |
page = await browser.new_page()
|
116 |
|
117 |
+
# Load image sources from JSON
|
118 |
+
image_sources = load_image_sources()
|
119 |
+
image_data = get_image_url(image_type, image_sources)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
+
image_url = image_data["url"]
|
122 |
+
width = image_data["width"]
|
123 |
+
height = image_data["height"]
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
+
print(f"Opening image: {image_url}")
|
126 |
+
await page.goto(image_url, timeout=120000) # Load page
|
|
|
|
|
|
|
|
|
127 |
|
128 |
+
print("Waiting for render...")
|
129 |
+
await page.wait_for_timeout(5000)
|
|
|
|
|
130 |
|
131 |
+
print(f"Setting viewport to {width}x{height}...")
|
132 |
+
await page.set_viewport_size({"width": width, "height": height})
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
+
print("Capturing screenshot...")
|
135 |
+
await page.locator("iframe").screenshot(path="img.png")
|
136 |
|
137 |
+
await browser.close()
|
138 |
+
print("Screenshot saved!")
|
139 |
|
140 |
|
141 |
|