# andreaswt/ai-video-sentiment-saas architecture

> Architecture diagram of the GitHub repository andreaswt/ai-video-sentiment-saas, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/andreaswt/ai-video-sentiment-saas
- Repository: https://github.com/andreaswt/ai-video-sentiment-saas

## Overview

##### Project type and purpose
- This is a full-stack SaaS application (“ai-video-sentiment-saas”) built with the T3 Stack (Next.js, React, Tailwind, Auth.js, Prisma).
- It provides video sentiment and emotion inference via AWS SageMaker endpoints, persists user data and API-key/quota information in a relational database (Prisma), and stores uploaded videos in AWS S3.

##### File structure
- Top-level config files: next.config.js, Prisma schema (prisma/schema.prisma), Tailwind and PostCSS configs, Auth.js (src/env.js), TypeScript config.
- Frontend/UI: src/app (Next.js pages, layouts, login/signup), src/components (client-side React components).
- API layer: src/app/api – serverless routes for auth ([...nextauth]/route.ts), presigned upload URLs (upload-url/route.ts), sentiment-inference (sentiment-inference/route.ts).
- Business logic modules: src/actions (high-level operations like auth.ts), src/lib (quota enforcement, helper functions), src/schemas (data validation).
- Server utilities: src/server/auth (Auth.js configuration), src/server/db.ts (Prisma client).
- Static assets: public/, styles/, thumbnail.png.

##### README for additional insights
- The system stages are:
a. User authentication & session management (Auth.js + NextAuth).
b. Video upload: frontend obtains a presigned URL, uploads to S3.
c. Inference invocation: API route calls SageMaker endpoint, returns sentiment/emotion results.
d. Quota tracking: each inference decrements the user’s monthly quota in the database.
e. Data persistence: user profiles, API keys, usage logs stored via Prisma/DB.

##### Main components and their interactions
a. Frontend (Next.js + React + Tailwind)
- Login/Signup pages
- Video uploader and Inference UI components
- Calls API endpoints under /api
b. Authentication layer
- NextAuth routes ([...nextauth])
- Auth.js config in src/server/auth/config.ts
- Session state passed to frontend components
c. API Layer (Next.js Serverless Functions)
- /api/upload-url → generates S3 presigned PUT URLs (uses AWS SDK)
- /api/sentiment-inference → invokes AWS SageMaker endpoint, returns results
- /api/auth/[...nextauth] → standard NextAuth endpoint
d. Business Logic & Services
- src/actions/auth.ts → high-level sign-in/out logic
- src/lib/quota.ts → tracks and enforces user quotas per month
- src/schemas/auth.ts → request/response validation
e. Data Storage
- Relational Database (e.g. PostgreSQL) accessed via Prisma (prisma/schema.prisma & src/server/db.ts)
- AWS S3 bucket for video storage
f. External AI Inference Service
- AWS SageMaker Endpoint for multimodal model inference

##### Architectural patterns and design principles
- Layered serverless architecture:
- Presentation layer (Next.js pages/components)
- API layer (Next.js serverless functions under /api)
- Service/business layer (actions, lib, schemas)
- Data layer (Prisma ORM + PostgreSQL, S3)
- Separation of concerns: UI vs. data vs. AI inference vs. auth vs. quota enforcement
- Scalability via AWS services (serverless functions, S3, SageMaker)

##### Elements
- Notes around quota enforcement (src/lib/quota.ts) and API key management

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User
    User["User"]:::external

    %% Frontend Layer
    subgraph "Frontend (Next.js + React + Tailwind)" 
        direction TB
        Layout["layout.tsx"]:::frontend
        Page["page.tsx"]:::frontend
        LoginPage["login/page.tsx"]:::frontend
        SignupPage["signup/page.tsx"]:::frontend
        UploadVideo["UploadVideo.tsx"]:::frontend
        InferenceComp["Inference.tsx"]:::frontend
        CodeExamples["code-examples.tsx"]:::frontend
        CopyButton["copy-button.tsx"]:::frontend
        SignOut["signout.tsx"]:::frontend
    end

    %% Auth Service Layer
    subgraph "Authentication Service" 
        direction TB
        NextAuthFunc["[...nextauth]/route.ts"]:::authLayer
        AuthConfig["config.ts"]:::authLayer
        AuthHelpers["index.ts"]:::authLayer
        EnvVars["env.js"]:::authLayer
    end

    %% API Layer
    subgraph "API Layer (Serverless Functions)"
        direction TB
        UploadURL["upload-url/route.ts"]:::api
        SentimentInf["sentiment-inference/route.ts"]:::api
    end

    %% Business Services
    subgraph "Business Services"
        direction TB
        AuthActions["auth.ts"]:::business
        QuotaLogic["quota.ts"]:::business
        ValidationSchemas["auth.ts"]:::business
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        PrismaSchema["schema.prisma"]:::data
        PrismaClient["db.ts"]:::data
    end

    %% AWS Services
    subgraph "AWS Services"
        direction TB
        S3Bucket["S3 Bucket"]:::aws
        SageMaker["SageMaker Endpoint"]:::aws
    end

    %% Connections
    User -->|"interacts with"| Layout
    Layout --> Page
    Page --> LoginPage
    Page --> SignupPage
    LoginPage -->|"uses component"| CopyButton
    SignupPage --> CopyButton
    Page --> UploadVideo
    Page --> InferenceComp
    UploadVideo -->|"calls API"| UploadURL
    InferenceComp -->|"calls API"| SentimentInf

    %% Auth flows
    LoginPage -->|"HTTP POST"| NextAuthFunc
    NextAuthFunc --> AuthConfig
    NextAuthFunc --> AuthHelpers
    NextAuthFunc --> EnvVars
    NextAuthFunc -->|"session data"| PrismaClient
    PrismaClient --> PrismaSchema

    %% API to AWS and Data
    UploadURL -->|"AWS SDK PUT"| S3Bucket
    UploadURL --> ValidationSchemas
    UploadURL --> AuthActions
    UploadURL --> PrismaClient
    SentimentInf -->|"AWS SDK invoke"| SageMaker
    SentimentInf --> ValidationSchemas
    SentimentInf --> QuotaLogic
    SentimentInf --> PrismaClient

    %% Business to Data
    AuthActions --> PrismaClient
    QuotaLogic --> PrismaClient
    ValidationSchemas --> PrismaClient

    %% Data to Database
    PrismaClient --> PrismaSchema

    %% External
    User -->|"views UI"| Page

    %% Click Events
    click Layout "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/app/layout.tsx"
    click Page "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/app/page.tsx"
    click LoginPage "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/app/login/page.tsx"
    click SignupPage "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/app/signup/page.tsx"
    click UploadVideo "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/components/client/UploadVideo.tsx"
    click InferenceComp "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/components/client/Inference.tsx"
    click CodeExamples "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/components/client/code-examples.tsx"
    click CopyButton "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/components/client/copy-button.tsx"
    click SignOut "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/components/client/signout.tsx"
    click NextAuthFunc "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/app/api/auth/[...nextauth]/route.ts"
    click AuthConfig "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/server/auth/config.ts"
    click AuthHelpers "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/server/auth/index.ts"
    click EnvVars "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/env.js"
    click UploadURL "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/app/api/upload-url/route.ts"
    click SentimentInf "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/app/api/sentiment-inference/route.ts"
    click AuthActions "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/actions/auth.ts"
    click QuotaLogic "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/lib/quota.ts"
    click ValidationSchemas "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/schemas/auth.ts"
    click PrismaSchema "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/prisma/schema.prisma"
    click PrismaClient "https://github.com/andreaswt/ai-video-sentiment-saas/blob/main/src/server/db.ts"

    %% Styles
    classDef external fill:#ffffff,stroke:#000000,color:#000
    classDef frontend fill:#d0e8ff,stroke:#0366d6,color:#0366d6
    classDef authLayer fill:#e0f7fa,stroke:#00796b,color:#00796b
    classDef api fill:#d0f8d0,stroke:#2e7d32,color:#2e7d32
    classDef business fill:#fff3cd,stroke:#c69500,color:#7a4f01
    classDef data fill:#f0f0f0,stroke:#4d4d4d,color:#333
    classDef aws fill:#ffe0b2,stroke:#e65100,color:#e65100
```
