Ehrii commited on
Commit
83c9669
·
1 Parent(s): 7c93a3d

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +19 -21
Dockerfile CHANGED
@@ -1,46 +1,44 @@
1
- # Use a stable Python base image
2
- FROM python:3.9-slim
3
 
4
  # Set environment variables
5
  ENV PYTHONUNBUFFERED=1 \
6
  LANG=C.UTF-8 \
7
- HF_HOME="/app/huggingface_cache" \
8
- HUGGINGFACE_HUB_CACHE="/app/huggingface_cache"
9
 
10
  # Set working directory
11
  WORKDIR /app
12
 
13
- # Copy the required files
14
  COPY requirements.txt .
15
- COPY main.py .
16
 
17
  # Install dependencies using a virtual environment
 
 
18
  RUN python -m venv /app/venv && \
19
  /app/venv/bin/pip install --no-cache-dir --upgrade pip && \
20
  /app/venv/bin/pip install --no-cache-dir -r requirements.txt
21
 
22
- # Ensure the model cache directory exists
23
- RUN mkdir -p $HF_HOME
 
 
 
24
 
25
- # Add Hugging Face token as an environment variable in the container (this will be injected by Hugging Face Spaces' secrets management)
26
  ARG HF_TOKEN
27
  ENV HF_TOKEN=${HF_TOKEN}
28
 
29
- ENV HF_HOME="/tmp/huggingface_cache"
30
- ENV HUGGINGFACE_HUB_CACHE="/tmp/huggingface_cache"
31
-
32
- RUN mkdir -p $HF_HOME && chmod -R 777 $HF_HOME
33
-
34
-
35
- # Pre-download models (handle errors gracefully)
36
  RUN /app/venv/bin/python -c "from transformers import pipeline; \
37
- pipeline('sentiment-analysis', model='tabularisai/multilingual-sentiment-analysis', use_auth_token='$HF_TOKEN')" || echo 'Failed to download model 1'
38
 
39
  RUN /app/venv/bin/python -c "from transformers import pipeline; \
40
- pipeline('sentiment-analysis', model='siebert/sentiment-roberta-large-english', use_auth_token='$HF_TOKEN')" || echo 'Failed to download model 2'
41
 
42
- # Expose port for FastAPI
43
  EXPOSE 7860
44
 
45
- # Run FastAPI server using virtual environment
46
- CMD ["/app/venv/bin/uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
1
+ # Use the official Python 3.9 slim image
2
+ FROM python:3.9-slim AS base
3
 
4
  # Set environment variables
5
  ENV PYTHONUNBUFFERED=1 \
6
  LANG=C.UTF-8 \
7
+ HF_HOME="/tmp/huggingface_cache" \
8
+ HUGGINGFACE_HUB_CACHE="/tmp/huggingface_cache"
9
 
10
  # Set working directory
11
  WORKDIR /app
12
 
13
+ # Copy requirements file
14
  COPY requirements.txt .
 
15
 
16
  # Install dependencies using a virtual environment
17
+ RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
18
+
19
  RUN python -m venv /app/venv && \
20
  /app/venv/bin/pip install --no-cache-dir --upgrade pip && \
21
  /app/venv/bin/pip install --no-cache-dir -r requirements.txt
22
 
23
+ # Ensure the Hugging Face cache directory exists
24
+ RUN mkdir -p $HF_HOME && chmod -R 777 $HF_HOME
25
+
26
+ # Copy application files
27
+ COPY main.py .
28
 
29
+ # Set up Hugging Face token as a build argument (secured via environment variables)
30
  ARG HF_TOKEN
31
  ENV HF_TOKEN=${HF_TOKEN}
32
 
33
+ # Pre-download models
 
 
 
 
 
 
34
  RUN /app/venv/bin/python -c "from transformers import pipeline; \
35
+ pipeline('sentiment-analysis', model='Ehrii/sentiment', use_auth_token='$HF_TOKEN')" || echo 'Failed to download multilingual model'
36
 
37
  RUN /app/venv/bin/python -c "from transformers import pipeline; \
38
+ pipeline('sentiment-analysis', model='siebert/sentiment-roberta-large-english', use_auth_token='$HF_TOKEN')" || echo 'Failed to download English model'
39
 
40
+ # Expose FastAPI port
41
  EXPOSE 7860
42
 
43
+ # Run FastAPI server using the virtual environment
44
+ CMD ["/app/venv/bin/uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]