soiz1 commited on
Commit
3403a49
·
verified ·
1 Parent(s): bcf77ba

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. README.md +5 -4
  2. app.docker +5 -0
  3. app.py +91 -0
  4. packages.txt +1 -0
  5. requirements.txt +5 -0
README.md CHANGED
@@ -1,12 +1,13 @@
1
  ---
2
  title: Selenium Screenshot Gradio
3
- emoji: 🦀
4
- colorFrom: purple
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 5.34.2
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Selenium Screenshot Gradio
3
+ emoji: 🐨
4
+ colorFrom: red
5
+ colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 4.36.0
8
  app_file: app.py
9
  pinned: false
10
+ license: mit
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.docker ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ # ベースイメージにUbuntuを使用
2
+ FROM huggingface/transformers-pytorch-cpu
3
+
4
+ # Noto CJKフォントのインストール
5
+ RUN apt-get update && apt-get install -y fonts-noto-cjk
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ from flask import Flask, request, send_file
4
+ from selenium import webdriver
5
+ from selenium.common.exceptions import WebDriverException
6
+ from PIL import Image
7
+ from io import BytesIO
8
+
9
+ # フォントをダウンロードする関数
10
+ def download_noto_sans_cjk():
11
+ url = "https://noto-website-2.storage.googleapis.com/pkgs/NotoSansCJKjp-hinted.zip"
12
+ response = requests.get(url)
13
+
14
+ # フォントディレクトリを作成
15
+ font_dir = "/usr/share/fonts/truetype/noto"
16
+ os.makedirs(font_dir, exist_ok=True)
17
+
18
+ # ダウンロードしたフォントを展開
19
+ with open("/tmp/NotoSansCJKjp.zip", "wb") as f:
20
+ f.write(response.content)
21
+
22
+ os.system("unzip /tmp/NotoSansCJKjp.zip -d " + font_dir)
23
+ os.system("fc-cache -fv") # フォントキャッシュを更新
24
+
25
+ # フォントのダウンロードとインストールを実行
26
+ download_noto_sans_cjk()
27
+
28
+ app = Flask(__name__)
29
+
30
+ def take_screenshot(url, screenw=1080, screenh=720):
31
+ options = webdriver.ChromeOptions()
32
+ options.add_argument('--headless')
33
+ options.add_argument('--no-sandbox')
34
+ options.add_argument('--disable-dev-shm-usage')
35
+
36
+ try:
37
+ wd = webdriver.Chrome(options=options)
38
+ wd.set_window_size(screenw, screenh) # Set window size based on screenw and screenh
39
+ wd.get(url)
40
+ wd.implicitly_wait(10)
41
+ screenshot = wd.get_screenshot_as_png()
42
+ except WebDriverException:
43
+ # Return a blank image as a placeholder if there's an error
44
+ return Image.new('RGB', (1, 1))
45
+ finally:
46
+ if wd:
47
+ wd.quit()
48
+
49
+ return Image.open(BytesIO(screenshot))
50
+
51
+ @app.route('/', methods=['GET'])
52
+ def screenshot():
53
+ url = request.args.get('url')
54
+ if not url:
55
+ return "URL parameter is required.", 400
56
+
57
+ # ウィンドウサイズのパラメーターを取得
58
+ screenw = request.args.get('screenw', default=1080, type=int)
59
+ screenh = request.args.get('screenh', default=720, type=int)
60
+
61
+ # 画像リサイズのパラメーターを取得
62
+ width = request.args.get('width', type=int)
63
+ height = request.args.get('height', type=int)
64
+
65
+ # スクリーンショットを取得
66
+ image = take_screenshot(url, screenw=screenw, screenh=screenh)
67
+
68
+ # リサイズ処理
69
+ if width and height:
70
+ # 両方のパラメーターが提供された場合
71
+ image = image.resize((width, height), Image.ANTIALIAS)
72
+ elif width:
73
+ # width のみが提供された場合、アスペクト比に基づいて height を計算
74
+ aspect_ratio = image.height / image.width
75
+ height = int(width * aspect_ratio)
76
+ image = image.resize((width, height), Image.ANTIALIAS)
77
+ elif height:
78
+ # height のみが提供された場合、アスペクト比に基づいて width を計算
79
+ aspect_ratio = image.width / image.height
80
+ width = int(height * aspect_ratio)
81
+ image = image.resize((width, height), Image.ANTIALIAS)
82
+
83
+ # 結果画像をメモリに保存してレスポンスとして返却
84
+ img_io = BytesIO()
85
+ image.save(img_io, 'PNG')
86
+ img_io.seek(0)
87
+
88
+ return send_file(img_io, mimetype='image/png')
89
+
90
+ if __name__ == "__main__":
91
+ app.run(host="0.0.0.0", port=7860)
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ chromium-driver
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ selenium >=4.0.0, < 5.0.0
2
+ gradio>=3.40.1
3
+ Pillow>=8.3.1,<9.0
4
+ flask
5
+ rinoh-typeface-notosanscjk