# hamza-eldefrawy/arabic-voice-agent architecture

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

- Interactive diagram: https://gitdiagram.com/hamza-eldefrawy/arabic-voice-agent
- Repository: https://github.com/hamza-eldefrawy/arabic-voice-agent

## Overview

This is an Arabic voice customer-service agent. Its main turn flow receives audio, recognizes speech, streams an LLM response, splits it into speakable sentences, synthesizes audio, and returns speech; the server also supports browser sessions over WebSocket. The Brain can invoke search and prepare email or Telegram actions, but sending requires a later spoken confirmation. The README says the PostgreSQL conversation layer is not used by the current Brain. Some server and pipeline internals were not excerpted, so only documented or indexed connections are shown.

Main entry points: [`src/server.py`](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/server.py) (WebSocket Server), [`src/brain/brain.py`](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/brain.py) (Conversation Brain), [`src/main.py`](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/main.py) (Local Voice Loop), [`src/brain/llm.py`](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/llm.py) (Streaming LLM)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_audio["Speech I/O"]
  node_source["Audio Sources<br/>[sources.py]"]
  node_vad["Speech Detection<br/>[vad.py]"]
  node_segmented["Segmented Recognition<br/>[segmented.py]"]
  node_whisper["Whisper ASR<br/>[whisper.py]"]
  node_hubert["HuBERT ASR<br/>[hubert.py]"]
  node_factory["ASR Selection<br/>[factory.py]"]
  node_tts["Speech Synthesis<br/>[tts_engine.py]"]
  node_normalizer["Text Normalizer<br/>[normalizer.py]"]
end

subgraph group_conversation["Conversation Brain"]
  node_brain["Conversation Brain<br/>[brain.py]"]
  node_prompt["Prompt Builder<br/>[prompt_builder.py]"]
  node_llm["Streaming LLM<br/>[llm.py]"]
end

subgraph group_actions["Tools and Actions"]
  node_registry["Tool Registry<br/>[__init__.py]"]
  node_search["Web Search<br/>[web_search.py]"]
  node_email["Email Action<br/>[email.py]"]
  node_telegram["Telegram Action<br/>[telegram.py]"]
  node_escalation["Human Escalation<br/>[human_escal.py]"]
end

subgraph group_serving["Session Serving"]
  node_main["Local Voice Loop<br/>[main.py]"]
  node_server["WebSocket Server<br/>[server.py]"]
  node_pipeline["Turn Pipeline<br/>[pipeline.py]"]
end

subgraph group_persistence["Conversation Storage"]
  node_repository["Conversation Repository<br/>[repository.py]"]
  node_models["Conversation Models<br/>[models.py]"]
  node_database[("PostgreSQL")]
end

node_user(("Speaker / Browser"))
node_interfaces["Shared Contracts<br/>[schema.py]"]
node_config["Runtime Settings<br/>[config.py]"]
node_openai{{"OpenAI API"}}
node_tavily{{"Tavily Search"}}
node_smtp{{"SMTP Service"}}
node_telegram_api{{"Telegram API"}}
node_edge_tts{{"Edge TTS Service"}}

node_user -->|"speaks to"| node_main
node_main -->|"captures audio"| node_source
node_source -->|"provides frames"| node_segmented
node_segmented -->|"detects speech"| node_vad
node_segmented -->|"decodes segments"| node_whisper
node_segmented -->|"decodes segments"| node_hubert
node_factory -->|"selects engine"| node_whisper
node_factory -->|"selects engine"| node_hubert
node_main -->|"submits text"| node_brain
node_brain -->|"builds messages"| node_prompt
node_brain -->|"streams response"| node_llm
node_llm -->|"requests completions"| node_openai
node_llm -->|"dispatches tools"| node_registry
node_registry -->|"registers tool"| node_search
node_registry -->|"registers tool"| node_email
node_registry -->|"registers tool"| node_telegram
node_llm -->|"uses escalation"| node_escalation
node_search -->|"searches web"| node_tavily
node_brain -->|"sends after approval"| node_email
node_brain -->|"sends after approval"| node_telegram
node_email -->|"sends email"| node_smtp
node_telegram -->|"posts message"| node_telegram_api
node_main -->|"synthesizes sentences"| node_tts
node_tts -->|"normalizes text"| node_normalizer
node_tts -->|"generates speech"| node_edge_tts
node_main -->|"plays audio"| node_user
node_user -->|"streams browser audio"| node_server
node_server -->|"builds recognizer"| node_factory
node_server -->|"creates session brain"| node_brain
node_server -->|"runs turns"| node_pipeline
node_server -->|"creates synthesizer"| node_tts
node_server -->|"returns audio"| node_user
node_repository -->|"uses models"| node_models
node_repository -.->|"reads and writes"| node_database
node_config -->|"configures engines"| node_factory
node_config -->|"configures client"| node_llm
node_interfaces -->|"defines message types"| node_main

click node_main "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/main.py"
click node_server "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/server.py"
click node_pipeline "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/pipeline.py"
click node_interfaces "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/interfaces/schema.py"
click node_config "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/config.py"
click node_source "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/asr/sources.py"
click node_vad "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/asr/vad.py"
click node_segmented "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/asr/segmented.py"
click node_whisper "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/asr/whisper.py"
click node_hubert "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/asr/hubert.py"
click node_factory "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/asr/factory.py"
click node_brain "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/brain.py"
click node_prompt "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/prompt_builder.py"
click node_llm "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/llm.py"
click node_tts "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/tts/tts_engine.py"
click node_normalizer "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/tts/normalizer.py"
click node_registry "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/tools/__init__.py"
click node_search "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/tools/web_search.py"
click node_email "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/tools/email.py"
click node_telegram "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/tools/telegram.py"
click node_escalation "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/tools/human_escal.py"
click node_repository "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/database/repository.py"
click node_models "https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/database/models.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_source,node_vad,node_segmented,node_whisper,node_hubert,node_factory,node_tts,node_normalizer,node_user toneBlue
class node_brain,node_prompt,node_llm toneAmber
class node_registry,node_search,node_email,node_telegram,node_escalation,node_openai,node_telegram_api toneMint
class node_main,node_server,node_pipeline toneRose
class node_repository,node_models,node_database,node_tavily,node_smtp,node_edge_tts toneIndigo
class node_interfaces,node_config toneTeal
```

## Components

### Speech I/O

- [Audio Sources](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/asr/sources.py): `src/asr/sources.py`
- [Speech Detection](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/asr/vad.py): `src/asr/vad.py`
- [Segmented Recognition](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/asr/segmented.py): `src/asr/segmented.py`
- [Whisper ASR](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/asr/whisper.py): `src/asr/whisper.py`
- [HuBERT ASR](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/asr/hubert.py): `src/asr/hubert.py`
- [ASR Selection](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/asr/factory.py): `src/asr/factory.py`
- [Speech Synthesis](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/tts/tts_engine.py): `src/tts/tts_engine.py`
- [Text Normalizer](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/tts/normalizer.py): `src/tts/normalizer.py`

### Conversation Brain

- [Conversation Brain](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/brain.py): `src/brain/brain.py`
- [Prompt Builder](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/prompt_builder.py): `src/brain/prompt_builder.py`
- [Streaming LLM](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/llm.py): `src/brain/llm.py`

### Tools and Actions

- [Tool Registry](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/tools/__init__.py): `src/brain/tools/__init__.py`
- [Web Search](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/tools/web_search.py): `src/brain/tools/web_search.py`
- [Email Action](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/tools/email.py): `src/brain/tools/email.py`
- [Telegram Action](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/tools/telegram.py): `src/brain/tools/telegram.py`
- [Human Escalation](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/tools/human_escal.py): `src/brain/tools/human_escal.py`

### Session Serving

- [Local Voice Loop](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/main.py): `src/main.py`
- [WebSocket Server](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/server.py): `src/server.py`
- [Turn Pipeline](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/pipeline.py): `src/pipeline.py`

### Conversation Storage

- [Conversation Repository](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/database/repository.py): `src/brain/database/repository.py`
- [Conversation Models](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/brain/database/models.py): `src/brain/database/models.py`
- **PostgreSQL**

### Other components

- **Speaker / Browser**
- [Shared Contracts](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/interfaces/schema.py): `src/interfaces/schema.py`
- [Runtime Settings](https://github.com/hamza-eldefrawy/arabic-voice-agent/blob/main/src/config.py): `src/config.py`
- **OpenAI API**
- **Tavily Search**
- **SMTP Service**
- **Telegram API**
- **Edge TTS Service**

## Connections

- Speaker / Browser → Local Voice Loop: speaks to (evidence: `README.md`)
- Local Voice Loop → Audio Sources: captures audio (evidence: `src/main.py`)
- Audio Sources → Segmented Recognition: provides frames (evidence: `src/asr/segmented.py`)
- Segmented Recognition → Speech Detection: detects speech (evidence: `src/asr/segmented.py`)
- Segmented Recognition → Whisper ASR: decodes segments (evidence: `src/asr/whisper.py`)
- Segmented Recognition → HuBERT ASR: decodes segments (evidence: `src/asr/hubert.py`)
- ASR Selection → Whisper ASR: selects engine (evidence: `src/asr/factory.py`)
- ASR Selection → HuBERT ASR: selects engine (evidence: `src/asr/factory.py`)
- Local Voice Loop → Conversation Brain: submits text (evidence: `README.md`)
- Conversation Brain → Prompt Builder: builds messages (evidence: `src/brain/brain.py`)
- Conversation Brain → Streaming LLM: streams response (evidence: `src/brain/brain.py`)
- Streaming LLM → OpenAI API: requests completions (evidence: `src/brain/llm.py`)
- Streaming LLM → Tool Registry: dispatches tools (evidence: `src/brain/llm.py`)
- Tool Registry → Web Search: registers tool (evidence: `src/brain/tools/__init__.py`)
- Tool Registry → Email Action: registers tool (evidence: `src/brain/tools/__init__.py`)
- Tool Registry → Telegram Action: registers tool (evidence: `src/brain/tools/__init__.py`)
- Streaming LLM → Human Escalation: uses escalation (evidence: `src/brain/llm.py`)
- Web Search → Tavily Search: searches web (evidence: `src/brain/tools/web_search.py`)
- Conversation Brain → Email Action: sends after approval (evidence: `src/brain/brain.py`)
- Conversation Brain → Telegram Action: sends after approval (evidence: `src/brain/brain.py`)
- Email Action → SMTP Service: sends email (evidence: `src/brain/tools/email.py`)
- Telegram Action → Telegram API: posts message (evidence: `src/brain/tools/telegram.py`)
- Local Voice Loop → Speech Synthesis: synthesizes sentences (evidence: `README.md`)
- Speech Synthesis → Text Normalizer: normalizes text (evidence: `src/tts/tts_engine.py`)
- Speech Synthesis → Edge TTS Service: generates speech (evidence: `src/tts/tts_engine.py`)
- Local Voice Loop → Speaker / Browser: plays audio (evidence: `README.md`)
- Speaker / Browser → WebSocket Server: streams browser audio (evidence: `src/server.py`)
- WebSocket Server → ASR Selection: builds recognizer (evidence: `src/server.py`)
- WebSocket Server → Conversation Brain: creates session brain (evidence: `src/server.py`)
- WebSocket Server → Turn Pipeline: runs turns (evidence: `src/server.py`)
- WebSocket Server → Speech Synthesis: creates synthesizer (evidence: `src/server.py`)
- WebSocket Server → Speaker / Browser: returns audio (evidence: `src/server.py`)
- Conversation Repository → Conversation Models: uses models (evidence: `src/brain/database/repository.py`)
- Conversation Repository → PostgreSQL: reads and writes
- Runtime Settings → ASR Selection: configures engines (evidence: `src/asr/factory.py`)
- Runtime Settings → Streaming LLM: configures client (evidence: `src/brain/llm.py`)
- Shared Contracts → Local Voice Loop: defines message types (evidence: `src/main.py`)
