Spaces:
Sleeping
Sleeping
Update app.py
Browse filestry to add tool to grab a screen shot of a p5.js sketch
2 unmodified lines345678910111213 unmodified lines262728293031323334353637383940414243444546474849505152535455565758596061626364656662 unmodified lines1291301311321331341352 unmodified linesimport requestsimport pytzimport yamlfrom tools.final_answer import FinalAnswerToolfrom Gradio_UI import GradioUI# Below is an example of a tool that does nothing. Amaze us with your creativity !@tooldef my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type13 unmodified lines# description="Generate an image from a prompt"# )#https://zenquotes.io/api/quotes/[YOUR_API_KEY]&keyword=${category}# @tool# def zenquotes(category: str) -> str:# """# Get a quote using the Zenquotes API.# Args:# category: The category for the quote.# Returns:# A string with the quote.# """# api_key = "your_api_key" # Replace with your API key from https://weatherstack.com/# //url = f"http://api.weatherstack.com/current?access_key={api_key}&query={location}&units={units}"# url = f"https://zenquotes.io/api/quotes/[YOUR_API_KEY]&keyword=${category}"# try:# response = requests.get(url)# response.raise_for_status() # Raise an exception for HTTP errors# data = response.json()# if data.get("error"): # Check if there's an error in the response# return f"Error: {data['error'].get('info', 'Unable to fetch a quote.')}"# quote = data["category"][0]# return f"Here is your quote about {category}: ."# except requests.exceptions.RequestException as e:# return f"Error fetching quote: {str(e)}"#https://github.com/huggingface/smolagents/blob/main/examples/multiple_tools.py@tool62 unmodified linesagent = CodeAgent(model=model,tools=[final_answer, get_joke,], ## add your tools here (don't remove final answer)max_steps=6,verbosity_level=1,grammar=None,2 unmodified lines34567891011121314151617181913 unmodified lines333435363738394041424344454647484950515253545556575859606162636465666768697071727362 unmodified lines1361371381391401411422 unmodified linesimport requestsimport pytzimport yamlimport asyncio#import nest_asynciofrom playwright.async_api import async_playwrightfrom PIL import Imagefrom tools.final_answer import FinalAnswerToolfrom Gradio_UI import GradioUIsubprocess.run(["playwright", "install"])# Below is an example of a tool that does nothing. Amaze us with your creativity !@tooldef my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type13 unmodified lines# description="Generate an image from a prompt"# )# Debugging: Print statements to track executionasync def capture_screenshot():print("Launching Playwright...")async with async_playwright() as p:browser = await p.chromium.launch(headless=True)page = await browser.new_page()print("Opening the p5.js sketch...")await page.goto("https://editor.p5js.org/kfahn/full/2XD5Y8MiV", timeout=6000)print("Waiting for canvas element...")await page.wait_for_selector("canvas") # Ensure p5.js has loadedawait page.wait_for_timeout(5000) # Additional waitprint("Capturing screenshot...")await page.screenshot(path="img.png")await browser.close()print("Screenshot saved!")@tooldef grab_image() -> image:"""Fetches an op art image from a p5 sketch.This function sends uses Playwright to launch a headles server and grab a screen shot of a p5.sketch.Returns:image: img"""loop = asyncio.get_event_loop()future = asyncio.run_coroutine_threadsafe(capture_screenshot(), loop)future.result() # Wait for coroutine to finishprint("Loading image for Gradio...")img = Image.open("img.png")return img#https://github.com/huggingface/smolagents/blob/main/examples/multiple_tools.py@tool62 unmodified linesagent = CodeAgent(model=model,tools=[final_answer, get_joke, grab_image], ## add your tools here (don't remove final answer)max_steps=6,verbosity_level=1,grammar=None,
app.py+39-32