Yassine Mhirsi commited on
Commit
03977cf
·
1 Parent(s): e304280

Refactor Dockerfile and main.py to set working directory and PYTHONPATH correctly; ensure imports function as expected

Browse files
Files changed (2) hide show
  1. Dockerfile +4 -3
  2. main.py +8 -0
Dockerfile CHANGED
@@ -2,10 +2,10 @@
2
  FROM python:3.10-slim
3
 
4
  # Set working directory
5
- WORKDIR /
6
 
7
  # Set PYTHONPATH to ensure imports work correctly
8
- ENV PYTHONPATH=/
9
 
10
  # Install system dependencies
11
  RUN apt-get update && apt-get install -y \
@@ -31,5 +31,6 @@ ENV RELOAD=False
31
 
32
  # Run the application
33
  # Hugging Face Spaces sets PORT environment variable automatically
34
- CMD uvicorn main:app --host 0.0.0.0 --port ${PORT:-7860}
 
35
 
 
2
  FROM python:3.10-slim
3
 
4
  # Set working directory
5
+ WORKDIR /app
6
 
7
  # Set PYTHONPATH to ensure imports work correctly
8
+ ENV PYTHONPATH=/app
9
 
10
  # Install system dependencies
11
  RUN apt-get update && apt-get install -y \
 
31
 
32
  # Run the application
33
  # Hugging Face Spaces sets PORT environment variable automatically
34
+ # Note: WORKDIR is already set to /app above, and main.py adds /app to sys.path
35
+ CMD ["sh", "-c", "uvicorn main:app --host 0.0.0.0 --port ${PORT:-7860}"]
36
 
main.py CHANGED
@@ -1,5 +1,13 @@
1
  """Main FastAPI application entry point"""
2
 
 
 
 
 
 
 
 
 
3
  from contextlib import asynccontextmanager
4
  from fastapi import FastAPI
5
  from fastapi.middleware.cors import CORSMiddleware
 
1
  """Main FastAPI application entry point"""
2
 
3
+ import sys
4
+ from pathlib import Path
5
+
6
+ # Add the app directory to Python path to ensure imports work
7
+ app_dir = Path(__file__).parent
8
+ if str(app_dir) not in sys.path:
9
+ sys.path.insert(0, str(app_dir))
10
+
11
  from contextlib import asynccontextmanager
12
  from fastapi import FastAPI
13
  from fastapi.middleware.cors import CORSMiddleware