Spaces:
Sleeping
Sleeping
Commit
·
f1b3987
1
Parent(s):
51ed73b
fixes
Browse files
main.py
CHANGED
@@ -1,26 +1,19 @@
|
|
|
|
1 |
from fastapi import FastAPI
|
2 |
from app.api.endpoints.poetry import router as poetry_router
|
3 |
import os
|
4 |
import logging
|
5 |
from typing import Tuple
|
6 |
-
from starlette.applications import Starlette
|
7 |
from starlette.responses import Response
|
8 |
-
from starlette.routing import Route
|
9 |
from starlette.staticfiles import StaticFiles
|
10 |
from huggingface_hub import login
|
11 |
from functools import lru_cache
|
12 |
from app.services.poetry_generation import PoetryGenerationService
|
13 |
|
14 |
-
#
|
15 |
-
poetry_service = PoetryGenerationService()
|
16 |
-
|
17 |
-
|
18 |
logging.basicConfig(level=logging.INFO)
|
19 |
logger = logging.getLogger(__name__)
|
20 |
|
21 |
-
app = FastAPI()
|
22 |
-
app.include_router(poetry_router, prefix="/api/v1/poetry")
|
23 |
-
|
24 |
@lru_cache()
|
25 |
def get_hf_token() -> str:
|
26 |
"""Get Hugging Face token from environment variables."""
|
@@ -42,29 +35,31 @@ def init_huggingface():
|
|
42 |
logger.error(f"Failed to login to Hugging Face: {str(e)}")
|
43 |
raise
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
async def startup_event():
|
50 |
-
# This will be executed when the application starts
|
51 |
await poetry_service.preload_models()
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
return Response("OK", media_type="text/plain")
|
54 |
|
|
|
|
|
|
|
55 |
if __name__ == "__main__":
|
56 |
-
|
57 |
-
init_huggingface()
|
58 |
|
59 |
-
|
60 |
-
Route("/", app.router),
|
61 |
-
Route("/healthz", lifecheck),
|
62 |
-
]
|
63 |
-
|
64 |
-
app_and_port = get_app_and_port()
|
65 |
-
app = app_and_port[0]
|
66 |
-
port = app_and_port[1]
|
67 |
-
|
68 |
-
logger.info(f"Starting FastAPI server on port {port}")
|
69 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
70 |
-
|
|
|
|
|
|
1 |
+
from contextlib import asynccontextmanager
|
2 |
from fastapi import FastAPI
|
3 |
from app.api.endpoints.poetry import router as poetry_router
|
4 |
import os
|
5 |
import logging
|
6 |
from typing import Tuple
|
|
|
7 |
from starlette.responses import Response
|
|
|
8 |
from starlette.staticfiles import StaticFiles
|
9 |
from huggingface_hub import login
|
10 |
from functools import lru_cache
|
11 |
from app.services.poetry_generation import PoetryGenerationService
|
12 |
|
13 |
+
# Configure logging once at module level
|
|
|
|
|
|
|
14 |
logging.basicConfig(level=logging.INFO)
|
15 |
logger = logging.getLogger(__name__)
|
16 |
|
|
|
|
|
|
|
17 |
@lru_cache()
|
18 |
def get_hf_token() -> str:
|
19 |
"""Get Hugging Face token from environment variables."""
|
|
|
35 |
logger.error(f"Failed to login to Hugging Face: {str(e)}")
|
36 |
raise
|
37 |
|
38 |
+
@asynccontextmanager
|
39 |
+
async def lifespan(app: FastAPI):
|
40 |
+
# Startup
|
41 |
+
poetry_service = PoetryGenerationService()
|
|
|
|
|
42 |
await poetry_service.preload_models()
|
43 |
+
init_huggingface()
|
44 |
+
yield
|
45 |
+
# Cleanup (if needed)
|
46 |
+
# Add cleanup code here
|
47 |
+
|
48 |
+
app = FastAPI(lifespan=lifespan)
|
49 |
+
app.include_router(poetry_router, prefix="/api/v1/poetry")
|
50 |
+
|
51 |
+
@app.get("/healthz")
|
52 |
+
async def lifecheck():
|
53 |
return Response("OK", media_type="text/plain")
|
54 |
|
55 |
+
def get_port() -> int:
|
56 |
+
return int(os.getenv("PORT", "8000"))
|
57 |
+
|
58 |
if __name__ == "__main__":
|
59 |
+
import uvicorn
|
|
|
60 |
|
61 |
+
port = get_port()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
63 |
+
|
64 |
+
logger.info(f"Starting FastAPI server on port {port}")
|
65 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|