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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Repository Reorganization Plan
|
| 2 |
+
|
| 3 |
+
This document outlines the plan to reorganize the repository into cleanly separated projects while maintaining shared code.
|
| 4 |
+
|
| 5 |
+
## Goals
|
| 6 |
+
|
| 7 |
+
- Separate CLI scraper (future GCP container) from Gradio apps
|
| 8 |
+
- Enable easy addition of new Gradio apps (KG dashboard, future apps)
|
| 9 |
+
- Centralize shared code (services, models, tracing) in one place
|
| 10 |
+
- Maintain clear import paths and testability
|
| 11 |
+
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
## Target Structure
|
| 15 |
+
|
| 16 |
+
```
|
| 17 |
+
day1-gen/
|
| 18 |
+
├── app.py # Thin wrapper for HuggingFace Spaces (imports from apps/)
|
| 19 |
+
├── core/ # Shared library
|
| 20 |
+
│ ├── __init__.py
|
| 21 |
+
│ ├── services/
|
| 22 |
+
│ │ ├── __init__.py
|
| 23 |
+
│ │ ├── llm.py # LLM provider abstraction
|
| 24 |
+
│ │ ├── kg.py # Knowledge graph client
|
| 25 |
+
│ │ ├── embedding.py # Embedding service
|
| 26 |
+
│ │ ├── claims.py # Claim extraction
|
| 27 |
+
│ │ ├── sources/
|
| 28 |
+
│ │ │ ├── __init__.py
|
| 29 |
+
│ │ │ ├── base.py
|
| 30 |
+
│ │ │ └── gdrive.py
|
| 31 |
+
│ │ └── storage/
|
| 32 |
+
│ │ ├── __init__.py
|
| 33 |
+
│ │ ├── base.py
|
| 34 |
+
│ │ └── gcs.py
|
| 35 |
+
│ ├── models/
|
| 36 |
+
│ │ ├── __init__.py
|
| 37 |
+
│ │ ├── internal.py # Document, SourceType, etc.
|
| 38 |
+
│ │ └── claims.py # Claim, Citation, DiligenceFlag
|
| 39 |
+
│ ├── tracing/
|
| 40 |
+
│ │ ├── __init__.py
|
| 41 |
+
│ │ ├── tracer.py
|
| 42 |
+
│ │ └── store.py
|
| 43 |
+
│ ├── utils/
|
| 44 |
+
│ │ ├── __init__.py
|
| 45 |
+
│ │ ├── file_parser.py
|
| 46 |
+
│ │ └── markdown.py
|
| 47 |
+
│ └── config/
|
| 48 |
+
│ ├── __init__.py
|
| 49 |
+
│ └── base.py # Shared config utilities
|
| 50 |
+
│
|
| 51 |
+
├── apps/ # Gradio applications
|
| 52 |
+
│ ├── __init__.py
|
| 53 |
+
│ ├── report_gen/ # Report generation app
|
| 54 |
+
│ │ ├── __init__.py
|
| 55 |
+
│ │ ├── app.py # Main Gradio UI
|
| 56 |
+
│ │ ├── routes.py # Generation logic
|
| 57 |
+
│ │ ├── models.py # Response schemas (StartupOverview, etc.)
|
| 58 |
+
│ │ └── config/
|
| 59 |
+
│ │ ├── __init__.py
|
| 60 |
+
│ │ ├── rules.py # SectionGenerationRule definitions
|
| 61 |
+
│ │ └── prompts.py # Prompt templates
|
| 62 |
+
│ │
|
| 63 |
+
│ ├── kg_dashboard/ # KG query dashboard (new)
|
| 64 |
+
│ │ ├── __init__.py
|
| 65 |
+
│ │ ├── app.py
|
| 66 |
+
│ │ └── ...
|
| 67 |
+
│ │
|
| 68 |
+
│ └── _shared/ # Shared Gradio utilities (if needed)
|
| 69 |
+
│ └── __init__.py
|
| 70 |
+
│
|
| 71 |
+
├── cli/ # CLI tools
|
| 72 |
+
│ ├── __init__.py
|
| 73 |
+
│ └── ingest/ # Ingestion pipeline
|
| 74 |
+
│ ├── __init__.py
|
| 75 |
+
│ ├── main.py # CLI entry point (click commands)
|
| 76 |
+
│ ├── orchestrator.py # IngestionOrchestrator
|
| 77 |
+
│ └── config.py # IngestionConfig, GoogleDriveConfig, etc.
|
| 78 |
+
│
|
| 79 |
+
├── tests/
|
| 80 |
+
│ ├── conftest.py
|
| 81 |
+
│ ├── unit/
|
| 82 |
+
│ │ ├── core/
|
| 83 |
+
│ │ │ ├── services/
|
| 84 |
+
│ │ │ ├── models/
|
| 85 |
+
│ │ │ └── ...
|
| 86 |
+
│ │ ├── apps/
|
| 87 |
+
│ │ │ └── report_gen/
|
| 88 |
+
│ │ └── cli/
|
| 89 |
+
│ │ └── ingest/
|
| 90 |
+
│ └── integration/
|
| 91 |
+
│
|
| 92 |
+
├── supabase/ # Unchanged
|
| 93 |
+
├── Makefile
|
| 94 |
+
├── pyproject.toml
|
| 95 |
+
├── CLAUDE.md
|
| 96 |
+
└── README.md
|
| 97 |
+
```
|
| 98 |
+
|
| 99 |
+
---
|
| 100 |
+
|
| 101 |
+
## Phase 1: Create Core Library
|
| 102 |
+
|
| 103 |
+
### Step 1.1: Create directory structure
|
| 104 |
+
|
| 105 |
+
```bash
|
| 106 |
+
mkdir -p core/{services/sources,services/storage,models,tracing,utils,config}
|
| 107 |
+
touch core/__init__.py
|
| 108 |
+
touch core/services/__init__.py
|
| 109 |
+
touch core/services/sources/__init__.py
|
| 110 |
+
touch core/services/storage/__init__.py
|
| 111 |
+
touch core/models/__init__.py
|
| 112 |
+
touch core/tracing/__init__.py
|
| 113 |
+
touch core/utils/__init__.py
|
| 114 |
+
touch core/config/__init__.py
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
### Step 1.2: Move shared services
|
| 118 |
+
|
| 119 |
+
| From | To |
|
| 120 |
+
|------|-----|
|
| 121 |
+
| `services/llm.py` | `core/services/llm.py` |
|
| 122 |
+
| `services/kg.py` | `core/services/kg.py` |
|
| 123 |
+
| `services/embedding.py` | `core/services/embedding.py` |
|
| 124 |
+
| `services/claims.py` | `core/services/claims.py` |
|
| 125 |
+
| `services/sources/base.py` | `core/services/sources/base.py` |
|
| 126 |
+
| `services/sources/gdrive.py` | `core/services/sources/gdrive.py` |
|
| 127 |
+
| `services/storage/base.py` | `core/services/storage/base.py` |
|
| 128 |
+
| `services/storage/gcs.py` | `core/services/storage/gcs.py` |
|
| 129 |
+
|
| 130 |
+
### Step 1.3: Move shared models
|
| 131 |
+
|
| 132 |
+
| From | To |
|
| 133 |
+
|------|-----|
|
| 134 |
+
| `models/internal.py` | `core/models/internal.py` |
|
| 135 |
+
| `models/claims.py` | `core/models/claims.py` |
|
| 136 |
+
|
| 137 |
+
### Step 1.4: Move tracing
|
| 138 |
+
|
| 139 |
+
| From | To |
|
| 140 |
+
|------|-----|
|
| 141 |
+
| `tracing/*.py` | `core/tracing/*.py` |
|
| 142 |
+
|
| 143 |
+
### Step 1.5: Move utils
|
| 144 |
+
|
| 145 |
+
| From | To |
|
| 146 |
+
|------|-----|
|
| 147 |
+
| `utils/file_parser.py` | `core/utils/file_parser.py` |
|
| 148 |
+
| `utils/markdown.py` | `core/utils/markdown.py` (if exists) |
|
| 149 |
+
|
| 150 |
+
### Step 1.6: Create core config base
|
| 151 |
+
|
| 152 |
+
Create `core/config/base.py` with shared configuration utilities (env loading helpers, base config classes).
|
| 153 |
+
|
| 154 |
+
### Step 1.7: Update core `__init__.py` files
|
| 155 |
+
|
| 156 |
+
```python
|
| 157 |
+
# core/__init__.py
|
| 158 |
+
"""Core shared library for day1-gen projects."""
|
| 159 |
+
|
| 160 |
+
# core/services/__init__.py
|
| 161 |
+
from core.services.llm import LlmProvider, GeminiLLM, OpenAiLLM
|
| 162 |
+
from core.services.kg import KGClient
|
| 163 |
+
from core.services.embedding import DocumentEmbeddingService
|
| 164 |
+
from core.services.claims import ClaimExtractor
|
| 165 |
+
|
| 166 |
+
# core/models/__init__.py
|
| 167 |
+
from core.models.internal import Document, SourceType, IngestionSource, RAGChunkDoc
|
| 168 |
+
from core.models.claims import Claim, Citation, DiligenceFlag
|
| 169 |
+
```
|
| 170 |
+
|
| 171 |
+
---
|
| 172 |
+
|
| 173 |
+
## Phase 2: Create Apps Directory
|
| 174 |
+
|
| 175 |
+
### Step 2.1: Create directory structure
|
| 176 |
+
|
| 177 |
+
```bash
|
| 178 |
+
mkdir -p apps/report_gen/config
|
| 179 |
+
mkdir -p apps/kg_dashboard
|
| 180 |
+
mkdir -p apps/_shared
|
| 181 |
+
touch apps/__init__.py
|
| 182 |
+
touch apps/report_gen/__init__.py
|
| 183 |
+
touch apps/report_gen/config/__init__.py
|
| 184 |
+
touch apps/kg_dashboard/__init__.py
|
| 185 |
+
touch apps/_shared/__init__.py
|
| 186 |
+
```
|
| 187 |
+
|
| 188 |
+
### Step 2.2: Move report generation app
|
| 189 |
+
|
| 190 |
+
| From | To |
|
| 191 |
+
|------|-----|
|
| 192 |
+
| `app.py` | `apps/report_gen/app.py` |
|
| 193 |
+
| `api/routes.py` | `apps/report_gen/routes.py` |
|
| 194 |
+
| `models/response.py` | `apps/report_gen/models.py` |
|
| 195 |
+
| `config/rules.py` | `apps/report_gen/config/rules.py` |
|
| 196 |
+
| `config/prompts.py` | `apps/report_gen/config/prompts.py` |
|
| 197 |
+
|
| 198 |
+
### Step 2.3: Create root wrapper for HuggingFace Spaces
|
| 199 |
+
|
| 200 |
+
HuggingFace Spaces requires `app.py` at root for auto-deployment. Create a **multi-app selector** using an environment variable:
|
| 201 |
+
|
| 202 |
+
```python
|
| 203 |
+
# app.py (root level)
|
| 204 |
+
"""
|
| 205 |
+
HuggingFace Spaces entry point.
|
| 206 |
+
Set APP env var to choose which app to launch.
|
| 207 |
+
|
| 208 |
+
Usage:
|
| 209 |
+
- HF Spaces: Set APP=report_gen or APP=kg_dashboard in Space settings
|
| 210 |
+
- Local: APP=kg_dashboard python app.py
|
| 211 |
+
- Default: report_gen
|
| 212 |
+
"""
|
| 213 |
+
import os
|
| 214 |
+
|
| 215 |
+
APP = os.environ.get("APP", "report_gen")
|
| 216 |
+
|
| 217 |
+
if APP == "report_gen":
|
| 218 |
+
from apps.report_gen.app import demo
|
| 219 |
+
elif APP == "kg_dashboard":
|
| 220 |
+
from apps.kg_dashboard.app import demo
|
| 221 |
+
# Add future apps here:
|
| 222 |
+
# elif APP == "some_future_app":
|
| 223 |
+
# from apps.some_future_app.app import demo
|
| 224 |
+
else:
|
| 225 |
+
raise ValueError(f"Unknown APP: {APP}. Options: report_gen, kg_dashboard")
|
| 226 |
+
|
| 227 |
+
if __name__ == "__main__":
|
| 228 |
+
demo.launch()
|
| 229 |
+
```
|
| 230 |
+
|
| 231 |
+
Each app in `apps/` exports a `demo` object:
|
| 232 |
+
|
| 233 |
+
```python
|
| 234 |
+
# apps/report_gen/app.py
|
| 235 |
+
import gradio as gr
|
| 236 |
+
|
| 237 |
+
# ... all app code ...
|
| 238 |
+
|
| 239 |
+
demo = gr.Blocks(title="Report Generator")
|
| 240 |
+
|
| 241 |
+
with demo:
|
| 242 |
+
# ... UI components ...
|
| 243 |
+
pass
|
| 244 |
+
|
| 245 |
+
if __name__ == "__main__":
|
| 246 |
+
demo.launch()
|
| 247 |
+
```
|
| 248 |
+
|
| 249 |
+
```python
|
| 250 |
+
# apps/kg_dashboard/app.py
|
| 251 |
+
import gradio as gr
|
| 252 |
+
from core.services.kg import KGClient
|
| 253 |
+
|
| 254 |
+
# ... dashboard code ...
|
| 255 |
+
|
| 256 |
+
demo = gr.Blocks(title="Knowledge Graph Dashboard")
|
| 257 |
+
|
| 258 |
+
with demo:
|
| 259 |
+
# ... UI components ...
|
| 260 |
+
pass
|
| 261 |
+
|
| 262 |
+
if __name__ == "__main__":
|
| 263 |
+
demo.launch()
|
| 264 |
+
```
|
| 265 |
+
|
| 266 |
+
**Deployment options:**
|
| 267 |
+
- **Same HF Space, different deploys**: Set `APP` env var per deployment
|
| 268 |
+
- **Separate HF Spaces**: Each Space sets its own `APP` value
|
| 269 |
+
- **Local dev**: Use Makefile shortcuts (`make gradio`, `make kg-dashboard`)
|
| 270 |
+
|
| 271 |
+
**Makefile updates:**
|
| 272 |
+
```makefile
|
| 273 |
+
gradio:
|
| 274 |
+
APP=report_gen uv run python app.py
|
| 275 |
+
|
| 276 |
+
kg-dashboard:
|
| 277 |
+
APP=kg_dashboard uv run python app.py
|
| 278 |
+
```
|
| 279 |
+
|
| 280 |
+
### Step 2.4: Update imports in report_gen
|
| 281 |
+
|
| 282 |
+
In `apps/report_gen/app.py`:
|
| 283 |
+
```python
|
| 284 |
+
# Old
|
| 285 |
+
from services.llm import GeminiLLM, OpenAiLLM
|
| 286 |
+
from models.internal import Document
|
| 287 |
+
from api.routes import run_im_feeling_lucky
|
| 288 |
+
from config.rules import OVERVIEW_RULE
|
| 289 |
+
|
| 290 |
+
# New
|
| 291 |
+
from core.services.llm import GeminiLLM, OpenAiLLM
|
| 292 |
+
from core.models.internal import Document
|
| 293 |
+
from apps.report_gen.routes import run_im_feeling_lucky
|
| 294 |
+
from apps.report_gen.config.rules import OVERVIEW_RULE
|
| 295 |
+
```
|
| 296 |
+
|
| 297 |
+
In `apps/report_gen/routes.py`:
|
| 298 |
+
```python
|
| 299 |
+
# Old
|
| 300 |
+
from services.llm import LlmProvider
|
| 301 |
+
from services.embedding import DocumentEmbeddingService
|
| 302 |
+
from models.internal import Document
|
| 303 |
+
|
| 304 |
+
# New
|
| 305 |
+
from core.services.llm import LlmProvider
|
| 306 |
+
from core.services.embedding import DocumentEmbeddingService
|
| 307 |
+
from core.models.internal import Document
|
| 308 |
+
```
|
| 309 |
+
|
| 310 |
+
In `apps/report_gen/config/rules.py`:
|
| 311 |
+
```python
|
| 312 |
+
# Old
|
| 313 |
+
from models.response import StartupOverview, KeyDebates
|
| 314 |
+
|
| 315 |
+
# New
|
| 316 |
+
from apps.report_gen.models import StartupOverview, KeyDebates
|
| 317 |
+
```
|
| 318 |
+
|
| 319 |
+
---
|
| 320 |
+
|
| 321 |
+
## Phase 3: Reorganize CLI
|
| 322 |
+
|
| 323 |
+
### Step 3.1: Create directory structure
|
| 324 |
+
|
| 325 |
+
```bash
|
| 326 |
+
mkdir -p cli/ingest
|
| 327 |
+
touch cli/__init__.py
|
| 328 |
+
touch cli/ingest/__init__.py
|
| 329 |
+
```
|
| 330 |
+
|
| 331 |
+
### Step 3.2: Move ingestion CLI
|
| 332 |
+
|
| 333 |
+
| From | To |
|
| 334 |
+
|------|-----|
|
| 335 |
+
| `cli/ingest.py` | `cli/ingest/main.py` |
|
| 336 |
+
| `services/ingestion/orchestrator.py` | `cli/ingest/orchestrator.py` |
|
| 337 |
+
| `config/ingestion.py` | `cli/ingest/config.py` |
|
| 338 |
+
| `config/claim_rules.py` | `cli/ingest/claim_rules.py` (if CLI-specific) |
|
| 339 |
+
|
| 340 |
+
### Step 3.3: Update imports in CLI
|
| 341 |
+
|
| 342 |
+
In `cli/ingest/main.py`:
|
| 343 |
+
```python
|
| 344 |
+
# Old
|
| 345 |
+
from services.ingestion.orchestrator import IngestionOrchestrator
|
| 346 |
+
from services.sources.gdrive import GoogleDriveSource
|
| 347 |
+
from services.storage.gcs import GCSStorageBackend
|
| 348 |
+
from services.kg import KGClient
|
| 349 |
+
from config.ingestion import IngestionConfig
|
| 350 |
+
|
| 351 |
+
# New
|
| 352 |
+
from cli.ingest.orchestrator import IngestionOrchestrator
|
| 353 |
+
from cli.ingest.config import IngestionConfig
|
| 354 |
+
from core.services.sources.gdrive import GoogleDriveSource
|
| 355 |
+
from core.services.storage.gcs import GCSStorageBackend
|
| 356 |
+
from core.services.kg import KGClient
|
| 357 |
+
```
|
| 358 |
+
|
| 359 |
+
In `cli/ingest/orchestrator.py`:
|
| 360 |
+
```python
|
| 361 |
+
# Old
|
| 362 |
+
from services.sources.base import ContentSource
|
| 363 |
+
from services.storage.base import StorageBackend
|
| 364 |
+
from services.kg import KGClient
|
| 365 |
+
from services.claims import ClaimExtractor
|
| 366 |
+
|
| 367 |
+
# New
|
| 368 |
+
from core.services.sources.base import ContentSource
|
| 369 |
+
from core.services.storage.base import StorageBackend
|
| 370 |
+
from core.services.kg import KGClient
|
| 371 |
+
from core.services.claims import ClaimExtractor
|
| 372 |
+
```
|
| 373 |
+
|
| 374 |
+
---
|
| 375 |
+
|
| 376 |
+
## Phase 4: Reorganize Tests
|
| 377 |
+
|
| 378 |
+
### Step 4.1: Create new test structure
|
| 379 |
+
|
| 380 |
+
```bash
|
| 381 |
+
mkdir -p tests/unit/core/{services,models,tracing,utils}
|
| 382 |
+
mkdir -p tests/unit/core/services/{sources,storage}
|
| 383 |
+
mkdir -p tests/unit/apps/report_gen
|
| 384 |
+
mkdir -p tests/unit/cli/ingest
|
| 385 |
+
```
|
| 386 |
+
|
| 387 |
+
### Step 4.2: Move tests
|
| 388 |
+
|
| 389 |
+
| From | To |
|
| 390 |
+
|------|-----|
|
| 391 |
+
| `tests/unit/test_kg.py` | `tests/unit/core/services/test_kg.py` |
|
| 392 |
+
| `tests/unit/test_claims.py` | `tests/unit/core/services/test_claims.py` |
|
| 393 |
+
| `tests/unit/sources/` | `tests/unit/core/services/sources/` |
|
| 394 |
+
| `tests/unit/storage/` | `tests/unit/core/services/storage/` |
|
| 395 |
+
| `tests/unit/ingestion/` | `tests/unit/cli/ingest/` |
|
| 396 |
+
| `tests/unit/cli/` | `tests/unit/cli/ingest/` (merge) |
|
| 397 |
+
|
| 398 |
+
### Step 4.3: Update test imports
|
| 399 |
+
|
| 400 |
+
All tests need import path updates matching the new structure.
|
| 401 |
+
|
| 402 |
+
Example in `tests/unit/core/services/test_kg.py`:
|
| 403 |
+
```python
|
| 404 |
+
# Old
|
| 405 |
+
from services.kg import KGClient
|
| 406 |
+
|
| 407 |
+
# New
|
| 408 |
+
from core.services.kg import KGClient
|
| 409 |
+
```
|
| 410 |
+
|
| 411 |
+
---
|
| 412 |
+
|
| 413 |
+
## Phase 5: Update Configuration Files
|
| 414 |
+
|
| 415 |
+
### Step 5.1: Update pyproject.toml
|
| 416 |
+
|
| 417 |
+
```toml
|
| 418 |
+
[project]
|
| 419 |
+
name = "day1-gen"
|
| 420 |
+
# ...
|
| 421 |
+
|
| 422 |
+
[project.scripts]
|
| 423 |
+
# CLI entry points
|
| 424 |
+
ingest = "cli.ingest.main:cli"
|
| 425 |
+
|
| 426 |
+
[tool.pytest.ini_options]
|
| 427 |
+
pythonpath = ["."]
|
| 428 |
+
testpaths = ["tests"]
|
| 429 |
+
|
| 430 |
+
[tool.mypy]
|
| 431 |
+
# Update paths
|
| 432 |
+
files = ["core/", "apps/", "cli/"]
|
| 433 |
+
|
| 434 |
+
[tool.ruff]
|
| 435 |
+
# Update paths if needed
|
| 436 |
+
src = ["core", "apps", "cli"]
|
| 437 |
+
```
|
| 438 |
+
|
| 439 |
+
### Step 5.2: Update Makefile
|
| 440 |
+
|
| 441 |
+
```makefile
|
| 442 |
+
# Update paths for linting/typecheck
|
| 443 |
+
CORE_PATH = core/
|
| 444 |
+
APPS_PATH = apps/
|
| 445 |
+
CLI_PATH = cli/
|
| 446 |
+
|
| 447 |
+
typecheck:
|
| 448 |
+
uv run mypy $(CORE_PATH) $(APPS_PATH) $(CLI_PATH)
|
| 449 |
+
|
| 450 |
+
lint:
|
| 451 |
+
uv run ruff check $(CORE_PATH) $(APPS_PATH) $(CLI_PATH)
|
| 452 |
+
|
| 453 |
+
# Update Gradio commands
|
| 454 |
+
gradio:
|
| 455 |
+
set -a && source .env.local && set +a && uv run python -m apps.report_gen.app
|
| 456 |
+
|
| 457 |
+
gradio-prod:
|
| 458 |
+
set -a && source .env.prod && set +a && uv run python -m apps.report_gen.app
|
| 459 |
+
|
| 460 |
+
# Add new dashboard command (when ready)
|
| 461 |
+
kg-dashboard:
|
| 462 |
+
set -a && source .env.local && set +a && uv run python -m apps.kg_dashboard.app
|
| 463 |
+
|
| 464 |
+
# Update CLI command
|
| 465 |
+
ingest-sync:
|
| 466 |
+
source .env.local && uv run python -m cli.ingest.main sync --full
|
| 467 |
+
```
|
| 468 |
+
|
| 469 |
+
### Step 5.3: Update CLAUDE.md
|
| 470 |
+
|
| 471 |
+
Update all paths and commands to reflect new structure.
|
| 472 |
+
|
| 473 |
+
---
|
| 474 |
+
|
| 475 |
+
## Phase 6: Cleanup
|
| 476 |
+
|
| 477 |
+
### Step 6.1: Remove old directories
|
| 478 |
+
|
| 479 |
+
After verifying everything works:
|
| 480 |
+
|
| 481 |
+
```bash
|
| 482 |
+
# Remove old locations (after confirming new structure works)
|
| 483 |
+
rm -rf services/
|
| 484 |
+
rm -rf models/
|
| 485 |
+
rm -rf api/
|
| 486 |
+
rm -rf config/ # Keep if any shared config remains
|
| 487 |
+
rm app.py # Moved to apps/report_gen/
|
| 488 |
+
```
|
| 489 |
+
|
| 490 |
+
### Step 6.2: Update .gitignore if needed
|
| 491 |
+
|
| 492 |
+
Ensure any new paths are properly handled.
|
| 493 |
+
|
| 494 |
+
---
|
| 495 |
+
|
| 496 |
+
## Import Cheatsheet
|
| 497 |
+
|
| 498 |
+
Quick reference for updating imports:
|
| 499 |
+
|
| 500 |
+
| Old Import | New Import |
|
| 501 |
+
|------------|------------|
|
| 502 |
+
| `from services.llm import ...` | `from core.services.llm import ...` |
|
| 503 |
+
| `from services.kg import ...` | `from core.services.kg import ...` |
|
| 504 |
+
| `from services.embedding import ...` | `from core.services.embedding import ...` |
|
| 505 |
+
| `from services.claims import ...` | `from core.services.claims import ...` |
|
| 506 |
+
| `from services.sources.gdrive import ...` | `from core.services.sources.gdrive import ...` |
|
| 507 |
+
| `from services.storage.gcs import ...` | `from core.services.storage.gcs import ...` |
|
| 508 |
+
| `from models.internal import ...` | `from core.models.internal import ...` |
|
| 509 |
+
| `from models.claims import ...` | `from core.models.claims import ...` |
|
| 510 |
+
| `from models.response import ...` | `from apps.report_gen.models import ...` |
|
| 511 |
+
| `from tracing import ...` | `from core.tracing import ...` |
|
| 512 |
+
| `from utils.file_parser import ...` | `from core.utils.file_parser import ...` |
|
| 513 |
+
| `from api.routes import ...` | `from apps.report_gen.routes import ...` |
|
| 514 |
+
| `from config.rules import ...` | `from apps.report_gen.config.rules import ...` |
|
| 515 |
+
| `from config.prompts import ...` | `from apps.report_gen.config.prompts import ...` |
|
| 516 |
+
| `from config.ingestion import ...` | `from cli.ingest.config import ...` |
|
| 517 |
+
| `from services.ingestion.orchestrator import ...` | `from cli.ingest.orchestrator import ...` |
|
| 518 |
+
|
| 519 |
+
---
|
| 520 |
+
|
| 521 |
+
## Verification Checklist
|
| 522 |
+
|
| 523 |
+
After each phase, verify:
|
| 524 |
+
|
| 525 |
+
- [ ] `make lint` passes
|
| 526 |
+
- [ ] `make typecheck` passes
|
| 527 |
+
- [ ] `make test-unit` passes
|
| 528 |
+
- [ ] `make gradio` starts successfully
|
| 529 |
+
- [ ] CLI commands work: `source .env.local && uv run python -m cli.ingest.main --help`
|
| 530 |
+
|
| 531 |
+
---
|
| 532 |
+
|
| 533 |
+
## Future: Adding New Gradio Apps
|
| 534 |
+
|
| 535 |
+
To add a new Gradio app (e.g., KG Dashboard):
|
| 536 |
+
|
| 537 |
+
1. Create directory: `mkdir -p apps/kg_dashboard`
|
| 538 |
+
2. Create `apps/kg_dashboard/__init__.py`
|
| 539 |
+
3. Create `apps/kg_dashboard/app.py` with Gradio UI
|
| 540 |
+
4. Import from `core.services.kg` for KG queries
|
| 541 |
+
5. Add Makefile target: `kg-dashboard: ... uv run python -m apps.kg_dashboard.app`
|
| 542 |
+
|
| 543 |
+
Example minimal `apps/kg_dashboard/app.py`:
|
| 544 |
+
```python
|
| 545 |
+
import gradio as gr
|
| 546 |
+
from core.services.kg import KGClient
|
| 547 |
+
|
| 548 |
+
def create_app():
|
| 549 |
+
kg = KGClient()
|
| 550 |
+
|
| 551 |
+
with gr.Blocks() as app:
|
| 552 |
+
gr.Markdown("# Knowledge Graph Dashboard")
|
| 553 |
+
# ... UI components
|
| 554 |
+
|
| 555 |
+
return app
|
| 556 |
+
|
| 557 |
+
if __name__ == "__main__":
|
| 558 |
+
app = create_app()
|
| 559 |
+
app.launch()
|
| 560 |
+
```
|
| 561 |
+
|
| 562 |
+
---
|
| 563 |
+
|
| 564 |
+
## Future: Containerizing CLI for GCP
|
| 565 |
+
|
| 566 |
+
The CLI is already isolated in `cli/ingest/`. To containerize:
|
| 567 |
+
|
| 568 |
+
```dockerfile
|
| 569 |
+
# Dockerfile.ingest
|
| 570 |
+
FROM python:3.11-slim
|
| 571 |
+
|
| 572 |
+
WORKDIR /app
|
| 573 |
+
|
| 574 |
+
# Install uv
|
| 575 |
+
RUN pip install uv
|
| 576 |
+
|
| 577 |
+
# Copy only what's needed
|
| 578 |
+
COPY pyproject.toml uv.lock ./
|
| 579 |
+
COPY core/ ./core/
|
| 580 |
+
COPY cli/ ./cli/
|
| 581 |
+
|
| 582 |
+
# Install dependencies
|
| 583 |
+
RUN uv sync --frozen
|
| 584 |
+
|
| 585 |
+
# Entry point
|
| 586 |
+
ENTRYPOINT ["uv", "run", "python", "-m", "cli.ingest.main"]
|
| 587 |
+
CMD ["sync", "--full"]
|
| 588 |
+
```
|
| 589 |
+
|
| 590 |
+
---
|
| 591 |
+
|
| 592 |
+
## Notes
|
| 593 |
+
|
| 594 |
+
- **Do phases incrementally** - Complete and test each phase before moving to the next
|
| 595 |
+
- **Use git branches** - Create a branch for this refactor, merge when stable
|
| 596 |
+
- **IDE refactoring** - Use IDE "move" features when possible to auto-update imports
|
| 597 |
+
- **Search for imports** - After moving files, search for old import paths: `grep -r "from services\." --include="*.py"`
|