# bsana1/arrodeio-llm architecture

> Architecture diagram of the GitHub repository bsana1/arrodeio-llm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bsana1/arrodeio-llm
- Repository: https://github.com/bsana1/arrodeio-llm

## Overview

arrodeio-llm is a hands-on Brazilian Portuguese language-model project. Its main learning workflow prepares public-domain sayings and cordel, then explores two distinct generation tracks: a small GPT trained from scratch and pretrained GPT-2 fine-tuning. Later stages add instruction tuning, reward scoring, and best-of-N selection; web interfaces expose trained stages. README documentation establishes several unsampled phase relationships, so those links are documented rather than source-verified.

Main entry points: [`data`](https://github.com/bsana1/arrodeio-llm/tree/main/data) (Merged corpus), [`scripts/train.py`](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/train.py) (Pretrain TinyGPT), [`model/tokenizer.py`](https://github.com/bsana1/arrodeio-llm/blob/main/model/tokenizer.py) (Char/BPE tokenizer), [`scripts/make_sft_data_v2.py`](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/make_sft_data_v2.py) (Instruction data)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_corpus["Corpus preparation"]
  node_sayings["Folk sayings<br/>[ditados.txt]"]
  node_cordel["Cordel texts"]
  node_fetch["Fetch cordel<br/>[fetch_cordel.py]"]
  node_prepare["Prepare corpus<br/>[prepare_data.py]"]
  node_corpusfile["Merged corpus"]
end

subgraph group_scratch["From-scratch GPT"]
  node_tokenizer_train["Train tokenizer<br/>[train_tokenizer.py]"]
  node_tokenizer["Char/BPE tokenizer<br/>[tokenizer.py]"]
  node_scratch_train["Pretrain TinyGPT<br/>[train.py]"]
  node_gpt["TinyGPT architecture<br/>[gpt.py]"]
  node_scratch_checkpoint["Scratch checkpoint"]
  node_scratch_generate["Scratch generation<br/>[generate.py]"]
end

subgraph group_tuning["Fine-tuning and SFT"]
  node_finetune["GPT-2 fine-tuning<br/>[finetune.py]"]
  node_ft_generate["Fine-tuned generation<br/>[generate_ft.py]"]
  node_sft_data["Instruction data"]
  node_sft["LoRA instruction tuning<br/>[sft.py]"]
  node_chat["Instruction chat<br/>[chat.py]"]
end

subgraph group_ranking["Reward and selection"]
  node_prefs["Preference pairs<br/>[make_prefs.py]"]
  node_reward["Reward model training<br/>[reward_model.py]"]
  node_score["Response scoring<br/>[score.py]"]
  node_bestn["Best-of-N selection<br/>[best_of_n.py]"]
end

subgraph group_serving["Generation interfaces"]
  node_samples["Stage comparisons<br/>[collect_samples.py]"]
  node_webapp["FastAPI web app<br/>[app.py]"]
  node_space["Gradio ZeroGPU app<br/>[app.py]"]
end

node_user(("Learner or user"))
node_webuser(("Web visitor"))

node_sayings -->|"corpus input"| node_prepare
node_cordel -->|"corpus input"| node_prepare
node_fetch -->|"refreshes texts"| node_cordel
node_prepare -->|"builds corpus"| node_corpusfile
node_corpusfile -->|"trains on"| node_tokenizer_train
node_tokenizer_train -->|"uses"| node_tokenizer
node_corpusfile -->|"trains on"| node_scratch_train
node_tokenizer -->|"encodes corpus"| node_scratch_train
node_scratch_train -->|"trains"| node_gpt
node_scratch_train -->|"saves weights"| node_scratch_checkpoint
node_scratch_checkpoint -->|"loads checkpoint"| node_scratch_generate
node_tokenizer -->|"encodes and decodes"| node_scratch_generate
node_user -->|"provides prompt"| node_scratch_generate
node_corpusfile -->|"continues training"| node_finetune
node_user -->|"provides prompt"| node_ft_generate
node_sft_data -->|"supplies examples"| node_sft
node_corpusfile -->|"provides stanzas"| node_sft_data
node_sft -->|"trains adapter"| node_chat
node_user -->|"sends instruction"| node_chat
node_sft_data -->|"preference source"| node_prefs
node_prefs -->|"trains scorer"| node_reward
node_reward -->|"loads scorer"| node_score
node_score -->|"ranks candidates"| node_bestn
node_user -->|"provides prompt"| node_bestn
node_user -->|"requests generation"| node_webapp
node_webuser -->|"requests generation"| node_space
node_user -->|"selects stages"| node_samples

click node_sayings "https://github.com/bsana1/arrodeio-llm/blob/main/data/ditados.txt"
click node_cordel "https://github.com/bsana1/arrodeio-llm/tree/main/data/cordel"
click node_fetch "https://github.com/bsana1/arrodeio-llm/blob/main/scripts/fetch_cordel.py"
click node_prepare "https://github.com/bsana1/arrodeio-llm/blob/main/scripts/prepare_data.py"
click node_corpusfile "https://github.com/bsana1/arrodeio-llm/tree/main/data"
click node_tokenizer_train "https://github.com/bsana1/arrodeio-llm/blob/main/scripts/train_tokenizer.py"
click node_tokenizer "https://github.com/bsana1/arrodeio-llm/blob/main/model/tokenizer.py"
click node_scratch_train "https://github.com/bsana1/arrodeio-llm/blob/main/scripts/train.py"
click node_gpt "https://github.com/bsana1/arrodeio-llm/blob/main/model/gpt.py"
click node_scratch_checkpoint "https://github.com/bsana1/arrodeio-llm/tree/main/model"
click node_scratch_generate "https://github.com/bsana1/arrodeio-llm/blob/main/scripts/generate.py"
click node_finetune "https://github.com/bsana1/arrodeio-llm/blob/main/scripts/finetune.py"
click node_ft_generate "https://github.com/bsana1/arrodeio-llm/blob/main/scripts/generate_ft.py"
click node_sft_data "https://github.com/bsana1/arrodeio-llm/blob/main/scripts/make_sft_data_v2.py"
click node_sft "https://github.com/bsana1/arrodeio-llm/blob/main/scripts/sft.py"
click node_chat "https://github.com/bsana1/arrodeio-llm/blob/main/scripts/chat.py"
click node_prefs "https://github.com/bsana1/arrodeio-llm/blob/main/scripts/make_prefs.py"
click node_reward "https://github.com/bsana1/arrodeio-llm/blob/main/scripts/reward_model.py"
click node_score "https://github.com/bsana1/arrodeio-llm/blob/main/scripts/score.py"
click node_bestn "https://github.com/bsana1/arrodeio-llm/blob/main/scripts/best_of_n.py"
click node_samples "https://github.com/bsana1/arrodeio-llm/blob/main/scripts/collect_samples.py"
click node_webapp "https://github.com/bsana1/arrodeio-llm/blob/main/webapp/app.py"
click node_space "https://github.com/bsana1/arrodeio-llm/blob/main/space/app.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_sayings,node_cordel,node_fetch,node_prepare,node_corpusfile,node_user toneBlue
class node_tokenizer_train,node_tokenizer,node_scratch_train,node_gpt,node_scratch_checkpoint,node_scratch_generate toneAmber
class node_finetune,node_ft_generate,node_sft_data,node_sft,node_chat toneMint
class node_prefs,node_reward,node_score,node_bestn toneRose
class node_samples,node_webapp,node_space,node_webuser toneIndigo
```

## Components

### Corpus preparation

- [Folk sayings](https://github.com/bsana1/arrodeio-llm/blob/main/data/ditados.txt): `data/ditados.txt`
- [Cordel texts](https://github.com/bsana1/arrodeio-llm/tree/main/data/cordel): `data/cordel`
- [Fetch cordel](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/fetch_cordel.py): `scripts/fetch_cordel.py`
- [Prepare corpus](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/prepare_data.py): `scripts/prepare_data.py`
- [Merged corpus](https://github.com/bsana1/arrodeio-llm/tree/main/data): `data`

### From-scratch GPT

- [Train tokenizer](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/train_tokenizer.py): `scripts/train_tokenizer.py`
- [Char/BPE tokenizer](https://github.com/bsana1/arrodeio-llm/blob/main/model/tokenizer.py): `model/tokenizer.py`
- [Pretrain TinyGPT](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/train.py): `scripts/train.py`
- [TinyGPT architecture](https://github.com/bsana1/arrodeio-llm/blob/main/model/gpt.py): `model/gpt.py`
- [Scratch checkpoint](https://github.com/bsana1/arrodeio-llm/tree/main/model): `model`
- [Scratch generation](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/generate.py): `scripts/generate.py`

### Fine-tuning and SFT

- [GPT-2 fine-tuning](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/finetune.py): `scripts/finetune.py`
- [Fine-tuned generation](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/generate_ft.py): `scripts/generate_ft.py`
- [Instruction data](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/make_sft_data_v2.py): `scripts/make_sft_data_v2.py`
- [LoRA instruction tuning](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/sft.py): `scripts/sft.py`
- [Instruction chat](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/chat.py): `scripts/chat.py`

### Reward and selection

- [Preference pairs](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/make_prefs.py): `scripts/make_prefs.py`
- [Reward model training](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/reward_model.py): `scripts/reward_model.py`
- [Response scoring](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/score.py): `scripts/score.py`
- [Best-of-N selection](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/best_of_n.py): `scripts/best_of_n.py`

### Generation interfaces

- [Stage comparisons](https://github.com/bsana1/arrodeio-llm/blob/main/scripts/collect_samples.py): `scripts/collect_samples.py`
- [FastAPI web app](https://github.com/bsana1/arrodeio-llm/blob/main/webapp/app.py): `webapp/app.py`
- [Gradio ZeroGPU app](https://github.com/bsana1/arrodeio-llm/blob/main/space/app.py): `space/app.py`

### Other components

- **Learner or user**
- **Web visitor**

## Connections

- Folk sayings → Prepare corpus: corpus input (evidence: `README.md`)
- Cordel texts → Prepare corpus: corpus input (evidence: `README.md`)
- Fetch cordel → Cordel texts: refreshes texts (evidence: `README.md`)
- Prepare corpus → Merged corpus: builds corpus (evidence: `README.md`)
- Merged corpus → Train tokenizer: trains on (evidence: `README.md`)
- Train tokenizer → Char/BPE tokenizer: uses (evidence: `scripts/train_tokenizer.py`)
- Merged corpus → Pretrain TinyGPT: trains on (evidence: `README.md`)
- Char/BPE tokenizer → Pretrain TinyGPT: encodes corpus (evidence: `scripts/train.py`)
- Pretrain TinyGPT → TinyGPT architecture: trains (evidence: `scripts/train.py`)
- Pretrain TinyGPT → Scratch checkpoint: saves weights (evidence: `scripts/train.py`)
- Scratch checkpoint → Scratch generation: loads checkpoint (evidence: `scripts/generate.py`)
- Char/BPE tokenizer → Scratch generation: encodes and decodes (evidence: `scripts/generate.py`)
- Learner or user → Scratch generation: provides prompt (evidence: `README.md`)
- Merged corpus → GPT-2 fine-tuning: continues training (evidence: `README.md`)
- Learner or user → Fine-tuned generation: provides prompt (evidence: `README.md`)
- Instruction data → LoRA instruction tuning: supplies examples (evidence: `README.md`)
- Merged corpus → Instruction data: provides stanzas (evidence: `README.md`)
- LoRA instruction tuning → Instruction chat: trains adapter (evidence: `README.md`)
- Learner or user → Instruction chat: sends instruction (evidence: `README.md`)
- Instruction data → Preference pairs: preference source (evidence: `README.md`)
- Preference pairs → Reward model training: trains scorer (evidence: `README.md`)
- Reward model training → Response scoring: loads scorer (evidence: `scripts/score.py`)
- Response scoring → Best-of-N selection: ranks candidates (evidence: `scripts/best_of_n.py`)
- Learner or user → Best-of-N selection: provides prompt (evidence: `scripts/best_of_n.py`)
- Learner or user → FastAPI web app: requests generation (evidence: `webapp/app.py`)
- Web visitor → Gradio ZeroGPU app: requests generation (evidence: `space/app.py`)
- Learner or user → Stage comparisons: selects stages (evidence: `scripts/collect_samples.py`)
