Skip to content

Commit 7919143

Browse files
committed
added containerization to backend services, both cpu and gpu support. Rearranged requirement modules
1 parent 152fca4 commit 7919143

File tree

5 files changed

+303
-4
lines changed

5 files changed

+303
-4
lines changed

backend/docker/Dockerfile

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Start with a universal base image for common setup
2+
FROM ubuntu:22.04 AS universal-base
3+
4+
# Set environment variables
5+
ENV PYTHONDONTWRITEBYTECODE=1 \
6+
PYTHONUNBUFFERED=1 \
7+
DEBIAN_FRONTEND=noninteractive \
8+
TZ=UTC
9+
10+
# Install system dependencies
11+
RUN apt-get update && apt-get install -y --no-install-recommends \
12+
python3.10 \
13+
python3-pip \
14+
python3-dev \
15+
git \
16+
curl \
17+
wget \
18+
ffmpeg \
19+
libsndfile1 \
20+
build-essential \
21+
&& apt-get clean \
22+
&& rm -rf /var/lib/apt/lists/*
23+
24+
# Set working directory
25+
WORKDIR /app
26+
27+
# Create directories for data and models
28+
RUN mkdir -p /app/models /app/datasets /app/logs /app/local_data/tts
29+
30+
# CPU version - Built directly from universal base
31+
FROM universal-base AS cpu
32+
33+
# Copy backend requirements
34+
COPY backend/requirements.txt /app/
35+
36+
# First, install torch and faiss-cpu
37+
RUN pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu \
38+
&& pip3 install faiss-cpu
39+
40+
# Next, install the remaining dependencies (filtering out torch and faiss-gpu)
41+
RUN grep -v "torch==.*\|torch-.*\|faiss-gpu" requirements.txt > cpu_requirements.txt \
42+
&& pip3 install --no-cache-dir -r cpu_requirements.txt
43+
44+
# GPU version - Use NVIDIA CUDA image
45+
FROM nvidia/cuda:12.4.0-runtime-ubuntu22.04 AS gpu-base
46+
47+
# Set environment variables
48+
ENV PYTHONDONTWRITEBYTECODE=1 \
49+
PYTHONUNBUFFERED=1 \
50+
DEBIAN_FRONTEND=noninteractive \
51+
TZ=UTC
52+
53+
# Install system dependencies
54+
RUN apt-get update && apt-get install -y --no-install-recommends \
55+
python3.10 \
56+
python3-pip \
57+
python3-dev \
58+
git \
59+
curl \
60+
wget \
61+
ffmpeg \
62+
libsndfile1 \
63+
build-essential \
64+
&& apt-get clean \
65+
&& rm -rf /var/lib/apt/lists/*
66+
67+
# Set working directory
68+
WORKDIR /app
69+
70+
# Create directories for data and models
71+
RUN mkdir -p /app/models /app/datasets /app/logs /app/local_data/tts
72+
73+
# Complete GPU setup
74+
FROM gpu-base AS gpu
75+
76+
# Copy backend requirements
77+
COPY backend/requirements.txt backend/gpu-requirements.txt /app/
78+
79+
# Install GPU-specific dependencies
80+
RUN pip3 install --no-cache-dir -r requirements.txt \
81+
&& pip3 install --no-cache-dir -r gpu-requirements.txt
82+
83+
# Common steps for both CPU and GPU - using a separate stage for code copying
84+
FROM ${BUILD_TYPE:-cpu} AS final
85+
86+
# Copy the application code from the backend directory
87+
COPY backend/ /app/
88+
89+
# Create an entrypoint script
90+
RUN echo '#!/bin/bash\n\
91+
if [ ! -f "/app/config.yaml" ]; then\n\
92+
echo "ERROR: Config file not found at /app/config.yaml"\n\
93+
echo "Make sure to mount the config file from the host system:"\n\
94+
echo " -v /path/to/FluentAI/config.yaml:/app/config.yaml:ro"\n\
95+
exit 1\n\
96+
fi\n\
97+
\n\
98+
# Set config path explicitly\n\
99+
export FLUENTAI_CONFIG_PATH=/app/config.yaml\n\
100+
echo "Using config from: $FLUENTAI_CONFIG_PATH"\n\
101+
\n\
102+
# Check if languages.json exists\n\
103+
if [ ! -f "/app/data/languages.json" ]; then\n\
104+
echo "ERROR: languages.json not found at /app/data/languages.json"\n\
105+
echo "Make sure the data directory is properly mounted:"\n\
106+
echo " -v /path/to/FluentAI/data:/app/data:ro"\n\
107+
exit 1\n\
108+
fi\n\
109+
\n\
110+
# Start the application using uvicorn\n\
111+
uvicorn fluentai.api.app:app --host 0.0.0.0 --port 8000 "$@"\n\
112+
' > /app/entrypoint.sh && chmod +x /app/entrypoint.sh
113+
114+
# Expose port
115+
EXPOSE 8000
116+
117+
# Set entrypoint
118+
ENTRYPOINT ["/app/entrypoint.sh"]
119+
120+
# Default command
121+
CMD ["--host", "0.0.0.0", "--port", "8000"]

backend/docker/ReadME.md

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 🐳 Docker Setup for FluentAI Backend
2+
3+
This directory contains Docker configuration files for running the FluentAI backend service.
4+
5+
## 📂 Directory Structure
6+
```
7+
FluentAI/
8+
├── config.yaml # Root-level configuration file
9+
├── backend/
10+
│ ├── docker/ # This directory
11+
│ │ ├── Dockerfile # Main Dockerfile for Linux NVIDIA
12+
│ │ ├── Dockerfile.mac # Special Dockerfile for Apple Silicon
13+
│ │ ├── docker-compose.yml
14+
│ │ └── README.md
15+
```
16+
## ⚙️ Configuration
17+
The Docker setup uses the project's root-level configuration file at /FluentAI/config.yaml. Make sure this file exists and contains the necessary configuration.
18+
19+
## 🚀 Usage
20+
Run all commands from the docker directory:
21+
```
22+
/FluentAI/backend/docker
23+
```
24+
### Choose the appropriate version for your hardware:
25+
#### 🖥️ For Linux/Windows with NVIDIA GPU:
26+
```
27+
docker-compose up -d fluentai-gpu
28+
```
29+
#### 💻 For Linux/Windows CPU only:
30+
```
31+
docker-compose up -d fluentai-cpu
32+
```
33+
#### 🍎 For Apple Silicon Macs (⚠️ work in progress 🚧):
34+
```
35+
docker-compose up -d fluentai-apple
36+
```
37+
38+
### 🌐 View logs:
39+
```
40+
docker-compose logs -f
41+
```
42+
43+
### 📊 Accessing the API
44+
- CPU version: http://localhost:8000
45+
- GPU version: http://localhost:8001
46+
- Apple Silicon version: http://localhost:8002
47+
48+
The FastAPI documentation is available at /docs (e.g., http://localhost:8000/docs)
49+
or visit: https://fastapi.tiangolo.com/#example-upgrade
50+
51+
### 🛑 Stopping the Containers
52+
```
53+
docker-compose down
54+
```
55+
56+
### 🔄 Rebuilding after Code Changes
57+
```
58+
docker-compose up -d --build fluentai-[cpu|gpu|apple]
59+
```
60+
### 💾 Data Persistence
61+
The following Docker volumes are used for data persistence:
62+
- models_data: Caches downloaded ML models
63+
- datasets_data: Stores datasets
64+
- local_data: Stores generated files (images, audio, etc.)
65+
- logs_data: Stores application logs
66+
67+
## ⚠️ Troubleshooting
68+
### Missing or incorrect config file
69+
Make sure the config file exists at /FluentAI/config.yaml. The Docker container mounts this file directly into the container.
70+
### Out of memory errors
71+
Increase the memory limit for your Docker container in Docker Desktop settings.
72+
### GPU not detected
73+
For NVIDIA GPU users, ensure NVIDIA Container Toolkit is properly installed:
74+
```
75+
docker run --rm --gpus all nvidia/cuda:12.4.0-base-ubuntu22.04 nvidia-smi
76+
```
77+
### Network issues downloading models
78+
If you experience issues downloading models, check network connectivity and ensure Hugging Face credentials are correctly set up if needed.

backend/docker/docker-compose.yml

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
services:
2+
# CPU Version
3+
fluentai-cpu:
4+
container_name: fluentai-backend-cpu
5+
build:
6+
context: ../..
7+
dockerfile: backend/docker/Dockerfile
8+
target: final
9+
args:
10+
- BUILD_TYPE=cpu
11+
ports:
12+
- "8000:8000"
13+
volumes:
14+
- ../../config.yaml:/app/config.yaml:ro
15+
- models_data:/app/models
16+
- datasets_data:/app/datasets
17+
- local_data:/app/local_data
18+
- logs_data:/app/logs
19+
- ../../data:/app/data:ro
20+
environment:
21+
- FLUENTAI_CONFIG_PATH=/app/config.yaml
22+
restart: unless-stopped
23+
healthcheck:
24+
test: [ "CMD", "curl", "-f", "http://localhost:8000/docs" ]
25+
interval: 30s
26+
timeout: 10s
27+
retries: 3
28+
start_period: 40s
29+
30+
# GPU Version
31+
fluentai-gpu:
32+
container_name: fluentai-backend-gpu
33+
build:
34+
context: ../..
35+
dockerfile: backend/docker/Dockerfile
36+
target: final
37+
args:
38+
- BUILD_TYPE=gpu
39+
ports:
40+
- "8001:8000"
41+
volumes:
42+
- ../../config.yaml:/app/config.yaml:ro
43+
- models_data:/app/models
44+
- datasets_data:/app/datasets
45+
- local_data:/app/local_data
46+
- logs_data:/app/logs
47+
- ../../data:/app/data:ro
48+
environment:
49+
- FLUENTAI_CONFIG_PATH=/app/config.yaml
50+
restart: unless-stopped
51+
deploy:
52+
resources:
53+
reservations:
54+
devices:
55+
- driver: nvidia
56+
count: 1
57+
capabilities: [ gpu ]
58+
healthcheck:
59+
test: [ "CMD", "curl", "-f", "http://localhost:8000/docs" ]
60+
interval: 30s
61+
timeout: 10s
62+
retries: 3
63+
start_period: 40s
64+
# # Apple Silicon Version
65+
# fluentai-apple:
66+
# container_name: fluentai-backend-mac
67+
# build:
68+
# context: ../..
69+
# dockerfile: backend/docker/Dockerfile.mac
70+
# platform: linux/arm64
71+
# ports:
72+
# - "8002:8000"
73+
# volumes:
74+
# - ../../config.yaml:/app/config.yaml:ro
75+
# - models_data:/app/models
76+
# - datasets_data:/app/datasets
77+
# - local_data:/app/local_data
78+
# - logs_data:/app/logs
79+
# - ../../data:/app/data:ro
80+
# environment:
81+
# - FLUENTAI_CONFIG_PATH=/app/config.yaml
82+
# restart: unless-stopped
83+
# healthcheck:
84+
# test: [ "CMD", "curl", "-f", "http://localhost:8000/docs" ]
85+
# interval: 30s
86+
# timeout: 10s
87+
# retries: 3
88+
# start_period: 40s
89+
90+
volumes:
91+
models_data:
92+
driver: local
93+
datasets_data:
94+
driver: local
95+
local_data:
96+
driver: local
97+
logs_data:
98+
driver: local

backend/gpu-requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ torchaudio==2.5.1+cu124
44
torchvision==0.20.1+cu124
55
faiss-gpu ; sys_platform == 'linux'
66
--extra-index-url https://download.pytorch.org/whl/cu124
7-
xformers==0.0.29
7+
xformers==0.0.29
8+
bitsandbytes==0.45.2

backend/requirements.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Levenshtein==0.26.1
1818
pyclts==3.2.0
1919
panphon==0.21.2
2020
soundvectors==1.0
21-
datasets==3.2.0
21+
datasets==3.3.2
2222
GitPython==3.1.44
2323
googletrans==4.0.2
2424
pycountry==24.6.1
@@ -28,5 +28,6 @@ uvicorn[standard]==0.34.0 # ASGI server for FastAPI
2828
gtts==2.5.4
2929
ftfy==6.3.1
3030
einops==0.8.0
31-
bitsandbytes==0.45.2
32-
peft==0.14.0
31+
bitsandbytes==0.42.0
32+
peft==0.14.0
33+
xformers==0.0.29

0 commit comments

Comments
 (0)