|
import gradio as gr |
|
from selenium import webdriver |
|
from selenium.common.exceptions import WebDriverException |
|
from PIL import Image |
|
from io import BytesIO |
|
import os |
|
import uuid |
|
|
|
SAVE_DIR = "/tmp/gradio" |
|
|
|
|
|
os.makedirs(SAVE_DIR, exist_ok=True) |
|
|
|
def take_screenshot(url): |
|
options = webdriver.ChromeOptions() |
|
options.add_argument('--headless') |
|
options.add_argument('--no-sandbox') |
|
options.add_argument('--disable-dev-shm-usage') |
|
|
|
try: |
|
wd = webdriver.Chrome(options=options) |
|
wd.set_window_size(1080, 720) |
|
wd.get(url) |
|
wd.implicitly_wait(20) |
|
screenshot = wd.get_screenshot_as_png() |
|
except WebDriverException: |
|
return "Error: Unable to take screenshot." |
|
finally: |
|
if wd: |
|
wd.quit() |
|
|
|
|
|
filename = f"{SAVE_DIR}/screenshot_{uuid.uuid4().hex}.png" |
|
image = Image.open(BytesIO(screenshot)) |
|
image.save(filename) |
|
|
|
|
|
return filename |
|
|
|
iface = gr.Interface( |
|
fn=take_screenshot, |
|
inputs=gr.Textbox(label="Website URL", value="https://kargaranamir.github.io"), |
|
outputs=gr.File(label="Download Screenshot"), |
|
title="Website Screenshot", |
|
description="Take a screenshot of a website and download it." |
|
) |
|
|
|
iface.launch(share=True) |
|
|