Tantawi65 commited on
Commit
1736c15
·
1 Parent(s): 832611d

Fix: Use temporary directory for uploads to avoid permission issues

Browse files
Files changed (2) hide show
  1. Dockerfile +4 -0
  2. main.py +5 -3
Dockerfile CHANGED
@@ -3,6 +3,9 @@ FROM python:3.8
3
  # Set working directory
4
  WORKDIR /code
5
 
 
 
 
6
  # Copy requirements first for better caching
7
  COPY ./requirements.txt /code/requirements.txt
8
 
@@ -18,6 +21,7 @@ EXPOSE 7860
18
 
19
  # Set environment variables
20
  ENV PYTHONPATH=/code
 
21
 
22
  # Command to run the application
23
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
3
  # Set working directory
4
  WORKDIR /code
5
 
6
+ # Create necessary directories with proper permissions
7
+ RUN mkdir -p /tmp/uploads && chmod 777 /tmp/uploads
8
+
9
  # Copy requirements first for better caching
10
  COPY ./requirements.txt /code/requirements.txt
11
 
 
21
 
22
  # Set environment variables
23
  ENV PYTHONPATH=/code
24
+ ENV TMPDIR=/tmp
25
 
26
  # Command to run the application
27
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
main.py CHANGED
@@ -28,8 +28,10 @@ app.add_middleware(
28
  allow_headers=["*"],
29
  )
30
 
31
- # Create uploads directory
32
- os.makedirs("uploads", exist_ok=True)
 
 
33
 
34
  @app.get("/")
35
  async def root():
@@ -47,7 +49,7 @@ async def analyze_image(file: UploadFile = File(...)):
47
  raise HTTPException(status_code=400, detail="File must be an image")
48
 
49
  unique_filename = f"{uuid.uuid4().hex}_{file.filename}"
50
- file_path = f"uploads/{unique_filename}"
51
 
52
  with open(file_path, "wb") as buffer:
53
  shutil.copyfileobj(file.file, buffer)
 
28
  allow_headers=["*"],
29
  )
30
 
31
+ # Create uploads directory in tmp (writable in containers)
32
+ import tempfile
33
+ UPLOAD_DIR = tempfile.mkdtemp()
34
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
35
 
36
  @app.get("/")
37
  async def root():
 
49
  raise HTTPException(status_code=400, detail="File must be an image")
50
 
51
  unique_filename = f"{uuid.uuid4().hex}_{file.filename}"
52
+ file_path = os.path.join(UPLOAD_DIR, unique_filename)
53
 
54
  with open(file_path, "wb") as buffer:
55
  shutil.copyfileobj(file.file, buffer)