kfahn commited on
Commit
cd2ac71
·
verified ·
1 Parent(s): 1a7268d

Update app.py

Browse files

try generalizing again

Files changed (1) hide show
  1. app.py +70 -20
app.py CHANGED
@@ -40,50 +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("image_sources.json", "r") as file:
44
  return json.load(file)
45
 
46
  image_sources = load_image_sources()
47
 
 
48
  def get_image_url(image_type: str):
49
  """Finds the best match for the given image type using fuzzy matching."""
50
  choices = list(image_sources.keys()) # Get all available keys
51
  best_match, score, *rest = process.extractOne(image_type, choices)
52
-
53
  if score > 90: # Set a threshold to ensure a reasonable match
54
- print(best_match)
55
- return image_sources[best_match]
 
 
 
 
56
  else:
57
- #return None # No good match found
58
- return "https://editor.p5js.org/kfahn/full/2XD5Y8MiV"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  async def capture_screenshot(image_type: str):
61
- """Launches Playwright and uses user input, if any, to captures a screenshot of an image from p5.js."""
62
  print("Launching Playwright...")
63
  async with async_playwright() as p:
64
  browser = await p.chromium.launch(headless=True)
65
  page = await browser.new_page()
66
 
67
- #url = "https://openprocessing.org/sketch/2539973"
68
- url = "https://editor.p5js.org/kfahn/full/2XD5Y8MiV"
69
-
70
- if image_type:
71
- image_url = get_image_url(image_type)
72
- else:
73
- image_url = url
74
-
75
  print(f"Opening image from p5 sketch: {image_url}")
76
  await page.goto(image_url, timeout=120000) # Wait for the image page to load
77
 
78
- print("Waiting for image element...")
79
- # await page.wait_for_selector("img", timeout=120000) # Wait for the <img> to be visible
80
  await page.wait_for_timeout(5000) # Allow sketch to fully render
81
- print("Capturing screenshot...")
82
- await page.set_viewport_size({"width": 800, "height": 800})
 
 
 
83
  await page.locator("iframe").screenshot(path="img.png")
 
84
  await browser.close()
85
  print("Screenshot saved!")
86
-
 
87
 
88
  @tool
89
  def grab_image(image_type: str) -> Image:
 
40
 
41
 
42
  def load_image_sources():
43
+ with open("expanded.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
  best_match, score, *rest = process.extractOne(image_type, choices)
53
+
54
  if score > 90: # Set a threshold to ensure a reasonable match
55
+ print(f"Best match found: {best_match}")
56
+ return {
57
+ "url": image_sources[best_match]["url"],
58
+ "width": image_sources[best_match]["width"],
59
+ "height": image_sources[best_match]["height"]
60
+ }
61
  else:
62
+ # Return default values if no good match is found
63
+ return {
64
+ "url": "https://editor.p5js.org/kfahn/full/2XD5Y8MiV",
65
+ "width": 800,
66
+ "height": 800
67
+ }
68
+
69
+
70
+ # def get_image_url(image_type: str):
71
+ # """Finds the best match for the given image type using fuzzy matching."""
72
+ # choices = list(image_sources.keys()) # Get all available keys
73
+ # best_match, score, *rest = process.extractOne(image_type, choices)
74
+
75
+ # if score > 90: # Set a threshold to ensure a reasonable match
76
+ # print(best_match)
77
+ # return image_sources[best_match]
78
+ # else:
79
+ # #return None # No good match found
80
+ # return "https://editor.p5js.org/kfahn/full/2XD5Y8MiV"
81
+
82
+ # async def capture_screenshot(image_type: str):
83
+ # """Launches Playwright and uses user input, if any, to captures a screenshot of an image from p5.js."""
84
+ # print("Launching Playwright...")
85
+ # async with async_playwright() as p:
86
+ # browser = await p.chromium.launch(headless=True)
87
+ # page = await browser.new_page()
88
+
89
+ # #url = "https://openprocessing.org/sketch/2539973"
90
+ # url = "https://editor.p5js.org/kfahn/full/2XD5Y8MiV"
91
+
92
+ # if image_type:
93
+ # image_url = get_image_url(image_type)
94
+ # else:
95
+ # image_url = url
96
+
97
+ # print(f"Opening image from p5 sketch: {image_url}")
98
+ # await page.goto(image_url, timeout=120000) # Wait for the image page to load
99
+
100
+ # print("Waiting for image element...")
101
+ # # await page.wait_for_selector("img", timeout=120000) # Wait for the <img> to be visible
102
+ # await page.wait_for_timeout(5000) # Allow sketch to fully render
103
+ # print("Capturing screenshot...")
104
+ # await page.set_viewport_size({"width": 800, "height": 800})
105
+ # await page.locator("iframe").screenshot(path="img.png")
106
+ # await browser.close()
107
+ # print("Screenshot saved!")
108
 
109
  async def capture_screenshot(image_type: str):
110
+ """Launches Playwright and captures a screenshot of an image from p5.js."""
111
  print("Launching Playwright...")
112
  async with async_playwright() as p:
113
  browser = await p.chromium.launch(headless=True)
114
  page = await browser.new_page()
115
 
116
+ image_data = get_image_url(image_type)
117
+ image_url = image_data["url"]
118
+ width = image_data["width"]
119
+ height = image_data["height"]
120
+
 
 
 
121
  print(f"Opening image from p5 sketch: {image_url}")
122
  await page.goto(image_url, timeout=120000) # Wait for the image page to load
123
 
124
+ print("Waiting for sketch to render...")
 
125
  await page.wait_for_timeout(5000) # Allow sketch to fully render
126
+
127
+ print(f"Setting viewport to {width}x{height}...")
128
+ await page.set_viewport_size({"width": width, "height": height})
129
+
130
+ print("Capturing screenshot of canvas...")
131
  await page.locator("iframe").screenshot(path="img.png")
132
+
133
  await browser.close()
134
  print("Screenshot saved!")
135
+
136
+
137
 
138
  @tool
139
  def grab_image(image_type: str) -> Image: