tlong-ds commited on
Commit
060703b
·
1 Parent(s): bba1e36
Dockerfile CHANGED
@@ -1,29 +1,36 @@
1
  # Use Python 3.9 slim image as base
2
  FROM python:3.9-slim
3
 
4
- # Set working directory
5
  WORKDIR /app
6
 
7
- # Set environment variables
8
- ENV PYTHONUNBUFFERED=1 \
9
- PYTHONDONTWRITEBYTECODE=1
10
-
11
- # Install system dependencies
12
- RUN apt-get update && \
13
- apt-get install -y --no-install-recommends \
14
- build-essential \
15
- curl \
16
- && rm -rf /var/lib/apt/lists/*
17
-
18
- # Copy requirements file
19
  COPY requirements.txt .
20
-
21
- # Install Python dependencies
22
- RUN pip install --no-cache-dir -r requirements.txt
23
-
24
- # Copy the rest of the application
 
 
 
 
 
 
 
 
25
  COPY . .
26
 
 
 
 
 
 
 
 
 
 
 
27
  # Expose the port
28
  EXPOSE 7860
29
 
 
1
  # Use Python 3.9 slim image as base
2
  FROM python:3.9-slim
3
 
4
+ # Create app directory
5
  WORKDIR /app
6
 
7
+ # Copy requirements and install dependencies
 
 
 
 
 
 
 
 
 
 
 
8
  COPY requirements.txt .
9
+ RUN pip install -r requirements.txt
10
+
11
+ # Create cache directories with proper permissions
12
+ RUN mkdir -p /tmp/huggingface \
13
+ && mkdir -p /tmp/transformers \
14
+ && mkdir -p /tmp/sentence-transformers \
15
+ && mkdir -p /tmp/matplotlib \
16
+ && chmod 777 /tmp/huggingface \
17
+ && chmod 777 /tmp/transformers \
18
+ && chmod 777 /tmp/sentence-transformers \
19
+ && chmod 777 /tmp/matplotlib
20
+
21
+ # Copy application code
22
  COPY . .
23
 
24
+ # Set environment variables
25
+ ENV HF_HOME=/tmp/huggingface
26
+ ENV TRANSFORMERS_CACHE=/tmp/transformers
27
+ ENV SENTENCE_TRANSFORMERS_HOME=/tmp/sentence-transformers
28
+ ENV MPLCONFIGDIR=/tmp/matplotlib
29
+ ENV TF_ENABLE_ONEDNN_OPTS=0
30
+
31
+ # Preload models during build
32
+ RUN python -c "from services.api.chatbot.model_init import embedding_model"
33
+
34
  # Expose the port
35
  EXPOSE 7860
36
 
services/api/chatbot/model_init.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from sentence_transformers import SentenceTransformer
3
+ from langchain_community.embeddings import HuggingFaceEmbeddings
4
+
5
+ # Set cache directories
6
+ os.environ['HF_HOME'] = '/tmp/huggingface'
7
+ os.environ['TRANSFORMERS_CACHE'] = '/tmp/transformers'
8
+ os.environ['SENTENCE_TRANSFORMERS_HOME'] = '/tmp/sentence-transformers'
9
+
10
+ # Initialize model singleton
11
+ def init_embedding_model():
12
+ return HuggingFaceEmbeddings(
13
+ model_name="sentence-transformers/all-MiniLM-L6-v2",
14
+ cache_folder="/tmp/transformers",
15
+ model_kwargs={'device': 'cpu'} # Force CPU usage for better compatibility
16
+ )
17
+
18
+ # Create global instance
19
+ embedding_model = init_embedding_model()
services/api/chatbot/retrieval.py CHANGED
@@ -24,7 +24,7 @@ from .config import (
24
  SENTENCE_TRANSFORMERS_HOME,
25
  MPLCONFIGDIR
26
  )
27
-
28
 
29
  load_dotenv()
30
 
@@ -35,12 +35,6 @@ MYSQL_HOST = os.getenv("MYSQL_HOST")
35
  MYSQL_DB = os.getenv("MYSQL_DB")
36
  MYSQL_PORT = int(os.getenv("MYSQL_PORT", 3306))
37
 
38
- # Initialize embedding model with a more compatible configuration
39
- embedding_model = HuggingFaceEmbeddings(
40
- model_name="sentence-transformers/all-MiniLM-L6-v2",
41
- model_kwargs={'device': 'cpu'},
42
- encode_kwargs={'normalize_embeddings': True}
43
- )
44
  client = qdrant_client.QdrantClient(QDRANT_HOST, api_key=QDRANT_API_KEY)
45
 
46
  def connect_db():
 
24
  SENTENCE_TRANSFORMERS_HOME,
25
  MPLCONFIGDIR
26
  )
27
+ from .model_init import embedding_model
28
 
29
  load_dotenv()
30
 
 
35
  MYSQL_DB = os.getenv("MYSQL_DB")
36
  MYSQL_PORT = int(os.getenv("MYSQL_PORT", 3306))
37
 
 
 
 
 
 
 
38
  client = qdrant_client.QdrantClient(QDRANT_HOST, api_key=QDRANT_API_KEY)
39
 
40
  def connect_db():