ASC8384 commited on
Commit
bee90e6
·
1 Parent(s): 51687f7
Files changed (2) hide show
  1. Dockerfile +2 -2
  2. init_playwright.py +42 -6
Dockerfile CHANGED
@@ -66,5 +66,5 @@ COPY . .
66
  # 暴露端口
67
  EXPOSE 7860
68
 
69
- # 启动应用 - 先初始化playwright,然后运行应用
70
- CMD ["sh", "-c", "python init_playwright.py && python app.py"]
 
66
  # 暴露端口
67
  EXPOSE 7860
68
 
69
+ CMD ["sh", "-c", "python init_playwright.py && python app.py"]
70
+ # CMD ["python", "app.py"]
init_playwright.py CHANGED
@@ -2,11 +2,32 @@
2
  """
3
  Playwright初始化脚本
4
  确保浏览器正确安装和配置
 
5
  """
6
 
7
  import subprocess
8
  import sys
9
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def check_playwright_installation():
12
  """检查playwright是否正确安装"""
@@ -30,21 +51,35 @@ def install_browsers():
30
  """安装playwright浏览器"""
31
  try:
32
  print("🔄 正在安装 Playwright 浏览器...")
33
- subprocess.run([sys.executable, "-m", "playwright", "install", "chromium"],
34
- check=True)
35
- subprocess.run([sys.executable, "-m", "playwright", "install-deps", "chromium"],
36
- check=True)
 
 
 
 
 
 
 
 
37
  print("✅ Playwright 浏览器安装完成!")
38
  return True
39
  except subprocess.CalledProcessError as e:
40
  print(f"❌ 浏览器安装失败: {e}")
41
  return False
 
 
 
42
 
43
  def main():
44
  """主函数"""
45
- print("🚀 初始化 Playwright...")
 
 
 
46
 
47
- # 首先检查是否已正确安装
48
  if check_playwright_installation():
49
  return True
50
 
@@ -60,5 +95,6 @@ if __name__ == "__main__":
60
  success = main()
61
  if not success:
62
  print("❌ Playwright 初始化失败!")
 
63
  sys.exit(1)
64
  print("🎉 Playwright 初始化成功!")
 
2
  """
3
  Playwright初始化脚本
4
  确保浏览器正确安装和配置
5
+ 针对Hugging Face Space环境优化
6
  """
7
 
8
  import subprocess
9
  import sys
10
  import os
11
+ from pathlib import Path
12
+
13
+ def setup_environment():
14
+ """设置环境变量,解决权限问题"""
15
+ # 设置缓存目录到用户主目录
16
+ home_dir = Path.home()
17
+ cache_dir = home_dir / ".cache"
18
+
19
+ # 确保缓存目录存在
20
+ cache_dir.mkdir(parents=True, exist_ok=True)
21
+
22
+ # 设置 Playwright 相关环境变量
23
+ os.environ["PLAYWRIGHT_BROWSERS_PATH"] = str(cache_dir / "ms-playwright")
24
+ os.environ["PLAYWRIGHT_SKIP_BROWSER_GC"] = "1"
25
+
26
+ # 设置系统缓存目录
27
+ os.environ["XDG_CACHE_HOME"] = str(cache_dir)
28
+
29
+ print(f"📁 设置缓存目录: {cache_dir}")
30
+ return str(cache_dir)
31
 
32
  def check_playwright_installation():
33
  """检查playwright是否正确安装"""
 
51
  """安装playwright浏览器"""
52
  try:
53
  print("🔄 正在安装 Playwright 浏览器...")
54
+
55
+ # 使用 --with-deps 选项一次性安装浏览器和依赖
56
+ result = subprocess.run([
57
+ sys.executable, "-m", "playwright", "install",
58
+ "chromium", "--with-deps"
59
+ ], capture_output=True, text=True)
60
+
61
+ if result.returncode != 0:
62
+ print(f"安装输出: {result.stdout}")
63
+ print(f"错误输出: {result.stderr}")
64
+ return False
65
+
66
  print("✅ Playwright 浏览器安装完成!")
67
  return True
68
  except subprocess.CalledProcessError as e:
69
  print(f"❌ 浏览器安装失败: {e}")
70
  return False
71
+ except Exception as e:
72
+ print(f"❌ 安装过程中出现异常: {e}")
73
+ return False
74
 
75
  def main():
76
  """主函数"""
77
+ print("🚀 初始化 Playwright (Hugging Face Space 优化版)...")
78
+
79
+ # 首先设置环境变量
80
+ cache_dir = setup_environment()
81
 
82
+ # 检查是否已正确安装
83
  if check_playwright_installation():
84
  return True
85
 
 
95
  success = main()
96
  if not success:
97
  print("❌ Playwright 初始化失败!")
98
+ print("💡 提示:如果仍有问题,可能需要在系统级别安装浏览器依赖")
99
  sys.exit(1)
100
  print("🎉 Playwright 初始化成功!")