Binayak Panigrahi commited on
Commit
5f4c445
Β·
1 Parent(s): 65a9ef2

Add application file

Browse files
Files changed (6) hide show
  1. .dockerignore +9 -0
  2. .env.example +15 -0
  3. Dockerfile +26 -0
  4. README.md +100 -10
  5. docker-compose.yml +41 -0
  6. requirements.txt +1 -2
.dockerignore ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ .env
2
+ .env.*
3
+ __pycache__/
4
+ *.pyc
5
+ *.pyo
6
+ .git/
7
+ .gitignore
8
+ *.md
9
+ sql_results/
.env.example ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copy this file to .env and fill in your values
2
+ # cp .env.example .env
3
+
4
+ # ── Required ───────────────────────────────────────────────────────────────────
5
+ # Your NVIDIA API key (get one at https://integrate.api.nvidia.com)
6
+ NVIDIA_API_KEY=nvapi-your_key_here
7
+
8
+ # ── Optional (defaults shown) ──────────────────────────────────────────────────
9
+ # URL of your PHP DB layer β€” update host/path to match your PHP server
10
+ # If PHP runs on your host machine: http://host.docker.internal/api_ct/db_api.php
11
+ # If PHP runs in another Docker container on the same network: http://php-container/api_ct/db_api.php
12
+ PHP_DB_URL=http://host.docker.internal/api_ct/db_api.php
13
+
14
+ # Shared secret between this API and the PHP layer (change this!)
15
+ INTERNAL_SECRET=change_this_secret_in_production
Dockerfile ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ # System dependencies
4
+ RUN apt-get update && apt-get install -y \
5
+ gcc \
6
+ g++ \
7
+ && rm -rf /var/lib/apt/lists/*
8
+
9
+ WORKDIR /app
10
+
11
+ # Install Python dependencies
12
+ COPY requirements.txt .
13
+ RUN pip install --no-cache-dir -r requirements.txt
14
+
15
+ # Copy application files
16
+ COPY app.py .
17
+ COPY index.html .
18
+
19
+ # Create directory for SQL results
20
+ RUN mkdir -p /app/sql_results
21
+
22
+ # Expose port
23
+ EXPOSE 8000
24
+
25
+ # Run the app
26
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
README.md CHANGED
@@ -1,13 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
- title: ChatRAG
3
- emoji: 🐒
4
- colorFrom: indigo
5
- colorTo: red
6
- sdk: gradio
7
- sdk_version: 6.14.0
8
- python_version: '3.13'
9
- app_file: app.py
10
- pinned: false
 
 
 
 
 
 
 
 
 
 
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ShopBot β€” Docker Setup
2
+
3
+ A RAG-powered product search API using NVIDIA Llama + FastAPI.
4
+
5
+ ## Project Structure
6
+
7
+ ```
8
+ .
9
+ β”œβ”€β”€ Dockerfile
10
+ β”œβ”€β”€ docker-compose.yml
11
+ β”œβ”€β”€ .env.example ← copy to .env and fill in
12
+ β”œβ”€β”€ .dockerignore
13
+ β”œβ”€β”€ requirements.txt
14
+ β”œβ”€β”€ app.py ← FastAPI application
15
+ β”œβ”€β”€ index.html ← Chat UI (served at /)
16
+ └── rag_integration.php ← PHP integration helper (deploy on your PHP server)
17
+ ```
18
+
19
+ ## Quick Start
20
+
21
+ ### 1. Set up environment variables
22
+
23
+ ```bash
24
+ cp .env.example .env
25
+ # Edit .env and add your NVIDIA_API_KEY and PHP_DB_URL
26
+ ```
27
+
28
+ ### 2. Build and run
29
+
30
+ ```bash
31
+ docker compose up --build
32
+ ```
33
+
34
+ The API will be available at **http://localhost:8000**
35
+
36
+ - Chat UI β†’ http://localhost:8000/
37
+ - API docs β†’ http://localhost:8000/docs
38
+ - Health check β†’ http://localhost:8000/health
39
+
40
+ ### 3. Run in background (production)
41
+
42
+ ```bash
43
+ docker compose up --build -d
44
+ ```
45
+
46
  ---
47
+
48
+ ## PHP Integration
49
+
50
+ Your PHP server needs to expose a `db_api.php` endpoint that:
51
+ - Accepts `POST` requests with JSON body `{"sql": "SELECT ..."}`
52
+ - Verifies the `X-Internal-Secret` header matches `INTERNAL_SECRET`
53
+ - Executes the SQL and returns `{"success": true, "results": [...], "row_count": N}`
54
+
55
+ Copy `rag_integration.php` to your PHP server and call `get_rag_recommendations($conn, $query)` from your chat handler.
56
+
57
+ ---
58
+
59
+ ## Environment Variables
60
+
61
+ | Variable | Required | Default | Description |
62
+ |---|---|---|---|
63
+ | `NVIDIA_API_KEY` | βœ… Yes | β€” | Your NVIDIA API key |
64
+ | `PHP_DB_URL` | No | `http://host.docker.internal/api_ct/db_api.php` | URL to your PHP DB layer |
65
+ | `INTERNAL_SECRET` | No | `change_this_secret_in_production` | Shared secret for PHP auth |
66
+
67
  ---
68
 
69
+ ## Connecting to Your PHP Server
70
+
71
+ **PHP on the same host machine:**
72
+ ```env
73
+ PHP_DB_URL=http://host.docker.internal/api_ct/db_api.php
74
+ ```
75
+
76
+ **PHP in another Docker container (same compose file):**
77
+ Add your PHP container to `docker-compose.yml` and use its service name:
78
+ ```env
79
+ PHP_DB_URL=http://php-service/api_ct/db_api.php
80
+ ```
81
+
82
+ **PHP on a remote server:**
83
+ ```env
84
+ PHP_DB_URL=https://yourserver.com/api_ct/db_api.php
85
+ ```
86
+
87
+ ---
88
+
89
+ ## Useful Commands
90
+
91
+ ```bash
92
+ # View logs
93
+ docker compose logs -f shopbot-api
94
+
95
+ # Rebuild after code changes
96
+ docker compose up --build
97
+
98
+ # Stop
99
+ docker compose down
100
+
101
+ # Stop and remove volumes (clears saved SQL results)
102
+ docker compose down -v
103
+ ```
docker-compose.yml ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ version: "3.9"
2
+
3
+ services:
4
+ shopbot-api:
5
+ build:
6
+ context: .
7
+ dockerfile: Dockerfile
8
+ container_name: shopbot-api
9
+ ports:
10
+ - "8000:8000"
11
+ environment:
12
+ # ── Required: Set your NVIDIA API key ──────────────────────────────────
13
+ - NVIDIA_API_KEY=${NVIDIA_API_KEY}
14
+
15
+ # ── PHP DB layer URL (update to your PHP server's address) ─────────────
16
+ # If your PHP server runs on the host machine, use host.docker.internal
17
+ # Example: http://host.docker.internal/api_ct/db_api.php
18
+ - PHP_DB_URL=${PHP_DB_URL:-http://host.docker.internal/api_ct/db_api.php}
19
+
20
+ # ── Internal secret shared between this API and your PHP layer ─────────
21
+ - INTERNAL_SECRET=${INTERNAL_SECRET:-change_this_secret_in_production}
22
+
23
+ volumes:
24
+ # Persist SQL query result files outside the container
25
+ - sql_results:/app/sql_results
26
+
27
+ restart: unless-stopped
28
+
29
+ healthcheck:
30
+ test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"]
31
+ interval: 30s
32
+ timeout: 10s
33
+ retries: 3
34
+ start_period: 15s
35
+
36
+ extra_hosts:
37
+ # Allows the container to reach services on your host machine
38
+ - "host.docker.internal:host-gateway"
39
+
40
+ volumes:
41
+ sql_results:
requirements.txt CHANGED
@@ -14,5 +14,4 @@ langchain-community>=0.0.10
14
  faiss-cpu>=1.13.0
15
  sentence-transformers>=2.2.2
16
  python-multipart>=0.0.18
17
- cors==1.0.1
18
- python-json-logger>=2.0.0
 
14
  faiss-cpu>=1.13.0
15
  sentence-transformers>=2.2.2
16
  python-multipart>=0.0.18
17
+ python-json-logger>=2.0.0