Spaces:
Paused
Paused
| # Project Structure π | |
| Complete overview of the Graph RAG Chatbot project files and their purposes. | |
| ``` | |
| graph-rag-chatbot/ | |
| βββ π Core Application Files | |
| β βββ app.py # Main Flask application (500+ lines) | |
| β βββ requirements.txt # Python dependencies | |
| β βββ .env.example # Environment variables template | |
| β | |
| βββ π³ Docker & Deployment | |
| β βββ Dockerfile # Docker image definition | |
| β βββ docker-compose.yml # Docker Compose configuration | |
| β βββ .dockerignore # Files to exclude from Docker build | |
| β βββ deploy.sh # Automated deployment script (Linux/Mac) | |
| β βββ deploy.bat # Automated deployment script (Windows) | |
| β | |
| βββ π Documentation | |
| β βββ README.md # Complete documentation | |
| β βββ QUICKSTART.md # 5-minute quick start guide | |
| β βββ TESTING.md # Comprehensive testing guide | |
| β βββ space_config.md # HF Spaces deployment guide | |
| β βββ PROJECT_STRUCTURE.md # This file | |
| β | |
| βββ π¨ Frontend | |
| β βββ templates/ | |
| β βββ index.html # Complete responsive UI (HTML + CSS + JS) | |
| β | |
| βββ π¦ Data Storage (created at runtime) | |
| β βββ data/ | |
| β β βββ uploads/ # Uploaded documents stored here | |
| β β β βββ .gitkeep | |
| β β βββ graph_data/ # Knowledge graphs (PNG images) | |
| β β βββ .gitkeep | |
| β | |
| βββ π§ Configuration | |
| β βββ .gitignore # Git ignore rules | |
| β | |
| βββ π Optional Files (for your reference) | |
| βββ LICENSE # MIT License (optional) | |
| βββ CONTRIBUTING.md # Contribution guidelines (optional) | |
| ``` | |
| --- | |
| ## File Details | |
| ### Core Application (`app.py`) | |
| **Size**: ~550 lines | |
| **Language**: Python 3.8+ | |
| **Dependencies**: Flask, Groq, SentenceTransformers, NetworkX | |
| **Key Components**: | |
| 1. **Flask Setup** (lines 1-50) | |
| - Initialize Flask app | |
| - Configure CORS | |
| - Set up upload folder | |
| - Initialize models | |
| 2. **Document Processing** (lines 51-150) | |
| - `DocumentProcessor` class | |
| - Text extraction (PDF, CSV, TXT) | |
| - Text chunking with LangChain | |
| 3. **Knowledge Graph Building** (lines 151-220) | |
| - `GraphBuilder` class | |
| - Create nodes and edges | |
| - Generate NetworkX graph | |
| - Visualize with Matplotlib | |
| 4. **API Endpoints** (lines 221-450) | |
| - `GET /` - Serve UI | |
| - `GET /api/documents` - List documents | |
| - `POST /api/upload` - Upload files | |
| - `POST /api/query` - RAG queries | |
| - `GET /graph-image/<filename>` - Get graph PNG | |
| - `DELETE /api/delete/<filename>` - Delete document | |
| 5. **Async Processing** (lines 451-550) | |
| - Background thread processing | |
| - Progress tracking | |
| - Error handling | |
| ### Frontend (`templates/index.html`) | |
| **Size**: ~700 lines | |
| **Language**: HTML + CSS + JavaScript | |
| **No external build step required** | |
| **Sections**: | |
| 1. **Styling** (lines 1-350) | |
| - Modern gradient design | |
| - Responsive grid layout | |
| - Dark mode ready | |
| - Animations and transitions | |
| 2. **HTML Structure** (lines 351-500) | |
| - Upload zone | |
| - Document list | |
| - Chat interface | |
| - Graph viewer | |
| - Tabbed interface | |
| 3. **JavaScript** (lines 501-700) | |
| - File upload handling | |
| - Real-time document refresh | |
| - Chat message display | |
| - Graph visualization | |
| - API communication | |
| ### Configuration Files | |
| #### `requirements.txt` | |
| ``` | |
| Flask==2.3.3 # Web framework | |
| Flask-CORS==4.0.0 # CORS support | |
| python-dotenv==1.0.0 # .env loading | |
| sentence-transformers==2.2.2 # Embeddings | |
| groq==0.4.1 # Groq API | |
| PyPDF2==3.0.1 # PDF parsing | |
| pandas==2.0.3 # Data handling | |
| langchain==0.0.283 # Text processing | |
| networkx==3.1 # Graph algorithms | |
| matplotlib==3.7.2 # Graph visualization | |
| numpy==1.24.3 # Numerical computing | |
| torch==2.0.1 # ML framework | |
| ``` | |
| #### `Dockerfile` | |
| - Base: `python:3.11-slim` (compact, secure) | |
| - Installs: gcc, g++ for C dependencies | |
| - Installs: Python packages from requirements.txt | |
| - Exposes: Port 7860 | |
| - CMD: Run Flask app | |
| #### `docker-compose.yml` | |
| - Service: `graph-rag` | |
| - Port mapping: 7860:7860 | |
| - Environment: GROQ_API_KEY, PORT | |
| - Volumes: ./data for persistence | |
| - Health check: HTTP 200 on / | |
| - Restart policy: unless-stopped | |
| ### Environment Variables (`.env`) | |
| ```env | |
| GROQ_API_KEY=your_groq_api_key_here # Required: LLM API access | |
| PORT=7860 # Optional: Application port | |
| FLASK_ENV=production # Optional: production/development | |
| ``` | |
| **Never commit .env file!** Use `.env.example` as template. | |
| ### Data Storage | |
| #### `data/uploads/` | |
| - **Purpose**: Store uploaded documents | |
| - **Contents**: PDF, CSV, TXT files | |
| - **Persistence**: Survives container restarts | |
| - **Size Limit**: 50MB per file | |
| #### `data/graph_data/` | |
| - **Purpose**: Store generated graph images | |
| - **Format**: PNG files (DPI: 150) | |
| - **Naming**: `{filename}_graph.png` | |
| - **Size**: ~50-200KB per graph | |
| --- | |
| ## Technology Stack π οΈ | |
| ### Backend | |
| - **Framework**: Flask (lightweight, easy to deploy) | |
| - **API**: RESTful with JSON | |
| - **Language**: Python 3.8+ | |
| - **LLM**: Groq Mixtral 8x7b | |
| - **Embeddings**: SentenceTransformers (all-MiniLM-L6-v2) | |
| - **Graphs**: NetworkX (algorithms, visualization) | |
| ### Frontend | |
| - **Language**: HTML5 + CSS3 + Vanilla JavaScript | |
| - **No frameworks**: Zero dependencies (lighter bundle) | |
| - **Features**: Drag-and-drop, real-time updates, responsive design | |
| - **Charts**: Native SVG visualization | |
| ### Infrastructure | |
| - **Containerization**: Docker (Alpine-based) | |
| - **Orchestration**: Docker Compose | |
| - **Deployment**: HF Spaces, AWS, GCP, Azure | |
| - **Storage**: Ephemeral (configurable) | |
| --- | |
| ## Data Flow π | |
| ### Upload Flow | |
| ``` | |
| User Upload | |
| β | |
| Browser β POST /api/upload | |
| β | |
| Flask receive file β Save to disk | |
| β | |
| Queue async thread | |
| β | |
| Return 200 OK (immediately) | |
| β | |
| Background: Extract text | |
| β | |
| Background: Chunk text | |
| β | |
| Background: Build graph | |
| β | |
| Background: Generate embeddings | |
| β | |
| Frontend polls GET /api/documents | |
| β | |
| Document shows "ready" status | |
| β | |
| Graph image available | |
| ``` | |
| ### Query Flow | |
| ``` | |
| User Query | |
| β | |
| Browser β POST /api/query | |
| β | |
| Embed query text | |
| β | |
| Calculate cosine similarity with chunks | |
| β | |
| Select top 3 similar chunks | |
| β | |
| Send to Groq API with context | |
| β | |
| Groq generates answer | |
| β | |
| Return to frontend | |
| β | |
| Display in chat | |
| ``` | |
| --- | |
| ## Development Workflow | |
| ### Local Development | |
| ```bash | |
| # Setup | |
| python -m venv venv | |
| source venv/bin/activate | |
| pip install -r requirements.txt | |
| # Run | |
| export GROQ_API_KEY=your_key | |
| python app.py | |
| # Access | |
| http://localhost:7860 | |
| # Debug | |
| tail -f app.log | |
| # or set FLASK_ENV=development for auto-reload | |
| ``` | |
| ### Docker Development | |
| ```bash | |
| # Build | |
| docker build -t graph-rag . | |
| # Run with logs | |
| docker run -p 7860:7860 \ | |
| -e GROQ_API_KEY=your_key \ | |
| -v $(pwd)/data:/app/data \ | |
| graph-rag | |
| # Or use Compose | |
| docker-compose up --build | |
| ``` | |
| ### Testing | |
| ```bash | |
| # See TESTING.md for detailed test cases | |
| # Quick test: manual UI testing | |
| # Run: navigate to http://localhost:7860 | |
| # Steps: upload β visualize β query | |
| ``` | |
| --- | |
| ## Customization Points | |
| ### Easy Customizations | |
| 1. **Styling**: Edit `templates/index.html` CSS section (lines 15-300) | |
| 2. **Colors**: Change `#667eea` to your brand color (all occurrences) | |
| 3. **Title**: Change "Graph RAG Chatbot" in HTML title and headers | |
| 4. **Icons**: Replace emoji with SVG icons | |
| 5. **Fonts**: Add Google Fonts in `<head>` | |
| ### Moderate Customizations | |
| 1. **Chunk Size**: `app.py` line 66 | |
| 2. **Embedding Model**: `app.py` line 27 | |
| 3. **LLM Model**: `app.py` line 153 | |
| 4. **Similarity Threshold**: `app.py` line 164 | |
| 5. **Graph Layout**: `app.py` NetworkX spring_layout parameters | |
| ### Advanced Customizations | |
| 1. **Database**: Replace in-memory `documents_state` with PostgreSQL | |
| 2. **Vector Storage**: Add ChromaDB or Pinecone | |
| 3. **Authentication**: Add user login with Flask-Login | |
| 4. **Caching**: Add Redis for embedding cache | |
| 5. **Monitoring**: Add Prometheus metrics | |
| --- | |
| ## Deployment Targets | |
| | Target | Path | Docs | | |
| |---|---|---| | |
| | Local | Direct Python | README.md | | |
| | Local Docker | Docker | README.md | | |
| | HF Spaces | Auto-deploy | space_config.md | | |
| | AWS | ECR β ECS | README.md | | |
| | Azure | ACR β App Service | README.md | | |
| | GCP | Artifact Registry | README.md | | |
| | DigitalOcean | App Platform | README.md | | |
| --- | |
| ## Performance Characteristics | |
| ### Startup | |
| - Cold start: 30-60s (model download) | |
| - Warm start: 2-3s (in-memory) | |
| - Model size: ~400MB | |
| ### Upload Processing | |
| - Small file (< 5MB): 5-10s | |
| - Medium file (5-20MB): 15-30s | |
| - Large file (20-50MB): 30-60s | |
| ### Query Response | |
| - Embedding: 0.5-1s | |
| - Similarity search: <0.1s | |
| - LLM generation: 1-3s | |
| - Total: 2-5s | |
| ### Concurrency | |
| - Single-threaded requests: No | |
| - Async upload: Yes (threading) | |
| - Parallel documents: Yes (3+ simultaneous) | |
| --- | |
| ## Security Considerations | |
| ### API Security | |
| - β No API authentication (add if needed) | |
| - β CORS enabled (all origins) | |
| - β File size limit: 50MB | |
| - β Groq API key not exposed to frontend | |
| ### Data Security | |
| - β Files stored server-side only | |
| - β No sensitive data logging | |
| - β Uploaded files deleted on request | |
| - β οΈ No encryption at rest (add for sensitive data) | |
| ### Deployment Security | |
| - β Python 3.11-slim base (minimal OS) | |
| - β No root user in container | |
| - β .env not committed | |
| - β Health checks enabled | |
| --- | |
| ## Known Limitations | |
| 1. **Storage**: Ephemeral (HF Spaces free tier) | |
| - Solution: Upgrade to persistent storage | |
| 2. **Processing Speed**: Single machine | |
| - Solution: Use GPU tier or distributed processing | |
| 3. **Concurrency**: Threading (Python GIL) | |
| - Solution: Use Gunicorn with multiple workers | |
| 4. **Graph Complexity**: Limited to 500 nodes | |
| - Solution: Implement hierarchical graph layouts | |
| 5. **API Rate Limits**: Groq free tier 30req/min | |
| - Solution: Implement caching or upgrade plan | |
| --- | |
| ## Future Enhancements | |
| - [ ] WebSocket for real-time updates | |
| - [ ] Database backend (PostgreSQL + pgvector) | |
| - [ ] Multi-user with authentication | |
| - [ ] Advanced graph algorithms (pagerank, centrality) | |
| - [ ] Export to PDF/HTML reports | |
| - [ ] Multi-language support | |
| - [ ] Fine-tuned embeddings model | |
| - [ ] Conversation memory/history | |
| - [ ] Advanced search (filters, facets) | |
| - [ ] API documentation (Swagger/OpenAPI) | |
| --- | |
| ## File Ownership & Maintenance | |
| | File | Created | Last Updated | Maintainer | | |
| |---|---|---|---| | |
| | app.py | Day 1 | Day 1 | You | | |
| | index.html | Day 1 | Day 1 | You | | |
| | Dockerfile | Day 1 | Day 1 | You | | |
| | requirements.txt | Day 1 | Day 1 | You | | |
| | README.md | Day 1 | Day 1 | You | | |
| --- | |
| **Total Project Size**: ~5MB (including dependencies on first run: ~2GB) | |
| **Source Code Size**: ~50KB (uncompressed) | |
| **Docker Image Size**: ~2.5GB (uncompressed) | |
| **Docker Image Size**: ~800MB (compressed) | |
| --- | |
| **Last Updated**: June 27, 2024 | |
| **Version**: 1.0.0 | |
| **Status**: Production Ready β | |