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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hugging Face Implementation Plan
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
This document outlines the plan to rebuild the RAG system using Hugging Face's models and capabilities instead of Google Cloud services, while preserving the original cloud implementation as a separate option.
|
| 5 |
+
|
| 6 |
+
## Repository Links
|
| 7 |
+
- GitHub: https://github.com/Daanworg/cloud-rag-webhook
|
| 8 |
+
- Hugging Face Space: https://huggingface.co/spaces/Ultronprime/cloud-rag-webhook
|
| 9 |
+
|
| 10 |
+
## Migration Strategy
|
| 11 |
+
The key difference in our approach is to **replace all Google Cloud dependencies with Hugging Face models and tools**:
|
| 12 |
+
|
| 13 |
+
1. **Replace Google's DocumentAI** → Use Hugging Face OCR models (like `microsoft/layoutlm-base-uncased`)
|
| 14 |
+
2. **Replace Vertex AI** → Use Hugging Face embeddings models (like `sentence-transformers/all-MiniLM-L6-v2`)
|
| 15 |
+
3. **Replace BigQuery** → Use FAISS/Chroma vector store with local storage or Hugging Face Datasets
|
| 16 |
+
4. **Replace Cloud Storage** → Use Hugging Face's persistent storage
|
| 17 |
+
5. **Replace Cloud Run** → Use Hugging Face Spaces continuous execution
|
| 18 |
+
|
| 19 |
+
## Implementation Steps
|
| 20 |
+
|
| 21 |
+
1. **Set Up New Architecture**:
|
| 22 |
+
- Create a revised Dockerfile for Hugging Face
|
| 23 |
+
- Set up persistent storage (20GB purchased)
|
| 24 |
+
- Configure A100 GPU using `accelerate` for pro users
|
| 25 |
+
|
| 26 |
+
2. **Replace Text Processing Pipeline**:
|
| 27 |
+
- Create a new OCR module using Transformers document models
|
| 28 |
+
- Implement a chunking system using pure Python
|
| 29 |
+
- Add text cleaning and processing without DocumentAI
|
| 30 |
+
|
| 31 |
+
3. **Replace Vector Database**:
|
| 32 |
+
- Implement FAISS/Chroma for vector storage
|
| 33 |
+
- Use Hugging Face Datasets for persistent indexed storage
|
| 34 |
+
- Create migration utility to move data from BigQuery
|
| 35 |
+
|
| 36 |
+
4. **Replace Embedding System**:
|
| 37 |
+
- Use `sentence-transformers` models for embeddings
|
| 38 |
+
- Implement similarity search using FAISS/Chroma
|
| 39 |
+
- Create a compatible API to replace Vertex AI functions
|
| 40 |
+
|
| 41 |
+
5. **Update Application Layer**:
|
| 42 |
+
- Modify Flask app to run on Hugging Face
|
| 43 |
+
- Update file handling to use local storage
|
| 44 |
+
- Create model caching for better performance
|
| 45 |
+
|
| 46 |
+
## Key Components
|
| 47 |
+
|
| 48 |
+
1. **Text Processing**:
|
| 49 |
+
```python
|
| 50 |
+
# New approach using Hugging Face models
|
| 51 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 52 |
+
from datasets import Dataset
|
| 53 |
+
|
| 54 |
+
def process_text(text_content):
|
| 55 |
+
"""Process text using Hugging Face models."""
|
| 56 |
+
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
|
| 57 |
+
model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased")
|
| 58 |
+
|
| 59 |
+
# Process and chunk the text
|
| 60 |
+
chunks = chunk_text(text_content)
|
| 61 |
+
|
| 62 |
+
# Store in persistent dataset
|
| 63 |
+
dataset = Dataset.from_dict({"text": chunks})
|
| 64 |
+
dataset.save_to_disk("./data/chunks")
|
| 65 |
+
|
| 66 |
+
return dataset
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
2. **Vector Storage**:
|
| 70 |
+
```python
|
| 71 |
+
# New approach using FAISS
|
| 72 |
+
import faiss
|
| 73 |
+
import numpy as np
|
| 74 |
+
from sentence_transformers import SentenceTransformer
|
| 75 |
+
|
| 76 |
+
class FAISSVectorStore:
|
| 77 |
+
def __init__(self):
|
| 78 |
+
self.model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 79 |
+
self.dimension = self.model.get_sentence_embedding_dimension()
|
| 80 |
+
self.index = faiss.IndexFlatL2(self.dimension)
|
| 81 |
+
self.texts = []
|
| 82 |
+
|
| 83 |
+
def add_texts(self, texts):
|
| 84 |
+
embeddings = self.model.encode(texts)
|
| 85 |
+
self.index.add(np.array(embeddings, dtype=np.float32))
|
| 86 |
+
self.texts.extend(texts)
|
| 87 |
+
|
| 88 |
+
def search(self, query, k=5):
|
| 89 |
+
query_embedding = self.model.encode([query])[0]
|
| 90 |
+
distances, indices = self.index.search(
|
| 91 |
+
np.array([query_embedding], dtype=np.float32), k
|
| 92 |
+
)
|
| 93 |
+
return [self.texts[i] for i in indices[0]]
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
3. **Hugging Face Space Configuration**:
|
| 97 |
+
```yaml
|
| 98 |
+
title: RAG Document Processing
|
| 99 |
+
emoji: 📄
|
| 100 |
+
colorFrom: blue
|
| 101 |
+
colorTo: green
|
| 102 |
+
sdk: docker
|
| 103 |
+
app_port: 7860
|
| 104 |
+
pinned: false
|
| 105 |
+
models:
|
| 106 |
+
- sentence-transformers/all-MiniLM-L6-v2
|
| 107 |
+
- facebook/bart-large-cnn
|
| 108 |
+
license: apache-2.0
|
| 109 |
+
```
|
| 110 |
+
|
| 111 |
+
## Automation Plan
|
| 112 |
+
|
| 113 |
+
1. **Background Processing**:
|
| 114 |
+
- Implement a file watcher for the persistent storage directory
|
| 115 |
+
- Process files automatically when added to upload directory
|
| 116 |
+
- Use Gradio/Streamlit for UI with background task system
|
| 117 |
+
|
| 118 |
+
2. **Scheduled Tasks**:
|
| 119 |
+
- Use Hugging Face Space's GitHub Actions for scheduling
|
| 120 |
+
- Run index maintenance tasks periodically
|
| 121 |
+
- Implement file processing queue for batch operations
|
| 122 |
+
|
| 123 |
+
3. **GitHub Integration**:
|
| 124 |
+
- Push processed data to GitHub repository as backup
|
| 125 |
+
- Use GitHub to store model configuration
|
| 126 |
+
- Implement version control for processed data
|
| 127 |
+
|
| 128 |
+
## Required Libraries
|
| 129 |
+
```
|
| 130 |
+
transformers==4.40.0
|
| 131 |
+
datasets==2.17.1
|
| 132 |
+
sentence-transformers==2.3.1
|
| 133 |
+
faiss-cpu==1.7.4 # or faiss-gpu for CUDA support
|
| 134 |
+
gradio==4.19.2
|
| 135 |
+
streamlit==1.32.0
|
| 136 |
+
langchain==0.1.5
|
| 137 |
+
torch==2.1.2
|
| 138 |
+
accelerate==0.28.0
|
| 139 |
+
```
|
| 140 |
+
|
| 141 |
+
## Hardware Requirements
|
| 142 |
+
- Use Hugging Face Pro's free A100 tier (zero.gpu)
|
| 143 |
+
- Configure model inference for optimal performance on GPU
|
| 144 |
+
- Set up model caching to reduce memory usage
|
| 145 |
+
- Utilize Hugging Face's persistent storage (20GB)
|
| 146 |
+
|
| 147 |
+
## Project Goals
|
| 148 |
+
Create a fully self-contained RAG system on Hugging Face:
|
| 149 |
+
1. Process text files automatically
|
| 150 |
+
2. Generate embeddings with Hugging Face models
|
| 151 |
+
3. Store vectors in FAISS/Chroma on persistent storage
|
| 152 |
+
4. Query the data with a simple API
|
| 153 |
+
5. Run continuously "under the hood"
|
| 154 |
+
6. Utilize Hugging Face Pro benefits (A100 GPU, persistent storage)
|
| 155 |
+
|
| 156 |
+
## Implementation Files
|
| 157 |
+
We'll create the following new files to implement the Hugging Face version:
|
| 158 |
+
|
| 159 |
+
1. `hf_process_text.py` - Text processing with HF models
|
| 160 |
+
2. `hf_embeddings.py` - Embedding generation with sentence-transformers
|
| 161 |
+
3. `hf_vector_store.py` - FAISS/Chroma implementation
|
| 162 |
+
4. `hf_app.py` - Gradio/Streamlit interface
|
| 163 |
+
5. `hf_rag_query.py` - Query interface for HF models
|
| 164 |
+
6. `requirements_hf.txt` - HF-specific dependencies
|
| 165 |
+
|
| 166 |
+
This will allow us to maintain both implementations in parallel.
|