IotaCluster commited on
Commit
fcfab3e
·
verified ·
1 Parent(s): a92ebbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -3,35 +3,41 @@ from selenium import webdriver
3
  from selenium.common.exceptions import WebDriverException
4
  from PIL import Image
5
  from io import BytesIO
6
- import time # <-- import time
7
 
8
  def take_screenshot(url):
9
  options = webdriver.ChromeOptions()
10
- options.add_argument('--headless')
11
- options.add_argument('--no-sandbox')
12
- options.add_argument('--disable-dev-shm-usage')
13
 
 
14
  try:
15
  wd = webdriver.Chrome(options=options)
16
- wd.set_window_size(1080, 720) # Adjust the window size here
17
  wd.get(url)
18
- wd.implicitly_wait(10)
19
- time.sleep(2) # <-- Add delay before screenshot
20
  screenshot = wd.get_screenshot_as_png()
 
 
 
 
 
 
 
21
  except WebDriverException as e:
22
- return Image.new('RGB', (1, 1))
 
23
  finally:
24
  if wd:
25
  wd.quit()
26
 
27
- return Image.open(BytesIO(screenshot))
28
-
29
  iface = gr.Interface(
30
  fn=take_screenshot,
31
- inputs=gr.Textbox(label="Website URL", value="https://github.com/aiclubiitropar"),
32
- outputs=gr.Image(type="pil", label="Screenshot", height=360, width=540),
33
  title="Website Screenshot",
34
- description="Take a screenshot of a website."
35
  )
36
 
37
  iface.launch()
 
3
  from selenium.common.exceptions import WebDriverException
4
  from PIL import Image
5
  from io import BytesIO
6
+ import time
7
 
8
  def take_screenshot(url):
9
  options = webdriver.ChromeOptions()
10
+ options.add_argument("--headless")
11
+ options.add_argument("--no-sandbox")
12
+ options.add_argument("--disable-dev-shm-usage")
13
 
14
+ wd = None
15
  try:
16
  wd = webdriver.Chrome(options=options)
17
+ wd.set_window_size(1280, 720)
18
  wd.get(url)
19
+ time.sleep(2)
 
20
  screenshot = wd.get_screenshot_as_png()
21
+
22
+ if len(screenshot) < 1000:
23
+ print("⚠ Screenshot too small, likely error.")
24
+ return None
25
+
26
+ return Image.open(BytesIO(screenshot))
27
+
28
  except WebDriverException as e:
29
+ print("WebDriverException:", e)
30
+ return None
31
  finally:
32
  if wd:
33
  wd.quit()
34
 
 
 
35
  iface = gr.Interface(
36
  fn=take_screenshot,
37
+ inputs=gr.Textbox(label="Website URL", value="https://github.com"),
38
+ outputs=gr.Image(type="pil", label="Screenshot"),
39
  title="Website Screenshot",
40
+ description="Takes a screenshot of the website and displays it here."
41
  )
42
 
43
  iface.launch()