Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
#from __future__ import annotations
|
| 2 |
from selenium import webdriver
|
| 3 |
-
|
| 4 |
import gradio as gr
|
| 5 |
import uuid
|
| 6 |
import re
|
| 7 |
from PIL import Image
|
| 8 |
from io import BytesIO
|
|
|
|
| 9 |
from selenium.common.exceptions import WebDriverException
|
| 10 |
from selenium.webdriver.common.keys import Keys
|
| 11 |
from selenium.webdriver.common.by import By
|
|
@@ -13,54 +13,90 @@ from selenium.webdriver.common.by import By
|
|
| 13 |
|
| 14 |
driver_type = 'chromedriver'
|
| 15 |
driver=False
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
uid=uuid.uuid4()
|
|
|
|
| 18 |
is_url=True
|
| 19 |
if is_url:
|
| 20 |
options = webdriver.ChromeOptions()
|
| 21 |
options.add_argument('--headless')
|
| 22 |
options.add_argument('--no-sandbox')
|
| 23 |
options.add_argument('--disable-dev-shm-usage')
|
| 24 |
-
|
| 25 |
try:
|
| 26 |
driver = webdriver.Chrome(options=options)
|
| 27 |
driver.get(url)
|
| 28 |
-
|
|
|
|
| 29 |
driver.implicitly_wait(30)
|
| 30 |
driver.set_window_size(int(width), int(height))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
obj = driver.find_element(By.CLASS_NAME, "main")
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
screenshot = obj.screenshot(f'{uid}-tmp.png')
|
| 53 |
-
#screenshot = obj.get_screenshot_as_png()
|
| 54 |
except WebDriverException as e:
|
| 55 |
-
return
|
| 56 |
finally:
|
| 57 |
if driver:
|
| 58 |
driver.quit()
|
| 59 |
-
|
| 60 |
-
#return [Image.open(BytesIO(screenshot)), 'operation success.',cookie_jar,html]
|
| 61 |
-
return [Image.open(f'{uid}-tmp.png'), '<center>operation success.']
|
| 62 |
else:
|
| 63 |
-
return
|
| 64 |
|
| 65 |
with gr.Blocks() as app:
|
| 66 |
|
|
@@ -70,10 +106,17 @@ with gr.Blocks() as app:
|
|
| 70 |
btn= gr.Button()
|
| 71 |
|
| 72 |
with gr.Column():
|
|
|
|
|
|
|
| 73 |
with gr.Row():
|
| 74 |
height=gr.Number(label="Height", value=4096)
|
| 75 |
width=gr.Number(label="Width",value=800)
|
| 76 |
message=gr.HTML('<center>Enter URL')
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
app.launch()
|
|
|
|
| 1 |
#from __future__ import annotations
|
| 2 |
from selenium import webdriver
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import uuid
|
| 5 |
import re
|
| 6 |
from PIL import Image
|
| 7 |
from io import BytesIO
|
| 8 |
+
from pathlib import Path
|
| 9 |
from selenium.common.exceptions import WebDriverException
|
| 10 |
from selenium.webdriver.common.keys import Keys
|
| 11 |
from selenium.webdriver.common.by import By
|
|
|
|
| 13 |
|
| 14 |
driver_type = 'chromedriver'
|
| 15 |
driver=False
|
| 16 |
+
|
| 17 |
+
def get_concat_h_cut(in1, in2):
|
| 18 |
+
im1=Image.open(in1)
|
| 19 |
+
im2=Image.open(in2)
|
| 20 |
+
dst = Image.new('RGB', (im1.width + im2.width,
|
| 21 |
+
min(im1.height, im2.height)))
|
| 22 |
+
dst.paste(im1, (0, 0))
|
| 23 |
+
dst.paste(im2, (im1.width, 0))
|
| 24 |
+
return dst
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def get_concat_v_cut(in1, in2):
|
| 28 |
+
im1=Image.open(in1)
|
| 29 |
+
im2=Image.open(in2)
|
| 30 |
+
dst = Image.new(
|
| 31 |
+
'RGB', (min(im1.width, im2.width), im1.height + im2.height))
|
| 32 |
+
dst.paste(im1, (0, 0))
|
| 33 |
+
dst.paste(im2, (0, im1.height))
|
| 34 |
+
return dst
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def run_script(url: str, height: int, width: int, check_b,check_h):
|
| 39 |
+
mes_box=[]
|
| 40 |
+
out_box=[]
|
| 41 |
uid=uuid.uuid4()
|
| 42 |
+
out=None
|
| 43 |
is_url=True
|
| 44 |
if is_url:
|
| 45 |
options = webdriver.ChromeOptions()
|
| 46 |
options.add_argument('--headless')
|
| 47 |
options.add_argument('--no-sandbox')
|
| 48 |
options.add_argument('--disable-dev-shm-usage')
|
| 49 |
+
mes='<center>operation success'
|
| 50 |
try:
|
| 51 |
driver = webdriver.Chrome(options=options)
|
| 52 |
driver.get(url)
|
| 53 |
+
html=driver.page_source
|
| 54 |
+
#print (driver.html)
|
| 55 |
driver.implicitly_wait(30)
|
| 56 |
driver.set_window_size(int(width), int(height))
|
| 57 |
+
#page = driver.find_element(By.TAG_NAME, "html")
|
| 58 |
+
#driver.execute_script("arguments[0].style.background = 'blue';",page)
|
| 59 |
+
main_head = driver.find_element(By.CLASS_NAME, "main-content-header")
|
| 60 |
+
head_shot = main_head.screenshot(f'head-{uid}-tmp.png')
|
| 61 |
obj = driver.find_element(By.CLASS_NAME, "main")
|
| 62 |
+
messages = driver.find_elements(By.CLASS_NAME, "message")
|
| 63 |
+
for i,ea in enumerate(check_b):
|
| 64 |
+
try:
|
| 65 |
+
ea = int(ea)
|
| 66 |
+
ea = (ea*2)-2
|
| 67 |
+
print (ea)
|
| 68 |
+
messages[ea].screenshot(f'{i}-{uid}-tmp.png')
|
| 69 |
+
messages[ea+1].screenshot(f'{i+1}-{uid}-tmp.png')
|
| 70 |
+
im_roll = get_concat_v_cut(f'{i}-{uid}-tmp.png',f'{i+1}-{uid}-tmp.png')
|
| 71 |
+
im_roll.save(f'comb{i}-{uid}-tmp.png')
|
| 72 |
+
out_box.append(f'comb{i}-{uid}-tmp.png')
|
| 73 |
+
except Exception:
|
| 74 |
+
mes="<center>Some blocks returned an error"
|
| 75 |
+
if out_box:
|
| 76 |
+
if len(out_box)>1:
|
| 77 |
+
im_roll = get_concat_v_cut(f'{out_box[0]}',f'{out_box[1]}')
|
| 78 |
+
im_roll.save(f'comb-{uid}-tmp.png')
|
| 79 |
+
for i in range(2,len(out_box)):
|
| 80 |
+
im_roll = get_concat_v_cut(f'comb-{uid}-tmp.png',f'{out_box[i]}')
|
| 81 |
+
im_roll.save(f'comb-{uid}-tmp.png')
|
| 82 |
+
out = f'comb-{uid}-tmp.png'
|
| 83 |
+
else:
|
| 84 |
+
tmp_im = Image.open(out_box[0])
|
| 85 |
+
tmp_im.save(f'comb-{uid}-tmp.png')
|
| 86 |
+
out = f'comb-{uid}-tmp.png'
|
| 87 |
+
if check_h:
|
| 88 |
+
head_paste = get_concat_v_cut(f'head-{uid}-tmp.png',f'comb-{uid}-tmp.png')
|
| 89 |
+
head_paste.save(f'comb-{uid}-tmp.png')
|
| 90 |
+
out = f'comb-{uid}-tmp.png'
|
| 91 |
screenshot = obj.screenshot(f'{uid}-tmp.png')
|
|
|
|
| 92 |
except WebDriverException as e:
|
| 93 |
+
return Image.new('RGB', (1, 1)), f'<center>{e}',out_box,out
|
| 94 |
finally:
|
| 95 |
if driver:
|
| 96 |
driver.quit()
|
| 97 |
+
return Image.open(f'{uid}-tmp.png'), mes,out_box,out
|
|
|
|
|
|
|
| 98 |
else:
|
| 99 |
+
return None, '<center>Please enter a valid URL of a website/host.',out_box,out
|
| 100 |
|
| 101 |
with gr.Blocks() as app:
|
| 102 |
|
|
|
|
| 106 |
btn= gr.Button()
|
| 107 |
|
| 108 |
with gr.Column():
|
| 109 |
+
check_h=gr.Checkbox(label="Show Header", value=True)
|
| 110 |
+
check_b=gr.CheckboxGroup(label="Chatblocks", choices=[1,2,3,4,5,6,7,8,9,10], value=[1])
|
| 111 |
with gr.Row():
|
| 112 |
height=gr.Number(label="Height", value=4096)
|
| 113 |
width=gr.Number(label="Width",value=800)
|
| 114 |
message=gr.HTML('<center>Enter URL')
|
| 115 |
+
with gr.Row():
|
| 116 |
+
with gr.Column():
|
| 117 |
+
out=gr.Image()
|
| 118 |
+
outgal=gr.Gallery()
|
| 119 |
+
outim = gr.Image()
|
| 120 |
+
|
| 121 |
+
btn.click(run_script,[inp,height,width,check_b,check_h],[outim,message,outgal,out])
|
| 122 |
app.launch()
|