Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,59 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from selenium import webdriver
|
| 3 |
from selenium.webdriver.chrome.options import Options
|
| 4 |
-
from
|
| 5 |
-
import
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
def capture_screenshot(
|
| 9 |
"""
|
| 10 |
-
指定された
|
| 11 |
-
|
| 12 |
-
Args:
|
| 13 |
-
html_content: スクリーンショットを撮りたいHTMLコンテンツの文字列。
|
| 14 |
-
width: ブラウザウィンドウの幅 (ピクセル単位)。
|
| 15 |
-
height: ブラウザウィンドウの高さ (ピクセル単位)。
|
| 16 |
-
|
| 17 |
-
Returns:
|
| 18 |
-
スクリーンショット画像のファイルパス。
|
| 19 |
-
エラーが発生した場合はNoneを返す。
|
| 20 |
"""
|
| 21 |
try:
|
| 22 |
-
#
|
| 23 |
chrome_options = Options()
|
| 24 |
-
chrome_options.add_argument("--headless") #
|
| 25 |
-
chrome_options.add_argument("--no-sandbox") #
|
| 26 |
-
chrome_options.add_argument("--disable-dev-shm-usage") # /dev/shm
|
| 27 |
-
chrome_options.add_argument(
|
| 28 |
-
|
| 29 |
-
#chromedriverのパスを指定(/usr/bin/chromedriver が標準)
|
| 30 |
-
chromedriver_path = "/usr/bin/chromedriver" # Hugging Face Spacesの標準パス
|
| 31 |
-
|
| 32 |
-
# Chrome WebDriver を初期化
|
| 33 |
-
driver = webdriver.Chrome(executable_path=chromedriver_path, options=chrome_options)
|
| 34 |
-
|
| 35 |
-
# HTMLコンテンツをファイルに書き込む(一時ファイル)
|
| 36 |
-
temp_html_path = "temp.html"
|
| 37 |
-
with open(temp_html_path, "w", encoding="utf-8") as f:
|
| 38 |
-
f.write(html_content)
|
| 39 |
-
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
driver
|
| 43 |
-
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
driver.save_screenshot(screenshot_path)
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
return screenshot_path
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
-
|
| 59 |
-
return
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
| 1 |
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 |
"""
|
| 10 |
+
指定された URL のスクリーンショットを撮り、PIL Image オブジェクトとして返す。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
"""
|
| 12 |
try:
|
| 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") # For security reasons on some platforms
|
| 17 |
+
chrome_options.add_argument("--disable-dev-shm-usage") # Fixes /dev/shm out-of-memory issues
|
| 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
|
| 25 |
+
png_bytes = driver.get_screenshot_as_png()
|
|
|
|
| 26 |
|
| 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"), # Use PIL Image output
|
| 51 |
+
title="HTML Screenshot Capture",
|
| 52 |
+
description="Enter a URL to capture a screenshot of the webpage.",
|
| 53 |
+
examples=[
|
| 54 |
+
["https://www.google.com"],
|
| 55 |
+
["https://www.wikipedia.org"],
|
| 56 |
+
["https://www.github.com"]
|
| 57 |
+
],
|
| 58 |
+
)
|
| 59 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|