# chetanaya/tweetsenseai architecture

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

- Interactive diagram: https://gitdiagram.com/chetanaya/tweetsenseai
- Repository: https://github.com/chetanaya/tweetsenseai

## Overview

##### Project Type and Purpose
- TweetSenseAI is a monolithic, full‑stack Python web application built with Streamlit.

##### File Structure
- Top‑level files only; no separate “frontend”/“backend” folders.
- app.py: the single entry point that defines UI (Streamlit) and orchestration.
- config.yaml + .env.example: configuration for API keys and app settings.
- requirements.txt: tech stack dependencies (e.g., streamlit, tweepy/twitterapi‑client, openai, pandas, plotly).

##### README Insights
- Confirms usage of: Streamlit (UI), Twitter API, OpenAI API, pandas (data), plotly/matplotlib (charts), CSV/JSON export.
- Mentions session state management (in‑memory state via Streamlit).

##### Main Components (to represent
a. User Interface Layer (Streamlit Web App)
– Hosted on Streamlit server, accessed via browser
– Sidebar for API key input, search parameters
– Main area: card/table/JSON views + charts + export buttons
b. Application Logic / Controller
– app.py handlers tie UI inputs to service functions
– Session State Manager (Streamlit session) to cache search results
c. Data Retrieval Service
– Twitter API Client (twitterapi.io or tweepy wrapper)
– Handles query construction, pagination, rate limiting
d. AI Analysis Service
– OpenAI Client for sentiment analysis & phrase extraction
– Manages prompts, receives model responses
e. Data Processing & Storage
– pandas DataFrames for transformations and aggregations
– In‑memory store via session state
– Export Module → CSV/JSON files (local download)
f. Visualization Layer
– Plotly or Matplotlib charts for metrics (volume, sentiment distribution)
– Table and card layouts rendered in Streamlit
g. Configuration & Environment
– config.yaml for default settings
– .env for secret keys (TWITTER_API_KEY, OPENAI_API_KEY)

##### Relationships & Data Flows
- User → (1) UI inputs parameters → Streamlit app
- Streamlit Controller → (2) invokes Twitter API Client → (3) returns raw tweet data
- Controller → (4) stores raw data in Session State → (5) invokes pandas for metrics
- Controller → (6) invokes OpenAI Client with tweet text → (7) receives sentiment/phrases → stores in Session
- Controller → (8) triggers Visualization Layer to render charts & tables
- User → (9) clicks “Export” → Export Module converts DataFrame → CSV/JSON download

##### Architectural Patterns & Design Principles
- Monolithic MVC‑style within a single process:
- Model: Data retrieval & processing (Twitter client, pandas)
- View: Streamlit UI components
- Controller: Handlers in app.py wiring UI ↔ services
- External Service Integration pattern for API clients
- Session State pattern for in‑memory caching between UI interactions

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Actors and External Interfaces
    User["User (Browser)"]:::external
    TwitterAPI["Twitter API"]:::external
    OpenAIAPI["OpenAI API"]:::external
    CSVJSON["CSV/JSON Downloads"]:::datastore

    %% Streamlit Server Block
    subgraph "Streamlit Server"
        direction TB
        UI["Streamlit UI"]:::internal
        Controller["Controller (app.py)"]:::internal
        SessionState["Session State Manager"]:::datastore
        subgraph "Configuration"
            direction TB
            ConfigYAML["config.yaml"]:::datastore
            EnvExample[".env.example"]:::datastore
        end
        subgraph "Services"
            direction TB
            TwitterClient["Twitter API Client"]:::internal
            OpenAIClient["OpenAI Client"]:::internal
            DataProcessor["Data Processor (pandas)"]:::internal
            Visualization["Visualization Renderer"]:::internal
            ExportModule["Export Module"]:::internal
        end
    end

    %% Documentation
    subgraph "Documentation"
        direction TB
        Requirements["requirements.txt"]:::external
        README["README.md"]:::external
    end

    %% Data Flows
    User -->|"accesses"| UI
    UI -->|"ui_event()"| Controller
    Controller -->|"load_config()"| ConfigYAML
    Controller -->|"load_env()"| EnvExample
    Controller -->|"fetch_tweets()"| TwitterClient
    TwitterClient -->|"API call"| TwitterAPI
    TwitterAPI -->|"tweets_data"| TwitterClient
    TwitterClient -->|"raw_data"| Controller
    Controller -->|"store_raw()"| SessionState
    Controller -->|"process_data()"| DataProcessor
    DataProcessor -->|"processed_df"| SessionState
    Controller -->|"analyze_sentiment()"| OpenAIClient
    OpenAIClient -->|"API call"| OpenAIAPI
    OpenAIAPI -->|"sentiment_results"| OpenAIClient
    OpenAIClient -->|"analysis_data"| SessionState
    Controller -->|"render_charts()"| Visualization
    Visualization -->|"ui_render"| UI
    UI -->|"export_request()"| ExportModule
    ExportModule -->|"generate_files()"| CSVJSON

    %% Click Events
    click UI "https://github.com/chetanaya/tweetsenseai/blob/main/app.py"
    click Controller "https://github.com/chetanaya/tweetsenseai/blob/main/app.py"
    click SessionState "https://github.com/chetanaya/tweetsenseai/blob/main/app.py"
    click ConfigYAML "https://github.com/chetanaya/tweetsenseai/blob/main/config.yaml"
    click EnvExample "https://github.com/chetanaya/tweetsenseai/blob/main/.env.example"
    click TwitterClient "https://github.com/chetanaya/tweetsenseai/blob/main/app.py"
    click OpenAIClient "https://github.com/chetanaya/tweetsenseai/blob/main/app.py"
    click DataProcessor "https://github.com/chetanaya/tweetsenseai/blob/main/app.py"
    click Visualization "https://github.com/chetanaya/tweetsenseai/blob/main/app.py"
    click ExportModule "https://github.com/chetanaya/tweetsenseai/blob/main/app.py"
    click Requirements "https://github.com/chetanaya/tweetsenseai/blob/main/requirements.txt"
    click README "https://github.com/chetanaya/tweetsenseai/blob/main/README.md"

    %% Styles
    classDef internal fill:#a8d0e6,stroke:#333,stroke-width:1px
    classDef external fill:#a8e6a8,stroke:#333,stroke-width:1px
    classDef datastore fill:#f6c26b,stroke:#333,stroke-width:1px
```
