AlexanderStaniel commited on
Commit
e62f06e
·
verified ·
1 Parent(s): 140ccff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -17
app.py CHANGED
@@ -24,37 +24,77 @@ from langchain_core.messages import SystemMessage, HumanMessage
24
  load_dotenv()
25
 
26
  # --- Playwright Browser and System Dependency Installation Workaround for Hugging Face Spaces ---
27
- # This block ensures Playwright browsers AND specific system dependencies are installed when the app starts.
28
  # It checks if browsers are already installed or if an attempt has been made.
29
  PLAYWRIGHT_BROWSERS_PATH = os.path.expanduser("~/.cache/ms-playwright")
30
  PLAYWRIGHT_INSTALLED_FLAG = "PLAYWRIGHT_BROWSERS_INSTALLED"
31
 
32
- # Define problematic system packages that apt-get struggles with via packages.txt
33
  # These will be installed manually via subprocess.
34
- PROBLEM_APT_PACKAGES = [
35
- "libwoff2-1.0",
36
- "libjpeg-turbo8"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  ]
38
 
39
  # Check if installation has been attempted during this container's lifetime
40
  if not os.path.exists(os.path.join(PLAYWRIGHT_BROWSERS_PATH, "chromium")) and \
41
  os.environ.get(PLAYWRIGHT_INSTALLED_FLAG, "false") == "false":
42
- print("Playwright browsers and/or specific system dependencies not found. Attempting to install them now (this might take a moment)...")
43
  try:
44
- # First, try to install the problematic system dependencies via apt-get directly
45
- print(f"Installing specific system dependencies via apt-get: {', '.join(PROBLEM_APT_PACKAGES)}...")
46
- apt_install_command = ["sudo", "apt-get", "update", "-qq"] # -qq for quiet update
47
- subprocess.run(apt_install_command, check=True, capture_output=True)
48
-
49
- apt_install_command = ["sudo", "apt-get", "install", "-y"] + PROBLEM_APT_PACKAGES
50
- subprocess.run(apt_install_command, check=True, capture_output=True)
51
- print("Specific system dependencies installed successfully!")
52
-
53
- # Then, run the playwright install command
 
 
 
 
 
 
 
54
  print("Running playwright install for browsers...")
55
- subprocess.run([sys.executable, "-m", "playwright", "install"], check=True)
56
  os.environ[PLAYWRIGHT_INSTALLED_FLAG] = "true" # Mark as installed for this session
57
  print("Playwright browsers installed successfully!")
 
 
 
 
 
58
  except subprocess.CalledProcessError as e:
59
  print(f"Error during Playwright/apt-get installation: {e}. STDOUT: {e.stdout.decode()} STDERR: {e.stderr.decode()}")
60
  except Exception as e:
 
24
  load_dotenv()
25
 
26
  # --- Playwright Browser and System Dependency Installation Workaround for Hugging Face Spaces ---
27
+ # This block ensures Playwright browsers AND all necessary system dependencies are installed when the app starts.
28
  # It checks if browsers are already installed or if an attempt has been made.
29
  PLAYWRIGHT_BROWSERS_PATH = os.path.expanduser("~/.cache/ms-playwright")
30
  PLAYWRIGHT_INSTALLED_FLAG = "PLAYWRIGHT_BROWSERS_INSTALLED"
31
 
32
+ # Define ALL system packages required for Playwright on Debian Bookworm
33
  # These will be installed manually via subprocess.
34
+ ALL_PLAYWRIGHT_APT_PACKAGES = [
35
+ "chromium", # Basic chromium package for good measure
36
+ "libgstreamer1.0-0",
37
+ "libgtk-4-1",
38
+ "libgraphene-1.0-0",
39
+ "libwoff2-1.0", # Reverted to original name, will be installed via apt-get in-app
40
+ "libgstreamer-plugins-base1.0-0",
41
+ "libgstreamer-gl1.0-0",
42
+ "libgstreamer-plugins-bad1.0-0",
43
+ "libavif15",
44
+ "libharfbuzz-icu0",
45
+ "libenchant-2-2",
46
+ "libhyphen0",
47
+ "libmanette-0.2-0",
48
+ "libnss3",
49
+ "libxss1",
50
+ "libasound2",
51
+ "libgbm-dev",
52
+ "libatk-bridge2.0-0",
53
+ "libcups2",
54
+ "libdrm-dev",
55
+ "libgdk-pixbuf2.0-0",
56
+ "libglib2.0-0",
57
+ "libjpeg-turbo8", # Reverted to original name, will be installed via apt-get in-app
58
+ "libnspr4",
59
+ "libxcb-dri3-0",
60
+ "libxcomposite1",
61
+ "libxrandr2",
62
+ "libxshmfence1",
63
+ "libxtst6",
64
+ "xdg-utils"
65
  ]
66
 
67
  # Check if installation has been attempted during this container's lifetime
68
  if not os.path.exists(os.path.join(PLAYWRIGHT_BROWSERS_PATH, "chromium")) and \
69
  os.environ.get(PLAYWRIGHT_INSTALLED_FLAG, "false") == "false":
70
+ print("Playwright browsers and/or system dependencies not found. Attempting a comprehensive installation now (this will take several minutes)...")
71
  try:
72
+ # First, update apt package lists
73
+ print("Updating apt package lists...")
74
+ # Use stdout=subprocess.PIPE, stderr=subprocess.PIPE to capture output and prevent it from filling up logs unless an error occurs
75
+ subprocess.run(["sudo", "apt-get", "update", "-qq"], check=True, capture_output=True)
76
+ print("Apt package lists updated.")
77
+
78
+ # Then, install all necessary system dependencies via apt-get directly
79
+ print(f"Installing all Playwright system dependencies via apt-get: {', '.join(ALL_PLAYWRIGHT_APT_PACKAGES)}...")
80
+ apt_install_command = ["sudo", "apt-get", "install", "-y"] + ALL_PLAYWRIGHT_APT_PACKAGES
81
+ result = subprocess.run(apt_install_command, check=True, capture_output=True, text=True)
82
+ print("All system dependencies installed successfully!")
83
+ if result.stdout:
84
+ print("APT Install STDOUT:\n", result.stdout)
85
+ if result.stderr:
86
+ print("APT Install STDERR:\n", result.stderr)
87
+
88
+ # Finally, run the playwright install command for the browsers themselves
89
  print("Running playwright install for browsers...")
90
+ result = subprocess.run([sys.executable, "-m", "playwright", "install"], check=True, capture_output=True, text=True)
91
  os.environ[PLAYWRIGHT_INSTALLED_FLAG] = "true" # Mark as installed for this session
92
  print("Playwright browsers installed successfully!")
93
+ if result.stdout:
94
+ print("Playwright Install STDOUT:\n", result.stdout)
95
+ if result.stderr:
96
+ print("Playwright Install STDERR:\n", result.stderr)
97
+
98
  except subprocess.CalledProcessError as e:
99
  print(f"Error during Playwright/apt-get installation: {e}. STDOUT: {e.stdout.decode()} STDERR: {e.stderr.decode()}")
100
  except Exception as e: