prince1604 commited on
Commit ·
d624fe1
1
Parent(s): 7eeeb7a
🚀 Deployment: Final HF optimization (UID 1000 + startup logging)
Browse files- Dockerfile +17 -14
- 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
|
| 9 |
COPY requirements.txt /code/requirements.txt
|
| 10 |
|
| 11 |
-
# Install
|
| 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
|
| 17 |
COPY . /code
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
| 31 |
EXPOSE 7860
|
| 32 |
|
| 33 |
-
#
|
| 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 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|