# afspies/ai-safety-papers architecture

> Architecture diagram of the GitHub repository afspies/ai-safety-papers, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/afspies/ai-safety-papers
- Repository: https://github.com/afspies/ai-safety-papers

## Overview

##### Project Type and Purpose
- The project is a full-stack research paper tracking and summarization system.
- Its core purpose is to automatically fetch AI safety papers, run AI-powered summarizations and figure extraction routines, and then distribute the content via a static website (currently with a Next.js–like frontend and/or a Hugo-based static site) and planned channels like Telegram and Twitter.

##### Analyzing the File Structure
- The top-level directories are “backend” and “frontend”, which indicates a clear separation between the processing backend and the client-side (or website) frontend.
– The backend directory contains a variety of modules: APIs (in backend/src/api), figure extraction (backend/src/figure_extraction), models (backend/src/models), summarizer functionality (backend/src/summarizer), and utility functions. There’s also a rich set of test cases and archived scripts.
– The frontend directory contains components, pages, hooks, and configuration files (with Next.js–styled file names like layout.tsx, page.tsx and next.config files), suggesting an interactive website for viewing the curated paper archive.
- Additionally, configuration files, deployment scripts, and environment templates are present, indicating careful separation of concerns and external dependencies, such as Cloudflare R2 integration, Google Sheets, Semantic Scholar, and the Anthropic Claude API.

##### Key Components
a. External Services / APIs:
– Semantic Scholar API (for paper discovery)
– Anthropic Claude API (for AI summarization)
– Google Sheets API (for tracking and managing papers)
– Cloudflare R2 (as seen in backend/src/utils/cloudflare_r2.py) for asset storage
– Future/planned integrations: Telegram bot and Twitter bot
b. Backend System:
– API Subsystem: API endpoints, routers, and schemas (backend/src/api)
– Processing and Pipeline Subsystem: Includes paper fetching (from Semantic Scholar), Claude-based summarization (backend/src/summarizer), and figure extraction and processing routines (backend/src/figure_extraction)
– Models: Data representation for articles, figures, posts, and superseded storage integration (backend/src/models)
– Utility Modules: Configuration loader, PDF utilities, cloud integrations
– Data Storage: Processed papers stored as Markdown files under backend/processed_papers and scripts to regenerate or sync these files
– Testing & Scripting: Various tests and helper scripts to validate API integration and processing scripts
c. Frontend / Static Website:
– The frontend directory encompasses the presentation layer, with components (UI components, paper cards, navigation bars, etc.), pages, and layouts.
– There is also a deployment configuration (e.g., Netlify-related badges in README) and integration with static site generation (the README references Hugo, while the file tree shows a Next.js structure – it might be that both approaches are supported or that the system evolved over time).

##### Relationships and Data Flow
- Data originates from external services:
– Semantic Scholar is queried to discover or update research papers.
– Paper data is sent to the backend where it is processed; summaries are generated via Claude and figures are extracted from PDFs.
– Google Sheets is used to track and manage paper metadata.
- The processed paper content (stored as markdown files) is then fed into the static website generator or served by the web frontend.
- There is a feedback loop from deployment (via Netlify or equivalent) back to the source control and pipeline automation.

##### Architectural Patterns / Design Principles
- Separation of Concerns: Clear splitting between backend processing and frontend presentation.
- Modular Design: Distinct directories for APIs, processing (summarization, figure extraction), and models.
- Integration of External Services: Use of dedicated modules for interacting with external APIs (Semantic Scholar, Google Sheets, Claude).
- Testing and Maintenance: Presence of comprehensive test suites, archived scripts, and configuration management indicates robust design and ease of maintenance.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Services
    subgraph "External Services"
        ext1["Semantic Scholar API"] 
        ext2["Anthropic Claude API"]
        ext3["Google Sheets API"]
        ext4["Cloudflare R2 API"]
        ext5["Telegram Bot (Planned)"]
        ext6["Twitter Bot (Planned)"]
    end

    %% Backend Processing
    subgraph "Backend Processing"
        b1["Paper Fetcher Module"]
        b2["Summarizer Module"]
        b3["Figure Extraction Module"]
        b4["API Router & Schemas"]
        b5["Data Models"]
        b6["Markdown Paper Storage"]
        b7["Utility Modules"]
    end

    %% Frontend / Static Website
    subgraph "Frontend / Static Website"
        f1["Static Website"]
        f2["User Interface"]
    end

    %% Data Flow Connections
    ext1 -- "fetches" --> b1
    b1 -- "sends_data" --> b2
    b1 -- "sends_data" --> b3
    b2 -- "summarizes_via" --> ext2
    b2 -- "updates" --> b5
    b3 -- "updates" --> b5
    b5 -- "stored_as" --> b6
    ext3 -- "tracks" --> b5
    b7 -- "handles" --> ext4
    b4 -- "utilizes" --> b5
    b6 -- "provides_content" --> f1
    f1 -- "renders" --> f2
    b2 -- "publishes_to" --> ext5
    b2 -- "publishes_to" --> ext6

    %% Click Events for Component Mapping
    click ext1 "https://github.com/afspies/ai-safety-papers/blob/main/backend/src/api/semantic_scholar.py"
    click b2 "https://github.com/afspies/ai-safety-papers/blob/main/backend/src/summarizer/paper_summarizer.py"
    click b2 "https://github.com/afspies/ai-safety-papers/blob/main/backend/src/summarizer/web_summarizer.py"
    click ext3 "https://github.com/afspies/ai-safety-papers/blob/main/backend/archive/sheets_db/sheets/sheets_db.py"
    click b7 "https://github.com/afspies/ai-safety-papers/blob/main/backend/src/utils/cloudflare_r2.py"
    click b4 "https://github.com/afspies/ai-safety-papers/blob/main/backend/src/api/routers.py"
    click b4 "https://github.com/afspies/ai-safety-papers/blob/main/backend/src/api/schemas.py"
    click b3 "https://github.com/afspies/ai-safety-papers/blob/main/backend/src/figure_extraction/ar5iv_figure_extractor.py"
    click b3 "https://github.com/afspies/ai-safety-papers/blob/main/backend/src/figure_extraction/pdf_figure_extractor.py"
    click b3 "https://github.com/afspies/ai-safety-papers/blob/main/backend/src/figure_extraction/figure_extractor_manager.py"
    click b5 "https://github.com/afspies/ai-safety-papers/tree/main/backend/src/models/"
    click b6 "https://github.com/afspies/ai-safety-papers/tree/main/backend/processed_papers/posts/"
    click f1 "https://github.com/afspies/ai-safety-papers/tree/main/frontend/"

    %% Styles
    class ext1,ext2,ext3,ext4,ext5,ext6 extStyle
    class b1,b2,b3,b4,b5,b6,b7 backendStyle
    class f1,f2 frontendStyle

    %% Class Definitions
    classDef extStyle fill:#F8E0E6,stroke:#C71585,stroke-width:2px;
    classDef backendStyle fill:#E0F7FA,stroke:#00838F,stroke-width:2px;
    classDef frontendStyle fill:#E8F5E9,stroke:#388E3C,stroke-width:2px;
```
