celary task
Browse files- Dockerfile +4 -14
- main.py +18 -28
Dockerfile
CHANGED
|
@@ -1,32 +1,22 @@
|
|
| 1 |
FROM python:3.12-slim
|
| 2 |
|
| 3 |
-
# Set the working directory to /code
|
| 4 |
WORKDIR /code
|
| 5 |
|
| 6 |
-
# Copy the requirements.txt file into the container
|
| 7 |
COPY requirements.txt .
|
| 8 |
-
|
| 9 |
-
# Install the dependencies
|
| 10 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 11 |
|
| 12 |
-
# Set up a new user named "user" with user ID 1000
|
| 13 |
RUN useradd -m -u 1000 user
|
| 14 |
-
|
| 15 |
-
# Switch to the "user" user
|
| 16 |
USER user
|
| 17 |
|
| 18 |
-
# Set home to the user's home directory
|
| 19 |
ENV HOME=/home/user \
|
| 20 |
PATH=/home/user/.local/bin:$PATH
|
| 21 |
|
| 22 |
-
# Set the working directory to the user's home directory
|
| 23 |
WORKDIR $HOME/app
|
| 24 |
-
|
| 25 |
-
# Copy the current directory contents into the container at $HOME/app
|
| 26 |
COPY --chown=user . $HOME/app
|
| 27 |
|
| 28 |
-
# Expose the
|
| 29 |
EXPOSE 7860
|
| 30 |
|
| 31 |
-
# Start
|
| 32 |
-
|
|
|
|
|
|
| 1 |
FROM python:3.12-slim
|
| 2 |
|
|
|
|
| 3 |
WORKDIR /code
|
| 4 |
|
|
|
|
| 5 |
COPY requirements.txt .
|
|
|
|
|
|
|
| 6 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 7 |
|
|
|
|
| 8 |
RUN useradd -m -u 1000 user
|
|
|
|
|
|
|
| 9 |
USER user
|
| 10 |
|
|
|
|
| 11 |
ENV HOME=/home/user \
|
| 12 |
PATH=/home/user/.local/bin:$PATH
|
| 13 |
|
|
|
|
| 14 |
WORKDIR $HOME/app
|
|
|
|
|
|
|
| 15 |
COPY --chown=user . $HOME/app
|
| 16 |
|
| 17 |
+
# Expose the mandatory Hugging Face port
|
| 18 |
EXPOSE 7860
|
| 19 |
|
| 20 |
+
# 1. Start a fake web server on port 7860 to pass the HF health check
|
| 21 |
+
# 2. Run the pure Celery worker and beat (beat is required for the periodic heartbeat)
|
| 22 |
+
CMD python3 -m http.server 7860 & celery -A main.celery_app worker --beat --loglevel=info
|
main.py
CHANGED
|
@@ -1,32 +1,22 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
from fastapi import FastAPI, UploadFile, File
|
| 4 |
-
from cnn_pipeline.pipeline import classify_tumor
|
| 5 |
-
from contextlib import asynccontextmanager
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
executor.shutdown(wait=True)
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
@app.post("/classify")
|
| 20 |
-
async def classify(file: UploadFile = File(...)):
|
| 21 |
-
|
| 22 |
-
# read the fiel content
|
| 23 |
-
image_bytes = await file.read()
|
| 24 |
-
|
| 25 |
-
# get the event Loop
|
| 26 |
-
loop = asyncio.get_running_loop()
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
result = await loop.run_in_executor(executor, classify_tumor, image_bytes)
|
| 30 |
-
|
| 31 |
-
return result
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from celery import Celery
|
| 2 |
+
import logging
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Set up logging so you can see outputs in the HF Spaces log tab
|
| 5 |
+
logging.basicConfig(level=logging.INFO)
|
| 6 |
+
logger = logging.getLogger(__name__)
|
|
|
|
| 7 |
|
| 8 |
+
# Celery isolated entirely to memory
|
| 9 |
+
celery_app = Celery(
|
| 10 |
+
"tasks",
|
| 11 |
+
broker="memory://",
|
| 12 |
+
backend="cache+memory://"
|
| 13 |
+
)
|
| 14 |
|
| 15 |
+
@celery_app.task
|
| 16 |
+
def heartbeat_test():
|
| 17 |
+
logger.info("--- CELERY WORKER IS ALIVE AND WORKING ON HF SPACES ---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Automatically trigger the heartbeat task every 5 seconds to test execution
|
| 20 |
+
@celery_app.on_after_configure.connect
|
| 21 |
+
def setup_periodic_tasks(sender, **kwargs):
|
| 22 |
+
sender.add_periodic_task(5.0, heartbeat_test.s(), name='check-every-5-seconds')
|