abhisheksan commited on
Commit
2901363
·
1 Parent(s): a4ec802

Refactor Dockerfile and enhance main.py with Hugging Face integration

Browse files
Files changed (3) hide show
  1. Dockerfile +0 -7
  2. main.py +43 -18
  3. requirements.txt +1 -2
Dockerfile CHANGED
@@ -19,12 +19,5 @@ RUN pip install --no-cache-dir -r requirements.txt
19
  # Copy the application code
20
  COPY . .
21
 
22
- # Create a non-root user
23
- RUN useradd -m -u 1000 appuser
24
- USER appuser
25
-
26
- # Expose the port the app runs on
27
- EXPOSE 8000
28
-
29
  # Command to run the application
30
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
 
19
  # Copy the application code
20
  COPY . .
21
 
 
 
 
 
 
 
 
22
  # Command to run the application
23
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
main.py CHANGED
@@ -1,28 +1,53 @@
1
  from fastapi import FastAPI
2
  from app.api.endpoints.poetry import router as poetry_router
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  app = FastAPI()
5
  app.include_router(poetry_router, prefix="/api/v1/poetry")
6
 
7
- if __name__ == "__main__":
8
- import os
9
- import logging
10
- from typing import Tuple
11
- from starlette.applications import Starlette
12
- from starlette.responses import Response
13
- from starlette.routing import Route
14
- from starlette.staticfiles import StaticFiles
15
-
16
- logging.basicConfig(level=logging.INFO)
17
- logger = logging.getLogger(__name__)
18
-
19
- def get_app_and_port() -> Tuple[Starlette, int]:
20
- port = int(os.getenv("PORT", "8000"))
21
- return app, port
22
-
23
- async def lifecheck(request):
24
- return Response("OK", media_type="text/plain")
 
 
 
 
 
 
 
 
 
25
 
 
 
 
 
26
  routes = [
27
  Route("/", app.router),
28
  Route("/healthz", lifecheck),
 
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
+
13
+ logging.basicConfig(level=logging.INFO)
14
+ logger = logging.getLogger(__name__)
15
 
16
  app = FastAPI()
17
  app.include_router(poetry_router, prefix="/api/v1/poetry")
18
 
19
+ @lru_cache()
20
+ def get_hf_token() -> str:
21
+ """Get Hugging Face token from environment variables."""
22
+ token = os.getenv("HF_TOKEN")
23
+ if not token:
24
+ raise EnvironmentError(
25
+ "HF_TOKEN environment variable not found. "
26
+ "Please set your Hugging Face access token."
27
+ )
28
+ return token
29
+
30
+ def init_huggingface():
31
+ """Initialize Hugging Face authentication."""
32
+ try:
33
+ token = get_hf_token()
34
+ login(token=token)
35
+ logger.info("Successfully logged in to Hugging Face")
36
+ except Exception as e:
37
+ logger.error(f"Failed to login to Hugging Face: {str(e)}")
38
+ raise
39
+
40
+ def get_app_and_port() -> Tuple[Starlette, int]:
41
+ port = int(os.getenv("PORT", "8000"))
42
+ return app, port
43
+
44
+ async def lifecheck(request):
45
+ return Response("OK", media_type="text/plain")
46
 
47
+ if __name__ == "__main__":
48
+ # Initialize Hugging Face authentication before starting the server
49
+ init_huggingface()
50
+
51
  routes = [
52
  Route("/", app.router),
53
  Route("/healthz", lifecheck),
requirements.txt CHANGED
@@ -1,9 +1,8 @@
1
  # Core dependencies
2
- gradio==4.19.2
3
  fastapi==0.109.2
4
  uvicorn==0.27.1
5
  pydantic==2.6.1
6
-
7
  # For API requests and handling
8
  requests==2.31.0
9
  python-multipart==0.0.9
 
1
  # Core dependencies
 
2
  fastapi==0.109.2
3
  uvicorn==0.27.1
4
  pydantic==2.6.1
5
+ huggingface_hub
6
  # For API requests and handling
7
  requests==2.31.0
8
  python-multipart==0.0.9