metadata
title: PyMind Assistant
emoji: π
colorFrom: blue
colorTo: indigo
sdk: docker
app_port: 7860
π PyMind β Enterprise Python AI Assistant
A premium, production-grade 12-stage RAG-powered Python assistant β grounded in 607K+ Stack Overflow posts, custom user document uploads, and dynamic Tavily Web Search fallback.
Active Deployment: Hugging Face Space
π Key Features
- 12-Stage Enterprise Pipeline: Built with LangGraph to execute a production-grade 12-stage pipeline:
Authentication β Query Validation β Intent Classification β Query Rewriting β Hybrid Retrieval (Vector + BM25) β Re-ranking (RRF) β Context Compression β Hallucination Check β Answer Generation β Citation Verification β Safety Guardrails β Observability Logging. - Dynamic Web Fallback (Tavily): If a Python-related query is not found in the local Stack Overflow database, the assistant automatically triggers a web search fallback via Tavily, citing live external sources.
- Topic Routing: Filters out-of-topic queries instantly during intent classification, bypassing database lookups to save latency.
- Persistent Cloud Sync (Turso): Fully integrated with Turso SQLite in the cloud to store user accounts, sessions, and chat history.
- User Authentication & Guest Mode:
- Authenticated Mode: Secure pbkdf2 password hashing, persistent session tracking.
- Guest Mode: Isolated, volatile in-memory storage (
GUEST_SESSIONS) that cleans up completely upon logout/exit.
- Dynamic Document Uploads: Upload
.pdf,.docx,.txt,.md, and.pyfiles. Chunks are embedded and queried dynamically alongside Stack Overflow. - Premium UI/UX: Cohesive light and midnight-blue dark themes, live interactive pipeline tracer, and expandable citation pill layouts.
πΊοΈ Pipeline Architecture
POST /stream {"question": "..."}
β
βΌ
ββββββββββββββββββββ
β FastAPI Server β
ββββββββββ¬ββββββββββ
β
βββββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββ
β 12-Stage LangGraph Pipeline β
β β
β β Authentication βΆ Validates API keys & cookies β
β β‘ Query Validation βΆ Disallows empty/gibberish queries β
β β’ Intent Classify βΆ Detects intent & topic boundaries β
β β£ Query Rewriting βΆ HyDE query expansion β
β β€ Hybrid Retrieval βΆ ChromaDB (Vector) + BM25 index β
β β₯ Re-ranking βΆ Reciprocal Rank Fusion (RRF) β
β β¦ Context Compression βΆ Fits context inside token budget β
β β§ Hallucination Check βΆ Grounds context (Fallback trigger) β
β β¨ Answer Generation βΆ Streams grounded answer (or Tavily) β
β β© Citation Verify βΆ Links sources (SO & Web) β
β βͺ Safety Guardrails βΆ LLM Content Moderation β
β β« Observability Log βΆ Saves to Turso/SQLite & computes logs β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π οΈ Tech Stack
| Layer | Component |
|---|---|
| Backend & API | FastAPI, SSE (Server-Sent Events) |
| Database & History | Turso (Cloud libSQL) / SQLite |
| Vector Index | ChromaDB (Local Embeddings: all-MiniLM-L6-v2) |
| Search Fallback | Tavily Web Search API |
| Orchestration | LangGraph & LangChain |
| LLM Core | Gemini 1.5 Flash (via Google Generative AI) |
| Frontend | Vanilla HTML5, JS (ES6), CSS3 custom design system |
π Setup & Installation
1. Prerequisites
2. Local Installation
# Clone the repository
git clone <your-repo-url>
cd dev
# Set up virtual environment
python -m venv venv
venv\Scripts\activate # On Windows
# source venv/bin/activate # On macOS/Linux
# Install dependencies
pip install -r requirements.txt
# Configure environment variables
copy .env.example .env # On Windows
# cp .env.example .env # On macOS/Linux
Open .env and fill in your keys:
GOOGLE_API_KEY=AIzaSy...
TAVILY_API_KEY=tvly-...
TURSO_DATABASE_URL=libsql://...
TURSO_AUTH_TOKEN=ey...
3. Startup
# Ingest Stack Overflow dataset (one-time setup, ingests ~28k chunks)
python -m app.ingest --sample-size 15000
# Start local server
python -m uvicorn app.main:app --host 127.0.0.1 --port 8000
Access the interactive web UI at http://127.0.0.1:8000.
π³ Docker & Cloud Deployment
Local Docker Build & Run
docker build -t python-qa .
docker run -p 7860:7860 --env-file .env python-qa
Deploying to Hugging Face Spaces (Free hosting, 24/7 uptime)
- Create a new Docker Space on Hugging Face using the Blank template.
- Under space Settings, add the environment secrets:
GOOGLE_API_KEY,TAVILY_API_KEY,TURSO_DATABASE_URL, andTURSO_AUTH_TOKEN. - Add Hugging Face remote and push your code:
git remote add hf https://huggingface.co/spaces/YOUR_HF_USERNAME/YOUR_SPACE_NAME git push -f hf main
π Project Structure
dev/
βββ app/
β βββ core/ # Embeddings initializer
β βββ memory/ # Turso/SQLite Database & Session Stores
β βββ observability/ # Logging, Event emitter & Pipeline metrics
β βββ pipeline/ # LangGraph stages & Nodes
β βββ static/ # UI Frontend (index.html with design system)
β βββ tools/ # Web Search (Tavily) Wrapper
β βββ upload/ # Document parser (PDF, DOCX, TXT, PY, MD)
β βββ utils/ # Authentication crypt utilities & PII scanners
β βββ main.py # FastAPI server configuration & routing
β βββ config.py # Application settings
βββ dataset/ # Source datasets
βββ vectorstore/ # Persistent ChromaDB collection
βββ test_api.py # Evaluation report builder
βββ Dockerfile # Container configs
βββ render.yaml # Render deployment template
βββ requirements.txt # Required packages
π License
This project is open-source. Stack Overflow dataset source is retrieved from the Kaggle Stack Overflow Python Questions Dataset and licensed under CC-BY-SA 3.0.