Tecnhotron commited on
Commit
48cea42
·
1 Parent(s): 2e63948
Files changed (2) hide show
  1. api.py +17 -1
  2. test_api.py +1 -1
api.py CHANGED
@@ -10,7 +10,7 @@ import io
10
  from google import genai
11
  from google.genai import types
12
  import computer_control_helper
13
- from fastapi import FastAPI, HTTPException
14
  from fastapi.responses import StreamingResponse
15
  from pydantic import BaseModel, Field
16
  from seleniumbase import Driver
@@ -21,6 +21,7 @@ from selenium.webdriver.support import expected_conditions as EC
21
  from selenium.common.exceptions import TimeoutException, NoSuchElementException, StaleElementReferenceException
22
  import random
23
  from streaming import StreamProcessor, create_streaming_response, StreamConfig, StreamingResponseGenerator
 
24
 
25
  # Virtual display setup for Linux headless environments
26
  import platform
@@ -644,6 +645,21 @@ async def lifespan(app: FastAPI):
644
 
645
  app = FastAPI(lifespan=lifespan)
646
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
647
  @app.get("/health")
648
  async def health_check():
649
  logger.info("Health check endpoint called.")
 
10
  from google import genai
11
  from google.genai import types
12
  import computer_control_helper
13
+ from fastapi import FastAPI, HTTPException, Request
14
  from fastapi.responses import StreamingResponse
15
  from pydantic import BaseModel, Field
16
  from seleniumbase import Driver
 
21
  from selenium.common.exceptions import TimeoutException, NoSuchElementException, StaleElementReferenceException
22
  import random
23
  from streaming import StreamProcessor, create_streaming_response, StreamConfig, StreamingResponseGenerator
24
+ import base64
25
 
26
  # Virtual display setup for Linux headless environments
27
  import platform
 
645
 
646
  app = FastAPI(lifespan=lifespan)
647
 
648
+ @app.middleware("http")
649
+ async def error_screenshot_middleware(request: Request, call_next):
650
+ try:
651
+ return await call_next(request)
652
+ except Exception as exc:
653
+ logger.error(f"Unhandled exception: {exc}", exc_info=True)
654
+ try:
655
+ driver = driver_manager.get_driver()
656
+ png = driver.get_screenshot_as_png()
657
+ b64 = base64.b64encode(png).decode('utf-8')
658
+ logger.error(f"Screenshot base64: {b64}")
659
+ except Exception as capture_err:
660
+ logger.error(f"Failed to capture screenshot: {capture_err}", exc_info=True)
661
+ raise
662
+
663
  @app.get("/health")
664
  async def health_check():
665
  logger.info("Health check endpoint called.")
test_api.py CHANGED
@@ -8,7 +8,7 @@ import random
8
  # Configure the OpenAI client to point to the local server.
9
  # The API key is required by the library but not used by the local server.
10
  client = OpenAI(
11
- base_url="http://localhost:8000",
12
  api_key=os.environ.get("OPENAI_API_KEY", "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
13
  )
14
 
 
8
  # Configure the OpenAI client to point to the local server.
9
  # The API key is required by the library but not used by the local server.
10
  client = OpenAI(
11
+ base_url="https://solarumasteridion-lenith.hf.space/",
12
  api_key=os.environ.get("OPENAI_API_KEY", "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
13
  )
14