1+ import gradio as gr
2+ import tempfile
3+ import os
4+ from selenium import webdriver
5+ from selenium .webdriver .chrome .options import Options
6+ print ("Gradio app loaded." )
7+ def capture_page (url : str , output_file : str = "screenshot.png" ):
8+ """
9+ Captures a screenshot of the given webpage.
10+
11+ :param url: The URL of the webpage to capture.
12+ :param output_file: The filename to save the screenshot.
13+ """
14+ options = Options ()
15+ options .add_argument ("--headless" ) # Run in headless mode
16+ options .add_argument ("--window-size=1920,1080" ) # Set window size
17+
18+ driver = webdriver .Chrome (options = options )
19+
20+ try :
21+ driver .get (url )
22+ driver .save_screenshot (output_file )
23+ print (f"Screenshot saved: { output_file } " )
24+ finally :
25+ driver .quit ()
26+
27+ def capture_and_show (url : str ):
28+ """Capture webpage and return the image"""
29+ try :
30+ # Create temporary file for the screenshot
31+ with tempfile .NamedTemporaryFile (suffix = '.png' , delete = False ) as tmp :
32+ temp_path = tmp .name
33+
34+ # Capture the webpage
35+ capture_page (url , temp_path )
36+
37+ # Return the image path
38+ return temp_path
39+ except Exception as e :
40+ return f"Error capturing page: { str (e )} "
41+
42+ def create_gradio_app ():
43+ """Create the main Gradio application with all components"""
44+ with gr .Blocks () as app :
45+ gr .Markdown ("# Webpage Screenshot Capture" )
46+
47+ with gr .Row ():
48+ url_input = gr .Textbox (
49+ label = "Website URL" ,
50+ placeholder = "Enter website URL (e.g., https://www.example.com)" ,
51+ scale = 4
52+ )
53+ capture_btn = gr .Button ("Capture" , scale = 1 )
54+
55+ with gr .Row ():
56+ output_image = gr .Image (
57+ label = "Captured Screenshot" ,
58+ type = "filepath"
59+ )
60+
61+ # Connect the components
62+ capture_btn .click (
63+ fn = capture_and_show ,
64+ inputs = [url_input ],
65+ outputs = [output_image ]
66+ )
67+
68+ return app
69+
70+ app = create_gradio_app ()
71+
72+ # Configure server settings for Docker deployment
73+ server_port = 7860 # Standard Gradio port
74+ server_name = "0.0.0.0" # Allow external connections
75+
76+ def main ():
77+ """Launch the Gradio application"""
78+ print ("Starting Gradio server..." )
79+ app .launch (
80+ server_name = server_name ,
81+ server_port = server_port ,
82+ share = False , # Disable sharing as we're running in Docker
83+ auth = None , # Can be configured if authentication is needed
84+ ssl_verify = False , # Disable SSL verification for internal Docker network
85+ show_error = True ,
86+ favicon_path = None
87+ )
88+
89+ main ()
0 commit comments