vumichien commited on
Commit
212dfc1
·
1 Parent(s): f7596a7

update permission

Browse files
Files changed (2) hide show
  1. Dockerfile +14 -2
  2. app.py +31 -6
Dockerfile CHANGED
@@ -2,6 +2,12 @@ FROM python:3.9-slim
2
 
3
  WORKDIR /app
4
 
 
 
 
 
 
 
5
  # Copy requirements first to leverage Docker cache
6
  COPY requirements.txt .
7
  RUN pip install --no-cache-dir -r requirements.txt
@@ -9,8 +15,14 @@ RUN pip install --no-cache-dir -r requirements.txt
9
  # Copy application code
10
  COPY . .
11
 
12
- # Create upload directory
13
- RUN mkdir -p static/uploads
 
 
 
 
 
 
14
 
15
  # Expose port for Hugging Face Spaces (uses port 7860)
16
  EXPOSE 7860
 
2
 
3
  WORKDIR /app
4
 
5
+ # Install system dependencies
6
+ RUN apt-get update && \
7
+ apt-get install -y --no-install-recommends \
8
+ build-essential \
9
+ && rm -rf /var/lib/apt/lists/*
10
+
11
  # Copy requirements first to leverage Docker cache
12
  COPY requirements.txt .
13
  RUN pip install --no-cache-dir -r requirements.txt
 
15
  # Copy application code
16
  COPY . .
17
 
18
+ # Create upload directory with proper permissions
19
+ RUN mkdir -p static/uploads && \
20
+ chmod -R 777 static/uploads
21
+
22
+ # Set environment variables for Hugging Face
23
+ ENV PYTHONUNBUFFERED=1
24
+ ENV HOST=0.0.0.0
25
+ ENV PORT=7860
26
 
27
  # Expose port for Hugging Face Spaces (uses port 7860)
28
  EXPOSE 7860
app.py CHANGED
@@ -28,6 +28,13 @@ app.add_middleware(
28
  UPLOAD_DIR = Path("static/uploads")
29
  UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
30
 
 
 
 
 
 
 
 
31
  # Mount static directory
32
  app.mount("/static", StaticFiles(directory="static"), name="static")
33
 
@@ -139,8 +146,29 @@ async def upload_image(request: Request, file: UploadFile = File(...)):
139
  file_path = UPLOAD_DIR / unique_filename
140
 
141
  # Save the file
142
- with file_path.open("wb") as buffer:
143
- shutil.copyfileobj(file.file, buffer)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
 
145
  # Return the file URL and embed code
146
  file_url = f"/static/uploads/{unique_filename}"
@@ -221,7 +249,4 @@ async def health_check():
221
 
222
  if __name__ == "__main__":
223
  # For local development
224
- uvicorn.run("app:app", host="127.0.0.1", port=8000, reload=True)
225
-
226
- # For production/Hugging Face (uncomment when deploying)
227
- # uvicorn.run("app:app", host="0.0.0.0", port=7860)
 
28
  UPLOAD_DIR = Path("static/uploads")
29
  UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
30
 
31
+ # Ensure upload directory has proper permissions (critical for Docker containers)
32
+ try:
33
+ import os
34
+ os.chmod(UPLOAD_DIR, 0o777) # Full permissions for uploads directory
35
+ except Exception as e:
36
+ print(f"Warning: Could not set permissions on uploads directory: {e}")
37
+
38
  # Mount static directory
39
  app.mount("/static", StaticFiles(directory="static"), name="static")
40
 
 
146
  file_path = UPLOAD_DIR / unique_filename
147
 
148
  # Save the file
149
+ try:
150
+ # Make sure parent directory exists with proper permissions
151
+ file_path.parent.mkdir(parents=True, exist_ok=True)
152
+ try:
153
+ os.chmod(file_path.parent, 0o777)
154
+ except Exception as e:
155
+ print(f"Warning: Could not set permissions: {e}")
156
+
157
+ # Save file contents
158
+ with open(file_path, "wb") as buffer:
159
+ shutil.copyfileobj(file.file, buffer)
160
+
161
+ # Set permissions on the file itself
162
+ try:
163
+ os.chmod(file_path, 0o666) # Read/write for everyone
164
+ except Exception as e:
165
+ print(f"Warning: Could not set file permissions: {e}")
166
+
167
+ except Exception as e:
168
+ raise HTTPException(
169
+ status_code=500,
170
+ detail=f"Failed to save file: {str(e)}"
171
+ )
172
 
173
  # Return the file URL and embed code
174
  file_url = f"/static/uploads/{unique_filename}"
 
249
 
250
  if __name__ == "__main__":
251
  # For local development
252
+ uvicorn.run("app:app", host="127.0.0.1", port=8000, reload=True)