Cnass commited on
Commit
6a23624
·
verified ·
1 Parent(s): 35bae08

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +14 -0
app/main.py CHANGED
@@ -1,8 +1,10 @@
1
  import asyncio
2
  import time
 
3
 
4
  from fastapi import Depends, FastAPI, Header, HTTPException
5
  from fastapi.middleware.cors import CORSMiddleware
 
6
  from pydantic import BaseModel, Field
7
 
8
  from . import config, llama_client
@@ -17,6 +19,11 @@ app.add_middleware(
17
  allow_headers=["*"],
18
  )
19
 
 
 
 
 
 
20
 
21
  async def verify_api_key(x_api_key: str | None = Header(default=None)) -> None:
22
  if config.API_KEY and x_api_key != config.API_KEY:
@@ -142,6 +149,13 @@ async def translate_batch(req: BatchTranslateRequest):
142
  )
143
 
144
 
 
 
 
 
 
 
 
145
  @app.on_event("shutdown")
146
  async def shutdown():
147
  await llama_client.aclose()
 
1
  import asyncio
2
  import time
3
+ from pathlib import Path
4
 
5
  from fastapi import Depends, FastAPI, Header, HTTPException
6
  from fastapi.middleware.cors import CORSMiddleware
7
+ from fastapi.staticfiles import StaticFiles
8
  from pydantic import BaseModel, Field
9
 
10
  from . import config, llama_client
 
19
  allow_headers=["*"],
20
  )
21
 
22
+ # Serves test-ui/index.html at "/" so visiting the Space's URL shows the
23
+ # benchmark page instead of a bare 404 - the API routes below are registered
24
+ # first so this catch-all mount can't shadow them.
25
+ _TEST_UI_DIR = Path(__file__).resolve().parent.parent / "test-ui"
26
+
27
 
28
  async def verify_api_key(x_api_key: str | None = Header(default=None)) -> None:
29
  if config.API_KEY and x_api_key != config.API_KEY:
 
149
  )
150
 
151
 
152
+ # Registered last so it can't shadow the API routes above: Starlette matches
153
+ # routes in registration order, and a "/" mount would otherwise catch
154
+ # everything not matched yet.
155
+ if _TEST_UI_DIR.is_dir():
156
+ app.mount("/", StaticFiles(directory=_TEST_UI_DIR, html=True), name="test-ui")
157
+
158
+
159
  @app.on_event("shutdown")
160
  async def shutdown():
161
  await llama_client.aclose()