Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from selenium import webdriver
|
| 3 |
+
from selenium.webdriver.chrome.service import Service
|
| 4 |
+
from selenium.webdriver.common.by import By
|
| 5 |
+
from selenium.webdriver.chrome.options import Options
|
| 6 |
+
from webdriver_manager.chrome import ChromeDriverManager
|
| 7 |
+
import os
|
| 8 |
+
from time import sleep
|
| 9 |
+
|
| 10 |
+
def take_screenshot(url):
|
| 11 |
+
# Set up Chrome options
|
| 12 |
+
chrome_options = Options()
|
| 13 |
+
chrome_options.add_argument("--headless") # Run in headless mode
|
| 14 |
+
chrome_options.add_argument("--disable-gpu")
|
| 15 |
+
chrome_options.add_argument("--no-sandbox")
|
| 16 |
+
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 17 |
+
|
| 18 |
+
# Set up Chrome WebDriver
|
| 19 |
+
service = Service(ChromeDriverManager().install())
|
| 20 |
+
driver = webdriver.Chrome(service=service, options=chrome_options)
|
| 21 |
+
|
| 22 |
+
# Open the URL and take screenshot
|
| 23 |
+
try:
|
| 24 |
+
driver.get(url)
|
| 25 |
+
sleep(2) # Wait for page to fully load
|
| 26 |
+
screenshot_path = "screenshot.png"
|
| 27 |
+
driver.save_screenshot(screenshot_path)
|
| 28 |
+
driver.quit()
|
| 29 |
+
return screenshot_path
|
| 30 |
+
except Exception as e:
|
| 31 |
+
driver.quit()
|
| 32 |
+
return str(e)
|
| 33 |
+
|
| 34 |
+
# Define the Gradio interface
|
| 35 |
+
with gr.Blocks() as demo:
|
| 36 |
+
gr.Markdown("## Website Screenshot Tool")
|
| 37 |
+
url_input = gr.Textbox(label="Enter Website URL", placeholder="e.g., https://example.com")
|
| 38 |
+
screenshot_output = gr.Image(label="Screenshot")
|
| 39 |
+
capture_button = gr.Button("Capture Screenshot")
|
| 40 |
+
|
| 41 |
+
capture_button.click(take_screenshot, inputs=url_input, outputs=screenshot_output)
|
| 42 |
+
|
| 43 |
+
# Launch the interface
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
demo.launch()
|