Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,8 @@ import asyncio
|
|
| 4 |
import json
|
| 5 |
import datetime
|
| 6 |
import re # For regular expressions (used in voucher scraping)
|
|
|
|
|
|
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
# --- Playwright Imports for Web Scraping ---
|
|
@@ -21,6 +23,30 @@ from langchain_core.messages import SystemMessage, HumanMessage
|
|
| 21 |
# On Hugging Face Spaces, secrets are automatically available as environment variables.
|
| 22 |
load_dotenv()
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# --- 1. API KEY CHECK ---
|
| 25 |
# This block ensures that the necessary API keys are set before the application starts.
|
| 26 |
try:
|
|
|
|
| 4 |
import json
|
| 5 |
import datetime
|
| 6 |
import re # For regular expressions (used in voucher scraping)
|
| 7 |
+
import subprocess # For running shell commands
|
| 8 |
+
import sys # For sys.executable
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
|
| 11 |
# --- Playwright Imports for Web Scraping ---
|
|
|
|
| 23 |
# On Hugging Face Spaces, secrets are automatically available as environment variables.
|
| 24 |
load_dotenv()
|
| 25 |
|
| 26 |
+
# --- Playwright Browser Installation Workaround for Hugging Face Spaces ---
|
| 27 |
+
# This block ensures Playwright browsers are installed when the app starts,
|
| 28 |
+
# especially useful when setup.sh is not reliably executed or for cold starts.
|
| 29 |
+
# It checks if browsers are already installed or if an attempt has been made.
|
| 30 |
+
PLAYWRIGHT_BROWSERS_PATH = os.path.expanduser("~/.cache/ms-playwright")
|
| 31 |
+
# Use a custom environment variable to track if installation has been attempted during this container's lifetime
|
| 32 |
+
# This prevents it from running on every single request.
|
| 33 |
+
if not os.path.exists(os.path.join(PLAYWRIGHT_BROWSERS_PATH, "chromium")) and \
|
| 34 |
+
os.environ.get("PLAYWRIGHT_BROWSERS_INSTALLED", "false") == "false":
|
| 35 |
+
print("Playwright browsers not found. Attempting to install them now (this might take a moment)...")
|
| 36 |
+
try:
|
| 37 |
+
# Run the playwright install command as a subprocess
|
| 38 |
+
# sys.executable ensures we use the correct Python interpreter's playwright
|
| 39 |
+
subprocess.run([sys.executable, "-m", "playwright", "install"], check=True)
|
| 40 |
+
os.environ["PLAYWRIGHT_BROWSERS_INSTALLED"] = "true" # Mark as installed for this session
|
| 41 |
+
print("Playwright browsers installed successfully!")
|
| 42 |
+
except subprocess.CalledProcessError as e:
|
| 43 |
+
print(f"Error installing Playwright browsers: {e}. Output: {e.stdout.decode() if e.stdout else ''} {e.stderr.decode() if e.stderr else ''}")
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"Unexpected error during Playwright browser installation: {e}")
|
| 46 |
+
else:
|
| 47 |
+
print("Playwright browsers already appear to be installed or installation skipped.")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
# --- 1. API KEY CHECK ---
|
| 51 |
# This block ensures that the necessary API keys are set before the application starts.
|
| 52 |
try:
|