diff --git a/Dockerfile b/Dockerfile index 91013da..535a6d9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,25 @@ -FROM python:3.14-alpine3.23 +FROM python:3.12-slim WORKDIR /app +# Install system dependencies for pdfplumber and psycopg2 +RUN apt-get update && apt-get install -y --no-install-recommends \ + gcc \ + libpq-dev \ + && rm -rf /var/lib/apt/lists/* + COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . -CMD ["python3", "main.py"] +# Create patents directory for PDF storage +RUN mkdir -p /app/patents + +# Expose ports for API and Dashboard +EXPOSE 8000 8501 + +# Default command runs the API (can be overridden in docker-compose) +CMD ["uvicorn", "SPARC.api:app", "--host", "0.0.0.0", "--port", "8000"] diff --git a/README.md b/README.md index deb45da..d4ae497 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,25 @@ SPARC/ ## Installation -### NixOS (Recommended) +### Docker (Recommended) + +```bash +# Clone and configure +git clone +cd SPARC +cp .env.example .env +# Edit .env with your API keys + +# Start all services (API, Dashboard, PostgreSQL) +docker-compose up -d + +# Access the services +# - API: http://localhost:8000 +# - Dashboard: http://localhost:8501 +# - API Docs: http://localhost:8000/docs +``` + +### NixOS ```bash nix develop @@ -262,4 +280,4 @@ For open source projects, say how it is licensed. Core functionality complete. Ready for production use with API keys configured. -Next steps: API wrapper, containerization, and multi-company support. +All major features implemented: REST API, Streamlit dashboard, Docker containerization, database storage, and multi-company batch processing. diff --git a/docker-compose.yml b/docker-compose.yml index cf5c2b5..4d471c8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,25 +12,59 @@ services: - postgres_data:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] - interval: 10s + interval: 5s timeout: 5s retries: 5 + restart: unless-stopped - app: - build: - context: . - dockerfile: Dockerfile - container_name: sparc-app + init-db: + build: . + container_name: sparc-init-db + command: python scripts/init_database.py + environment: + DATABASE_URL: postgresql://postgres:postgres@postgres:5432/sparc + USE_DATABASE: "true" depends_on: postgres: condition: service_healthy + restart: "no" + + api: + build: . + container_name: sparc-api + command: uvicorn SPARC.api:app --host 0.0.0.0 --port 8000 environment: - USE_DATABASE: true + API_KEY: ${API_KEY} + OPENROUTER_API_KEY: ${OPENROUTER_API_KEY} DATABASE_URL: postgresql://postgres:postgres@postgres:5432/sparc + USE_DATABASE: "true" ports: - "8000:8000" + depends_on: + postgres: + condition: service_healthy + init-db: + condition: service_completed_successfully volumes: - - .:/app + - ./patents:/app/patents + restart: unless-stopped + + dashboard: + build: . + container_name: sparc-dashboard + command: streamlit run dashboard.py --server.port 8501 --server.address 0.0.0.0 + environment: + API_KEY: ${API_KEY} + OPENROUTER_API_KEY: ${OPENROUTER_API_KEY} + DATABASE_URL: postgresql://postgres:postgres@postgres:5432/sparc + USE_DATABASE: "true" + ports: + - "8501:8501" + depends_on: + - api + volumes: + - ./patents:/app/patents + restart: unless-stopped volumes: postgres_data: diff --git a/docs/DEPLOYMENT.md b/docs/DEPLOYMENT.md index d98f403..ed43b64 100644 --- a/docs/DEPLOYMENT.md +++ b/docs/DEPLOYMENT.md @@ -55,28 +55,25 @@ USE_DATABASE=true ## Step 2: Start Services with Docker Compose ```bash -# Start PostgreSQL database -docker-compose up -d postgres +# Start all services (PostgreSQL, API, and Dashboard) +docker-compose up -d -# Wait for postgres to be healthy (check with) +# Check status docker-compose ps -# You should see sparc-postgres with status "healthy" +# You should see: +# - sparc-postgres (healthy) +# - sparc-api (running on port 8000) +# - sparc-dashboard (running on port 8501) ``` +The database is automatically initialized by the `init-db` service. + --- -## Step 3: Initialize the Database +## Step 3: Database Schema -```bash -# Option A: If running locally with Python -python scripts/init_database.py - -# Option B: If using Docker, run inside container -docker-compose run --rm sparc-app python scripts/init_database.py -``` - -This creates the `llm_messages` table with the following schema: +The `init-db` service automatically creates the `llm_messages` table with the following schema: | Column | Type | Purpose | |--------|------|---------| @@ -95,9 +92,30 @@ This creates the `llm_messages` table with the following schema: ## Step 4: Run the Services -### Option A: Run Locally (Development) +### Option A: Run with Docker Compose (Recommended) + +All services are started automatically with `docker-compose up -d` from Step 2. ```bash +# View logs +docker-compose logs -f + +# View specific service logs +docker-compose logs -f api +docker-compose logs -f dashboard +``` + +### Option B: Run Locally (Development) + +If you prefer running services locally without Docker: + +```bash +# Start PostgreSQL with Docker +docker-compose up -d postgres + +# Wait for database to be healthy, then initialize +python scripts/init_database.py + # Terminal 1: Start FastAPI backend uvicorn SPARC.api:app --host 0.0.0.0 --port 8000 --reload @@ -105,14 +123,6 @@ uvicorn SPARC.api:app --host 0.0.0.0 --port 8000 --reload streamlit run dashboard.py --server.port 8501 --server.address 0.0.0.0 ``` -### Option B: Run with Docker (Production) - -See [Production Docker Compose](#production-docker-compose) section below for a complete `docker-compose.prod.yml` configuration. - -```bash -docker-compose -f docker-compose.prod.yml up -d -``` - --- ## Step 5: Verify Deployment @@ -256,97 +266,41 @@ postgresql://postgres:postgres@localhost:5432/sparc --- -## Production Docker Compose +## Docker Compose Services -Create a `docker-compose.prod.yml` file for full production deployment: +The `docker-compose.yml` includes all services needed for production: -```yaml -version: '3.8' +| Service | Container | Port | Description | +|---------|-----------|------|-------------| +| `postgres` | sparc-postgres | 5432 | PostgreSQL database | +| `init-db` | sparc-init-db | - | One-time database initialization | +| `api` | sparc-api | 8000 | FastAPI REST API | +| `dashboard` | sparc-dashboard | 8501 | Streamlit web UI | -services: - postgres: - image: postgres:16-alpine - container_name: sparc-postgres - environment: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: postgres - POSTGRES_DB: sparc - volumes: - - postgres_data:/var/lib/postgresql/data - ports: - - "5432:5432" - healthcheck: - test: ["CMD-SHELL", "pg_isready -U postgres"] - interval: 5s - timeout: 5s - retries: 5 - restart: unless-stopped - - api: - build: . - container_name: sparc-api - command: uvicorn SPARC.api:app --host 0.0.0.0 --port 8000 - environment: - - API_KEY=${API_KEY} - - OPENROUTER_API_KEY=${OPENROUTER_API_KEY} - - DATABASE_URL=postgresql://postgres:postgres@postgres:5432/sparc - - USE_DATABASE=true - ports: - - "8000:8000" - depends_on: - postgres: - condition: service_healthy - volumes: - - ./patents:/app/patents - restart: unless-stopped - - dashboard: - build: . - container_name: sparc-dashboard - command: streamlit run dashboard.py --server.port 8501 --server.address 0.0.0.0 - environment: - - API_KEY=${API_KEY} - - OPENROUTER_API_KEY=${OPENROUTER_API_KEY} - - DATABASE_URL=postgresql://postgres:postgres@postgres:5432/sparc - - USE_DATABASE=true - ports: - - "8501:8501" - depends_on: - - api - volumes: - - ./patents:/app/patents - restart: unless-stopped - - init-db: - build: . - container_name: sparc-init-db - command: python scripts/init_database.py - environment: - - DATABASE_URL=postgresql://postgres:postgres@postgres:5432/sparc - - USE_DATABASE=true - depends_on: - postgres: - condition: service_healthy - restart: "no" - -volumes: - postgres_data: -``` - -### Deploy with Production Compose +### Common Docker Compose Commands ```bash # Start all services -docker-compose -f docker-compose.prod.yml up -d +docker-compose up -d + +# Start with rebuild (after code changes) +docker-compose up -d --build # View logs -docker-compose -f docker-compose.prod.yml logs -f +docker-compose logs -f + +# View specific service logs +docker-compose logs -f api +docker-compose logs -f dashboard # Stop all services -docker-compose -f docker-compose.prod.yml down +docker-compose down # Stop and remove volumes (WARNING: deletes data) -docker-compose -f docker-compose.prod.yml down -v +docker-compose down -v + +# Restart a specific service +docker-compose restart api ``` --- @@ -417,7 +371,12 @@ docker-compose logs -f dashboard ## Quick Reference ```bash -# Development setup +# Docker setup (recommended) +cp .env.example .env +# Edit .env with API keys +docker-compose up -d + +# Local development setup cp .env.example .env # Edit .env with API keys docker-compose up -d postgres @@ -425,9 +384,6 @@ python scripts/init_database.py uvicorn SPARC.api:app --reload & streamlit run dashboard.py -# Production setup -docker-compose -f docker-compose.prod.yml up -d - # Check status curl http://localhost:8000/health open http://localhost:8501