Skip to content

Commit f850e36

Browse files
committed
Add : Docker Support
1 parent b85a871 commit f850e36

6 files changed

Lines changed: 103 additions & 142 deletions

File tree

Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use a lightweight Python base image
2+
FROM python:3.10-slim
3+
4+
# Set the working directory inside the container
5+
WORKDIR /app
6+
7+
# Install system dependencies (required for PyMuPDF and other tools)
8+
RUN apt-get update && apt-get install -y \
9+
build-essential \
10+
&& rm -rf /var/lib/apt/lists/*
11+
12+
# Copy only the requirements first to leverage Docker caching
13+
COPY requirements.txt .
14+
15+
# Install Python dependencies
16+
RUN pip install --no-cache-dir --default-timeout=1000 --retries 5 -i https://pypi.org/simple/ --extra-index-url https://download.pytorch.org/whl/cpu -r requirements.txt
17+
18+
# Copy the rest of your application code
19+
COPY . .
20+
21+
# Expose the port Streamlit runs on
22+
EXPOSE 8501
23+
24+
# Command to run the application
25+
CMD ["streamlit", "run", "main.py", "--server.port=8501", "--server.address=0.0.0.0"]

docker-compose.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
services:
2+
mongodb:
3+
image: mongo:6.0
4+
container_name: hr_mongodb
5+
ports:
6+
- "27017:27017"
7+
volumes:
8+
- mongo_data:/data/db
9+
10+
web:
11+
build:
12+
context: .
13+
network: host
14+
container_name: hr_streamlit
15+
ports:
16+
- "8501:8501"
17+
depends_on:
18+
- mongodb
19+
extra_hosts:
20+
- "host.docker.internal:host-gateway"
21+
22+
volumes:
23+
mongo_data:

main.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@
1919
import streamlit as st
2020
import hashlib
2121

22+
# Variables
23+
MONGO_URL = "mongodb://mongodb:27017/"
24+
OLLAMA_BASE_URL = "http://host.docker.internal:11434"
25+
DB_NAME = "hr_buddy_db"
26+
PDF_PATH = "rag_source/nasscom-hr-manual-2016.pdf"
27+
2228
# Streamlit webpage Setup
23-
st.set_page_config(page_title="HR Buddy", page_icon="🤖")
29+
st.set_page_config(page_title="HR Buddy")
2430
st.title("HR Policy Assistant")
2531

2632
# Cached Setup for loading model and the database
@@ -33,7 +39,7 @@ def initialize_backend():
3339

3440
# Connecting to default local MongoDB Port
3541
print("[INFO] Connecting to local MongoDB...")
36-
client = MongoClient("mongodb://localhost:27017/")
42+
client = MongoClient(MONGO_URL)
3743

3844
# Retrieve Memory Database
3945
db = client["hr_assistant"]
@@ -42,7 +48,7 @@ def initialize_backend():
4248

4349
# Load and Chunk the Data
4450
print("[INFO] Reading HR Policy PDF with PyMuPDF...")
45-
loader = PyMuPDFLoader("rag_source/nasscom-hr-manual-2016.pdf")
51+
loader = PyMuPDFLoader(PDF_PATH)
4652
pages = loader.load()
4753

4854
# Break the dense PDF into small, 1000-character chunks
@@ -53,7 +59,10 @@ def initialize_backend():
5359
print("[INFO] Initializing vector embeddings. This might take a second...")
5460

5561
# Embed and Store in Vector DB
56-
embeddings = OllamaEmbeddings(model="nomic-embed-text")
62+
embeddings = OllamaEmbeddings(model="nomic-embed-text",
63+
base_url=OLLAMA_BASE_URL
64+
)
65+
5766
vectorstore = Chroma.from_documents(documents=chunks, embedding=embeddings)
5867
retriever = vectorstore.as_retriever(search_kwargs={"k": 2})
5968

@@ -176,7 +185,8 @@ def initialize_backend():
176185
with st.chat_message("assistant"):
177186

178187
# Generate the answer using Llama 3.2
179-
response = ollama.chat(model='llama3.2', messages=[{'role': 'user', 'content': rag_prompt}])
188+
ollama_client = ollama.Client(host=OLLAMA_BASE_URL)
189+
response = ollama_client.chat(model='llama3.2', messages=[{'role': 'user', 'content': rag_prompt}])
180190
ai_answer = response['message']['content']
181191
st.markdown(ai_answer)
182192

dependencies.txt renamed to requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ pymupdf
1010
protobuf==3.20.3
1111
pymongo
1212
streamlit
13-
torchvision
14-
hashlib
13+
torchvision

run.sh

Lines changed: 39 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,53 @@
1-
# Variables
2-
PKG_MGR="pip"
3-
FILE="dependencies.txt"
4-
5-
# Check for dependency file
6-
if [ ! -f "$FILE" ]; then
7-
echo "[ERROR] '$FILE' not found."
8-
exit 1
9-
fi
10-
11-
echo "[OK] dependencies.txt found"
12-
13-
# Parser for choosing Package Manager (pip or conda)
14-
while [[ "$#" -gt 0 ]]; do
15-
case $1 in
16-
-m)
17-
PKG_MGR="$2"
18-
shift 2
19-
;;
20-
21-
*)
22-
echo "[ERROR] Unknown Option: $1. Usage ./run.sh [-m pip | conda]"
23-
exit 1
24-
;;
25-
esac
26-
done
27-
28-
echo "[INFO] Selected Package Manager: '$PKG_MGR'"
29-
30-
# Install Dependencies
31-
if [ "$PKG_MGR" == "conda" ]; then
32-
echo "[OK] Starting installation using Conda..."
33-
conda install --file "$FILE" -c conda-forge -y
34-
35-
elif [ "$PKG_MGR" == "pip" ]; then
36-
echo "[OK] Starting installation using Pip..."
37-
pip install -r "$FILE"
38-
1+
#!/bin/bash
2+
3+
# nstall/Check Docker Engine
4+
if ! command -v docker &> /dev/null; then
5+
echo "[INFO] Docker not found. Installing..."
6+
curl -fsSL https://get.docker.com -o get-docker.sh && sudo sh get-docker.sh
7+
sudo usermod -aG docker $USER
8+
echo "[OK] Docker installed. Note: You may need to reboot for permissions."
399
else
40-
echo "[Error] Invalid manager '$PKG_MANAGER'. Please use 'pip' or 'conda'."
41-
exit 1
10+
echo "[OK] Docker is already installed."
4211
fi
4312

44-
# Check for Ollama
45-
echo "[INFO] Verifying Python environment binding..."
46-
if ! python3 -c "import ollama" &> /dev/null; then
47-
echo "[WARNING] Python cannot find 'ollama' library. Forcing strict inline installation..."
48-
python3 -m pip install ollama
49-
50-
if [ $? -ne 0 ]; then
51-
echo "[ERROR] Could not force-install Python ollama library."
52-
exit 1
53-
fi
54-
echo "[OK] 'ollama' bound to current Python environment."
55-
else
56-
echo "[OK] Python recognizes 'ollama'."
57-
fi
13+
# Ensure Docker service is active
14+
sudo systemctl start docker
15+
sudo systemctl enable docker &> /dev/null
5816

59-
# System Ollama Engine Check and Install
60-
echo "[INFO] Checking system Ollama engine..."
17+
# Install/Check Ollama Engine
6118
if ! command -v ollama &> /dev/null; then
62-
echo "[WARNING] Ollama engine not found. Installing now (may require password)..."
19+
echo "[INFO] Ollama not found. Installing..."
6320
curl -fsSL https://ollama.com/install.sh | sh
64-
65-
if [ $? -ne 0 ]; then
66-
echo "[ERROR] Ollama engine installation failed."
67-
exit 1
68-
fi
69-
echo "[OK] Ollama engine installed successfully."
7021
else
71-
echo "[OK] Ollama engine is already installed."
22+
echo "[OK] Ollama is already installed."
7223
fi
7324

74-
# Check for PyMuPDF
75-
if ! python3 -c "import pymupdf" &> /dev/null; then
76-
echo "[WARNING] Python cannot find 'pymupdf' library. Forcing strict inline installation..."
77-
python3 -m pip install pymupdf
78-
79-
if [ $? -ne 0 ]; then
80-
echo "[ERROR] Could not force-install Python pymupdf library."
81-
exit 1
82-
fi
83-
echo "[OK] 'pymupdf' bound to current Python environment."
84-
else
85-
echo "[OK] Python recognizes 'pymupdf'."
25+
# Patch Ollama for Docker Networking
26+
# Allows the container to communicate with the host machine
27+
CONF_DIR="/etc/systemd/system/ollama.service.d"
28+
CONF_FILE="$CONF_DIR/override.conf"
29+
30+
if [ ! -f "$CONF_FILE" ]; then
31+
echo "[INFO] Patching Ollama to allow Docker connections..."
32+
sudo mkdir -p "$CONF_DIR"
33+
echo -e "[Service]\nEnvironment=\"OLLAMA_HOST=0.0.0.0\"" | sudo tee "$CONF_FILE" > /dev/null
34+
sudo systemctl daemon-reload
35+
sudo systemctl restart ollama
36+
echo "[OK] Ollama network patch applied."
8637
fi
8738

88-
# Check and configure MongoDB
89-
chmod +x scripts/mongodb_cfg.sh
90-
./scripts/mongodb_cfg.sh
91-
92-
# Required Models for RAG
93-
echo "[INFO] Verifying AI models (Llama3.2 & Nomic)..."
39+
# Pull AI Models
40+
echo "[INFO] Syncing AI models..."
9441
ollama pull llama3.2
9542
ollama pull nomic-embed-text
96-
echo "[OK] All models are ready."
43+
echo "[OK] Models are ready."
44+
45+
# Launch Docker Compose
46+
echo "[INFO] Building and Launching Containers..."
9747

98-
# Start Application
99-
echo "[INFO] Starting RAG application..."
100-
streamlit run main.py
48+
# Try 'docker compose' first, fallback to 'docker-compose'
49+
if docker compose version &> /dev/null; then
50+
docker compose up --build
51+
else
52+
docker-compose up --build
53+
fi

scripts/mongodb_cfg.sh

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)