Spaces:
Sleeping
Sleeping
Update app.py
Browse filestry to add tool to grab a screen shot of a p5.js sketch
app.py
CHANGED
@@ -3,10 +3,17 @@ import datetime
|
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
|
|
|
|
|
|
|
|
|
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
|
|
|
|
10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
11 |
@tool
|
12 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
@@ -26,41 +33,41 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
26 |
# description="Generate an image from a prompt"
|
27 |
# )
|
28 |
|
29 |
-
#https://zenquotes.io/api/quotes/[YOUR_API_KEY]&keyword=${category}
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
#
|
38 |
-
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
# data = response.json()
|
54 |
-
|
55 |
-
# if data.get("error"): # Check if there's an error in the response
|
56 |
-
# return f"Error: {data['error'].get('info', 'Unable to fetch a quote.')}"
|
57 |
-
|
58 |
-
# quote = data["category"][0]
|
59 |
-
|
60 |
-
# return f"Here is your quote about {category}: ."
|
61 |
-
|
62 |
-
# except requests.exceptions.RequestException as e:
|
63 |
-
# return f"Error fetching quote: {str(e)}"
|
64 |
|
65 |
#https://github.com/huggingface/smolagents/blob/main/examples/multiple_tools.py
|
66 |
@tool
|
@@ -129,7 +136,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
129 |
|
130 |
agent = CodeAgent(
|
131 |
model=model,
|
132 |
-
tools=[final_answer, get_joke,], ## add your tools here (don't remove final answer)
|
133 |
max_steps=6,
|
134 |
verbosity_level=1,
|
135 |
grammar=None,
|
|
|
3 |
import requests
|
4 |
import pytz
|
5 |
import yaml
|
6 |
+
import asyncio
|
7 |
+
#import nest_asyncio
|
8 |
+
from playwright.async_api import async_playwright
|
9 |
+
from PIL import Image
|
10 |
+
|
11 |
from tools.final_answer import FinalAnswerTool
|
12 |
|
13 |
from Gradio_UI import GradioUI
|
14 |
|
15 |
+
subprocess.run(["playwright", "install"])
|
16 |
+
|
17 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
18 |
@tool
|
19 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
|
|
33 |
# description="Generate an image from a prompt"
|
34 |
# )
|
35 |
|
|
|
36 |
|
37 |
+
# Debugging: Print statements to track execution
|
38 |
+
async def capture_screenshot():
|
39 |
+
print("Launching Playwright...")
|
40 |
+
async with async_playwright() as p:
|
41 |
+
browser = await p.chromium.launch(headless=True)
|
42 |
+
page = await browser.new_page()
|
43 |
+
print("Opening the p5.js sketch...")
|
44 |
+
await page.goto("https://editor.p5js.org/kfahn/full/2XD5Y8MiV", timeout=6000)
|
45 |
|
46 |
+
print("Waiting for canvas element...")
|
47 |
+
await page.wait_for_selector("canvas") # Ensure p5.js has loaded
|
48 |
+
await page.wait_for_timeout(5000) # Additional wait
|
49 |
|
50 |
+
print("Capturing screenshot...")
|
51 |
+
await page.screenshot(path="img.png")
|
52 |
+
await browser.close()
|
53 |
+
print("Screenshot saved!")
|
54 |
+
|
55 |
+
@tool
|
56 |
+
def grab_image() -> image:
|
57 |
+
"""
|
58 |
+
Fetches an op art image from a p5 sketch.
|
59 |
+
This function sends uses Playwright to launch a headles server and grab a screen shot of a p5.sketch.
|
60 |
+
|
61 |
+
Returns:
|
62 |
+
image: img
|
63 |
+
"""
|
64 |
+
loop = asyncio.get_event_loop()
|
65 |
+
future = asyncio.run_coroutine_threadsafe(capture_screenshot(), loop)
|
66 |
+
future.result() # Wait for coroutine to finish
|
67 |
|
68 |
+
print("Loading image for Gradio...")
|
69 |
+
img = Image.open("img.png")
|
70 |
+
return img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
#https://github.com/huggingface/smolagents/blob/main/examples/multiple_tools.py
|
73 |
@tool
|
|
|
136 |
|
137 |
agent = CodeAgent(
|
138 |
model=model,
|
139 |
+
tools=[final_answer, get_joke, grab_image], ## add your tools here (don't remove final answer)
|
140 |
max_steps=6,
|
141 |
verbosity_level=1,
|
142 |
grammar=None,
|