Spaces:
Sleeping
A newer version of the Gradio SDK is available: 6.20.0
Quick Start Guide: Intelligent Documentation Crawler & RAG Assistant
π Getting Started (5 minutes)
Step 1: Prerequisites
- Python 3.10+ (already installed: Python 3.11)
- Ollama running locally with
llama3model - All dependencies installed (see
requiements.txt)
Step 2: Start Ollama (in a separate terminal)
ollama serve
Then in another terminal, ensure the model is pulled:
ollama pull llama3
Step 3: Run the Dashboard
python ui/gradio-dashboard.py
Open: http://localhost:7860
Step 4: Use It!
Option A: Use the Gradio UI
- Paste URLs in the right sidebar (one per line)
- Type your question in the chat
- Get instant answers from the crawled/uploaded documents
Option B: Use the FastAPI Server
python -m src.api
Then query via curl:
curl -X POST http://localhost:8000/query \
-H "Content-Type: application/json" \
-d '{
"question": "What is async/await?",
"urls": ["https://docs.python.org/3/library/asyncio.html"]
}'
Option C: Use Python Directly
from src.app_enhanced import answer_question
answer = answer_question(
"How do I use decorators?",
urls=["https://docs.python.org"]
)
print(answer)
π Adding Data Sources
Method 1: PDF Files
- Create
my_docs/folder (already exists) - Add your PDF files there
- The system will automatically load them on next query
Method 2: Website URLs
- Paste URLs in the Gradio dashboard, OR
- Pass
urlsparameter toanswer_question(), OR - Use the
/queryAPI endpoint withurlsfield
Method 3: Crawl a Website
from src.crawler import DocumentationCrawler
from src.app_enhanced import index_crawler_results
# Crawl a documentation site
crawler = DocumentationCrawler(
base_url="https://docs.python.org/3",
max_depth=3,
max_pages=100
)
documents = crawler.crawl()
index_crawler_results(documents)
# Now query
from src.app_enhanced import answer_question
answer = answer_question("What's the difference between list and tuple?")
print(answer)
π§ API Endpoints
All endpoints require Ollama running with llama3 model.
GET /health
Check if the API is running
curl http://localhost:8000/health
POST /query
Ask a question (complete response)
curl -X POST http://localhost:8000/query \
-H "Content-Type: application/json" \
-d '{"question": "What is a context manager?", "urls": ["https://realpython.com"]}'
POST /query/stream
Ask a question (streamed response)
curl -X POST http://localhost:8000/query/stream \
-H "Content-Type: application/json" \
-d '{"question": "Explain generators"}'
POST /crawl/prepare
Crawl a website (returns status)
curl -X POST http://localhost:8000/crawl/prepare \
-H "Content-Type: application/json" \
-d '{
"base_url": "https://docs.python.org/3",
"max_depth": 2,
"max_pages": 50
}'
POST /index/from-crawl
Crawl and automatically index
curl -X POST http://localhost:8000/index/from-crawl \
-H "Content-Type: application/json" \
-d '{
"base_url": "https://docs.python.org/3",
"max_depth": 2
}'
π Project Structure
fd/
βββ src/ # Core application modules
β βββ app_enhanced.py # Enhanced RAG with crawler integration β
β βββ app_hf.py # Hugging Face inference RAG app
β βββ api.py # FastAPI server with streaming β
β βββ crawler.py # Web crawler module β
β βββ config.py # Configuration helpers
β βββ util.py # Utility helpers
β βββ __init__.py # Package entrypoint
βββ ui/ # UI and dashboard scripts
β βββ gradio-dashboard.py # User-friendly UI β
β βββ spaces_app.py # Spaces-compatible UI
β βββ gradio_chatbot_info.txt
βββ tests/ # Test suite
β βββ test_system.py
βββ notebooks/ # Project notebooks
β βββ project_code.ipynb
βββ my_docs/ # PDF storage
βββ chroma_db/ # Persisted vector DB storage
βββ requirements.txt # Dependencies
βββ IMPLEMENTATION_GUIDE.md # Detailed technical guide
βββ README.md # This file
β = New/Enhanced components
π§ͺ Testing
Quick Test
python tests/test_system.py
Test with Crawler
python tests/test_system.py --crawl
Test API
# Terminal 1: Start API
python -m src.api
# Terminal 2: Test it
curl http://localhost:8000/health
βοΈ Configuration
Crawler Parameters
DocumentationCrawler(
base_url="https://...",
max_depth=3, # How deep to crawl
delay=0.5, # Politeness delay (seconds)
timeout=10, # Request timeout
max_pages=100 # Stop after this many pages
)
Text Splitting
Edit in app_enhanced.py:
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000, # Characters per chunk
chunk_overlap=200 # Overlap between chunks
)
Embedding Model
Edit in app_enhanced.py:
hf_embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2"
# Other options:
# - "sentence-transformers/all-mpnet-base-v2"
# - "BAAI/bge-small-en"
)
LLM Model
Edit in app_enhanced.py:
llm = ChatOllama(
model="llama3", # Change to "llama2" or other available models
temperature=0 # 0 = deterministic, 1 = creative
)
π Troubleshooting
Error: "Ollama connection error"
- Ensure Ollama service is running:
ollama serve - Check model exists:
ollama list - Pull if needed:
ollama pull llama3
Error: "No documents found"
- Add PDFs to
my_docs/folder, OR - Provide URLs via API/dashboard, OR
- Run crawler first to crawl a website
Slow First Query
- First query loads embeddings (~30-60 seconds)
- Subsequent queries are cached and faster (~1-5 seconds)
Timeout During Crawl
- Increase delay:
delay=1.0 - Reduce pages:
max_pages=50 - Check target site is accessible
Memory Issues
- ChromaDB runs locally (low overhead)
- Embeddings model cached after first load
- For large crawls, reduce
max_pages
π Performance
Typical latencies (after warmup):
- Embedding generation: 5-10ms per chunk
- Vector search: 50-100ms
- LLM inference: 1-5 seconds
- Total query time: 1-10 seconds
π Documentation
For more details:
- Full Implementation Guide
- SRS Specification
- API Swagger Docs (when API running)
β¨ Features
β
Recursive web crawling with rate limiting
β
Local PDF loading
β
Code-aware text chunking
β
Local embeddings (no API key needed)
β
Semantic search
β
Streaming responses
β
FastAPI with Swagger docs
β
Gradio user interface
β
Multi-source data integration
β
Graceful error handling
π― Next Steps
- Test with real docs: Try crawling your target website
- Evaluate quality: Check answer accuracy and relevance
- Fine-tune: Adjust chunk size, depth, embedding model
- Deploy: Use production server (Gunicorn + Uvicorn)
- Monitor: Log queries and feedback for improvement
Happy documenting! π