Python Backend with Vector Similarity Search
This is the Python implementation of the crossword generator backend, featuring true AI word generation via vector similarity search.
π Features
- True Vector Search: Uses sentence-transformers + FAISS for semantic word discovery
- 30K+ Vocabulary: Searches through full model vocabulary instead of limited static lists
- FastAPI: Modern, fast Python web framework
- Same API: Compatible with existing React frontend
- Hybrid Approach: AI vector search with static word fallback
π Differences from JavaScript Backend
| Feature | JavaScript Backend | Python Backend |
|---|---|---|
| Word Generation | Embedding filtering of static lists | True vector similarity search |
| Vocabulary Size | ~100 words per topic | 30K+ words from model |
| AI Approach | Semantic similarity filtering | Nearest neighbor search |
| Performance | Fast but limited | Slower startup, better results |
| Dependencies | Node.js + HuggingFace API | Python + ML libraries |
π οΈ Setup & Installation
Prerequisites
- Python 3.11+ (3.11 recommended for Docker compatibility)
- pip (Python package manager)
Basic Setup (Core Functionality)
# Clone and navigate to backend directory
cd crossword-app/backend-py
# Create virtual environment (recommended)
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install core dependencies
pip install -r requirements.txt
# Start the server
python app.py
Full Development Setup (with AI features)
# Install development dependencies including AI/ML libraries
pip install -r requirements-dev.txt
# This includes:
# - All core dependencies
# - AI/ML libraries (torch, sentence-transformers, etc.)
# - Development tools (pytest, coverage, etc.)
Requirements Files
requirements.txt: Core dependencies for basic functionalityrequirements-dev.txt: Full development environment with AI features
Note: The AI/ML dependencies are large (~2GB). For basic testing without AI features, use
requirements.txtonly.
Python Version: Both local development and Docker use Python 3.11+ for optimal performance and latest package compatibility.
π Structure
backend-py/
βββ app.py # FastAPI application entry point
βββ requirements.txt # Core Python dependencies
βββ requirements-dev.txt # Full development dependencies
βββ src/
β βββ services/
β β βββ vector_search.py # Core vector similarity search
β β βββ crossword_generator.py # Puzzle generation logic
β βββ routes/
β βββ api.py # API endpoints (matches JS backend)
βββ test-unit/ # Unit tests (pytest framework) - 5 files
β βββ test_crossword_generator.py
β βββ test_api_routes.py
β βββ test_vector_search.py
βββ test-integration/ # Integration tests (standalone scripts) - 16 files
β βββ test_simple_generation.py
β βββ test_boundary_fix.py
β βββ test_local.py # (+ 13 more test files)
βββ data/ -> ../backend/data/ # Symlink to shared word data
βββ public/ # Frontend static files (copied during build)
π Dependencies
Core ML Stack
sentence-transformers: Local model loading and embeddingsfaiss-cpu: Fast vector similarity searchtorch: PyTorch for model inferencenumpy: Vector operations
Web Framework
fastapi: Modern Python web frameworkuvicorn: ASGI serverpydantic: Data validation
Testing
pytest: Testing frameworkpytest-asyncio: Async test support
π§ͺ Testing
π Test Organization (Reorganized for Clarity)
We've reorganized the test structure for better developer experience:
| Test Type | Location | Purpose | Framework | Count |
|---|---|---|---|---|
| Unit Tests | test-unit/ |
Test individual components in isolation | pytest | 5 files |
| Integration Tests | test-integration/ |
Test complete workflows end-to-end | Standalone scripts | 16 files |
Benefits of this structure:
- β Clear separation between unit and integration testing
- β Intuitive naming - developers immediately understand test types
- β Better tooling - can run different test types independently
- β Easier maintenance - organized by testing strategy
Note: Previously tests were mixed in
tests/folder and root-leveltest_*.pyfiles. The new structure provides much better organization.
Unit Tests Details (test-unit/)
What they test: Individual components with mocking and isolation
test_crossword_generator.py- Core crossword generation logictest_api_routes.py- FastAPI endpoint handlerstest_crossword_generator_wrapper.py- Service wrapper layertest_index_bug_fix.py- Specific bug fix validationstest_vector_search.py- AI vector search functionality (requires torch)
Run Unit Tests (Formal Test Suite)
# Run all unit tests
python run_tests.py
# Run specific test modules
python run_tests.py crossword_generator
pytest test-unit/test_crossword_generator.py -v
# Run core tests (excluding AI dependencies)
pytest test-unit/ -v --ignore=test-unit/test_vector_search.py
# Run individual unit test classes
pytest test-unit/test_crossword_generator.py::TestCrosswordGenerator::test_init -v
Integration Tests Details (test-integration/)
What they test: Complete workflows without mocking - real functionality
test_simple_generation.py- End-to-end crossword generationtest_boundary_fix.py- Word boundary validation (our major fix!)test_local.py- Local environment and dependenciestest_word_boundaries.py- Comprehensive boundary testingtest_bounds_comprehensive.py- Advanced bounds checkingtest_final_validation.py- API integration testing- And 10 more specialized feature tests...
Run Integration Tests (End-to-End Scripts)
# Test core functionality
python test-integration/test_simple_generation.py
python test-integration/test_boundary_fix.py
python test-integration/test_local.py
# Test specific features
python test-integration/test_word_boundaries.py
python test-integration/test_bounds_comprehensive.py
# Test API integration
python test-integration/test_final_validation.py
Test Coverage
# Run core tests with coverage (requires requirements-dev.txt)
pytest test-unit/test_crossword_generator.py --cov=src --cov-report=html
pytest test-unit/test_crossword_generator.py --cov=src --cov-report=term
# Full coverage report (may fail without AI dependencies)
pytest test-unit/ --cov=src --cov-report=html --ignore=test-unit/test_vector_search.py
Test Status
- β Core crossword generation: 15/19 unit tests passing
- β Boundary validation: All integration tests passing
- β οΈ AI/Vector search: Requires torch dependencies
- β οΈ Some async mocking: Minor test infrastructure issues
π Migration Guide (For Existing Developers)
If you had previous commands, update them:
| Old Command | New Command |
|---|---|
pytest tests/ |
pytest test-unit/ |
python test_simple_generation.py |
python test-integration/test_simple_generation.py |
pytest tests/ --cov=src |
pytest test-unit/ --cov=src |
All functionality is preserved - just organized better!
π§ Configuration
Environment variables (set in HuggingFace Spaces):
# Core settings
PORT=7860
NODE_ENV=production
# AI Configuration
EMBEDDING_MODEL=sentence-transformers/all-mpnet-base-v2
WORD_SIMILARITY_THRESHOLD=0.65
# Optional
LOG_LEVEL=INFO
π― Vector Search Process
Initialization:
- Load sentence-transformers model locally
- Extract 30K+ vocabulary from model tokenizer
- Pre-compute embeddings for all vocabulary words
- Build FAISS index for fast similarity search
Word Generation:
- Get topic embedding:
"Animals" β [768-dim vector] - Search FAISS index for nearest neighbors
- Filter by similarity threshold (0.65+)
- Filter by difficulty (word length)
- Return top matches with generated clues
- Get topic embedding:
Fallback:
- If vector search fails β use static word lists
- If insufficient AI words β supplement with static words
π§ͺ Testing
# Local testing (without full vector search)
cd backend-py
python test_local.py
# Start development server
python app.py
π³ Docker Deployment
The Dockerfile has been updated to use Python backend:
FROM python:3.9-slim
# ... install dependencies
# ... build frontend (same as before)
# ... copy to backend-py/public/
CMD ["python", "app.py"]
π§ͺ Testing
Quick Test
# Basic functionality test (no model download)
python test_local.py
Comprehensive Unit Tests
# Run all unit tests
python run_tests.py
# Or use pytest directly
pytest tests/ -v
# Run specific test file
python run_tests.py crossword_generator_fixed
pytest tests/test_crossword_generator_fixed.py -v
# Run with coverage
pytest tests/ --cov=src --cov-report=html
Test Structure
tests/test_crossword_generator_fixed.py- Core grid generation logictests/test_vector_search.py- Vector similarity searchtests/test_crossword_generator_wrapper.py- Service wrappertests/test_api_routes.py- FastAPI endpoints
Key Test Features
- β Index alignment fix: Tests the list index out of range bug fix
- β Mocked vector search: Tests without downloading models
- β API validation: Tests all endpoints and error cases
- β Async support: Full pytest-asyncio integration
- β Error handling: Tests malformed inputs and edge cases
π Performance Comparison
Startup Time:
- JavaScript: ~2 seconds
- Python: ~30-60 seconds (model download + index building)
Word Quality:
- JavaScript: Limited by static word lists
- Python: Access to full model vocabulary with semantic understanding
Memory Usage:
- JavaScript: ~100MB
- Python: ~500MB-1GB (model + embeddings + FAISS index)
API Response Time:
- JavaScript: ~100ms (after cache warm-up)
- Python: ~200-500ms (vector search + filtering)
π Migration Strategy
- Phase 1 β : Basic Python backend structure
- Phase 2: Test vector search functionality
- Phase 3: Docker deployment and production testing
- Phase 4: Compare with JavaScript backend
- Phase 5: Production switch with rollback capability
π― Next Steps
- Test vector search with real model
- Optimize FAISS index performance
- Add more sophisticated crossword grid generation
- Implement LLM-based clue generation
- Add caching for frequently requested topics