# pn-projects/tarangfx architecture

> Architecture diagram of the GitHub repository pn-projects/tarangfx, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pn-projects/tarangfx
- Repository: https://github.com/pn-projects/tarangfx

## Overview

TarangFX is a Telegram audio-processing bot with a fairly clean three-layer shape: a Telegram interaction layer, a processing engine, and shared infrastructure for state, persistence, and limits.

1. **Entry point and runtime**
   - `bot.py` is the main bootstrap. It likely loads configuration from `.env`, initializes the Telegram client, wires up handlers, and starts the event loop.
   - The repo is designed to run either as a local Python service or in Docker (`Dockerfile`, `docker/entrypoint.sh`), with environment-driven deployment.

2. **Telegram-facing interaction layer**
   - `handlers/` is the user interaction boundary.
   - `start.py`, `help.py`, and `ui.py` handle onboarding and inline menu flows.
   - `callbacks.py` is the callback-query router for button presses and session choices.
   - `audio.py` is the main media workflow handler: it receives user audio/media, kicks off processing, and returns results.
   - `sudo.py` suggests privileged admin/owner commands or maintenance actions.

3. **Session-based user workflow**
   - The README describes a v2 “configure first, process once” model.
   - That means the bot likely stores per-user choices such as output format, bitrate, sample rate, and effects before starting the actual conversion job.
   - This makes the UI flow stateful across multiple callback interactions rather than a single command.

4. **Processing pipeline**
   - `processors/` is the main media engine boundary.
   - The README says the bot uses Spotify’s Pedalboard for studio-style effects and FFmpeg for conversion/encoding.
   - The pipeline appears to combine multiple operations in one session, including format conversion, bitrate selection, sample-rate changes, normalization, and premium effects like reverb, delay, chorus, distortion, bass boost, and vocal enhancement.

5. **Infrastructure for state and caching**
   - `core/cache.py` is the cache/session layer.
   - Redis is a key part of the architecture for session state, rate limiting, and possibly temporary workflow metadata.
   - The README indicates support for either a Redis URL or Upstash-style Redis REST settings, which means cache backend selection is environment-driven.

6. **Concurrency and load control**
   - `core/concurrency.py` handles per-user or global operation limits.
   - This is important because audio processing is expensive and the bot supports asynchronous downloads, transformations, and uploads.
   - The architectural boundary here is “accept many Telegram updates, but only allow a controlled number of active jobs.”

7. **Persistence layer**
   - `core/database.py` and `models/` represent the database boundary.
   - PostgreSQL is used for durable user data, likely including premium status, preferences, usage history, or admin records.
   - The `models` package suggests a structured data layer instead of raw SQL scattered through handlers.

8. **Premium and access control**
   - The feature set in the README distinguishes free vs premium capabilities: lossless formats, higher sample rates, and advanced effects.
   - `tests/test_premium_logic.py` and `handlers/sudo.py` imply the system enforces tier-based access at the application layer.
   - In the architecture diagram, premium checks should sit between the UI/handler layer and the processor pipeline.

9. **Data flow for an audio job**
   - A user sends media to Telegram.
   - `handlers/audio.py` receives the message, checks limits/access, and records session choices.
   - The file is downloaded to a temp area, processed through the relevant processor chain, then uploaded back to Telegram.
   - Temporary files are isolated per user and cleaned up after completion, which is an important security and resource boundary.

10. **External services and deployment dependencies**
   - Telegram is the primary external API via Telethon.
   - PostgreSQL provides durable storage.
   - Redis provides fast ephemeral state and throttling.
   - Docker is a first-class deployment mode, and the README also documents cloud-friendly deployments using Hugging Face Spaces plus optional Neon/Upstash managed services.

11. **Validation and operational checks**
   - `tests/` covers audio effects, premium rules, refinements, and environment/login verification.
   - `check_speed_deps.py` and `check_time.py` look like operational or benchmark-style scripts for dependency/performance validation.
   - These are useful indicators that processing latency and environment correctness are treated as first-order concerns.

12. **Repository boundaries to show in a diagram**
   - **Telegram update ingress**: handlers
   - **Session/rate-limit cache**: Redis-backed core/cache
   - **Durable user/config storage**: PostgreSQL via core/database and models
   - **Job control**: core/concurrency
   - **Audio engine**: processors using Pedalboard + FFmpeg
   - **Deployment/runtime**: bot.py, Docker, entrypoint
   - **QA/verification**: tests and check scripts

Overall, the architecture is a stateful, async Telegram bot with a clear separation between message handling, session orchestration, media processing, and infrastructure services.

Main entry points: [`bot.py`](https://github.com/pn-projects/tarangfx/blob/main/bot.py) (bot.py), [`handlers/audio.py`](https://github.com/pn-projects/tarangfx/blob/main/handlers/audio.py) (Audio), [`processors/__init__.py`](https://github.com/pn-projects/tarangfx/blob/main/processors/__init__.py) (Processors), [`core/database.py`](https://github.com/pn-projects/tarangfx/blob/main/core/database.py) (Database)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_bot_py["bot.py<br/>bootstrap<br/>[bot.py]"]
  node_dockerfile["Dockerfile<br/>container build"]
  node_entrypoint_sh["entrypoint<br/>[entrypoint.sh]"]
  node_config_py["Config<br/>[config.py]"]
end

subgraph group_telegram["Telegram layer"]
  node_handlers_start["Start<br/>onboarding handler<br/>[start.py]"]
  node_handlers_help["Help<br/>[help.py]"]
  node_handlers_ui["UI<br/>menu flow<br/>[ui.py]"]
  node_handlers_callbacks["Callbacks<br/>callback router<br/>[callbacks.py]"]
  node_handlers_audio(("Audio<br/>media workflow<br/>[audio.py]"))
  node_handlers_sudo["Sudo<br/>admin commands<br/>[sudo.py]"]
end

subgraph group_core["Core services"]
  node_core_cache[("Cache<br/>[cache.py]")]
  node_core_concurrency["Concurrency<br/>job limits<br/>[concurrency.py]"]
  node_core_database[("Database<br/>persistence<br/>[database.py]")]
  node_models_pkg["Models<br/>[__init__.py]"]
end

subgraph group_processing["Audio engine"]
  node_processors_pkg{{"Processors<br/>audio pipeline<br/>[__init__.py]"}}
end

subgraph group_ops["Operations"]
  node_tests_pkg["Tests<br/>verification<br/>[test_effects.py]"]
  node_check_speed_deps_py["Speed check<br/>ops script"]
  node_check_time_py["Time check<br/>ops script<br/>[check_time.py]"]
end

node_bot_py -->|"loads"| node_config_py
node_bot_py -->|"registers"| node_handlers_start
node_bot_py -->|"registers"| node_handlers_help
node_bot_py -->|"registers"| node_handlers_ui
node_bot_py -->|"registers"| node_handlers_callbacks
node_bot_py -->|"registers"| node_handlers_audio
node_bot_py -->|"registers"| node_handlers_sudo
node_handlers_ui -->|"drives"| node_handlers_callbacks
node_handlers_callbacks -->|"stores state"| node_core_cache
node_handlers_audio -->|"acquires slot"| node_core_concurrency
node_handlers_audio -->|"reads session"| node_core_cache
node_handlers_audio -->|"checks account"| node_core_database
node_core_database -->|"uses models"| node_models_pkg
node_handlers_audio -->|"invokes"| node_processors_pkg
node_processors_pkg -.->|"temp metadata"| node_core_cache
node_handlers_sudo -->|"admin writes"| node_core_database
node_handlers_sudo -.->|"controls limits"| node_core_cache
node_dockerfile -->|"starts"| node_entrypoint_sh
node_entrypoint_sh -->|"executes"| node_bot_py
node_tests_pkg -.->|"verifies"| node_processors_pkg
node_tests_pkg -.->|"verifies"| node_core_database
node_check_speed_deps_py -.->|"benchmarks"| node_processors_pkg
node_check_time_py -.->|"measures"| node_processors_pkg

click node_bot_py "https://github.com/pn-projects/tarangfx/blob/main/bot.py"
click node_dockerfile "https://github.com/pn-projects/tarangfx/tree/main/Dockerfile"
click node_entrypoint_sh "https://github.com/pn-projects/tarangfx/blob/main/docker/entrypoint.sh"
click node_handlers_start "https://github.com/pn-projects/tarangfx/blob/main/handlers/start.py"
click node_handlers_help "https://github.com/pn-projects/tarangfx/blob/main/handlers/help.py"
click node_handlers_ui "https://github.com/pn-projects/tarangfx/blob/main/handlers/ui.py"
click node_handlers_callbacks "https://github.com/pn-projects/tarangfx/blob/main/handlers/callbacks.py"
click node_handlers_audio "https://github.com/pn-projects/tarangfx/blob/main/handlers/audio.py"
click node_handlers_sudo "https://github.com/pn-projects/tarangfx/blob/main/handlers/sudo.py"
click node_core_cache "https://github.com/pn-projects/tarangfx/blob/main/core/cache.py"
click node_core_concurrency "https://github.com/pn-projects/tarangfx/blob/main/core/concurrency.py"
click node_core_database "https://github.com/pn-projects/tarangfx/blob/main/core/database.py"
click node_models_pkg "https://github.com/pn-projects/tarangfx/blob/main/models/__init__.py"
click node_processors_pkg "https://github.com/pn-projects/tarangfx/blob/main/processors/__init__.py"
click node_config_py "https://github.com/pn-projects/tarangfx/blob/main/config.py"
click node_tests_pkg "https://github.com/pn-projects/tarangfx/blob/main/tests/test_effects.py"
click node_check_speed_deps_py "https://github.com/pn-projects/tarangfx/blob/main/check_speed_deps.py"
click node_check_time_py "https://github.com/pn-projects/tarangfx/blob/main/check_time.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_bot_py,node_dockerfile,node_entrypoint_sh,node_config_py toneBlue
class node_handlers_start,node_handlers_help,node_handlers_ui,node_handlers_callbacks,node_handlers_audio,node_handlers_sudo toneAmber
class node_core_cache,node_core_concurrency,node_core_database,node_models_pkg toneMint
class node_processors_pkg toneRose
class node_tests_pkg,node_check_speed_deps_py,node_check_time_py toneIndigo
```

## Components

### Runtime

Bootstrap and deployment entrypoints

- [bot.py](https://github.com/pn-projects/tarangfx/blob/main/bot.py): bootstrap · Main application bootstrap and event-loop startup · `bot.py`
- [Dockerfile](https://github.com/pn-projects/tarangfx/tree/main/Dockerfile): container build · Container image definition for deployment · `Dockerfile`
- [entrypoint](https://github.com/pn-projects/tarangfx/blob/main/docker/entrypoint.sh): container entrypoint · Docker runtime entry script · `docker/entrypoint.sh`
- [Config](https://github.com/pn-projects/tarangfx/blob/main/config.py): env config · Environment-driven settings and service wiring · `config.py`

### Telegram layer

User interaction and command handling

- [Start](https://github.com/pn-projects/tarangfx/blob/main/handlers/start.py): onboarding handler · Entry commands and first-time user flow · `handlers/start.py`
- [Help](https://github.com/pn-projects/tarangfx/blob/main/handlers/help.py): help handler · Usage guidance and bot help responses · `handlers/help.py`
- [UI](https://github.com/pn-projects/tarangfx/blob/main/handlers/ui.py): menu flow · Inline menus and session configuration screens · `handlers/ui.py`
- [Callbacks](https://github.com/pn-projects/tarangfx/blob/main/handlers/callbacks.py): callback router · Button presses and session-choice routing · `handlers/callbacks.py`
- [Audio](https://github.com/pn-projects/tarangfx/blob/main/handlers/audio.py): media workflow · Receives media, orchestrates processing, returns results · `handlers/audio.py`
- [Sudo](https://github.com/pn-projects/tarangfx/blob/main/handlers/sudo.py): admin commands · Privileged maintenance and owner actions · `handlers/sudo.py`

### Core services

Shared state, limits, and persistence

- [Cache](https://github.com/pn-projects/tarangfx/blob/main/core/cache.py): session cache · Ephemeral session state and rate-limit backing store · `core/cache.py`
- [Concurrency](https://github.com/pn-projects/tarangfx/blob/main/core/concurrency.py): job limits · Per-user and global active-job control · `core/concurrency.py`
- [Database](https://github.com/pn-projects/tarangfx/blob/main/core/database.py): persistence · Durable storage access layer · `core/database.py`
- [Models](https://github.com/pn-projects/tarangfx/blob/main/models/__init__.py): data models · Structured persisted entities and records · `models/__init__.py`

### Audio engine

Media transformation pipeline

- [Processors](https://github.com/pn-projects/tarangfx/blob/main/processors/__init__.py): audio pipeline · Media processing boundary using FFmpeg and Pedalboard · `processors/__init__.py`

### Operations

Validation, checks, and tests

- [Tests](https://github.com/pn-projects/tarangfx/blob/main/tests/test_effects.py): verification · Effects, premium rules, refinements, and env/login checks · `tests/test_effects.py`
- [Speed check](https://github.com/pn-projects/tarangfx/blob/main/check_speed_deps.py): ops script · Dependency or performance verification script · `check_speed_deps.py`
- [Time check](https://github.com/pn-projects/tarangfx/blob/main/check_time.py): ops script · Timing or latency verification script · `check_time.py`

## Connections

- bot.py → Config: loads · Reads environment and runtime settings
- bot.py → Start: registers · Wires onboarding handlers
- bot.py → Help: registers · Wires help handler
- bot.py → UI: registers · Wires menu flows
- bot.py → Callbacks: registers · Wires callback router
- bot.py → Audio: registers · Wires media workflow
- bot.py → Sudo: registers · Wires admin commands
- UI → Callbacks: drives · UI actions continue through callback routing
- Callbacks → Cache: stores state · Persists session choices and workflow state
- Audio → Concurrency: acquires slot · Enforces active-job limits before processing
- Audio → Cache: reads session · Uses cached preferences and rate-limit state
- Audio → Database: checks account · Loads durable user and premium data
- Database → Models: uses models · Maps persisted records through structured entities
- Audio → Processors: invokes · Sends media into the audio engine
- Processors → Cache: temp metadata · May use ephemeral metadata during jobs
- Sudo → Database: admin writes · Updates durable account or access records
- Sudo → Cache: controls limits · Can adjust or inspect operational state
- Dockerfile → entrypoint: starts · Container launches through the entrypoint script
- entrypoint → bot.py: executes · Starts the bot process inside the container
- Tests → Processors: verifies · Covers effects and refinements in the engine
- Tests → Database: verifies · Covers premium and environment-related logic
- Speed check → Processors: benchmarks · Checks processing dependencies and speed-related behavior
- Time check → Processors: measures · Checks timing characteristics of processing flows
