Spaces:
Sleeping
Sleeping
Update app.py
Browse filesrevert to previous version
app.py
CHANGED
@@ -40,100 +40,100 @@ 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 |
-
#choices = list(image_sources.keys()) # Get all available keys
|
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(f"Best match found: {best_match}")
|
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 |
-
# Return default values if no good match is found
|
64 |
-
return {
|
65 |
-
"url": "https://editor.p5js.org/kfahn/full/2XD5Y8MiV",
|
66 |
-
"width": 800,
|
67 |
-
"height": 800
|
68 |
-
}
|
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
|
|
|
|
|
|
|
|
|
79 |
# else:
|
80 |
-
#
|
81 |
-
# return
|
82 |
-
|
83 |
-
#
|
84 |
-
#
|
85 |
-
#
|
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 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
#
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
109 |
|
110 |
async def capture_screenshot(image_type: str):
|
111 |
-
"""Launches Playwright and 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 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
122 |
print(f"Opening image from p5 sketch: {image_url}")
|
123 |
await page.goto(image_url, timeout=120000) # Wait for the image page to load
|
124 |
|
125 |
-
print("Waiting for
|
|
|
126 |
await page.wait_for_timeout(5000) # Allow sketch to fully render
|
127 |
-
|
128 |
-
|
129 |
-
await page.set_viewport_size({"width": width, "height": height})
|
130 |
-
|
131 |
-
print("Capturing screenshot of canvas...")
|
132 |
await page.locator("iframe").screenshot(path="img.png")
|
133 |
-
|
134 |
await browser.close()
|
135 |
print("Screenshot saved!")
|
136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
|
139 |
@tool
|
|
|
40 |
|
41 |
|
42 |
def load_image_sources():
|
43 |
+
with open("image_sources.json", "r") as file:
|
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 |
+
# #choices = list(image_sources.keys()) # Get all available keys
|
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(f"Best match found: {best_match}")
|
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 |
+
# # Return default values if no good match is found
|
64 |
+
# return {
|
65 |
+
# "url": "https://editor.p5js.org/kfahn/full/2XD5Y8MiV",
|
66 |
+
# "width": 800,
|
67 |
+
# "height": 800
|
68 |
+
# }
|
|
|
|
|
|
|
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 |
+
# """Launches Playwright and 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 |
+
# image_data = get_image_url(image_type)
|
118 |
+
# image_url = image_data["url"]
|
119 |
+
# width = image_data["width"]
|
120 |
+
# height = image_data["height"]
|
121 |
+
|
122 |
+
# print(f"Opening image from p5 sketch: {image_url}")
|
123 |
+
# await page.goto(image_url, timeout=120000) # Wait for the image page to load
|
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 |
+
# print("Capturing screenshot of canvas...")
|
132 |
+
# await page.locator("iframe").screenshot(path="img.png")
|
133 |
+
|
134 |
+
# await browser.close()
|
135 |
+
# print("Screenshot saved!")
|
136 |
+
|
137 |
|
138 |
|
139 |
@tool
|