tomo2chin2 commited on
Commit
58c1477
·
verified ·
1 Parent(s): b0ff57a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -67
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 selenium.webdriver.common.by import By
5
- import time
6
- import os
7
 
8
- def capture_screenshot(html_content, width=1280, height=720):
9
  """
10
- 指定されたHTMLコンテンツのスクリーンショットを撮る関数
11
-
12
- Args:
13
- html_content: スクリーンショットを撮りたいHTMLコンテンツの文字列。
14
- width: ブラウザウィンドウの幅 (ピクセル単位)。
15
- height: ブラウザウィンドウの高さ (ピクセル単位)。
16
-
17
- Returns:
18
- スクリーンショット画像のファイルパス。
19
- エラーが発生した場合はNoneを返す。
20
  """
21
  try:
22
- # Headless Chrome のオプションを設定
23
  chrome_options = Options()
24
- chrome_options.add_argument("--headless") # ヘッドレスモードを有効にする
25
- chrome_options.add_argument("--no-sandbox") # sandboxを無効にする(rootで実行する場合に必要)
26
- chrome_options.add_argument("--disable-dev-shm-usage") # /dev/shm を使わないようにする
27
- chrome_options.add_argument(f"--window-size={width},{height}") # ウィンドウサイズ
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
- # ブラウザで一時HTMLファイルを開く
42
- driver.get("file://" + os.path.abspath(temp_html_path))
43
- time.sleep(1) # 念のため少し待つ(JavaScriptの実行などを考慮)
44
 
45
- # スクリーンショットを撮る
46
- screenshot_path = "screenshot.png"
47
- driver.save_screenshot(screenshot_path)
48
 
49
- # ブラウザを閉じる
50
- driver.quit()
51
 
52
- # 一時HTMLファイルを削除
53
- os.remove(temp_html_path)
54
-
55
- return screenshot_path
56
 
57
  except Exception as e:
58
- print(f"Error: {e}")
59
- return None
60
-
61
-
62
- # Gradio インターフェースの定義
63
- iface = gr.Interface(
64
- fn=capture_screenshot,
65
- inputs=[
66
- gr.HTML(label="HTML Content"), # HTML入力用のコンポーネント
67
- gr.Slider(minimum=320, maximum=1920, value=1280, label="Width"), # 幅指定
68
- gr.Slider(minimum=240, maximum=1080, value=720, label="Height"), # 高さ指定
69
-
70
- ],
71
- outputs=gr.Image(type="filepath", label="Screenshot"), # 画像出力
72
- title="HTML Screenshot Capture",
73
- description="Enter HTML content and capture a screenshot.",
74
- examples=[
75
- ["<h1>Hello, World!</h1><p>This is a test.</p>", 800, 600], # サンプル
76
- ["<div style='background-color: lightblue; padding: 20px;'><h2>Styled Div</h2></div>", 640, 480], # サンプル
77
- ]
78
- )
79
-
80
- if __name__ == "__main__":
81
- iface.launch()
 
 
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)