Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c4341ca8dc | |||
| 6105ba7793 | |||
| e8cdc089fa |
+122
@@ -0,0 +1,122 @@
|
|||||||
|
# SPARC Roadmap
|
||||||
|
|
||||||
|
Semiconductor Patent & Analytics Report Core -- development priorities.
|
||||||
|
|
||||||
|
## Current State
|
||||||
|
|
||||||
|
SPARC is a patent analysis platform with a working end-to-end pipeline:
|
||||||
|
Python/FastAPI backend, React/TypeScript frontend, PostgreSQL for persistence
|
||||||
|
and caching, Docker Compose for local development, and Gitea Actions CI/CD for
|
||||||
|
image builds. Core features (patent retrieval via SerpAPI, PDF parsing, LLM
|
||||||
|
analysis via OpenRouter/Claude, batch processing, JWT authentication, analytics
|
||||||
|
dashboard) are all implemented and functional.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## P1 -- High Priority
|
||||||
|
|
||||||
|
These items address correctness, security, and reliability gaps that should be
|
||||||
|
resolved before broader production use.
|
||||||
|
|
||||||
|
### Security hardening
|
||||||
|
|
||||||
|
- **Rotate default JWT secret.** `auth.py` ships a fallback
|
||||||
|
`sparc-secret-key-change-in-production` that will be used if `JWT_SECRET` is
|
||||||
|
unset. Add a startup check that refuses to start with the default secret in
|
||||||
|
non-development environments.
|
||||||
|
- **CORS allow-origins are hardcoded.** `api.py` only permits
|
||||||
|
`localhost:3000` and `localhost:5173`. Make the allowed origins configurable
|
||||||
|
via environment variable so the dashboard works when deployed behind a real
|
||||||
|
domain.
|
||||||
|
- **Database credentials in docker-compose.yml.** The compose file embeds
|
||||||
|
`postgres:postgres` in plain text. Reference a `.env` file or Docker secrets
|
||||||
|
instead.
|
||||||
|
|
||||||
|
### Error handling and resilience
|
||||||
|
|
||||||
|
- **`get_db_client()` in `auth.py` creates a new `DatabaseClient` on every
|
||||||
|
call.** This bypasses the connection pool and can exhaust database
|
||||||
|
connections under load. Refactor to share a single pooled client.
|
||||||
|
- **`_jobs` dict is in-memory only.** Job state is lost on API restart. Persist
|
||||||
|
job status in PostgreSQL or Redis so async batch results survive restarts.
|
||||||
|
- **No rate limiting on auth endpoints.** `/auth/login` and `/auth/register`
|
||||||
|
are unprotected against brute-force or abuse. Add rate limiting middleware.
|
||||||
|
|
||||||
|
### Test coverage for auth and admin
|
||||||
|
|
||||||
|
- The existing API tests (`tests/test_api.py`) bypass authentication entirely.
|
||||||
|
Add tests that exercise the JWT flow: registration, login, protected-route
|
||||||
|
access, token refresh, and admin-only endpoints.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## P2 -- Medium Priority
|
||||||
|
|
||||||
|
Improvements to usability, performance, and developer experience.
|
||||||
|
|
||||||
|
### Backend
|
||||||
|
|
||||||
|
- **Add structured logging.** Replace `print()` calls throughout `analyzer.py`,
|
||||||
|
`serp_api.py`, and `llm.py` with Python `logging` so log levels and
|
||||||
|
formatting are consistent.
|
||||||
|
- **Make LLM model configurable.** `llm.py` hardcodes
|
||||||
|
`anthropic/claude-3.5-sonnet`. Accept a `MODEL` environment variable to allow
|
||||||
|
switching models without code changes.
|
||||||
|
- **SERP cache TTL is hardcoded to 24 hours.** Expose `SERP_CACHE_TTL_HOURS`
|
||||||
|
as an environment variable in `config.py`.
|
||||||
|
- **Patent PDF storage.** PDFs are saved to a local `patents/` directory. For
|
||||||
|
containerized deployments, consider object storage (S3/MinIO) or at minimum
|
||||||
|
document the volume mount requirement more prominently.
|
||||||
|
- **`analyze_single_patent` assumes local file path.** The method constructs
|
||||||
|
`patents/{patent_id}.pdf` and reads from disk, but does not download the PDF
|
||||||
|
first. Either integrate the download step or document the prerequisite.
|
||||||
|
- **`Patent.patent_id` typed as `int` in `types.py` but used as `str`
|
||||||
|
everywhere.** Fix the type annotation to `str`.
|
||||||
|
|
||||||
|
### Frontend
|
||||||
|
|
||||||
|
- **No loading/error states on several pages.** The Batch and Analytics pages
|
||||||
|
would benefit from skeleton loaders and user-friendly error messages.
|
||||||
|
- **No dark mode.** Tailwind is configured but no dark variant is applied.
|
||||||
|
- **Missing `package-lock.json` or `pnpm-lock.yaml`.** The frontend has no
|
||||||
|
lockfile committed, leading to non-reproducible builds.
|
||||||
|
|
||||||
|
### CI/CD
|
||||||
|
|
||||||
|
- **No test stage in the Gitea Actions workflow.** `build.yaml` builds and
|
||||||
|
pushes images but never runs `pytest`. Add a test job that gates the build.
|
||||||
|
- **No linting or type checking.** Add `ruff` (Python) and `tsc --noEmit`
|
||||||
|
(TypeScript) to CI.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## P3 -- Nice to Have
|
||||||
|
|
||||||
|
Lower-urgency enhancements and future features.
|
||||||
|
|
||||||
|
- **Export analysis reports.** Allow users to download analysis results as PDF
|
||||||
|
or CSV from the dashboard.
|
||||||
|
- **Comparison view.** Side-by-side comparison of two companies' patent
|
||||||
|
portfolios.
|
||||||
|
- **Scheduled/recurring analysis.** Periodically re-analyze tracked companies
|
||||||
|
and alert on significant changes.
|
||||||
|
- **Webhook/notification support.** Send alerts (Slack, Discord, email) when
|
||||||
|
batch jobs complete or when a company's innovation score changes
|
||||||
|
significantly.
|
||||||
|
- **Multi-model support.** Let users choose between LLM providers per analysis
|
||||||
|
(e.g., GPT-4o, Gemini, Claude) and compare outputs.
|
||||||
|
- **Patent trend charts.** Visualize patent filing frequency and technology
|
||||||
|
category distribution over time in the Analytics page.
|
||||||
|
- **API pagination.** The `/analyze/batch` and `/jobs` endpoints could benefit
|
||||||
|
from cursor-based pagination for large result sets.
|
||||||
|
- **OpenAPI client generation.** Auto-generate the TypeScript API client from
|
||||||
|
the FastAPI OpenAPI spec to keep frontend types in sync.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Infrastructure and Deployment
|
||||||
|
|
||||||
|
Kubernetes manifests, Helm charts, and cluster-level concerns (MetalLB,
|
||||||
|
storage, FluxCD sync) are tracked in the
|
||||||
|
[Talos](https://10.0.1.10/leeworks-agents/Talos) repository. File
|
||||||
|
infrastructure-related issues there, not here.
|
||||||
+21
-17
@@ -5,10 +5,13 @@ to provide company performance estimation based on patent portfolios.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import logging
|
||||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
|
|
||||||
from SPARC import config
|
from SPARC import config
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
from SPARC.database import DatabaseClient
|
from SPARC.database import DatabaseClient
|
||||||
from SPARC.serp_api import SERP
|
from SPARC.serp_api import SERP
|
||||||
from SPARC.llm import LLMAnalyzer
|
from SPARC.llm import LLMAnalyzer
|
||||||
@@ -52,13 +55,13 @@ class CompanyAnalyzer:
|
|||||||
query_hash = hashlib.sha256(company_name.lower().encode()).hexdigest()
|
query_hash = hashlib.sha256(company_name.lower().encode()).hexdigest()
|
||||||
cached_ids = self.db.get_cached_serp_query(query_hash)
|
cached_ids = self.db.get_cached_serp_query(query_hash)
|
||||||
if cached_ids is not None:
|
if cached_ids is not None:
|
||||||
print(f"Using cached SERP results for {company_name} ({len(cached_ids)} patents)")
|
logger.info("Using cached SERP results for %s (%d patents)", company_name, len(cached_ids))
|
||||||
patents = Patents(patents=[
|
patents = Patents(patents=[
|
||||||
Patent(patent_id=pid, pdf_link="")
|
Patent(patent_id=pid, pdf_link="")
|
||||||
for pid in cached_ids
|
for pid in cached_ids
|
||||||
])
|
])
|
||||||
else:
|
else:
|
||||||
print(f"Retrieving patents for {company_name}...")
|
logger.info("Retrieving patents for %s...", company_name)
|
||||||
patents = SERP.query(company_name)
|
patents = SERP.query(company_name)
|
||||||
# Cache the SERP results
|
# Cache the SERP results
|
||||||
if patents.patents:
|
if patents.patents:
|
||||||
@@ -66,12 +69,13 @@ class CompanyAnalyzer:
|
|||||||
company_name=company_name,
|
company_name=company_name,
|
||||||
query_hash=query_hash,
|
query_hash=query_hash,
|
||||||
patent_ids=[p.patent_id for p in patents.patents],
|
patent_ids=[p.patent_id for p in patents.patents],
|
||||||
|
ttl_hours=config.serp_cache_ttl_hours,
|
||||||
)
|
)
|
||||||
|
|
||||||
if not patents.patents:
|
if not patents.patents:
|
||||||
return f"No patents found for {company_name}"
|
return f"No patents found for {company_name}"
|
||||||
|
|
||||||
print(f"Found {len(patents.patents)} patents. Processing...")
|
logger.info("Found %d patents. Processing...", len(patents.patents))
|
||||||
|
|
||||||
# Download, parse, and minimize patents in parallel
|
# Download, parse, and minimize patents in parallel
|
||||||
processed_patents = []
|
processed_patents = []
|
||||||
@@ -87,12 +91,12 @@ class CompanyAnalyzer:
|
|||||||
if result:
|
if result:
|
||||||
processed_patents.append(result)
|
processed_patents.append(result)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Warning: Failed to process {patent.patent_id}: {e}")
|
logger.warning("Failed to process %s: %s", patent.patent_id, e)
|
||||||
|
|
||||||
if not processed_patents:
|
if not processed_patents:
|
||||||
return f"Failed to process any patents for {company_name}"
|
return f"Failed to process any patents for {company_name}"
|
||||||
|
|
||||||
print(f"Analyzing portfolio with LLM...")
|
logger.info("Analyzing portfolio with LLM...")
|
||||||
|
|
||||||
# Analyze the full portfolio with LLM
|
# Analyze the full portfolio with LLM
|
||||||
analysis = self.llm_analyzer.analyze_patent_portfolio(
|
analysis = self.llm_analyzer.analyze_patent_portfolio(
|
||||||
@@ -115,7 +119,7 @@ class CompanyAnalyzer:
|
|||||||
"""
|
"""
|
||||||
# Note: This simplified version assumes the patent PDF is already downloaded
|
# Note: This simplified version assumes the patent PDF is already downloaded
|
||||||
# A more complete implementation would support direct patent ID lookup
|
# A more complete implementation would support direct patent ID lookup
|
||||||
print(f"Analyzing patent {patent_id} for {company_name}...")
|
logger.info("Analyzing patent %s for %s...", patent_id, company_name)
|
||||||
|
|
||||||
patent_path = f"patents/{patent_id}.pdf"
|
patent_path = f"patents/{patent_id}.pdf"
|
||||||
|
|
||||||
@@ -169,7 +173,7 @@ class CompanyAnalyzer:
|
|||||||
|
|
||||||
return {"patent_id": patent.patent_id, "content": minimized_content}
|
return {"patent_id": patent.patent_id, "content": minimized_content}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Warning: Failed to process {patent.patent_id}: {e}")
|
logger.warning("Failed to process %s: %s", patent.patent_id, e)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _analyze_company_safe(self, company_name: str) -> CompanyAnalysisResult:
|
def _analyze_company_safe(self, company_name: str) -> CompanyAnalysisResult:
|
||||||
@@ -240,7 +244,7 @@ class CompanyAnalyzer:
|
|||||||
results: list[CompanyAnalysisResult] = []
|
results: list[CompanyAnalysisResult] = []
|
||||||
total = len(companies)
|
total = len(companies)
|
||||||
|
|
||||||
print(f"Starting batch analysis of {total} companies...")
|
logger.info("Starting batch analysis of %d companies...", total)
|
||||||
|
|
||||||
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
||||||
future_to_company = {
|
future_to_company = {
|
||||||
@@ -257,8 +261,8 @@ class CompanyAnalyzer:
|
|||||||
result = future.result()
|
result = future.result()
|
||||||
results.append(result)
|
results.append(result)
|
||||||
|
|
||||||
status = "✓" if result.success else "✗"
|
status = "OK" if result.success else "FAIL"
|
||||||
print(f"[{completed}/{total}] {status} {company}")
|
logger.info("[%d/%d] %s %s", completed, total, status, company)
|
||||||
|
|
||||||
if progress_callback:
|
if progress_callback:
|
||||||
progress_callback(company, completed, total)
|
progress_callback(company, completed, total)
|
||||||
@@ -273,12 +277,12 @@ class CompanyAnalyzer:
|
|||||||
error=str(e),
|
error=str(e),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
print(f"[{completed}/{total}] ✗ {company}: {e}")
|
logger.error("[%d/%d] FAIL %s: %s", completed, total, company, e)
|
||||||
|
|
||||||
successful = sum(1 for r in results if r.success)
|
successful = sum(1 for r in results if r.success)
|
||||||
failed = total - successful
|
failed = total - successful
|
||||||
|
|
||||||
print(f"\nBatch complete: {successful} succeeded, {failed} failed")
|
logger.info("Batch complete: %d succeeded, %d failed", successful, failed)
|
||||||
|
|
||||||
return BatchAnalysisResult(
|
return BatchAnalysisResult(
|
||||||
results=results,
|
results=results,
|
||||||
@@ -304,20 +308,20 @@ class CompanyAnalyzer:
|
|||||||
results: list[CompanyAnalysisResult] = []
|
results: list[CompanyAnalysisResult] = []
|
||||||
total = len(companies)
|
total = len(companies)
|
||||||
|
|
||||||
print(f"Starting sequential analysis of {total} companies...")
|
logger.info("Starting sequential analysis of %d companies...", total)
|
||||||
|
|
||||||
for idx, company in enumerate(companies, 1):
|
for idx, company in enumerate(companies, 1):
|
||||||
print(f"\n[{idx}/{total}] Analyzing {company}...")
|
logger.info("[%d/%d] Analyzing %s...", idx, total, company)
|
||||||
result = self._analyze_company_safe(company)
|
result = self._analyze_company_safe(company)
|
||||||
results.append(result)
|
results.append(result)
|
||||||
|
|
||||||
status = "✓" if result.success else "✗"
|
status = "OK" if result.success else "FAIL"
|
||||||
print(f"[{idx}/{total}] {status} {company}")
|
logger.info("[%d/%d] %s %s", idx, total, status, company)
|
||||||
|
|
||||||
successful = sum(1 for r in results if r.success)
|
successful = sum(1 for r in results if r.success)
|
||||||
failed = total - successful
|
failed = total - successful
|
||||||
|
|
||||||
print(f"\nBatch complete: {successful} succeeded, {failed} failed")
|
logger.info("Batch complete: %d succeeded, %d failed", successful, failed)
|
||||||
|
|
||||||
return BatchAnalysisResult(
|
return BatchAnalysisResult(
|
||||||
results=results,
|
results=results,
|
||||||
|
|||||||
+16
-1
@@ -2,11 +2,20 @@
|
|||||||
|
|
||||||
Loads environment variables from .env file for API keys and other secrets.
|
Loads environment variables from .env file for API keys and other secrets.
|
||||||
"""
|
"""
|
||||||
from dotenv import load_dotenv
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
|
# Logging configuration
|
||||||
|
log_level = os.getenv("LOG_LEVEL", "INFO").upper()
|
||||||
|
logging.basicConfig(
|
||||||
|
level=getattr(logging, log_level, logging.INFO),
|
||||||
|
format="%(asctime)s %(levelname)s %(name)s %(message)s",
|
||||||
|
)
|
||||||
|
|
||||||
# SerpAPI key for patent search
|
# SerpAPI key for patent search
|
||||||
api_key = os.getenv("API_KEY")
|
api_key = os.getenv("API_KEY")
|
||||||
|
|
||||||
@@ -30,6 +39,12 @@ use_database = os.getenv("USE_DATABASE", "false").lower() in ("true", "1", "yes"
|
|||||||
patent_search_days = int(os.getenv("PATENT_SEARCH_DAYS", "90"))
|
patent_search_days = int(os.getenv("PATENT_SEARCH_DAYS", "90"))
|
||||||
patent_thread_workers = int(os.getenv("PATENT_THREAD_WORKERS", "5"))
|
patent_thread_workers = int(os.getenv("PATENT_THREAD_WORKERS", "5"))
|
||||||
|
|
||||||
|
# LLM model to use via OpenRouter (e.g. "anthropic/claude-3.5-sonnet", "openai/gpt-4o")
|
||||||
|
model = os.getenv("MODEL", "anthropic/claude-3.5-sonnet")
|
||||||
|
|
||||||
|
# SERP cache TTL in hours (how long cached search results are considered fresh)
|
||||||
|
serp_cache_ttl_hours = int(os.getenv("SERP_CACHE_TTL_HOURS", "24"))
|
||||||
|
|
||||||
# Root path for running behind a reverse proxy (e.g., "/api" when served at /api/)
|
# Root path for running behind a reverse proxy (e.g., "/api" when served at /api/)
|
||||||
# This ensures OpenAPI docs work correctly when accessed via the proxy
|
# This ensures OpenAPI docs work correctly when accessed via the proxy
|
||||||
root_path = os.getenv("ROOT_PATH", "")
|
root_path = os.getenv("ROOT_PATH", "")
|
||||||
|
|||||||
+9
-8
@@ -1,9 +1,14 @@
|
|||||||
"""LLM integration for patent analysis using OpenRouter."""
|
"""LLM integration for patent analysis using OpenRouter."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import Dict
|
||||||
|
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
|
||||||
from SPARC import config
|
from SPARC import config
|
||||||
from SPARC.database import DatabaseClient
|
from SPARC.database import DatabaseClient
|
||||||
from typing import Dict
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class LLMAnalyzer:
|
class LLMAnalyzer:
|
||||||
@@ -20,7 +25,7 @@ class LLMAnalyzer:
|
|||||||
"""
|
"""
|
||||||
self.test_mode = test_mode
|
self.test_mode = test_mode
|
||||||
self.use_cache = use_cache if use_cache is not None else config.use_cache
|
self.use_cache = use_cache if use_cache is not None else config.use_cache
|
||||||
self.model = "anthropic/claude-3.5-sonnet"
|
self.model = config.model
|
||||||
|
|
||||||
# Always initialize database client for storage and caching
|
# Always initialize database client for storage and caching
|
||||||
self.db_client = DatabaseClient(config.database_url)
|
self.db_client = DatabaseClient(config.database_url)
|
||||||
@@ -59,11 +64,7 @@ Patent Content:
|
|||||||
Provide a concise analysis (2-3 paragraphs) focusing on what this patent reveals about the company's technical direction and competitive advantage."""
|
Provide a concise analysis (2-3 paragraphs) focusing on what this patent reveals about the company's technical direction and competitive advantage."""
|
||||||
|
|
||||||
if self.test_mode:
|
if self.test_mode:
|
||||||
print("=" * 80)
|
logger.debug("TEST MODE - Prompt that would be sent to LLM:\n%s", prompt)
|
||||||
print("TEST MODE - Prompt that would be sent to LLM:")
|
|
||||||
print("=" * 80)
|
|
||||||
print(prompt)
|
|
||||||
print("=" * 80)
|
|
||||||
return "[TEST MODE - No API call made]"
|
return "[TEST MODE - No API call made]"
|
||||||
|
|
||||||
# Check cache first
|
# Check cache first
|
||||||
@@ -165,7 +166,7 @@ Patent Portfolio:
|
|||||||
Provide a comprehensive analysis (4-5 paragraphs) with a final verdict on the company's innovation strength and performance outlook."""
|
Provide a comprehensive analysis (4-5 paragraphs) with a final verdict on the company's innovation strength and performance outlook."""
|
||||||
|
|
||||||
if self.test_mode:
|
if self.test_mode:
|
||||||
print(prompt)
|
logger.debug("TEST MODE - Portfolio prompt:\n%s", prompt)
|
||||||
return "[TEST MODE]"
|
return "[TEST MODE]"
|
||||||
|
|
||||||
metadata = {
|
metadata = {
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ from datetime import datetime
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Patent:
|
class Patent:
|
||||||
patent_id: int
|
patent_id: str
|
||||||
pdf_link: str
|
pdf_link: str
|
||||||
pdf_path: str | None = None
|
pdf_path: str | None = None
|
||||||
summary: dict | None = None
|
summary: dict | None = None
|
||||||
|
|||||||
Reference in New Issue
Block a user