File size: 1,494 Bytes
c3b7bd1 10ec180 0c6cbf1 10ec180 0c6cbf1 10ec180 c3b7bd1 10ec180 c3b7bd1 7bb3caf c3b7bd1 10ec180 c3b7bd1 0c6cbf1 10ec180 0c6cbf1 c3b7bd1 20e60cb 0c6cbf1 c3b7bd1 10ec180 c3b7bd1 0c6cbf1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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" # Ensure files are stored where Gradio can serve them
# Ensure the folder exists
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()
# Save the screenshot in /tmp/gradio with a unique filename
filename = f"{SAVE_DIR}/screenshot_{uuid.uuid4().hex}.png"
image = Image.open(BytesIO(screenshot))
image.save(filename)
# Return the file path (Gradio will expose it as a public link)
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"), # Ensures public access
title="Website Screenshot",
description="Take a screenshot of a website and download it."
)
iface.launch(share=True) # Enables external access
|