kfahn commited on
Commit
bd61bf6
·
verified ·
1 Parent(s): dab15df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -4,14 +4,17 @@ 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 !
@@ -53,22 +56,25 @@ async def capture_screenshot():
53
  print("Screenshot saved!")
54
 
55
  @tool
56
- def grab_image() -> img:
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
74
  def get_joke() -> str:
 
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
+ import gradio as gr
11
 
12
  from tools.final_answer import FinalAnswerTool
13
 
14
  from Gradio_UI import GradioUI
15
 
16
+ nest_asyncio.apply() # Ensure async works in a Jupyter/Colab/HF Spaces environment
17
+
18
  subprocess.run(["playwright", "install"])
19
 
20
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
56
  print("Screenshot saved!")
57
 
58
  @tool
59
+ def grab_image():
60
+ """
61
+ Fetches an op art image from a p5.js sketch.
62
+ This function sends uses Playwright to launch a headless server and grab a screenshot of a p5.js sketch.
63
+
64
  Returns:
65
+ PIL.Image: Captured image
66
  """
67
+ print("Running async Playwright screenshot...")
 
 
68
 
69
+ loop = asyncio.new_event_loop() # Create a new event loop (Fixes issues on HF Spaces)
70
+ asyncio.set_event_loop(loop)
71
+ loop.run_until_complete(capture_screenshot())
72
+
73
  print("Loading image for Gradio...")
74
  img = Image.open("img.png")
75
  return img
76
 
77
+
78
  #https://github.com/huggingface/smolagents/blob/main/examples/multiple_tools.py
79
  @tool
80
  def get_joke() -> str: