imravi commited on
Commit
52274a4
·
verified ·
1 Parent(s): 804c7fb

updated dockerfile to use download the model.

Browse files
Files changed (1) hide show
  1. Dockerfile +48 -14
Dockerfile CHANGED
@@ -7,22 +7,24 @@ WORKDIR /app
7
  # Install system dependencies
8
  RUN apt-get update && apt-get install -y \
9
  build-essential \
 
10
  && rm -rf /var/lib/apt/lists/*
11
 
12
  # Create a non-root user
13
  RUN useradd -m -u 1000 appuser
14
 
15
  # Create cache directories and set permissions
16
- RUN mkdir -p /.cache/huggingface && \
 
 
17
  mkdir -p /.cache/torch && \
18
- mkdir -p /.cache/sentence_transformers && \
19
- chown -R appuser:appuser /.cache && \
20
- chmod -R 777 /.cache
21
 
22
- # Set environment variables for cache locations
23
  ENV HF_HOME="/.cache/huggingface"
24
  ENV TORCH_HOME="/.cache/torch"
25
- ENV SENTENCE_TRANSFORMERS_HOME="/.cache/sentence_transformers"
 
26
 
27
  # Install Python dependencies
28
  RUN pip install --no-cache-dir \
@@ -39,12 +41,39 @@ RUN pip install --no-cache-dir \
39
  huggingface_hub \
40
  accelerate>=0.26.0
41
 
42
- # Copy the necessary files into the container
43
- COPY plant_data_chunks_and_embeddings.csv /app/plant_data_chunks_and_embeddings.csv
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  COPY main.py /app/main.py
 
 
45
 
46
- # Set proper permissions for the app directory
47
- RUN chown -R appuser:appuser /app
 
 
 
 
 
48
 
49
  # Switch to non-root user
50
  USER appuser
@@ -52,8 +81,13 @@ USER appuser
52
  # Set the environment variable to point to the app directory
53
  ENV PYTHONPATH=/app
54
 
55
- # Expose the port the app will run on
56
- EXPOSE 5000
 
 
 
 
 
57
 
58
- # Command to run the app using Uvicorn
59
- CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "5000"]
 
7
  # Install system dependencies
8
  RUN apt-get update && apt-get install -y \
9
  build-essential \
10
+ git \
11
  && rm -rf /var/lib/apt/lists/*
12
 
13
  # Create a non-root user
14
  RUN useradd -m -u 1000 appuser
15
 
16
  # Create cache directories and set permissions
17
+ RUN mkdir -p /app/models/sentence_transformer && \
18
+ mkdir -p /app/models/qwen && \
19
+ mkdir -p /.cache/huggingface && \
20
  mkdir -p /.cache/torch && \
21
+ mkdir -p /.cache/sentence_transformers
 
 
22
 
23
+ # Set environment variables for cache and model locations
24
  ENV HF_HOME="/.cache/huggingface"
25
  ENV TORCH_HOME="/.cache/torch"
26
+ ENV SENTENCE_TRANSFORMERS_HOME="/app/models/sentence_transformer"
27
+ ENV TRANSFORMERS_OFFLINE=1
28
 
29
  # Install Python dependencies
30
  RUN pip install --no-cache-dir \
 
41
  huggingface_hub \
42
  accelerate>=0.26.0
43
 
44
+ # Create a script to download models
45
+ RUN echo 'import os\n\
46
+ from sentence_transformers import SentenceTransformer\n\
47
+ from transformers import AutoTokenizer, AutoModelForCausalLM\n\
48
+ \n\
49
+ # Download and save sentence transformer model\n\
50
+ model = SentenceTransformer("sentence-transformers/all-mpnet-base-v2")\n\
51
+ model.save("/app/models/sentence_transformer")\n\
52
+ \n\
53
+ # Download and save Qwen model and tokenizer\n\
54
+ model_name = "Qwen/Qwen2.5-1.5B-Instruct"\n\
55
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)\n\
56
+ model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, torch_dtype="auto")\n\
57
+ \n\
58
+ tokenizer.save_pretrained("/app/models/qwen")\n\
59
+ model.save_pretrained("/app/models/qwen")\n\
60
+ print("Models downloaded successfully!")' > download_models.py
61
+
62
+ # Download models during build
63
+ RUN python download_models.py
64
+
65
+ # Modify the main.py to use local models
66
  COPY main.py /app/main.py
67
+ RUN sed -i 's|"sentence-transformers/all-mpnet-base-v2"|"/app/models/sentence_transformer"|g' main.py && \
68
+ sed -i 's|"Qwen/Qwen2.5-1.5B-Instruct"|"/app/models/qwen"|g' main.py
69
 
70
+ # Copy the data file
71
+ COPY plant_data_chunks_and_embeddings.csv /app/plant_data_chunks_and_embeddings.csv
72
+
73
+ # Set proper permissions
74
+ RUN chown -R appuser:appuser /app && \
75
+ chown -R appuser:appuser /.cache && \
76
+ chmod -R 755 /app/models
77
 
78
  # Switch to non-root user
79
  USER appuser
 
81
  # Set the environment variable to point to the app directory
82
  ENV PYTHONPATH=/app
83
 
84
+ # Expose the port
85
+ EXPOSE 8000
86
+
87
+ # Reduce model loading time by setting specific environment variables
88
+ ENV OMP_NUM_THREADS=1
89
+ ENV MKL_NUM_THREADS=1
90
+ ENV TORCH_NUM_THREADS=1
91
 
92
+ # Command to run the app
93
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "5000", "--timeout-keep-alive", "300"]