File size: 9,760 Bytes
4bce094 a71ea0a 4bce094 a71ea0a 66c4741 a71ea0a 66c4741 eb02516 66c4741 eb02516 66c4741 eb02516 a71ea0a eb02516 a71ea0a eb02516 66c4741 a71ea0a 66c4741 a71ea0a 66c4741 a71ea0a 66c4741 a71ea0a 66c4741 a71ea0a 66c4741 a71ea0a 66c4741 a71ea0a 66c4741 a71ea0a 66c4741 a71ea0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | ---
title: RAG Chatbot
emoji: π€
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 6.3.0
app_file: app.py
pinned: false
---
# Hugging Face RAG Chatbot
A Retrieval-Augmented Generation (RAG) chatbot that can ingest PDFs and URLs, then answer questions based on the ingested documents. Built for deployment on Hugging Face Spaces.
## Features
- π **PDF Ingestion**: Upload and process PDF documents
- π **URL Ingestion**: Extract and process content from web URLs
- π **Vector Search**: Semantic search using sentence transformers
- π¬ **Chatbot Interface**: Interactive Gradio interface for querying documents
- π **Hugging Face Ready**: Configured for easy deployment to Hugging Face Spaces
## Setup
### Local Development
1. **Install dependencies:**
```bash
pip install -r requirements.txt
```
2. **Run the application:**
```bash
python app.py
```
3. **Access the interface:**
- Open your browser to `http://localhost:7860`
### Usage
1. **Ingest Documents (Run this first or periodically to update):**
- Add PDF files to the `pdfs/` folder
- Edit `ingest_documents.py` and add your URLs to the `URLS` list
- Run the ingestion script:
```bash
py ingest_documents.py
```
- Wait for processing to complete (this creates/updates the vector store)
2. **Chat with Documents:**
- Run the chatbot app:
```bash
py app.py
```
- Open your browser to `http://localhost:7860`
- Toggle "Use RAG" to enable/disable document retrieval
- Ask questions about your ingested documents
- The chatbot will retrieve relevant context and generate answers
## Deployment to Hugging Face Spaces
### Option 1: Using Hugging Face CLI
1. **Install Hugging Face CLI:**
```bash
pip install huggingface_hub
```
2. **Login to Hugging Face:**
```bash
huggingface-cli login
```
3. **Create a new Space:**
- Go to https://huggingface.co/new-space
- Choose a name and select "Gradio" as the SDK
- Create the space
4. **Clone and push your code:**
```bash
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
cd YOUR_SPACE_NAME
# Copy your files here
git add .
git commit -m "Initial commit"
git push
```
### Option 2: Using Git
1. **Initialize git repository:**
```bash
git init
git add .
git commit -m "Initial commit"
```
2. **Add Hugging Face remote:**
```bash
git remote add origin https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
git push -u origin main
```
### Required Files for Hugging Face Spaces
Your Space needs these files:
- `app.py` - Main Gradio application
- `requirements.txt` - Python dependencies
- `README.md` - This file (optional but recommended)
### Optional: Add app.py to README
For Hugging Face Spaces, you can also add a `app.py` reference in your README:
```yaml
---
title: RAG Chatbot
emoji: π€
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 4.0.0
app_file: app.py
pinned: false
---
```
## Configuration
### Setting Up Hugging Face Token (Required)
The chatbot uses Hugging Face Inference API to access high-quality models. You need to set up an API token:
1. **Get your token:**
- Go to https://huggingface.co/settings/tokens
- Create a new token with "Read" permissions
- Copy the token
2. **For local development:**
- Set environment variable: `export HF_TOKEN=your_token_here` (Linux/Mac)
- Or: `set HF_TOKEN=your_token_here` (Windows)
- Or create a `.env` file with `HF_TOKEN=your_token_here`
3. **For Hugging Face Spaces:**
- Go to your Space β Settings β Secrets
- Add a new secret: Name = `HF_TOKEN`, Value = your token
- The app will automatically use this token
### Chatbot Model and Finding Available Models
The chatbot uses the Hugging Face Inference API. **Which models you can use depends on which providers you have enabled.**
**How to see which models are available to you:**
1. **Browse models that support Inference API**
- https://huggingface.co/inference/models β lists providers and models
- https://huggingface.co/models?inference_provider=hf-inference β filter Hub models by βHF Inference APIβ
2. **Enable providers (required)**
See **βHow to enable a provider and modelβ** below.
3. **Pick a chat model**
- From the links above, choose a **text generation / chat** model thatβs supported by a provider you enabled. Note its **model id** (e.g. `meta-llama/Llama-3.2-1B-Instruct`).
4. **Use it in the app**
- In `app.py`, pass that model id when creating the chatbot:
```python
chatbot = RAGChatbot(model_name="meta-llama/Llama-3.2-1B-Instruct") # use an id you enabled
```
- The app also tries fallbacks (Phi-2, Zephyr, Qwen) by default; if none are available, enable a provider that supports at least one of them, or set `model_name` to a model you enabled as above.
### How to enable a provider and model
1. **Log in** to [Hugging Face](https://huggingface.co).
2. **Open Inference Provider settings** (one of these, depending on the current UI):
- https://huggingface.co/settings/inference-providers
- https://huggingface.co/settings/inference-api
3. **Enable a provider**
- On that page youβll see a list of **providers** (e.g. Hugging Face, Together, Groq, etc.).
- **Turn on** the provider that serves your model (e.g. **Together** for `ServiceNow-AI/Apriel-1.6-15b-Thinker:together`).
- You can set the **order** of providers; βautoβ uses this order to pick which provider handles the request.
4. **Credits / billing**
- Free accounts get a small amount of monthly credits; usage is deducted from that.
- If you use a third-party provider (e.g. Together), you can either use HF-routed billing (credits on your HF account) or add a **custom provider API key** (e.g. Together API key) in the same settings so that provider is billed directly.
5. **Confirm the model**
- Browse models for that provider:
[Together models on the Hub](https://huggingface.co/models?inference_provider=together&sort=trending)
- Open the model page (e.g. `ServiceNow-AI/Apriel-1.6-15b-Thinker`) and check the inference widget; if you see βTogetherβ and can run it, that model is available with your enabled provider.
6. **Use it in this app**
- The app default is already `ServiceNow-AI/Apriel-1.6-15b-Thinker:together`.
- Ensure **Together** is enabled in your Inference Provider settings and you have credits (or a Together API key). Then restart the app and send a message.
### Embedding Model
The default embedding model is **all-mpnet-base-v2**, which provides high-quality embeddings for better retrieval.
To change the embedding model, edit both `app.py` and `ingest_documents.py`:
```python
# In app.py
chatbot = RAGChatbot(embedding_model="all-mpnet-base-v2")
# In ingest_documents.py
ingestion = DocumentIngestion(embedding_model="all-mpnet-base-v2")
```
**Note:** If you change the embedding model, you must re-run `ingest_documents.py` to rebuild the vector store.
### Ingestion Parameters
The ingestion system uses optimized parameters:
- **Chunk size**: 600 characters (for precise retrieval)
- **Chunk overlap**: 150 characters (to avoid cutting sentences)
- **Retrieval count**: 5 chunks (for comprehensive context)
These parameters are set in `ingestion.py` and can be adjusted if needed.
## Project Structure
```
.
βββ app.py # Main Gradio chatbot application
βββ ingest_documents.py # Standalone script to ingest PDFs and URLs
βββ ingestion.py # Document ingestion and vector store module
βββ requirements.txt # Python dependencies
βββ README.md # This file
βββ pdfs/ # Folder for PDF files (add your PDFs here)
β βββ README.md
βββ data/
βββ vector_store/ # Saved vector store (created after ingestion)
βββ index.faiss
βββ documents.pkl
βββ embeddings.pkl
```
## How It Works
The chatbot uses **Retrieval-Augmented Generation (RAG)**:
1. **Document Ingestion**: PDFs and URLs are processed into chunks and embedded using sentence transformers
2. **Vector Search**: When you ask a question, the system searches for the most relevant document chunks
3. **Answer Generation**: The retrieved context is sent to Mistral-7B-Instruct via Inference API, which synthesizes a coherent answer based on the context
This approach combines the accuracy of document retrieval with the natural language capabilities of a large language model.
## Limitations
- Vector store is stored locally (not persistent on Hugging Face Spaces by default)
- Large documents may take time to process
- Some URLs may be blocked or require authentication
- Requires HF_TOKEN for Inference API access (free tier available)
- If you change embedding model or chunk parameters, you must re-run ingestion
## Troubleshooting
### Out of Memory Errors
- Use smaller models
- Reduce chunk size in `ingestion.py`
- Process fewer documents at once
### URL Fetching Issues
- Some websites block automated requests
- Try different URLs or use PDF uploads instead
### Inference API Issues
- Verify your `HF_TOKEN` is set correctly
- Check that the token has "Read" permissions
- Ensure you have API access (free tier available)
- If you get rate limit errors, you may need to upgrade your Hugging Face account
### Ingestion Issues
- If you changed embedding model or chunk parameters, re-run `ingest_documents.py`
- Ensure you have enough disk space for the vector store
- Large documents may take time to process
## License
This project is open source and available under the MIT License.
|