A newer version of the Gradio SDK is available: 6.25.0
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
Install dependencies:
pip install -r requirements.txtRun the application:
python app.pyAccess the interface:
- Open your browser to
http://localhost:7860
- Open your browser to
Usage
Ingest Documents (Run this first or periodically to update):
- Add PDF files to the
pdfs/folder - Edit
ingest_documents.pyand add your URLs to theURLSlist - Run the ingestion script:
py ingest_documents.py - Wait for processing to complete (this creates/updates the vector store)
- Add PDF files to the
Chat with Documents:
- Run the chatbot app:
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
- Run the chatbot app:
Deployment to Hugging Face Spaces
Option 1: Using Hugging Face CLI
Install Hugging Face CLI:
pip install huggingface_hubLogin to Hugging Face:
huggingface-cli loginCreate a new Space:
- Go to https://huggingface.co/new-space
- Choose a name and select "Gradio" as the SDK
- Create the space
Clone and push your code:
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
Initialize git repository:
git init git add . git commit -m "Initial commit"Add Hugging Face remote:
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 applicationrequirements.txt- Python dependenciesREADME.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:
---
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:
Get your token:
- Go to https://huggingface.co/settings/tokens
- Create a new token with "Read" permissions
- Copy the token
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
.envfile withHF_TOKEN=your_token_here
- Set environment variable:
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:
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β
Enable providers (required)
See βHow to enable a provider and modelβ below.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).
- From the links above, choose a text generation / chat model thatβs supported by a provider you enabled. Note its model id (e.g.
Use it in the app
- In
app.py, pass that model id when creating the chatbot:
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_nameto a model you enabled as above.
- In
How to enable a provider and model
Log in to Hugging Face.
Open Inference Provider settings (one of these, depending on the current UI):
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.
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.
Confirm the model
- Browse models for that provider:
Together models on the Hub - 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.
- Browse models for that provider:
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.
- The app default is already
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:
# 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):
- Document Ingestion: PDFs and URLs are processed into chunks and embedded using sentence transformers
- Vector Search: When you ask a question, the system searches for the most relevant document chunks
- 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_TOKENis 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.