Kajise commited on
Commit
d496e67
·
1 Parent(s): 52c2ff7

fix: use PIL for image rendering

Browse files
Files changed (1) hide show
  1. app.py +23 -8
app.py CHANGED
@@ -2,12 +2,15 @@ from __future__ import annotations
2
  from selenium import webdriver
3
  from typing import Iterable
4
 
5
- import re
6
- import os
7
  import gradio as Gradio
8
  from gradio.themes.base import Base
9
  from gradio.themes.utils import colors, fonts, sizes
10
 
 
 
 
 
 
11
  theme = Gradio.themes.Monochrome(
12
  primary_hue="purple",
13
  secondary_hue="purple",
@@ -73,19 +76,31 @@ def run_imaginor(text: str):
73
  is_url = re.search(regex, text)
74
 
75
  if is_url:
76
- driver = webdriver.Chrome()
77
- driver.get(text)
78
- screenshot = driver.save_screenshot('./site_screenshot.png')
79
- driver.quit()
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
- return ['./site_screenshot.png', 'Website imagined by Imaginor, operation success.']
82
  else:
83
  return [None, 'Please enter a valid URL of a website/host.']
84
 
85
  Gradio.Interface(
86
  run_imaginor,
87
  Gradio.Textbox(placeholder="Enter your URL here", label="Website URL / Endpoint"),
88
- [Gradio.Image(type="filepath", label="Screenshot"),
89
  Gradio.Textbox(label="Text result", interactive=False)],
90
  title=title,
91
  description=description,
 
2
  from selenium import webdriver
3
  from typing import Iterable
4
 
 
 
5
  import gradio as Gradio
6
  from gradio.themes.base import Base
7
  from gradio.themes.utils import colors, fonts, sizes
8
 
9
+ import re
10
+ from PIL import Image
11
+ from io import BytesIO
12
+ from selenium.common.exceptions import WebDriverException
13
+
14
  theme = Gradio.themes.Monochrome(
15
  primary_hue="purple",
16
  secondary_hue="purple",
 
76
  is_url = re.search(regex, text)
77
 
78
  if is_url:
79
+ options = webdriver.ChromeOptions()
80
+ options.add_argument('--headless')
81
+ options.add_argument('--no-sandbox')
82
+ options.add_argument('--disable-dev-shm-usage')
83
+
84
+ try:
85
+ driver = webdriver.Chrome(options=options)
86
+ driver.get(text)
87
+ driver.implicitly_wait(10)
88
+ driver.set_window_size(1920, 1080)
89
+ screenshot = driver.get_screenshot_as_png()
90
+ except WebDriverException as e:
91
+ return [Image.new('RGB', (1, 1)), 'Website imagined by Imaginor, operation failed.']
92
+ finally:
93
+ if driver:
94
+ driver.quit()
95
 
96
+ return [Image.open(BytesIO(screenshot)), 'Website imagined by Imaginor, operation success.']
97
  else:
98
  return [None, 'Please enter a valid URL of a website/host.']
99
 
100
  Gradio.Interface(
101
  run_imaginor,
102
  Gradio.Textbox(placeholder="Enter your URL here", label="Website URL / Endpoint"),
103
+ [Gradio.Image(type="pil", label="Screenshot"),
104
  Gradio.Textbox(label="Text result", interactive=False)],
105
  title=title,
106
  description=description,