Jeevant10 commited on
Commit
6b27270
Β·
1 Parent(s): a4538e5

Configure Git LFS for FAISS index + HF Spaces Docker deployment

Browse files
Files changed (8) hide show
  1. .gitattributes +31 -0
  2. .gitignore +2 -2
  3. Dockerfile +22 -39
  4. README.md +55 -1
  5. app.py +1 -1
  6. faiss_store/faiss.index +3 -0
  7. faiss_store/metadata.pkl +3 -0
  8. requirements.txt +1 -9
.gitattributes ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Git LFS tracking for large binary files
2
+ # Hugging Face Spaces supports Git LFS natively
3
+
4
+ # FAISS vector index (grows as more documents are indexed)
5
+ faiss_store/faiss.index filter=lfs diff=lfs merge=lfs -text
6
+ faiss_store/*.index filter=lfs diff=lfs merge=lfs -text
7
+
8
+ # Pickle files (metadata store, grows with index)
9
+ faiss_store/metadata.pkl filter=lfs diff=lfs merge=lfs -text
10
+ *.pkl filter=lfs diff=lfs merge=lfs -text
11
+
12
+ # Common large binary formats (future-proofing)
13
+ *.bin filter=lfs diff=lfs merge=lfs -text
14
+ *.pt filter=lfs diff=lfs merge=lfs -text
15
+ *.pth filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
18
+ *.h5 filter=lfs diff=lfs merge=lfs -text
19
+ *.hdf5 filter=lfs diff=lfs merge=lfs -text
20
+ *.npy filter=lfs diff=lfs merge=lfs -text
21
+ *.npz filter=lfs diff=lfs merge=lfs -text
22
+ *.parquet filter=lfs diff=lfs merge=lfs -text
23
+ *.arrow filter=lfs diff=lfs merge=lfs -text
24
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
25
+
26
+ # Large data files
27
+ *.pdf filter=lfs diff=lfs merge=lfs -text
28
+ *.zip filter=lfs diff=lfs merge=lfs -text
29
+ *.tar.gz filter=lfs diff=lfs merge=lfs -text
30
+ *.sqlite filter=lfs diff=lfs merge=lfs -text
31
+ *.db filter=lfs diff=lfs merge=lfs -text
.gitignore CHANGED
@@ -20,8 +20,8 @@ src/__pycache__/
20
  __pycache__/
21
  *.pyc
22
 
23
- # Vector DB
24
- faiss_store/
25
  vector_store/
26
 
27
  # LangGraph
 
20
  __pycache__/
21
  *.pyc
22
 
23
+ # Vector DB (keep faiss_store/ for deployment β€” DO NOT ignore)
24
+ # faiss_store/
25
  vector_store/
26
 
27
  # LangGraph
Dockerfile CHANGED
@@ -1,64 +1,47 @@
1
- # Modern Multi-stage Dockerfile for RAG System
2
- FROM python:3.11-slim as builder
 
 
 
3
 
4
- # Set environment variables
5
  ENV PYTHONDONTWRITEBYTECODE=1 \
6
- PYTHONUNBUFFERED=1 \
7
- POETRY_NO_INTERACTION=1 \
8
- POETRY_VENV_IN_PROJECT=1 \
9
- POETRY_CACHE_DIR=/tmp/poetry_cache
10
 
11
- # Install system dependencies
12
  RUN apt-get update && apt-get install -y --no-install-recommends \
13
  build-essential \
14
- curl \
15
  && rm -rf /var/lib/apt/lists/*
16
 
17
- # Install Python dependencies
18
  WORKDIR /app
19
  COPY requirements.txt .
20
  RUN pip install --no-cache-dir --upgrade pip && \
21
  pip install --no-cache-dir -r requirements.txt
22
 
23
- # Production stage
24
- FROM python:3.11-slim as production
25
 
26
- # Set environment variables
27
  ENV PYTHONDONTWRITEBYTECODE=1 \
28
- PYTHONUNBUFFERED=1 \
29
- PYTHONPATH="/app/src:$PYTHONPATH"
30
 
31
- # Install runtime dependencies only
32
- RUN apt-get update && apt-get install -y --no-install-recommends \
33
- curl \
34
- && rm -rf /var/lib/apt/lists/*
35
 
36
- # Create non-root user
37
- RUN groupadd -r appuser && useradd -r -g appuser appuser
38
-
39
- # Copy Python packages from builder stage
40
  COPY --from=builder /usr/local/lib/python3.11/site-packages/ /usr/local/lib/python3.11/site-packages/
41
  COPY --from=builder /usr/local/bin/ /usr/local/bin/
42
 
43
- # Set working directory
44
  WORKDIR /app
45
 
46
- # Copy application code
47
- COPY --chown=appuser:appuser . .
48
-
49
- # Create necessary directories
50
- RUN mkdir -p /app/faiss_store /app/data /app/logs && \
51
- chown -R appuser:appuser /app
52
 
53
- # Switch to non-root user
54
- USER appuser
 
55
 
56
- # Health check
57
- HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
58
- CMD curl -f http://localhost:8000/health || exit 1
59
 
60
- # Expose port
61
- EXPOSE 8000
62
 
63
- # Default command
64
- CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]
 
1
+ # Hugging Face Spaces - Docker SDK
2
+ # Docs: https://huggingface.co/docs/hub/spaces-sdks-docker
3
+
4
+ # ── Build stage ──────────────────────────────────────────────
5
+ FROM python:3.11-slim AS builder
6
 
 
7
  ENV PYTHONDONTWRITEBYTECODE=1 \
8
+ PYTHONUNBUFFERED=1
 
 
 
9
 
 
10
  RUN apt-get update && apt-get install -y --no-install-recommends \
11
  build-essential \
 
12
  && rm -rf /var/lib/apt/lists/*
13
 
 
14
  WORKDIR /app
15
  COPY requirements.txt .
16
  RUN pip install --no-cache-dir --upgrade pip && \
17
  pip install --no-cache-dir -r requirements.txt
18
 
19
+ # ── Production stage ─────────────────────────────────────────
20
+ FROM python:3.11-slim AS production
21
 
 
22
  ENV PYTHONDONTWRITEBYTECODE=1 \
23
+ PYTHONUNBUFFERED=1
 
24
 
25
+ # HF Spaces requires a user with UID 1000
26
+ RUN useradd -m -u 1000 user
27
+ ENV PATH="/home/user/.local/bin:$PATH"
 
28
 
29
+ # Copy installed packages from builder
 
 
 
30
  COPY --from=builder /usr/local/lib/python3.11/site-packages/ /usr/local/lib/python3.11/site-packages/
31
  COPY --from=builder /usr/local/bin/ /usr/local/bin/
32
 
 
33
  WORKDIR /app
34
 
35
+ # Copy application code (owned by user)
36
+ COPY --chown=user . /app
 
 
 
 
37
 
38
+ # Ensure necessary directories exist
39
+ RUN mkdir -p /app/faiss_store /app/Data && \
40
+ chown -R user:user /app
41
 
42
+ USER user
 
 
43
 
44
+ # HF Spaces requires port 7860
45
+ EXPOSE 7860
46
 
47
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
README.md CHANGED
@@ -1 +1,55 @@
1
- # Langchain-Model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Book Model - RAG QA API
3
+ emoji: πŸ“š
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: docker
7
+ pinned: false
8
+ license: mit
9
+ app_port: 7860
10
+ ---
11
+
12
+ # Book Model β€” RAG Question Answering API
13
+
14
+ A Retrieval-Augmented Generation (RAG) API built with **FastAPI**, **FAISS**, **SentenceTransformers**, and **Groq LLM**.
15
+
16
+ ## API Endpoints
17
+
18
+ | Method | Path | Description |
19
+ |--------|------|-------------|
20
+ | `GET` | `/` | Health message |
21
+ | `GET` | `/health` | Detailed system status |
22
+ | `POST` | `/query` | Ask a question against the indexed documents |
23
+ | `GET` | `/docs` | Interactive Swagger UI |
24
+
25
+ ### Example β€” Query the API
26
+
27
+ ```bash
28
+ curl -X POST "https://jeevant010-book-model.hf.space/query" \
29
+ -H "Content-Type: application/json" \
30
+ -d '{"query": "What is machine learning?", "top_k": 3}'
31
+ ```
32
+
33
+ ### Response
34
+
35
+ ```json
36
+ {
37
+ "query": "What is machine learning?",
38
+ "answer": "Machine learning is ...",
39
+ "sources": [
40
+ {"index": 0, "distance": 0.42, "text": "..."}
41
+ ]
42
+ }
43
+ ```
44
+
45
+ ## Environment Variables
46
+
47
+ Set `GROQ_API_KEY` as a **Secret** in your Hugging Face Space settings.
48
+
49
+ ## Tech Stack
50
+
51
+ - **FastAPI** β€” async web framework
52
+ - **FAISS** β€” vector similarity search
53
+ - **SentenceTransformers** β€” embedding model (`all-MiniLM-L6-v2`)
54
+ - **Groq** β€” LLM inference (`llama-3.1-8b-instant`)
55
+ - **LangChain** β€” document loading & text splitting
app.py CHANGED
@@ -125,6 +125,6 @@ if __name__ == "__main__":
125
  uvicorn.run(
126
  "app:app",
127
  host="0.0.0.0",
128
- port=int(os.getenv("PORT", "8000")),
129
  reload=True
130
  )
 
125
  uvicorn.run(
126
  "app:app",
127
  host="0.0.0.0",
128
+ port=int(os.getenv("PORT", "7860")),
129
  reload=True
130
  )
faiss_store/faiss.index ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:99934a79e76336eae73893ceb3e4bd88c83f0af49a9886e8956a9ecce6b16899
3
+ size 3117
faiss_store/metadata.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:15ceff12866c206bb4bc1842aedacc9d616474427811f95016531a89ffaa2653
3
+ size 1101
requirements.txt CHANGED
@@ -7,15 +7,10 @@ langchain-groq>=0.2.0
7
 
8
  # Document processing
9
  pypdf>=4.0.1
10
- pymupdf>=1.24.0
11
 
12
  # Embeddings and vector databases
13
  sentence-transformers>=3.0.0
14
  faiss-cpu>=1.8.0
15
- chromadb>=0.5.0
16
-
17
- # Search and indexing
18
- typesense>=0.19.0
19
 
20
  # Web framework and API
21
  fastapi>=0.115.0
@@ -23,7 +18,4 @@ uvicorn[standard]>=0.32.0
23
  pydantic>=2.9.0
24
 
25
  # Environment and utilities
26
- python-dotenv>=1.0.0
27
-
28
- # Build tools (only if needed for development)
29
- setuptools>=75.0.0
 
7
 
8
  # Document processing
9
  pypdf>=4.0.1
 
10
 
11
  # Embeddings and vector databases
12
  sentence-transformers>=3.0.0
13
  faiss-cpu>=1.8.0
 
 
 
 
14
 
15
  # Web framework and API
16
  fastapi>=0.115.0
 
18
  pydantic>=2.9.0
19
 
20
  # Environment and utilities
21
+ python-dotenv>=1.0.0