Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,47 @@
|
|
| 1 |
-
from
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#from __future__ import annotations
|
| 2 |
+
from selenium import webdriver
|
| 3 |
+
#from typing import Iterable
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
import re
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
from selenium.common.exceptions import WebDriverException
|
| 11 |
+
|
| 12 |
+
driver_type = 'chromedriver'
|
| 13 |
+
|
| 14 |
+
def run_script(text: str):
|
| 15 |
+
regex = r"^(https?://)"
|
| 16 |
+
is_url = re.search(regex, text)
|
| 17 |
+
|
| 18 |
+
if is_url:
|
| 19 |
+
options = webdriver.ChromeOptions()
|
| 20 |
+
options.add_argument('--headless')
|
| 21 |
+
options.add_argument('--no-sandbox')
|
| 22 |
+
options.add_argument('--disable-dev-shm-usage')
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
driver = webdriver.Chrome(options=options)
|
| 26 |
+
driver.get(text)
|
| 27 |
+
driver.implicitly_wait(30)
|
| 28 |
+
driver.set_window_size(1920, 1080)
|
| 29 |
+
screenshot = driver.get_screenshot_as_png()
|
| 30 |
+
except WebDriverException as e:
|
| 31 |
+
return [Image.new('RGB', (1, 1)), 'operation failed.']
|
| 32 |
+
finally:
|
| 33 |
+
if driver:
|
| 34 |
+
driver.quit()
|
| 35 |
+
|
| 36 |
+
return [Image.open(BytesIO(screenshot)), 'operation success.']
|
| 37 |
+
else:
|
| 38 |
+
return [None, 'Please enter a valid URL of a website/host.']
|
| 39 |
+
|
| 40 |
+
with gr.Blocks() as app:
|
| 41 |
+
inp = gr.Textbox()
|
| 42 |
+
btn= gr.Button()
|
| 43 |
+
with gr.Row():
|
| 44 |
+
outim = gr.Image()
|
| 45 |
+
outp = gr.Textbox()
|
| 46 |
+
btn.click(run_script,inp,[outim,outp])
|
| 47 |
+
app.launch()
|