Mayank2027 commited on
Commit
42f7abc
·
verified ·
1 Parent(s): 4e926bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -21
app.py CHANGED
@@ -1,6 +1,6 @@
1
  """
2
- Ultimate Web Agent – Production-Ready with Fast Startup
3
- Fixes: forced headless, Playwright health check, semaphore, env vars for keys
4
  """
5
  import os
6
  import json
@@ -204,7 +204,7 @@ async def run_agent(sid, task, headless):
204
  browser = None
205
  try:
206
  playwright = await async_playwright().start()
207
- # Force headless=True regardless of input (HF Spaces have no display)
208
  browser = await playwright.chromium.launch(headless=True)
209
  context = await browser.new_context(viewport={"width": 1280, "height": 720})
210
  page = await context.new_page()
@@ -214,7 +214,6 @@ async def run_agent(sid, task, headless):
214
  agent = Agent(tools, state)
215
  result = await agent.run(task)
216
  status[sid] = {"status": "completed", "result": result, "progress": 100}
217
- # Notify WebSocket clients
218
  if sid in ws_connections:
219
  for ws in ws_connections[sid]:
220
  try:
@@ -420,24 +419,9 @@ async def health():
420
  return {"status": "healthy", "running": True}
421
 
422
  # =====================================================================
423
- # STARTUP: Verify Playwright works before uvicorn starts
 
424
  # =====================================================================
425
- async def test_playwright():
426
- """Test that Playwright can launch a headless browser."""
427
- try:
428
- async with async_playwright() as p:
429
- browser = await p.chromium.launch(headless=True)
430
- page = await browser.new_page()
431
- await page.goto("about:blank")
432
- await browser.close()
433
- logger.info("✅ Playwright health check passed.")
434
- except Exception as e:
435
- logger.critical(f"❌ Playwright startup test failed: {e}")
436
- raise RuntimeError("Playwright is not functional") from e
437
-
438
- # Only run the test when executed directly (not when imported)
439
  if __name__ == "__main__":
440
- # Run the health check
441
- asyncio.run(test_playwright())
442
  import uvicorn
443
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  """
2
+ Ultimate Web Agent – Production-Ready for Hugging Face Spaces
3
+ Fixed: forced headless, semaphore, env vars, no startup crash
4
  """
5
  import os
6
  import json
 
204
  browser = None
205
  try:
206
  playwright = await async_playwright().start()
207
+ # Force headless=True regardless of input
208
  browser = await playwright.chromium.launch(headless=True)
209
  context = await browser.new_context(viewport={"width": 1280, "height": 720})
210
  page = await context.new_page()
 
214
  agent = Agent(tools, state)
215
  result = await agent.run(task)
216
  status[sid] = {"status": "completed", "result": result, "progress": 100}
 
217
  if sid in ws_connections:
218
  for ws in ws_connections[sid]:
219
  try:
 
419
  return {"status": "healthy", "running": True}
420
 
421
  # =====================================================================
422
+ # STARTUP: NO health check – browser will be tested on first task
423
+ # This prevents early crashes due to Playwright environment issues.
424
  # =====================================================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
425
  if __name__ == "__main__":
 
 
426
  import uvicorn
427
  uvicorn.run(app, host="0.0.0.0", port=7860)