prince1604 commited on
Commit
d624fe1
·
1 Parent(s): 7eeeb7a

🚀 Deployment: Final HF optimization (UID 1000 + startup logging)

Browse files
Files changed (2) hide show
  1. Dockerfile +17 -14
  2. api.py +17 -7
Dockerfile CHANGED
@@ -1,34 +1,37 @@
1
  # Use the official Playwright Python image
2
- # This image includes Python, Playwright, and the necessary browser binaries
3
  FROM mcr.microsoft.com/playwright/python:v1.40.0-jammy
4
 
 
 
 
 
 
 
 
5
  # Set up a working directory
6
  WORKDIR /code
7
 
8
- # Copy requirements file first to leverage Docker cacheA
9
  COPY requirements.txt /code/requirements.txt
10
 
11
- # Install Python dependencies
12
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
13
  RUN pip install playwright-stealth
14
  RUN playwright install --with-deps chromium
15
 
16
- # Copy the rest of the application code
17
  COPY . /code
18
 
19
- # Create a non-root user (Hugging Face Spaces recommendation)
20
- # We let the system pick the UID (likely 1001) since 1000 is already taken in this image
21
- RUN useradd -m user
22
-
23
- # Change ownership of the application directory to the non-root user
24
  RUN chown -R user:user /code
25
-
26
  USER user
27
- ENV HOME=/home/user \
28
- PATH=/home/user/.local/bin:$PATH
29
 
30
- # Expose port 7860 for Hugging Face Spaces
 
 
 
31
  EXPOSE 7860
32
 
33
- # Command to run the application using Uvicorn
34
  CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "7860"]
 
1
  # Use the official Playwright Python image
 
2
  FROM mcr.microsoft.com/playwright/python:v1.40.0-jammy
3
 
4
+ # Set Environment Variables
5
+ ENV PYTHONUNBUFFERED=1 \
6
+ PYTHONDONTWRITEBYTECODE=1 \
7
+ PIP_NO_CACHE_DIR=off \
8
+ PIP_DISABLE_PIP_VERSION_CHECK=on \
9
+ DEFAULT_PORT=7860
10
+
11
  # Set up a working directory
12
  WORKDIR /code
13
 
14
+ # Copy requirements file
15
  COPY requirements.txt /code/requirements.txt
16
 
17
+ # Install dependencies
18
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
19
  RUN pip install playwright-stealth
20
  RUN playwright install --with-deps chromium
21
 
22
+ # Copy the application code
23
  COPY . /code
24
 
25
+ # Set permissions for Hugging Face (UID 1000)
26
+ RUN useradd -m -u 1000 user || echo "User already exists"
 
 
 
27
  RUN chown -R user:user /code
 
28
  USER user
 
 
29
 
30
+ # Ensure the static directory exists and is writable
31
+ RUN mkdir -p /code/static
32
+
33
+ # Expose the HF port
34
  EXPOSE 7860
35
 
36
+ # Run the app
37
  CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "7860"]
api.py CHANGED
@@ -100,13 +100,23 @@ class ScheduledCrawler(threading.Thread):
100
  # --- Startup Event ---
101
  @app.on_event("startup")
102
  async def startup_event():
103
- # Start Background Threads
104
- logger.info("Starting background services...")
105
- pinger = KeepAlive(interval=300)
106
- pinger.start()
107
-
108
- scheduler = ScheduledCrawler(interval=14400)
109
- scheduler.start()
 
 
 
 
 
 
 
 
 
 
110
 
111
  # --- Core Logic ---
112
 
 
100
  # --- Startup Event ---
101
  @app.on_event("startup")
102
  async def startup_event():
103
+ logger.info("--- API STARTUP: Initializing Services ---")
104
+ try:
105
+ # Check permissions
106
+ if not os.path.exists("static"):
107
+ logger.info("Creating static directory...")
108
+ os.makedirs("static", exist_ok=True)
109
+
110
+ # Start Background Threads
111
+ logger.info("Starting background services...")
112
+ pinger = KeepAlive(interval=300)
113
+ pinger.start()
114
+
115
+ scheduler = ScheduledCrawler(interval=14400)
116
+ scheduler.start()
117
+ logger.info("--- API STARTUP: Ready ---")
118
+ except Exception as e:
119
+ logger.error(f"FATAL STARTUP ERROR: {e}")
120
 
121
  # --- Core Logic ---
122