imravi commited on
Commit
1c6e0d5
·
verified ·
1 Parent(s): be8f9e6

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +8 -104
Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- # Use an official PyTorch image as a base
2
  FROM pytorch/pytorch:2.1.0-cuda11.8-cudnn8-runtime
3
 
4
  # Set the working directory inside the container
@@ -7,24 +7,8 @@ WORKDIR /app
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/llm && \
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
-
28
  # Install Python dependencies
29
  RUN pip install --no-cache-dir \
30
  pandas \
@@ -37,97 +21,17 @@ RUN pip install --no-cache-dir \
37
  uvicorn[standard] \
38
  pydantic \
39
  python-multipart \
40
- huggingface_hub \
41
- accelerate>=0.26.0
42
-
43
- # Create a script to download models
44
- COPY <<EOF /app/download_models.py
45
- import os
46
- import gc
47
- import torch
48
- from sentence_transformers import SentenceTransformer
49
- from transformers import AutoTokenizer, AutoModelForCausalLM
50
-
51
- def download_and_save_model(model_name, save_path, is_sentence_transformer=False):
52
- try:
53
- print(f"Downloading {model_name}...")
54
- if is_sentence_transformer:
55
- model = SentenceTransformer(model_name)
56
- model.save(save_path)
57
- else:
58
- tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
59
- tokenizer.save_pretrained(save_path)
60
-
61
- # Load model with memory optimizations
62
- model = AutoModelForCausalLM.from_pretrained(
63
- model_name,
64
- trust_remote_code=True,
65
- torch_dtype=torch.float16,
66
- low_cpu_mem_usage=True,
67
- device_map="auto"
68
- )
69
- model.save_pretrained(save_path)
70
-
71
- print(f"Successfully saved {model_name}")
72
-
73
- # Clean up memory
74
- del model
75
- if not is_sentence_transformer:
76
- del tokenizer
77
- torch.cuda.empty_cache()
78
- gc.collect()
79
-
80
- except Exception as e:
81
- print(f"Error downloading {model_name}: {str(e)}")
82
- raise
83
-
84
- # Download sentence transformer model
85
- download_and_save_model(
86
- "sentence-transformers/all-mpnet-base-v2",
87
- "/app/models/sentence_transformer",
88
- is_sentence_transformer=True
89
- )
90
-
91
- # Download LLM (using a smaller model)
92
- download_and_save_model(
93
- "microsoft/phi-2", # Using Phi-2 instead of Qwen as it's smaller
94
- "/app/models/llm"
95
- )
96
- EOF
97
-
98
- # Download models during build
99
- RUN python /app/download_models.py
100
 
101
- # Only set TRANSFORMERS_OFFLINE after downloading models
102
- ENV TRANSFORMERS_OFFLINE=1
103
-
104
- # Copy the main.py and modify it to use local paths
105
- COPY main.py /app/main.py
106
- RUN sed -i 's|"sentence-transformers/all-mpnet-base-v2"|"/app/models/sentence_transformer"|g' main.py && \
107
- sed -i 's|"Qwen/Qwen2.5-1.5B-Instruct"|"/app/models/llm"|g' main.py
108
-
109
- # Copy the data file
110
  COPY plant_data_chunks_and_embeddings.csv /app/plant_data_chunks_and_embeddings.csv
111
-
112
- # Set proper permissions
113
- RUN chown -R appuser:appuser /app && \
114
- chown -R appuser:appuser /.cache && \
115
- chmod -R 755 /app/models
116
-
117
- # Switch to non-root user
118
- USER appuser
119
 
120
  # Set the environment variable to point to the app directory
121
  ENV PYTHONPATH=/app
122
 
123
- # Expose the port
124
- EXPOSE 8000
125
-
126
- # Optimize memory usage
127
- ENV OMP_NUM_THREADS=1
128
- ENV MKL_NUM_THREADS=1
129
- ENV TORCH_NUM_THREADS=1
130
- ENV MALLOC_TRIM_THRESHOLD_=0
131
 
132
- # Command to run the app
133
- CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "5000", "--timeout-keep-alive", "300"]
 
1
+ # Use an official PyTorch image as a base (includes CUDA for GPU support if available)
2
  FROM pytorch/pytorch:2.1.0-cuda11.8-cudnn8-runtime
3
 
4
  # Set the working directory inside the container
 
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
  # Install Python dependencies
13
  RUN pip install --no-cache-dir \
14
  pandas \
 
21
  uvicorn[standard] \
22
  pydantic \
23
  python-multipart \
24
+ huggingface_hub
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ # Copy the necessary files into the container
 
 
 
 
 
 
 
 
27
  COPY plant_data_chunks_and_embeddings.csv /app/plant_data_chunks_and_embeddings.csv
28
+ COPY main.py /app/main.py
 
 
 
 
 
 
 
29
 
30
  # Set the environment variable to point to the app directory
31
  ENV PYTHONPATH=/app
32
 
33
+ # Expose the port the app will run on
34
+ EXPOSE 5000
 
 
 
 
 
 
35
 
36
+ # Command to run the app using Uvicorn (ASGI server for FastAPI)
37
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "5000"]