Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,11 @@ import gradio as gr
|
|
| 2 |
import helium
|
| 3 |
from selenium import webdriver
|
| 4 |
from selenium.webdriver.chrome.options import Options
|
|
|
|
| 5 |
from io import BytesIO
|
| 6 |
from PIL import Image
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def capture_screenshot(url):
|
| 9 |
"""
|
|
@@ -13,12 +16,20 @@ def capture_screenshot(url):
|
|
| 13 |
# Chrome options for headless browsing
|
| 14 |
chrome_options = Options()
|
| 15 |
chrome_options.add_argument("--headless") # Headless mode
|
| 16 |
-
chrome_options.add_argument("--no-sandbox")
|
| 17 |
-
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 18 |
chrome_options.add_argument("--window-size=1024,768") # Set a reasonable window size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
# Start the browser with helium (using the options)
|
| 21 |
-
driver = helium.start_chrome(options=chrome_options)
|
| 22 |
helium.go_to(url)
|
| 23 |
|
| 24 |
# Get screenshot as bytes
|
|
@@ -27,27 +38,24 @@ def capture_screenshot(url):
|
|
| 27 |
# Convert bytes to PIL Image
|
| 28 |
image = Image.open(BytesIO(png_bytes))
|
| 29 |
|
| 30 |
-
# Kill the browser
|
| 31 |
-
helium.kill_browser()
|
| 32 |
return image
|
| 33 |
|
| 34 |
except Exception as e:
|
| 35 |
# Handle errors gracefully.
|
| 36 |
-
return f"Error: {e}"
|
| 37 |
finally:
|
| 38 |
try:
|
| 39 |
-
helium.kill_browser()
|
| 40 |
except:
|
| 41 |
pass
|
| 42 |
|
| 43 |
|
| 44 |
-
|
| 45 |
# Gradio Interface
|
| 46 |
if __name__ == '__main__':
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=capture_screenshot,
|
| 49 |
inputs=gr.Textbox(label="Enter URL", placeholder="https://www.example.com"),
|
| 50 |
-
outputs=gr.Image(type="pil", label="Screenshot"),
|
| 51 |
title="HTML Screenshot Capture",
|
| 52 |
description="Enter a URL to capture a screenshot of the webpage.",
|
| 53 |
examples=[
|
|
@@ -56,4 +64,5 @@ if __name__ == '__main__':
|
|
| 56 |
["https://www.github.com"]
|
| 57 |
],
|
| 58 |
)
|
| 59 |
-
|
|
|
|
|
|
| 2 |
import helium
|
| 3 |
from selenium import webdriver
|
| 4 |
from selenium.webdriver.chrome.options import Options
|
| 5 |
+
from selenium.webdriver.chrome.service import Service # Import Service
|
| 6 |
from io import BytesIO
|
| 7 |
from PIL import Image
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
|
| 11 |
def capture_screenshot(url):
|
| 12 |
"""
|
|
|
|
| 16 |
# Chrome options for headless browsing
|
| 17 |
chrome_options = Options()
|
| 18 |
chrome_options.add_argument("--headless") # Headless mode
|
| 19 |
+
chrome_options.add_argument("--no-sandbox") # For security reasons on some platforms
|
| 20 |
+
chrome_options.add_argument("--disable-dev-shm-usage") # Fixes /dev/shm out-of-memory issues
|
| 21 |
chrome_options.add_argument("--window-size=1024,768") # Set a reasonable window size
|
| 22 |
+
# chrome_options.binary_location = '/usr/bin/google-chrome' # これは不要 or 間違い
|
| 23 |
+
|
| 24 |
+
# Use Service object (explicitly specify chromedriver path if needed)
|
| 25 |
+
# Hugging Face Spaces should handle chromedriver automatically,
|
| 26 |
+
# so we usually don't need to specify the path. But if it still fails,
|
| 27 |
+
# we can try to find it and specify.
|
| 28 |
+
# service = Service(executable_path="/path/to/chromedriver") # 通常は不要
|
| 29 |
+
service = Service() # これで動くはず
|
| 30 |
|
| 31 |
+
# Start the browser with helium (using the options and service)
|
| 32 |
+
driver = helium.start_chrome(options=chrome_options, service=service)
|
| 33 |
helium.go_to(url)
|
| 34 |
|
| 35 |
# Get screenshot as bytes
|
|
|
|
| 38 |
# Convert bytes to PIL Image
|
| 39 |
image = Image.open(BytesIO(png_bytes))
|
| 40 |
|
|
|
|
|
|
|
| 41 |
return image
|
| 42 |
|
| 43 |
except Exception as e:
|
| 44 |
# Handle errors gracefully.
|
| 45 |
+
return f"Error: {e}" # Return the error message
|
| 46 |
finally:
|
| 47 |
try:
|
| 48 |
+
helium.kill_browser() # Always kill the browser
|
| 49 |
except:
|
| 50 |
pass
|
| 51 |
|
| 52 |
|
|
|
|
| 53 |
# Gradio Interface
|
| 54 |
if __name__ == '__main__':
|
| 55 |
iface = gr.Interface(
|
| 56 |
fn=capture_screenshot,
|
| 57 |
inputs=gr.Textbox(label="Enter URL", placeholder="https://www.example.com"),
|
| 58 |
+
outputs=gr.Image(type="pil", label="Screenshot"),
|
| 59 |
title="HTML Screenshot Capture",
|
| 60 |
description="Enter a URL to capture a screenshot of the webpage.",
|
| 61 |
examples=[
|
|
|
|
| 64 |
["https://www.github.com"]
|
| 65 |
],
|
| 66 |
)
|
| 67 |
+
# Disable caching (to avoid potential caching-related errors during development)
|
| 68 |
+
iface.launch(server_name="0.0.0.0", server_port=7860, cache_examples=False)
|