File size: 1,604 Bytes
170c253
9cc3c36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
pip install gradio selenium webdriver-manager
import gradio as gr
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import os
from time import sleep

def take_screenshot(url):
    # Set up Chrome options
    chrome_options = Options()
    chrome_options.add_argument("--headless")  # Run in headless mode
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-dev-shm-usage")

    # Set up Chrome WebDriver
    service = Service(ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service, options=chrome_options)

    # Open the URL and take screenshot
    try:
        driver.get(url)
        sleep(2)  # Wait for page to fully load
        screenshot_path = "screenshot.png"
        driver.save_screenshot(screenshot_path)
        driver.quit()
        return screenshot_path
    except Exception as e:
        driver.quit()
        return str(e)

# Define the Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("## Website Screenshot Tool")
    url_input = gr.Textbox(label="Enter Website URL", placeholder="e.g., https://example.com")
    screenshot_output = gr.Image(label="Screenshot")
    capture_button = gr.Button("Capture Screenshot")

    capture_button.click(take_screenshot, inputs=url_input, outputs=screenshot_output)

# Launch the interface
if __name__ == "__main__":
    demo.launch()