Update app.py
Browse files
app.py
CHANGED
|
@@ -23,28 +23,44 @@ from langchain_core.messages import SystemMessage, HumanMessage
|
|
| 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 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
if not os.path.exists(os.path.join(PLAYWRIGHT_BROWSERS_PATH, "chromium")) and \
|
| 34 |
-
os.environ.get(
|
| 35 |
-
print("Playwright browsers not found. Attempting to install them now (this might take a moment)...")
|
| 36 |
try:
|
| 37 |
-
#
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
subprocess.run([sys.executable, "-m", "playwright", "install"], check=True)
|
| 40 |
-
os.environ[
|
| 41 |
print("Playwright browsers installed successfully!")
|
| 42 |
except subprocess.CalledProcessError as e:
|
| 43 |
-
print(f"Error
|
| 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 ---
|
|
@@ -173,7 +189,7 @@ async def scrape_flight_vouchers(details):
|
|
| 173 |
})
|
| 174 |
continue
|
| 175 |
except Exception as e_browser_launch: # Catching general Exception for browser launch errors
|
| 176 |
-
print(f"Playwright browser launch error: {e_browser_launch}. Ensure `playwright install` was run.")
|
| 177 |
return [{
|
| 178 |
"source": "Scraper Initialization Error",
|
| 179 |
"type": "voucher",
|
|
@@ -339,7 +355,7 @@ async def ask_bot(question):
|
|
| 339 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
| 340 |
gr.Markdown(
|
| 341 |
"""
|
| 342 |
-
# ✨ The Ultimate Global Travel Planner Bot - Bazinga
|
| 343 |
Your smart AI assistant for finding the best flights, hotels, and activities worldwide BAZINGA!
|
| 344 |
"""
|
| 345 |
)
|
|
|
|
| 23 |
# On Hugging Face Spaces, secrets are automatically available as environment variables.
|
| 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:
|
| 61 |
+
print(f"Unexpected error during Playwright browser or system dependency installation: {e}")
|
| 62 |
else:
|
| 63 |
+
print("Playwright browsers and required system dependencies already appear to be installed or installation skipped.")
|
| 64 |
|
| 65 |
|
| 66 |
# --- 1. API KEY CHECK ---
|
|
|
|
| 189 |
})
|
| 190 |
continue
|
| 191 |
except Exception as e_browser_launch: # Catching general Exception for browser launch errors
|
| 192 |
+
print(f"Playwright browser launch error: {e_browser_launch}. Ensure `playwright install` was run and system dependencies are met.")
|
| 193 |
return [{
|
| 194 |
"source": "Scraper Initialization Error",
|
| 195 |
"type": "voucher",
|
|
|
|
| 355 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
| 356 |
gr.Markdown(
|
| 357 |
"""
|
| 358 |
+
# ✨ The Ultimate Global Travel Planner Bot - Bazinga Edition ✨
|
| 359 |
Your smart AI assistant for finding the best flights, hotels, and activities worldwide BAZINGA!
|
| 360 |
"""
|
| 361 |
)
|