Skip to content

Commit 3b7ec04

Browse files
committed
Rename project to "capture-page-tools" and implement Gradio app for webpage screenshot capture
1 parent 582714f commit 3b7ec04

File tree

7 files changed

+674
-103
lines changed

7 files changed

+674
-103
lines changed

.github/workflows/deploy-to-huggingface.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
SPACE_NAME: ${{ secrets.SPACE_NAME }}
4444
run: |
4545
# Create repository URL
46-
REPO_URL="https://huggingface.co/spaces/$HF_USERNAME/template-python"
46+
REPO_URL="https://huggingface.co/spaces/$HF_USERNAME/capture-page-tools"
4747
4848
# Add Hugging Face as a remote and push
4949
git remote add space $REPO_URL || git remote set-url space $REPO_URL

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ ENTRYPOINT []
3232

3333
RUN python setup.py
3434

35-
EXPOSE 8080
35+
EXPOSE 7860
3636
# Run the FastAPI application by default
3737
# Uses `fastapi dev` to enable hot-reloading when the `watch` sync occurs
3838
# Uses `--host 0.0.0.0` to allow access from outside the container

main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def main() -> None:
1010
"""Main application entry point."""
1111
try:
1212
print("Start app...")
13-
import src.modules.api
13+
import src.modules.apps
1414

1515
except Exception as e:
1616
logging.error(f"Application failed to start: {e}", exc_info=True)

pyproject.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
[project]
2-
name = "template-python"
2+
name = "capture-page-tools"
33
version = "0.1.0"
44
description = "Add your description here"
55
readme = "README.md"
66
requires-python = ">=3.11"
77
dependencies = [
88
"fastapi[standard]>=0.115.8",
9+
"gradio>=5.3.0",
910
"ipykernel>=6.29.5",
1011
"numpy>=2.2.3",
1112
"pytest>=8.3.4",
13+
"selenium>=4.29.0",
1214
]
1315

1416
[tool.black]

src/modules/apps/__init__.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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()

src/utils/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""
22
Main application package.
33
This is the root package of the application.
4-
"""
4+
"""

0 commit comments

Comments
 (0)