apexherbert200 commited on
Commit
c8a2d9e
·
1 Parent(s): d345be4

Done with readme

Browse files
Files changed (2) hide show
  1. Dockerfile +23 -13
  2. preprocessing.py +9 -0
Dockerfile CHANGED
@@ -1,8 +1,11 @@
1
  FROM python:3.11-slim
2
 
3
  # Do not write .pyc files and ensure output is sent straight to the terminal
4
- ENV PYTHONDONTWRITEBYTECODE=1
5
- ENV PYTHONUNBUFFERED=1
 
 
 
6
 
7
  # Install small set of system dependencies that are commonly required
8
  RUN apt-get update && apt-get install -y --no-install-recommends \
@@ -13,24 +16,31 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
13
 
14
  WORKDIR /app
15
 
16
- # Copy requirements first to leverage Docker layer caching
17
- COPY requirements.txt /app/requirements.txt
 
 
 
 
 
 
 
 
 
18
 
19
  # Upgrade pip and install python dependencies
20
  RUN pip install --upgrade pip setuptools wheel && \
21
  pip install --no-cache-dir -r /app/requirements.txt
22
 
23
- # Copy application code
24
- COPY . /app
25
 
26
- # Create a non-root user and switch to it (best-effort, some distros already have adduser)
27
- RUN addgroup --system app && adduser --system --ingroup app app || true
28
- USER app
29
 
30
- # Expose the port that uvicorn will listen on
31
- EXPOSE 80
32
 
33
  # Start the FastAPI app with uvicorn (exec form is preferred)
34
- # main:app -> the FastAPI instance in main.py
35
- CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
36
 
 
1
  FROM python:3.11-slim
2
 
3
  # Do not write .pyc files and ensure output is sent straight to the terminal
4
+ ENV PYTHONDONTWRITEBYTECODE=1 \
5
+ PYTHONUNBUFFERED=1 \
6
+ HF_HOME=/app/.cache/huggingface \
7
+ TRANSFORMERS_CACHE=/app/.cache/huggingface \
8
+ XDG_CACHE_HOME=/app/.cache
9
 
10
  # Install small set of system dependencies that are commonly required
11
  RUN apt-get update && apt-get install -y --no-install-recommends \
 
16
 
17
  WORKDIR /app
18
 
19
+ # Create cache directory and make it writable
20
+ RUN mkdir -p /app/.cache/huggingface && chmod -R 777 /app/.cache
21
+
22
+ # Create a non-root user and ensure ownership
23
+ RUN addgroup --system app && adduser --system --ingroup app app && chown -R app:app /app
24
+
25
+ # Copy requirements first to leverage Docker layer caching and set ownership
26
+ COPY --chown=app:app requirements.txt /app/requirements.txt
27
+
28
+ # Switch to non-root user
29
+ USER app
30
 
31
  # Upgrade pip and install python dependencies
32
  RUN pip install --upgrade pip setuptools wheel && \
33
  pip install --no-cache-dir -r /app/requirements.txt
34
 
35
+ # Pre-download the model during build to avoid runtime downloads
36
+ RUN python -c "from transformers import pipeline; pipeline('zero-shot-classification', model='MoritzLaurer/DeBERTa-v3-base-mnli-fever-anli', device=-1)"
37
 
38
+ # Copy the rest of the application code
39
+ COPY --chown=app:app . /app
 
40
 
41
+ # Expose the port that uvicorn will listen on (Spaces default is 7860)
42
+ EXPOSE 7860
43
 
44
  # Start the FastAPI app with uvicorn (exec form is preferred)
45
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", "--log-level", "info"]
 
46
 
preprocessing.py CHANGED
@@ -1,3 +1,12 @@
 
 
 
 
 
 
 
 
 
1
  from transformers import pipeline
2
 
3
  # Create the pipeline directly with the model name
 
1
+ import os
2
+
3
+ # Ensure Hugging Face caches are set to a writable location inside the container
4
+ os.environ.setdefault("HF_HOME", "/app/.cache/huggingface")
5
+ os.environ.setdefault("TRANSFORMERS_CACHE", "/app/.cache/huggingface")
6
+ os.environ.setdefault("XDG_CACHE_HOME", "/app/.cache")
7
+
8
+ os.makedirs(os.environ["HF_HOME"], exist_ok=True)
9
+
10
  from transformers import pipeline
11
 
12
  # Create the pipeline directly with the model name