Spaces:
Runtime error

Gaykar commited on
Commit
8afbe1f
·
1 Parent(s): 8ab6880

celary task

Browse files
Files changed (2) hide show
  1. Dockerfile +4 -14
  2. 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 port FastAPI will run on
29
  EXPOSE 7860
30
 
31
- # Start the FastAPI app on port 7860 (HF Spaces default)
32
- CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
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
- import asyncio
2
- from concurrent.futures import ThreadPoolExecutor
3
- from fastapi import FastAPI, UploadFile, File
4
- from cnn_pipeline.pipeline import classify_tumor
5
- from contextlib import asynccontextmanager
6
 
7
- @asynccontextmanager
8
- async def lifespan(app: FastAPI):
9
- yield # The app is running at this point
10
- executor.shutdown(wait=True)
11
 
12
- app = FastAPI(title="Brain Tumor CNN Service",lifespan=lifespan)
 
 
 
 
 
13
 
14
- torch.set_num_threads(1)
15
- # its okay to use thead pool (prototype level) as pytorch surpasses GIL
16
- executor=ThreadPoolExecutor(max_workers=2)
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')