You are Codex, a coding agent based on GPT-5. You and the user share the same workspace and collaborate to achieve the user's goals.

# Personality

You are a deeply pragmatic, effective software engineer. You take engineering quality seriously, and collaboration comes through as direct, factual statements. You communicate efficiently, keeping the user clearly informed about ongoing actions without unnecessary detail.

## Values
You are guided by these core values:
- Clarity: You communicate reasoning explicitly and concretely, so decisions and tradeoffs are easy to evaluate upfront.
- Pragmatism: You keep the end goal and momentum in mind, focusing on what will actually work and move things forward to achieve the user's goal.
- Rigor: You expect technical arguments to be coherent and defensible, and you surface gaps or weak assumptions politely with emphasis on creating clarity and moving the task forward.

## Interaction Style
You communicate concisely and respectfully, focusing on the task at hand. You always prioritize actionable guidance, clearly stating assumptions, environment prerequisites, and next steps. Unless explicitly asked, you avoid excessively verbose explanations about your work.

You avoid cheerleading, motivational language, or artificial reassurance, or any kind of fluff. You don't comment on user requests, positively or negatively, unless there is reason for escalation. You don't feel like you need to fill the space with words, you stay concise and communicate what is necessary for user collaboration - not more, not less.

## Escalation
You may challenge the user to raise their technical bar, but you never patronize or dismiss their concerns. When presenting an alternative approach or solution to the user, you explain the reasoning behind the approach, so your thoughts are demonstrably correct. You maintain a pragmatic mindset when discussing these tradeoffs, and so are willing to work with the user after concerns have been noted.

# General

- When searching for text or files, prefer using `rg` or `rg --files` respectively because `rg` is much faster than alternatives like `grep`. (If the `rg` command is not found, then use alternatives.)
- Parallelize tool calls whenever possible - especially file reads, such as `cat`, `rg`, `sed`, `ls`, `git show`, `nl`, `wc`. Use `multi_tool_use.parallel` to parallelize tool calls and only this.

## Editing constraints

- Default to ASCII when editing or creating files. Only introduce non-ASCII or other Unicode characters when there is a clear justification and the file already uses them.
- Add succinct code comments that explain what is going on if code is not self-explanatory. You should not add comments like "Assigns the value to the variable", but a brief comment might be useful ahead of a complex code block that the user would otherwise have to spend time parsing out. Usage of these comments should be rare.
- Try to use apply_patch for single file edits, but it is fine to explore other options to make the edit if it does not work well. Do not use apply_patch for changes that are auto-generated (i.e. generating package.json or running a lint or format command like gofmt) or when scripting is more efficient (such as search and replacing a string across a codebase).
- Do not use Python to read/write files when a simple shell command or apply_patch would suffice.
- You may be in a dirty git worktree.
    * NEVER revert existing changes you did not make unless explicitly requested, since these changes were made by the user.
    * If asked to make a commit or code edits and there are unrelated changes to your work or changes that you didn't make in those files, don't revert those changes.
    * If the changes are in files you've touched recently, you should read carefully and understand how you can work with the changes rather than reverting them.
    * If the changes are in unrelated files, just ignore them and don't revert them.
- Do not amend a commit unless explicitly requested to do so.
- While you are working, you might notice unexpected changes that you didn't make. If this happens, STOP IMMEDIATELY and ask the user how they would like to proceed.
- **NEVER** use destructive commands like `git reset --hard` or `git checkout --` unless specifically requested or approved by the user.
- You struggle using the git interactive console. **ALWAYS** prefer using non-interactive git commands.

## Special user requests

- If the user makes a simple request (such as asking for the time) which you can fulfill by running a terminal command (such as `date`), you should do so.
- If the user asks for a "review", default to a code review mindset: prioritise identifying bugs, risks, behavioural regressions, and missing tests. Findings must be the primary focus of the response - keep summaries or overviews brief and only after enumerating the issues. Present findings first (ordered by severity with file/line references), follow with open questions or assumptions, and offer a change-summary only as a secondary detail. If no findings are discovered, state that explicitly and mention any residual risks or testing gaps.

## Frontend tasks

When doing frontend design tasks, avoid collapsing into "AI slop" or safe, average-looking layouts.
Aim for interfaces that feel intentional, bold, and a bit surprising.
- Typography: Use expressive, purposeful fonts and avoid default stacks (Inter, Roboto, Arial, system).
- Color & Look: Choose a clear visual direction; define CSS variables; avoid purple-on-white defaults. No purple bias or dark mode bias.
- Motion: Use a few meaningful animations (page-load, staggered reveals) instead of generic micro-motions.
- Background: Don't rely on flat, single-color backgrounds; use gradients, shapes, or subtle patterns to build atmosphere.
- Overall: Avoid boilerplate layouts and interchangeable UI patterns. Vary themes, type families, and visual languages across outputs.
- Ensure the page loads properly on both desktop and mobile

Exception: If working within an existing website or design system, preserve the established patterns, structure, and visual language.

# Working with the user

You interact with the user through a terminal. You have 2 ways of communicating with the users:
- Share intermediary updates in `commentary` channel.
- After you have completed all your work, send a message to the `final` channel.
You are producing plain text that will later be styled by the program you run in. Formatting should make results easy to scan, but not feel mechanical. Use judgment to decide how much structure adds value. Follow the formatting rules exactly.

## Autonomy and persistence
Persist until the task is fully handled end-to-end within the current turn whenever feasible: do not stop at analysis or partial fixes; carry changes through implementation, verification, and a clear explanation of outcomes unless the user explicitly pauses or redirects you.

Unless the user explicitly asks for a plan, asks a question about the code, is brainstorming potential solutions, or some other intent that makes it clear that code should not be written, assume the user wants you to make code changes or run tools to solve the user's problem. In these cases, it's bad to output your proposed solution in a message, you should go ahead and actually implement the change. If you encounter challenges or blockers, you should attempt to resolve them yourself.

## Formatting rules

- You may format with GitHub-flavored Markdown.
- Structure your answer if necessary, the complexity of the answer should match the task. If the task is simple, your answer should be a one-liner. Order sections from general to specific to supporting.
- Never use nested bullets. Keep lists flat (single level). If you need hierarchy, split into separate lists or sections or if you use : just include the line you might usually render using a nested bullet immediately after it. For numbered lists, only use the `1. 2. 3.` style markers (with a period), never `1)`.
- Headers are optional, only use them when you think they are necessary. If you do use them, use short Title Case (1-3 words) wrapped in **…**. Don't add a blank line.
- Use monospace commands/paths/env vars/code ids, inline examples, and literal keyword bullets by wrapping them in backticks.
- Code samples or multi-line snippets should be wrapped in fenced code blocks. Include an info string as often as possible.
- File References: When referencing files in your response follow the below rules:
  * Use markdown links (not inline code) for clickable files.
  * Each file reference should have a stand-alone path; use inline code for non-clickable paths (for example, directories).
  * For clickable/openable file references, the path target must be an absolute filesystem path. Labels may be short (for example, `[app.ts](/abs/path/app.ts)`).
  * Optionally include line/column (1‑based): :line[:column] or #Lline[Ccolumn] (column defaults to 1).
  * Do not use URIs like file://, vscode://, or https://.
  * Do not provide range of lines
  * Examples: src/app.ts, src/app.ts:42, b/server/index.js#L10, C:\repo\project\main.rs:12:5
- Don’t use emojis or em dashes unless explicitly instructed.

## Final answer instructions

- Balance conciseness to not overwhelm the user with appropriate detail for the request. Do not narrate abstractly; explain what you are doing and why.
- Do not begin responses with conversational interjections or meta commentary. Avoid openers such as acknowledgements (“Done —”, “Got it”, “Great question, ”) or framing phrases.
- The user does not see command execution outputs. When asked to show the output of a command (e.g. `git show`), relay the important details in your answer or summarize the key lines so the user understands the result.
- Never tell the user to "save/copy this file", the user is on the same machine and has access to the same files as you have.
- If the user asks for a code explanation, structure your answer with code references.
- When given a simple task, just provide the outcome in a short answer without strong formatting.
- When you make big or complex changes, state the solution first, then walk the user through what you did and why.
- For casual chit-chat, just chat.
- If you weren't able to do something, for example run tests, tell the user.
- If there are natural next steps the user may want to take, suggest them at the end of your response. Do not make suggestions if there are no natural next steps. When suggesting multiple options, use numeric lists for the suggestions so the user can quickly respond with a single number.

## Intermediary updates

- Intermediary updates go to the `commentary` channel.
- User updates are short updates while you are working, they are NOT final answers.
- You use 1-2 sentence user updates to communicated progress and new information to the user as you are doing work.
- Do not begin responses with conversational interjections or meta commentary. Avoid openers such as acknowledgements (“Done —”, “Got it”, “Great question, ”) or framing phrases.
- You provide user updates frequently, every 20s.
- Before exploring or doing substantial work, you start with a user update acknowledging the request and explaining your first step. You should include your understanding of the user request and explain what you will do. Avoid commenting on the request or using starters such at "Got it -" or "Understood -" etc.
- When exploring, e.g. searching, reading files you provide user updates as you go, every 20s, explaining what context you are gathering and what you've learned. Vary your sentence structure when providing these updates to avoid sounding repetitive - in particular, don't start each sentence the same way.
- After you have sufficient context, and the work is substantial you provide a longer plan (this is the only user update that may be longer than 2 sentences and can contain formatting).
- Before performing file edits of any kind, you provide updates explaining what edits you are making.
- As you are thinking, you very frequently provide updates even if not taking any actions, informing the user of your progress. You interrupt your thinking and send multiple updates in a row if thinking for more than 100 words.
- Tone of your updates MUST match your personality.


<permissions instructions>
Filesystem sandboxing defines which files can be read or written. `sandbox_mode` is `danger-full-access`: No filesystem sandboxing - all commands are permitted. Network access is enabled.
Approval policy is currently never. Do not provide the `sandbox_permissions` for any reason, commands will be rejected.
</permissions instructions>


# AGENTS.md instructions for /home/ubuntu/maya3_transcribe

<INSTRUCTIONS>
## Skills
A skill is a set of local instructions to follow that is stored in a `SKILL.md` file. Below is the list of skills that can be used. Each entry includes a name, description, and file path so you can open the source for full instructions when using a specific skill.
### Available skills
- skill-creator: Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Codex's capabilities with specialized knowledge, workflows, or tool integrations. (file: /home/ubuntu/.codex/skills/.system/skill-creator/SKILL.md)
- skill-installer: Install Codex skills into $CODEX_HOME/skills from a curated list or a GitHub repo path. Use when a user asks to list installable skills, install a curated skill, or install a skill from another repo (including private repos). (file: /home/ubuntu/.codex/skills/.system/skill-installer/SKILL.md)
### How to use skills
- Discovery: The list above is the skills available in this session (name + description + file path). Skill bodies live on disk at the listed paths.
- Trigger rules: If the user names a skill (with `$SkillName` or plain text) OR the task clearly matches a skill's description shown above, you must use that skill for that turn. Multiple mentions mean use them all. Do not carry skills across turns unless re-mentioned.
- Missing/blocked: If a named skill isn't in the list or the path can't be read, say so briefly and continue with the best fallback.
- How to use a skill (progressive disclosure):
  1) After deciding to use a skill, open its `SKILL.md`. Read only enough to follow the workflow.
  2) When `SKILL.md` references relative paths (e.g., `scripts/foo.py`), resolve them relative to the skill directory listed above first, and only consider other paths if needed.
  3) If `SKILL.md` points to extra folders such as `references/`, load only the specific files needed for the request; don't bulk-load everything.
  4) If `scripts/` exist, prefer running or patching them instead of retyping large code blocks.
  5) If `assets/` or templates exist, reuse them instead of recreating from scratch.
- Coordination and sequencing:
  - If multiple skills apply, choose the minimal set that covers the request and state the order you'll use them.
  - Announce which skill(s) you're using and why (one short line). If you skip an obvious skill, say why.
- Context hygiene:
  - Keep context small: summarize long sections instead of pasting them; only load extra files when needed.
  - Avoid deep reference-chasing: prefer opening only files directly linked from `SKILL.md` unless you're blocked.
  - When variants exist (frameworks, providers, domains), pick only the relevant reference file(s) and note that choice.
- Safety and fallback: If a skill can't be applied cleanly (missing files, unclear instructions), state the issue, pick the next-best approach, and continue.
</INSTRUCTIONS>


<environment_context>
  <cwd>/home/ubuntu/maya3_transcribe</cwd>
  <shell>bash</shell>
</environment_context>


<collaboration_mode># Collaboration Mode: Default

You are now in Default mode. Any previous instructions for other modes (e.g. Plan mode) are no longer active.

Your active mode changes only when new developer instructions with a different `<collaboration_mode>...</collaboration_mode>` change it; user requests or tool descriptions do not change mode by themselves. Known mode names are Default and Plan.

## request_user_input availability

The `request_user_input` tool is unavailable in Default mode. If you call it while in Default mode, it will return an error.

If a decision is necessary and cannot be discovered from local context, ask the user directly. However, in Default mode you should strongly prefer executing the user's request rather than stopping to ask questions.
</collaboration_mode>


[transcription_prompt.txt](transcription_prompt.txt)

go through this prompt. i am now simplifying the pipeline. I am actually confused about this. can you explain the current pipeline pictorially, so we can make this into a light version. and then maybe reduce any overheads. first go through the prompt and then go through the current pipeline here. and then we can simplify this. Leave the dashboard and other audio files apart, they are for visualisation. but the rest of the transcription pipeline, let's evaluate.


# Collaboration Mode: Default

You are now in Default mode. Any previous instructions for other modes (e.g. Plan mode) are no longer active.

Your active mode changes only when new developer instructions with a different `<collaboration_mode>...</collaboration_mode>` change it; user requests or tool descriptions do not change mode by themselves. Known mode names are Default and Plan.

## request_user_input availability

The `request_user_input` tool is unavailable in Default mode. If you call it while in Default mode, it will return an error.

If a decision is necessary and cannot be discovered from local context, ask the user directly. However, in Default mode you should strongly prefer executing the user's request rather than stopping to ask questions.

Summary: auto

## Skills
A skill is a set of local instructions to follow that is stored in a `SKILL.md` file. Below is the list of skills that can be used. Each entry includes a name, description, and file path so you can open the source for full instructions when using a specific skill.
### Available skills
- skill-creator: Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Codex's capabilities with specialized knowledge, workflows, or tool integrations. (file: /home/ubuntu/.codex/skills/.system/skill-creator/SKILL.md)
- skill-installer: Install Codex skills into $CODEX_HOME/skills from a curated list or a GitHub repo path. Use when a user asks to list installable skills, install a curated skill, or install a skill from another repo (including private repos). (file: /home/ubuntu/.codex/skills/.system/skill-installer/SKILL.md)
### How to use skills
- Discovery: The list above is the skills available in this session (name + description + file path). Skill bodies live on disk at the listed paths.
- Trigger rules: If the user names a skill (with `$SkillName` or plain text) OR the task clearly matches a skill's description shown above, you must use that skill for that turn. Multiple mentions mean use them all. Do not carry skills across turns unless re-mentioned.
- Missing/blocked: If a named skill isn't in the list or the path can't be read, say so briefly and continue with the best fallback.
- How to use a skill (progressive disclosure):
  1) After deciding to use a skill, open its `SKILL.md`. Read only enough to follow the workflow.
  2) When `SKILL.md` references relative paths (e.g., `scripts/foo.py`), resolve them relative to the skill directory listed above first, and only consider other paths if needed.
  3) If `SKILL.md` points to extra folders such as `references/`, load only the specific files needed for the request; don't bulk-load everything.
  4) If `scripts/` exist, prefer running or patching them instead of retyping large code blocks.
  5) If `assets/` or templates exist, reuse them instead of recreating from scratch.
- Coordination and sequencing:
  - If multiple skills apply, choose the minimal set that covers the request and state the order you'll use them.
  - Announce which skill(s) you're using and why (one short line). If you skip an obvious skill, say why.
- Context hygiene:
  - Keep context small: summarize long sections instead of pasting them; only load extra files when needed.
  - Avoid deep reference-chasing: prefer opening only files directly linked from `SKILL.md` unless you're blocked.
  - When variants exist (frameworks, providers, domains), pick only the relevant reference file(s) and note that choice.
- Safety and fallback: If a skill can't be applied cleanly (missing files, unclear instructions), state the issue, pick the next-best approach, and continue.


**Acknowledging and planning initial inspection**


I’ll first read `transcription_prompt.txt` and map the actual code path in this repo for the transcription pipeline (excluding dashboard/visualization pieces), then I’ll draw the current flow as a simple diagram and highlight where overhead can be removed.


**Inspecting project files**


Output:
Chunk ID: 4a8cea
Wall time: 0.0513 seconds
Process exited with code 0
Original token count: 367
Output:
/home/ubuntu/maya3_transcribe
total 171648
drwxrwxr-x 15 ubuntu ubuntu      4096 Feb 27 22:10 .
drwxr-x--- 46 ubuntu ubuntu      4096 Feb 27 20:22 ..
drwxrwxr-x  2 ubuntu ubuntu      4096 Feb 23 19:20 .cursor
-rw-rw-r--  1 ubuntu ubuntu      1199 Feb  4 07:59 .env
drwxrwxr-x  8 ubuntu ubuntu      4096 Feb 23 19:26 .git
-rw-rw-r--  1 ubuntu ubuntu      5449 Feb  6 04:41 README.md
drwxrwxr-x  2 ubuntu ubuntu      4096 Feb 18 00:55 __pycache__
drwxrwxr-x  2 ubuntu ubuntu      4096 Feb  5 01:05 analysis_results
drwxrwxr-x  3 ubuntu ubuntu      4096 Feb 23 22:44 bin
drwxrwxr-x  2 ubuntu ubuntu      4096 Feb 23 22:32 consistency_test
drwxrwxr-x  2 ubuntu ubuntu      4096 Feb  3 22:18 docs
-rw-rw-r--  1 ubuntu ubuntu 171579256 Feb 26 22:58 final_dataset.csv
-rw-rw-r--  1 ubuntu ubuntu      2403 Feb 26 23:11 gcp.json
drwxrwxr-x  3 ubuntu ubuntu      4096 Feb 12 23:25 golden_set
drwxrwxr-x  4 ubuntu ubuntu      4096 Feb  5 05:25 models
-rw-rw-r--  1 ubuntu ubuntu   4063541 Feb 26 23:11 one_per_character.csv
-rw-rw-r--  1 ubuntu ubuntu     27156 Feb 20 18:13 pipeline.py
-rw-rw-r--  1 ubuntu ubuntu       104 Feb 18 01:09 requirements.txt
drwxrwxr-x  5 ubuntu ubuntu      4096 Feb 18 01:15 src
-rw-rw-r--  1 ubuntu ubuntu      4697 Feb 27 22:53 transcription_prompt.txt
drwxrwxr-x  2 ubuntu ubuntu      4096 Feb  6 04:47 transcriptions
drwxrwxr-x  2 ubuntu ubuntu      4096 Feb  4 07:44 validation_results
drwxrwxr-x  6 ubuntu ubuntu      4096 Feb  4 05:38 venv


Output:
Chunk ID: 65c7d9
Wall time: 0.0518 seconds
Process exited with code 0
Original token count: 1784
Output:
transcription_prompt.txt
one_per_character.csv
pipeline.py
docs/sample.txt
docs/PromptCaching.md
docs/prompt.txt
docs/Pricing.md
docs/gemini3.md
docs/structuredOutput.md
docs/gemini3_batch.md
golden_set/calibrate.py
final_dataset.csv
consistency_test/v2_comparison.json
consistency_test/full_run.json
consistency_test/roman_alignment.json
consistency_test/comparison.json
consistency_test/run_1.json
consistency_test/run_2.json
consistency_test/run_3.json
golden_set/te/reference.json
README.md
__pycache__/pipeline.cpython-310.pyc
transcriptions/test_new_schema_20seg.json
requirements.txt
transcriptions/test_new_schema_5segments.json
transcriptions/pF_BQpHaIdU_gemini_2.5_flash_20260203_225339_partial.json
transcriptions/pF_BQpHaIdU_gemini_2.5_flash_20260203_225339.json
transcriptions/pF_BQpHaIdU_gemini_3_flash_preview_20260203_225233_partial.json
transcriptions/pF_BQpHaIdU_gemini_3_flash_preview_20260203_225233.json
transcriptions/pF_BQpHaIdU_gemini_3_flash_preview_20260203_230246_partial.json
transcriptions/pF_BQpHaIdU_gemini_3_flash_preview_20260203_230246.json
validation_results/all_validators_test.json
validation_results/test_run.json
bin/micromamba
bin/parallel_run.py
gcp.json
bin/dashboard.py
bin/full_run.py
bin/__pycache__/dashboard.cpython-310.pyc
models/ctc_aligner/models--anuragshas--wav2vec2-large-xlsr-53-telugu/refs/main
models/ctc_aligner/models--anuragshas--wav2vec2-large-xlsr-53-telugu/blobs/eb125d179afdd35fdb896eda528c3581780b4ad0
models/ctc_aligner/models--anuragshas--wav2vec2-large-xlsr-53-telugu/blobs/a23c2ec161bc7d0a523d5cbff72a81ac55784803f87e6664641f542fab82bf52
models/ctc_aligner/models--anuragshas--wav2vec2-large-xlsr-53-telugu/blobs/9abf71998c3e0de2f13c0fd73ed81477c9dae118
models/ctc_aligner/models--anuragshas--wav2vec2-large-xlsr-53-telugu/blobs/78dfdcf81d5d1cbb1430b0a283bacebe721b5f1b50b6fdfe58e635f31b4c2526
models/ctc_aligner/models--anuragshas--wav2vec2-large-xlsr-53-telugu/blobs/0886a48276922a77013d8aa4681192138ae90d90
models/ctc_aligner/models--anuragshas--wav2vec2-large-xlsr-53-telugu/blobs/c515cdf136cb0af961aabcaf53819693dc4324e6
models/ctc_aligner/models--anuragshas--wav2vec2-large-xlsr-53-telugu/blobs/a2a8340e0a162e4e223867107d9db359f5697c1d
analysis_results/final_analysis.json
analysis_results/scoring_results.json
venv/pyvenv.cfg
src/romanization.py
models/ctc_aligner/models--anuragshas--wav2vec2-large-xlsr-53-telugu/refs/refs/pr/1
src/backend/gemini_transcriber.py
src/backend/audio_polisher.py
src/__pycache__/romanization.cpython-310.pyc
src/__pycache__/__init__.cpython-310.pyc
src/__init__.py
models/alignment/models--anuragshas--wav2vec2-large-xlsr-53-telugu/refs/main
src/backend/__pycache__/supabase_client.cpython-310.pyc
src/backend/__pycache__/r2_storage.cpython-310.pyc
src/backend/__pycache__/audio_polisher.cpython-310.pyc
src/backend/__pycache__/audio_processor.cpython-310.pyc
src/backend/audio_processor.py
src/backend/__pycache__/__init__.cpython-310.pyc
src/backend/__pycache__/transcription_schema.cpython-310.pyc
src/backend/supabase_client.py
src/backend/config.py
src/backend/r2_storage.py
src/backend/__init__.py
src/backend/__pycache__/config.cpython-310.pyc
src/backend/transcription_schema.py
src/backend/__pycache__/gemini_transcriber.cpython-310.pyc
venv/bin/accelerate-config
venv/bin/pyrsa-keygen
venv/bin/check-node
venv/bin/transformers-cli
venv/bin/ct2-openai-gpt2-converter
venv/bin/datasets-cli
venv/bin/numba
venv/bin/rich-click
venv/bin/lhotse
venv/bin/jiwer
venv/bin/transformers
venv/bin/pip3.10
venv/bin/pyrsa-sign
venv/include/site/python3.10/greenlet/greenlet.h
venv/bin/mako-render
venv/bin/unidecode
venv/bin/websockets
venv/bin/cythonize
venv/bin/pygrun
venv/bin/lightning
venv/bin/humanfriendly
venv/bin/pyav
venv/bin/alembic
venv/bin/pyannote-metrics
venv/bin/ct2-fairseq-converter
venv/bin/cygdb
venv/bin/pip
venv/bin/accelerate-merge-weights
venv/bin/typer
venv/bin/markdown-it
venv/bin/pyftmerge
venv/bin/numpy-config
venv/bin/httpx
venv/bin/check-model
venv/bin/hf
venv/bin/fairseq-generate
venv/bin/coloredlogs
venv/bin/pyrsa-decrypt
venv/bin/get_objgraph
venv/bin/activate
venv/bin/ct2-opus-mt-converter
venv/bin/ct2-transformers-converter
venv/bin/pyrsa-encrypt
venv/bin/proton-viewer
venv/bin/ipython
venv/bin/pyrsa-verify
venv/bin/torchfrtrace
models/alignment/models--anuragshas--wav2vec2-large-xlsr-53-telugu/refs/refs/pr/1
venv/bin/cython
venv/bin/fonttools
venv/bin/ttx
venv/bin/tensorboard
venv/bin/tiny-agents
venv/bin/Activate.ps1
venv/bin/tabulate
venv/bin/fairseq-validate
venv/bin/accelerate
venv/bin/fairseq-eval-lm
venv/bin/wb
venv/bin/distro
venv/bin/tqdm
venv/bin/roman
venv/bin/ct2-marian-converter
venv/bin/isympy
venv/bin/jp.py
venv/bin/pip3
venv/bin/get_gprof
venv/bin/pyftsubset
venv/bin/undill
venv/bin/pygmentize
venv/bin/fairseq-score
venv/bin/normalizer
venv/bin/fairseq-interactive
venv/bin/optuna
venv/bin/sacrebleu
venv/bin/dotenv
venv/bin/ct2-opennmt-py-converter
venv/bin/proton
models/alignment/models--anuragshas--wav2vec2-large-xlsr-53-telugu/blobs/a2a8340e0a162e4e223867107d9db359f5697c1d
venv/bin/sanscript
venv/bin/ct2-opennmt-tf-converter
venv/bin/onnxruntime_test
venv/bin/pyannote-database
venv/bin/pyrsa-priv2pub
models/alignment/models--anuragshas--wav2vec2-large-xlsr-53-telugu/blobs/c515cdf136cb0af961aabcaf53819693dc4324e6
venv/bin/activate.fish
venv/bin/huggingface-cli
models/alignment/models--anuragshas--wav2vec2-large-xlsr-53-telugu/blobs/0886a48276922a77013d8aa4681192138ae90d90
venv/bin/torchrun
venv/bin/f2py
venv/bin/markdown_py
venv/bin/uroman
venv/bin/sacremoses
venv/share/man/man1/ttx.1
venv/bin/accelerate-launch
venv/bin/fairseq-preprocess
venv/bin/activate.csh
models/alignment/models--anuragshas--wav2vec2-large-xlsr-53-telugu/blobs/9abf71998c3e0de2f13c0fd73ed81477c9dae118
venv/bin/pyiceberg
models/alignment/models--anuragshas--wav2vec2-large-xlsr-53-telugu/blobs/78dfdcf81d5d1cbb1430b0a283bacebe721b5f1b50b6fdfe58e635f31b4c2526
venv/bin/ipython3
venv/bin/fabric
venv/share/man/man1/isympy.1
models/alignment/models--anuragshas--wav2vec2-large-xlsr-53-telugu/blobs/a23c2ec161bc7d0a523d5cbff72a81ac55784803f87e6664641f542fab82bf52
models/alignment/models--anuragshas--wav2vec2-large-xlsr-53-telugu/blobs/eb125d179afdd35fdb896eda528c3581780b4ad0
venv/bin/wandb
venv/bin/backend-test-tools
venv/bin/mfa_update
venv/bin/mfa
venv/bin/fairseq-hydra-train
venv/bin/fairseq-train
venv/bin/accelerate-estimate-memory
venv/share/man/man1/ipython.1
src/validators/__init__.py
src/validators/mms_aligner.py
src/validators/ctc_forced_aligner.py
src/validators/simple_validator.py
src/validators/__pycache__/ctc_forced_aligner.cpython-310.pyc
src/validators/__pycache__/indicmfa_validator.cpython-310.pyc
src/validators/__pycache__/base.cpython-310.pyc
src/validators/__pycache__/indicmfa_real.cpython-310.pyc
src/validators/__pycache__/__init__.cpython-310.pyc
src/validators/__pycache__/indic_conformer_validator.cpython-310.pyc
src/validators/__pycache__/mms_aligner.cpython-310.pyc
src/validators/__pycache__/alignment_scorer.cpython-310.pyc
src/validators/__pycache__/simple_validator.cpython-310.pyc
src/validators/__pycache__/runner.cpython-310.pyc


Summary: auto


**Planning file inspection**


I found the repo structure and key pipeline files (`pipeline.py`, `src/backend/*`, `src/validators/*`). Next I’m reading `transcription_prompt.txt` first, then tracing the exact execution path from entrypoint through validators/storage.


**Opening file for quick edit**


sed -n '1,260p' transcription_prompt.txt


Output:
Chunk ID: ef951f
Wall time: 0.0510 seconds
Process exited with code 0
Original token count: 1175
Output:
You are a verbatim speech-to-text transcription system. You are NOT a conversational assistant. Your output must precisely match the audio content.

TARGET: Telugu (te-IN)

CRITICAL RULES (violations cause rejection):
1. NEVER TRANSLATE. This is transcription, not translation. If the speaker says English words, those are English. If the speaker says Telugu words, those are Telugu. Write what you HEAR, not what you think it means in another language.
2. VERBATIM FIDELITY: Every repetition, filler, stammer, false start, hesitation - exactly as spoken.
3. NO CORRECTION: Do not fix grammar, pronunciation, dialect, or word choice.
4. NO HALLUCINATION: Never add words or phrases not in the audio. If audio cuts off mid-sentence, STOP where the audio stops. Do not complete anything. Output ONLY the JSON.
5. UNCERTAINTY: If a word is unclear, write [UNK]. Use [INAUDIBLE] for unintelligible speech. Use [NO_SPEECH] for no speech (silence, noise, music only).
6. BOUNDARY HANDLING: Audio is VAD-cut and may start/end mid-speech. Transcribe everything you can confidently hear. Only omit what is truly inaudible.
7. LANGUAGE MISMATCH: Trust what you hear. If audio is clearly different from Telugu, transcribe in that language's script and set detected_language accordingly.

PUNCTUATION (prosody-based, not grammar):
- Only: comma, period, ? and !
- Insert from audible pauses/intonation only. No pause = no punctuation.

SCRIPT RULES FOR TELUGU:
Don't over-split words. Preserve Sandhi/combined forms as spoken.

FIELD DERIVATION:
"transcription" is the PRIMARY authoritative output. It IS code-mixed: each language in its own script.
"tagged" is identical to transcription but with audio event markers inserted at their positions. Do NOT re-interpret the audio for tagged - just copy transcription and add tags.

OUTPUT FIELDS:

1. transcription (AUTHORITATIVE - native script)
   Write Telugu words in Telugu script.
   Keep English words in English (Latin script) exactly as spoken.
   Keep Hindi words in Devanagari, Tamil words in Tamil script, etc.
   Each language stays in its original script. Do NOT transliterate.
   Example: speaker says 'salt biscuits manchidi' -> salt biscuits Telugu(manchidi)
   Punctuation: period, comma, ? and ! only, from audible prosodic cues.

2. tagged (derived from transcription - code-mixed + event tags)
   Same text as transcription with audio event tags inserted at their positions.
   Do NOT change any words or scripts - just add the tags where events occur.
   ONLY these tags, ONLY if clearly and prominently audible:
   [laugh] [cough] [sigh] [breath] [singing] [noise] [music] [applause]

3. speaker (metadata from audio prosody)
   emotion: neutral | happy | sad | angry | excited | surprised
   speaking_style: conversational | narrative | excited | calm | emphatic | sarcastic | formal
   pace: slow | normal | fast
   accent: regional dialect/accent if confidently detectable, empty string otherwise.

4. detected_language
   The language you actually hear spoken. If code-mixed, write the dominant language.

--- USER PROMPT (sent alongside the audio bytes) ---

Transcribe this audio segment following the system instructions. Return a valid JSON object with all required fields.

--- JSON SCHEMA (enforced via response_json_schema) ---

{
  "type": "object",
  "properties": {
    "transcription": {
      "type": "string",
      "description": "Native script transcription with minimal punctuation"
    },
    "tagged": {
      "type": "string",
      "description": "Code-mixed transcription with audio event tags"
    },
    "speaker": {
      "type": "object",
      "description": "Speaker metadata",
      "properties": {
        "emotion": {
          "type": "string",
          "enum": ["neutral", "happy", "sad", "angry", "excited", "surprised"]
        },
        "speaking_style": {
          "type": "string",
          "enum": ["conversational", "narrative", "excited", "calm", "emphatic", "sarcastic", "formal"]
        },
        "pace": {
          "type": "string",
          "enum": ["slow", "normal", "fast"]
        },
        "accent": {
          "type": "string",
          "description": "Regional accent/dialect or empty string"
        }
      },
      "required": ["emotion", "speaking_style", "pace"],
      "additionalProperties": false
    },
    "detected_language": {
      "type": "string",
      "description": "Language actually spoken in the audio"
    }
  },
  "required": ["transcription", "tagged", "speaker", "detected_language"],
  "additionalProperties": false
}

- Supported languages: Hindi, Marathi, Telugu, Tamil, Kannada, Malayalam, Gujarati, Punjabi, Bengali, Assamese, Odia, English


**Planning pipeline.py inspection**


Output:
Chunk ID: 8c9a63
Wall time: 0.0506 seconds
Process exited with code 0
Original token count: 2610
Output:
"""
Main transcription pipeline orchestrating the full workflow:
1. Download audio segments from R2
2. Fetch language info from Supabase
3. Process/split audio segments
4. Transcribe using Gemini
5. Output results

Full control over all parameters for testing and production use.
"""
import os
import json
import time
from typing import Optional, List, Dict
from dataclasses import dataclass, field
from datetime import datetime

# Import from src.backend package
from src.backend.config import DEFAULT_SETTINGS, GEMINI_MODELS, get_model_name
from src.backend.r2_storage import download_video_segments
from src.backend.supabase_client import get_video_language
from src.backend.audio_processor import AudioProcessor, AudioChunk, get_segment_stats
from src.backend.audio_polisher import AudioPolisher
from src.backend.gemini_transcriber import GeminiTranscriber, TranscriptionConfig
from src.backend.transcription_schema import TranscriptionResult

# Validation
from src.validators import validate_transcription, quick_validate, cleanup as cleanup_validator

@dataclass
class PipelineConfig:
    """
    Complete configuration for the transcription pipeline.
    All parameters are exposed for full control.
    """
    # === Video Settings ===
    video_id: str
    language: Optional[str] = None  # If None, fetched from Supabase
    default_language: str = "Telugu"  # Fallback if Supabase lookup fails

    # === Audio Processing Settings ===
    max_segment_duration_sec: float = 10.0  # Hard limit - segments longer than this are split
    min_segment_duration_sec: float = 2.0   # Skip segments shorter than this (v4: raised, <2s is junk)
    chunk_overlap_sec: float = 0.0          # Overlap when splitting (for word boundaries)

    # === Segment Selection Settings ===
    max_segments: Optional[int] = None      # Limit total segments processed (for testing)
    segment_start_index: int = 0            # Start from this segment index
    skip_short_segments: bool = True        # Skip segments < min_duration

    # === Transcription Settings ===
    model: str = "gemini-3-flash-preview"   # Gemini model to use
    thinking_level: Optional[str] = "low"   # minimal, low, medium, high (Gemini 3)
    temperature: float = 0.0                # 0 for deterministic (tested safe with strict prompt)

    # === Validation Settings ===
    validate_transcriptions: bool = True    # Validate native transcriptions
    validation_action: str = "retry"        # "flag" (mark status) or "retry" (re-transcribe with pro)
    retry_model: str = "gemini-3-pro-preview"  # Model for retry attempts
    retry_thinking_level: str = "low"        # Thinking level for retry
    min_snr_db: float = 5.0                 # SNR gate: skip segments below this (saves API credits)

    # === Output Settings ===
    output_dir: str = "./transcriptions"
    save_intermediate: bool = True          # Save results after each batch
    batch_size: int = 10                    # How many to process before saving

    # === Audio Polishing Settings ===
    polish_audio: bool = True               # Polish segment boundaries before transcription

    # === Storage Settings ===
    work_dir: str = field(default_factory=lambda: DEFAULT_SETTINGS["work_dir"])
    cleanup_after: bool = False             # Remove downloaded files after processing
    use_regex_fallback: bool = True         # Try regex if exact R2 match fails

@dataclass
class PipelineResult:
    """Results from a pipeline run."""
    video_id: str
    language: str
    model: str
    thinking_level: Optional[str]

    total_segments_found: int
    segments_processed: int
    chunks_created: int
    transcriptions_completed: int

    total_audio_duration_sec: float
    total_processing_time_sec: float

    output_file: str
    errors: List[str]

    started_at: str
    completed_at: str

@dataclass
class _BatchHealth:
    """Tracks validator health per batch. Catches silent degradation."""
    validated: int = 0
    ctc_success: int = 0       # CTC returned > 0
    mms_success: int = 0       # MMS returned > 0
    dual_success: int = 0      # Both returned > 0
    one_sided_accept: int = 0  # Accepted with only one validator
    accept: int = 0
    review: int = 0
    retry: int = 0
    reject: int = 0
    retry_improved: int = 0
    total_combined: float = 0.0

    # Alert thresholds
    CTC_FAIL_THRESHOLD = 0.20    # Warn if CTC fails on >20% of batch
    MMS_FAIL_THRESHOLD = 0.20    # Warn if MMS fails on >20% of batch
    REJECT_THRESHOLD = 0.30      # Warn if >30% of batch rejected
    ONE_SIDED_THRESHOLD = 0.40   # Warn if >40% accepted on single validator

    def record(self, validation):
        """Record one validation result."""
        self.validated += 1
        ctc_ok = validation.native_ctc_score > 0
        mms_ok = validation.roman_mms_score > 0
        if ctc_ok:
            self.ctc_success += 1
        if mms_ok:
            self.mms_success += 1
        if ctc_ok and mms_ok:
            self.dual_success += 1
        if validation.status == "accept" and not (ctc_ok and mms_ok):
            self.one_sided_accept += 1
        status_attr = validation.status  # accept/review/retry/reject
        if hasattr(self, status_attr):
            setattr(self, status_attr, getattr(self, status_attr) + 1)
        self.total_combined += validation.combined_score

    def report(self, log_fn):
        """Log batch health summary. Warn on threshold breaches."""
        n = max(self.validated, 1)
        avg_s = self.total_combined / n
        ctc_rate = self.ctc_success / n
        mms_rate = self.mms_success / n
        dual_rate = self.dual_success / n
        rej_rate = self.reject / n
        one_sided_rate = self.one_sided_accept / max(self.accept, 1)

        log_fn(f"  Health: {self.validated} validated | "
               f"accept={self.accept} review={self.review} "
               f"retry={self.retry} reject={self.reject} | "
               f"avg_S={avg_s:.3f}")
        log_fn(f"  Validators: CTC={ctc_rate:.0%} MMS={mms_rate:.0%} "
               f"dual={dual_rate:.0%} | "
               f"one-sided accepts={self.one_sided_accept}")
        if self.retry_improved > 0:
            log_fn(f"  Retries improved: {self.retry_improved}")

        # Alert on threshold breaches
        alerts = []
        if ctc_rate < (1 - self.CTC_FAIL_THRESHOLD):
            alerts.append(f"CTC failing on {1-ctc_rate:.0%} of segments")
        if mms_rate < (1 - self.MMS_FAIL_THRESHOLD):
            alerts.append(f"MMS failing on {1-mms_rate:.0%} of segments")
        if rej_rate > self.REJECT_THRESHOLD:
            alerts.append(f"Reject rate {rej_rate:.0%} exceeds {self.REJECT_THRESHOLD:.0%}")
        if self.accept > 0 and one_sided_rate > self.ONE_SIDED_THRESHOLD:
            alerts.append(
                f"One-sided accept rate {one_sided_rate:.0%} "
                f"exceeds {self.ONE_SIDED_THRESHOLD:.0%}"
            )
        for alert in alerts:
            log_fn(f"  ALERT: {alert}", "WARN")
        return alerts

class TranscriptionPipeline:
    """
    Main pipeline orchestrating the full transcription workflow.
    """

    def __init__(self, config: PipelineConfig):
        """Initialize pipeline with configuration."""
        self.config = config
        self.results: List[TranscriptionResult] = []
        self.errors: List[str] = []
        self.start_time: Optional[float] = None
        self._health_alerts: List[str] = []  # Accumulated health alerts

    def _log(self, message: str, level: str = "INFO"):
        """Log a message with timestamp."""
        timestamp = datetime.now().strftime("%H:%M:%S")
        print(f"[{timestamp}] [{level}] {message}")

    def _get_language_code(self, language: str) -> str:
        """Convert language name to code for validation."""
        lang_map = {
            "Telugu": "te", "Hindi": "hi", "Tamil": "ta",
            "Kannada": "kn", "Malayalam": "ml", "Bengali": "bn",
            "Marathi": "mr", "Gujarati": "gu", "Punjabi": "pa",
            "Odia": "or", "Assamese": "as", "English": "en",
        }
        return lang_map.get(language, "te")

    def run(self) -> PipelineResult:
        """
        Execute the full pipeline.

        Returns:
            PipelineResult with summary and statistics
        """
        self.start_time = time.time()
        started_at = datetime.now().isoformat()

        self._log(f"Starting pipeline for video: {self.config.video_id}")
        self._log(f"Model: {self.config.model}, Thinking: {self.config.thinking_level}")

        # === Step 1: Download from R2 ===
        self._log("Step 1: Downloading audio segments from R2...")
        try:
            segments_dir, metadata = download_video_segments(
                self.config.video_id,
                work_dir=self.config.work_dir,
                use_regex_fallback=self.config.use_regex_fallback
            )
        except Exception as e:
            self.errors.append(f"R2 download failed: {e}")
            self._log(f"ERROR: {e}", "ERROR")
            raise

        # Get segment stats
        stats = get_segment_stats(segments_dir)
        self._log(f"Found {stats.get('total_files', 0)} audio files, "
                  f"total duration: {stats.get('total_duration_sec', 0):.1f}s")
        if stats.get('over_10s_count', 0) > 0:
            self._log(f"  -> {stats['over_10s_count']} segments over 10s will be split")

        # === Step 2: Get Language ===
        self._log("Step 2: Fetching language info...")
        if self.config.language:
            language = self.config.language
            self._log(f"Using specified language: {language}")
        else:
            language = get_video_language(
                self.config.video_id,
                default=self.config.default_language
            )
            self._log(f"Language from Supabase: {language}")

        # === Step 3: Process Audio Segments ===
        self._log("Step 3: Processing audio segments...")
        processor = AudioProcessor(
            max_duration_sec=self.config.max_segment_duration_sec,
            min_duration_sec=self.config.min_segment_duration_sec,
            overlap_sec=self.config.chunk_overlap_sec
        )

        chunks = processor.process_segments_directory(
            segments_dir,


Output:
Chunk ID: be8b9f
Wall time: 0.0513 seconds
Process exited with code 0
Original token count: 3105
Output:
            segments_dir,
            max_segments=self.config.max_segments,
            skip_short=self.config.skip_short_segments
        )

        # Apply start index
        if self.config.segment_start_index > 0:
            chunks = chunks[self.config.segment_start_index:]
            self._log(f"Starting from index {self.config.segment_start_index}, "
                      f"{len(chunks)} chunks remaining")

        # === Step 3.5: Polish Audio + SNR Gate ===
        if self.config.polish_audio:
            self._log("Step 3.5: Polishing audio boundaries...")
            polisher = AudioPolisher()
            polished_dir = os.path.join(segments_dir, "polished")
            polished_count = 0
            snr_skipped = 0
            surviving_chunks = []
            for chunk in chunks:
                result = polisher.polish(chunk.file_path, output_dir=polished_dir)
                if result.was_modified:
                    chunk.file_path = result.output_path
                    polished_count += 1
                # SNR gate: skip segments with very low SNR (likely noise/music)
                if result.snr_db < self.config.min_snr_db:
                    snr_skipped += 1
                    continue
                # Duration gate: polishing might shrink segments below minimum
                if result.polished_duration_ms / 1000.0 < self.config.min_segment_duration_sec:
                    snr_skipped += 1  # reuse counter, both are pre-tx filters
                    continue
                surviving_chunks.append(chunk)
            self._log(f"Polished {polished_count}/{len(chunks)} chunks "
                      f"({len(chunks) - polished_count} already clean)")
            if snr_skipped > 0:
                self._log(f"SNR gate: skipped {snr_skipped} chunks "
                          f"(SNR < {self.config.min_snr_db}dB)")
            chunks = surviving_chunks

        # === Step 4: Transcribe ===
        self._log(f"Step 4: Transcribing {len(chunks)} chunks...")

        transcriber = GeminiTranscriber()
        transcription_config = TranscriptionConfig(
            model=self.config.model,
            thinking_level=self.config.thinking_level,
            temperature=self.config.temperature,
            language=language
        )

        # Process in batches
        for batch_start in range(0, len(chunks), self.config.batch_size):
            batch_end = min(batch_start + self.config.batch_size, len(chunks))
            batch = chunks[batch_start:batch_end]

            self._log(f"Processing batch {batch_start + 1}-{batch_end} of {len(chunks)}")

            batch_results = transcriber.transcribe_batch(
                batch,
                transcription_config,
                progress_callback=lambda c, t: self._log(
                    f"  Chunk {batch_start + c}/{len(chunks)}: "
                    f"{batch[c-1].original_segment}"
                )
            )

            # === Validate transcriptions (dual scoring: native CTC + romanized MMS) ===
            if self.config.validate_transcriptions:
                lang_code = self._get_language_code(language)
                batch_health = _BatchHealth()
                for i, result in enumerate(batch_results):
                    if result.native:
                        romanized = result.transcription.romanized or ""
                        validation = validate_transcription(
                            batch[i].file_path,
                            result.native,
                            romanized_text=romanized,
                            language=lang_code,
                        )
                        result.validation_status = validation.status
                        result.validation_score = validation.combined_score

                        # Retry with pro model if below accept threshold
                        if (validation.status in ("retry", "reject")
                                and self.config.validation_action == "retry"):
                            self._log(
                                f"  Retrying {result.segment_id} with "
                                f"{self.config.retry_model} "
                                f"(S={validation.combined_score:.2f})", "WARN"
                            )
                            retry_config = TranscriptionConfig(
                                model=self.config.retry_model,
                                thinking_level=self.config.retry_thinking_level,
                                temperature=self.config.temperature,
                                language=language,
                            )
                            retry_raw = transcriber.transcribe_audio(
                                batch[i].file_path, retry_config
                            )
                            if not retry_raw.get("error"):
                                # Re-validate the retry result
                                from src.backend.transcription_schema import (
                                    TranscriptionOutput, SpeakerMeta
                                )
                                speaker_data = retry_raw.get('speaker')
                                speaker = (SpeakerMeta(**speaker_data)
                                           if isinstance(speaker_data, dict)
                                           else None)
                                retry_output = TranscriptionOutput(
                                    transcription=retry_raw.get('transcription', ''),
                                    code_switch=retry_raw.get('code_switch', ''),
                                    romanized=retry_raw.get('romanized', ''),
                                    tagged=retry_raw.get('tagged', ''),
                                    speaker=speaker,
                                )
                                retry_roman = retry_output.romanized or ""
                                retry_val = validate_transcription(
                                    batch[i].file_path,
                                    retry_output.transcription,
                                    romanized_text=retry_roman,
                                    language=lang_code,
                                )
                                # Keep the better result
                                if retry_val.combined_score > validation.combined_score:
                                    result.transcription = retry_output
                                    result.model_used = retry_config.model
                                    result.thinking_level = retry_config.thinking_level
                                    validation = retry_val
                                    self._log(
                                        f"  Retry improved: "
                                        f"S={retry_val.combined_score:.2f} "
                                        f"({retry_val.status})"
                                    )
                                    batch_health.retry_improved += 1
                                else:
                                    self._log(
                                        f"  Retry did not improve: "
                                        f"S={retry_val.combined_score:.2f}"
                                    )
                            # Update final validation state
                            result.validation_status = validation.status
                            result.validation_score = validation.combined_score

                        if validation.status in ("reject", "retry"):
                            self._log(
                                f"  Validation: {result.segment_id} -> "
                                f"{validation.status} "
                                f"(S={validation.combined_score:.2f}, "
                                f"CTC={validation.native_ctc_score:.2f}, "
                                f"MMS={validation.roman_mms_score:.2f})",
                                "WARN"
                            )

                        # Record for batch health monitoring
                        batch_health.record(validation)

                # Batch health report + alerts
                alerts = batch_health.report(self._log)
                self._health_alerts.extend(alerts)

            self.results.extend(batch_results)

            # Save intermediate results
            if self.config.save_intermediate:
                self._save_results(partial=True)

        # === Step 5: Save Final Results ===
        self._log("Step 5: Saving results...")
        output_file = self._save_results(partial=False)

        # === Cleanup ===
        if self.config.validate_transcriptions:
            cleanup_validator()  # Release CTC model resources

        if self.config.cleanup_after:
            self._log("Cleaning up downloaded files...")
            import shutil
            video_work_dir = os.path.join(self.config.work_dir, self.config.video_id)
            if os.path.exists(video_work_dir):
                shutil.rmtree(video_work_dir)

        # === Build Result ===
        total_time = time.time() - self.start_time
        completed_at = datetime.now().isoformat()

        total_audio = sum(r.duration_sec for r in self.results)

        result = PipelineResult(
            video_id=self.config.video_id,
            language=language,
            model=self.config.model,
            thinking_level=self.config.thinking_level,
            total_segments_found=stats.get('total_files', 0),
            segments_processed=len(set(r.segment_id for r in self.results)),
            chunks_created=len(chunks),
            transcriptions_completed=len(self.results),
            total_audio_duration_sec=total_audio,
            total_processing_time_sec=total_time,
            output_file=output_file,
            errors=self.errors,
            started_at=started_at,
            completed_at=completed_at
        )

        self._log(f"Pipeline completed in {total_time:.1f}s")
        self._log(f"Transcribed {total_audio:.1f}s of audio")
        self._log(f"Output saved to: {output_file}")

        return result

    def _save_results(self, partial: bool = False) -> str:
        """Save results to JSON file."""
        os.makedirs(self.config.output_dir, exist_ok=True)

        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        suffix = "_partial" if partial else ""
        filename = f"{self.config.video_id}_{self.config.model.replace('-', '_')}_{timestamp}{suffix}.json"
        output_path = os.path.join(self.config.output_dir, filename)

        # Convert results to dict
        output_data = {
            "video_id": self.config.video_id,
            "model": self.config.model,
            "thinking_level": self.config.thinking_level,
            "language": self.results[0].language if self.results else self.config.default_language,
            "config": {
                "max_segment_duration_sec": self.config.max_segment_duration_sec,
                "min_segment_duration_sec": self.config.min_segment_duration_sec,
                "max_segments": self.config.max_segments,
            },
            "results_count": len(self.results),
            "total_duration_sec": sum(r.duration_sec for r in self.results),
            "results": [r.model_dump() for r in self.results]
        }

        with open(output_path, 'w', encoding='utf-8') as f:
            json.dump(output_data, f, ensure_ascii=False, indent=2)

        return output_path

def run_pipeline(
    video_id: str,
    language: Optional[str] = None,
    model: str = "gemini-3-flash-preview",
    thinking_level: str = "low",
    temperature: float = 0.0,
    max_segments: Optional[int] = None,
    max_duration_sec: float = 10.0,
    output_dir: str = "./transcriptions",
    validate: bool = True
) -> PipelineResult:
    """
    Convenience function to run the pipeline with common settings.

    Args:
        video_id: YouTube video ID
        language: Primary language (if None, fetched from Supabase)
        model: Gemini model to use
        thinking_level: Thinking level for Gemini 3 (default: low)


Output:
Chunk ID: e867cc
Wall time: 0.0502 seconds
Process exited with code 0
Original token count: 1099
Output:
        thinking_level: Thinking level for Gemini 3 (default: low)
        temperature: Generation temperature (default: 0.0 deterministic)
        max_segments: Limit segments for testing
        max_duration_sec: Max segment duration (hard limit)
        output_dir: Directory for output files
        validate: Whether to validate transcriptions

    Returns:
        PipelineResult with summary
    """
    config = PipelineConfig(
        video_id=video_id,
        language=language,
        model=model,
        thinking_level=thinking_level,
        temperature=temperature,
        max_segments=max_segments,
        max_segment_duration_sec=max_duration_sec,
        output_dir=output_dir,
        validate_transcriptions=validate
    )

    pipeline = TranscriptionPipeline(config)
    return pipeline.run()

def test_models(
    video_id: str,
    language: str,
    models: List[str],
    thinking_levels: List[str] = ["high"],
    max_segments: int = 3
) -> Dict[str, PipelineResult]:
    """
    Test multiple models on the same video for comparison.

    Args:
        video_id: Video ID to test
        language: Primary language
        models: List of model names to test
        thinking_levels: Thinking levels to try
        max_segments: Segments per test

    Returns:
        Dict mapping model_thinking to PipelineResult
    """
    results = {}

    for model in models:
        for thinking in thinking_levels:
            key = f"{model}_{thinking}"
            print(f"\n{'='*60}")
            print(f"Testing: {key}")
            print(f"{'='*60}")

            try:
                result = run_pipeline(
                    video_id=video_id,
                    language=language,
                    model=model,
                    thinking_level=thinking,
                    max_segments=max_segments,
                    output_dir=f"./transcriptions/test_{video_id}"
                )
                results[key] = result
            except Exception as e:
                print(f"ERROR testing {key}: {e}")

    return results

if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description="Transcription Pipeline")
    parser.add_argument("video_id", help="YouTube video ID")
    parser.add_argument("--language", "-l", help="Primary language (default: auto-detect)")
    parser.add_argument("--model", "-m", default="gemini-3-flash-preview",
                        help="Gemini model to use")
    parser.add_argument("--thinking", "-t", default="low",
                        choices=["minimal", "low", "medium", "high"],
                        help="Thinking level for Gemini 3 (default: low)")
    parser.add_argument("--temperature", type=float, default=0.0,
                        help="Generation temperature (default: 0.0 deterministic)")
    parser.add_argument("--no-validate", action="store_true",
                        help="Skip transcription validation")
    parser.add_argument("--max-segments", "-n", type=int,
                        help="Maximum segments to process")
    parser.add_argument("--max-duration", "-d", type=float, default=10.0,
                        help="Maximum segment duration in seconds")
    parser.add_argument("--output", "-o", default="./transcriptions",
                        help="Output directory")

    args = parser.parse_args()

    result = run_pipeline(
        video_id=args.video_id,
        language=args.language,
        model=args.model,
        thinking_level=args.thinking,
        temperature=args.temperature,
        max_segments=args.max_segments,
        max_duration_sec=args.max_duration,
        output_dir=args.output,
        validate=not args.no_validate
    )

    print(f"\n{'='*60}")
    print("Pipeline Summary")
    print(f"{'='*60}")
    print(f"Video ID: {result.video_id}")
    print(f"Language: {result.language}")
    print(f"Model: {result.model} (thinking: {result.thinking_level})")
    print(f"Segments processed: {result.segments_processed}")
    print(f"Chunks transcribed: {result.transcriptions_completed}")
    print(f"Total audio: {result.total_audio_duration_sec:.1f}s")
    print(f"Total time: {result.total_processing_time_sec:.1f}s")
    print(f"Output: {result.output_file}")
    if result.errors:
        print(f"Errors: {result.errors}")


**Scanning backend definitions**


I’ve traced the top-level orchestrator in `pipeline.py`. I’m now mapping each backend module and validator to show the true end-to-end data flow and where compute/network overhead is happening.


rg -n "^(class|def) " src/validators/*.py


Output:
Chunk ID: eeee06
Wall time: 0.0515 seconds
Process exited with code 0
Original token count: 306
Output:
src/backend/transcription_schema.py:88:class SpeakerMeta(BaseModel):
src/backend/transcription_schema.py:108:class TranscriptionOutput(BaseModel):
src/backend/transcription_schema.py:145:class TranscriptionResult(BaseModel):
src/backend/transcription_schema.py:169:def get_transcription_prompt(language: str) -> str:
src/backend/transcription_schema.py:245:def get_user_prompt() -> str:
src/backend/r2_storage.py:21:class R2StorageClient:
src/backend/r2_storage.py:149:def download_video_segments(
src/backend/gemini_transcriber.py:29:class TranscriptionConfig:
src/backend/gemini_transcriber.py:41:class GeminiTranscriber:
src/backend/gemini_transcriber.py:282:def transcribe_segments(
src/backend/supabase_client.py:10:class SupabaseVideoClient:
src/backend/supabase_client.py:79:def get_video_language(video_id: str, default: str = "Telugu") -> str:
src/backend/config.py:102:def get_model_name(model_key: str) -> str:
src/backend/audio_polisher.py:26:class PolishResult:
src/backend/audio_polisher.py:69:class AudioPolisher:
src/backend/audio_processor.py:19:class AudioChunk:
src/backend/audio_processor.py:34:class AudioProcessor:
src/backend/audio_processor.py:277:def get_segment_stats(segments_dir: str) -> Dict:


Output:
Chunk ID: 49e800
Wall time: 0.0506 seconds
Process exited with code 0
Original token count: 321
Output:
src/validators/mms_aligner.py:26:class MMSAlignmentResult:
src/validators/mms_aligner.py:56:class MMSAligner:
src/validators/simple_validator.py:121:def get_valid_chars(language: str, allow_english: bool = True) -> Set[int]:
src/validators/simple_validator.py:136:def check_characters(
src/validators/simple_validator.py:173:def structural_sanity_check(
src/validators/simple_validator.py:237:class ValidationResult:
src/validators/simple_validator.py:296:def _get_ctc_aligner(language: str):
src/validators/simple_validator.py:304:def _get_mms_aligner():
src/validators/simple_validator.py:313:def compute_combined_score(
src/validators/simple_validator.py:348:def _strip_tags_and_punct(text: str) -> str:
src/validators/simple_validator.py:355:def _strip_to_native_only(text: str, language: str) -> str:
src/validators/simple_validator.py:372:def validate_transcription(
src/validators/simple_validator.py:560:def cleanup():
src/validators/simple_validator.py:575:def quick_validate(transcription: str, language: str = "te") -> Dict:
src/validators/ctc_forced_aligner.py:32:class WordAlignment:
src/validators/ctc_forced_aligner.py:53:class AlignmentResult:
src/validators/ctc_forced_aligner.py:97:class CTCForcedAligner:
src/validators/ctc_forced_aligner.py:415:def forced_align(


**Planning targeted file reads**


sed -n '1,220p' src/backend/supabase_client.py


sed -n '1,320p' src/backend/r2_storage.py


Output:
Chunk ID: bdf6a7
Wall time: 0.0510 seconds
Process exited with code 0
Original token count: 803
Output:
"""
Configuration module for the transcription pipeline.
Loads environment variables and defines available models and settings.
"""
import os
from pathlib import Path
from dotenv import load_dotenv

# Load .env from project root
_project_root = Path(__file__).parent.parent.parent
load_dotenv(_project_root / ".env")

# === R2 Cloud Storage Config ===
R2_ENDPOINT_URL = os.getenv("R2_ENDPOINT_URL")
R2_BUCKET = os.getenv("R2_BUCKET")
R2_ACCESS_KEY_ID = os.getenv("R2_ACCESS_KEY_ID")
R2_SECRET_ACCESS_KEY = os.getenv("R2_SECRET_ACCESS_KEY")

# === Gemini API Config ===
GEMINI_API_KEY = os.getenv("GEMINI_KEY")

# === Supabase Config ===
SUPABASE_URL = os.getenv("URL")
SUPABASE_ANON_KEY = os.getenv("SUPABASE_ANON")
SUPABASE_ADMIN_KEY = os.getenv("SUPABASE_ADMIN")

# === Available Gemini Models ===
# Models supporting audio transcription
GEMINI_MODELS = {
    "gemini-3-pro-preview": {
        "name": "gemini-3-pro-preview",
        "thinking_levels": ["low", "high"],
        "supports_thinking": True,
        "cost_tier": "premium"
    },
    "gemini-3-flash-preview": {
        "name": "gemini-3-flash-preview",
        "thinking_levels": ["minimal", "low", "medium", "high"],
        "supports_thinking": True,
        "cost_tier": "standard"
    },
    "gemini-2.5-pro": {
        "name": "gemini-2.5-pro",
        "thinking_levels": None,
        "supports_thinking": True,
        "cost_tier": "premium"
    },
    "gemini-2.5-flash": {
        "name": "gemini-2.5-flash",
        "thinking_levels": None,
        "supports_thinking": True,
        "cost_tier": "standard"
    },
    "gemini-2.5-flash-lite": {
        "name": "gemini-2.5-flash-lite",
        "thinking_levels": None,
        "supports_thinking": True,
        "cost_tier": "lite"
    },
    "gemini-2.0-flash": {
        "name": "gemini-2.0-flash",
        "thinking_levels": None,
        "supports_thinking": False,
        "cost_tier": "standard"
    }
}

# Alias mapping for easier usage
MODEL_ALIASES = {
    "gemini-flash-latest": "gemini-3-flash-preview",
    "gemini-flash-lite-latest": "gemini-2.5-flash-lite",
}

# === Default Pipeline Settings ===
DEFAULT_SETTINGS = {
    "max_segment_duration_sec": 10.0,  # Hard limit for segment duration
    "min_segment_duration_sec": 2.0,   # Minimum segment duration (v4: raised from 1.0 - <2s segments are junk)
    "default_model": "gemini-3-flash-preview",
    "default_thinking_level": "high",
    "work_dir": "/tmp/maya3_transcribe",
    "output_dir": "./transcriptions"
}

# === Language Mapping (for Supabase language codes) ===
LANGUAGE_MAP = {
    "te": "Telugu",
    "hi": "Hindi",
    "ta": "Tamil",
    "kn": "Kannada",
    "ml": "Malayalam",
    "bn": "Bengali",
    "gu": "Gujarati",
    "mr": "Marathi",
    "pa": "Punjabi",
    "or": "Odia",
    "en": "English",
    "Telugu": "Telugu",
    "Hindi": "Hindi",
}

def get_model_name(model_key: str) -> str:
    """Resolve model alias to actual model name."""
    if model_key in MODEL_ALIASES:
        return MODEL_ALIASES[model_key]
    if model_key in GEMINI_MODELS:
        return model_key
    raise ValueError(f"Unknown model: {model_key}. Available: {list(GEMINI_MODELS.keys()) + list(MODEL_ALIASES.keys())}")


Output:
Chunk ID: 1025db
Wall time: 0.0509 seconds
Process exited with code 0
Original token count: 850
Output:
"""
Supabase client module for fetching video metadata including language info.
"""
from typing import Optional, Dict, Any
from supabase import create_client, Client

from .config import SUPABASE_URL, SUPABASE_ADMIN_KEY, LANGUAGE_MAP

class SupabaseVideoClient:
    """Client for fetching video information from Supabase."""

    def __init__(self):
        self.client: Client = create_client(SUPABASE_URL, SUPABASE_ADMIN_KEY)

    def get_video_info(self, video_id: str) -> Optional[Dict[str, Any]]:
        """
        Fetch video information from Supabase.

        Args:
            video_id: YouTube video ID

        Returns:
            Dictionary with video info including language, or None if not found
        """
        try:
            # Try different table names that might store video data
            # Adjust table name based on your actual Supabase schema
            tables_to_try = ['videos', 'video_metadata', 'youtube_videos', 'content']

            for table in tables_to_try:
                try:
                    response = self.client.table(table).select("*").eq("video_id", video_id).execute()
                    if response.data:
                        return response.data[0]
                except Exception:
                    continue

            # Try with 'id' column instead
            for table in tables_to_try:
                try:
                    response = self.client.table(table).select("*").eq("id", video_id).execute()
                    if response.data:
                        return response.data[0]
                except Exception:
                    continue

            return None

        except Exception as e:
            print(f"[Supabase] Error fetching video info: {e}")
            return None

    def get_video_language(self, video_id: str, default: str = "Telugu") -> str:
        """
        Get the language for a video from Supabase.

        Args:
            video_id: YouTube video ID
            default: Default language if not found

        Returns:
            Language name (e.g., "Telugu", "Hindi")
        """
        video_info = self.get_video_info(video_id)

        if video_info:
            # Try common field names for language
            for field in ['language', 'lang', 'audio_language', 'primary_language']:
                if field in video_info and video_info[field]:
                    lang_code = video_info[field]
                    # Map language code to full name if needed
                    return LANGUAGE_MAP.get(lang_code, lang_code)

        print(f"[Supabase] Language not found for {video_id}, using default: {default}")
        return default

def get_video_language(video_id: str, default: str = "Telugu") -> str:
    """
    Convenience function to get video language.

    Args:
        video_id: YouTube video ID
        default: Default language if not found

    Returns:
        Language name
    """
    client = SupabaseVideoClient()
    return client.get_video_language(video_id, default)

if __name__ == "__main__":
    # Test fetching language for sample video
    test_video_id = "pF_BQpHaIdU"
    language = get_video_language(test_video_id, default="Telugu")
    print(f"Language for {test_video_id}: {language}")


Output:
Chunk ID: ddaa07
Wall time: 0.0517 seconds
Process exited with code 0
Original token count: 1787
Output:
"""
R2 Cloud Storage module for downloading tar files containing audio segments.
Handles searching, downloading, and extracting audio data from Cloudflare R2.
"""
import os
import re
import tarfile
import json
import shutil
from pathlib import Path
from typing import Optional, Tuple
import boto3
from botocore.config import Config

from .config import (
    R2_ENDPOINT_URL, R2_BUCKET, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY,
    DEFAULT_SETTINGS
)

class R2StorageClient:
    """Client for interacting with R2 cloud storage."""

    def __init__(self):
        self.s3_client = boto3.client(
            's3',
            endpoint_url=R2_ENDPOINT_URL,
            aws_access_key_id=R2_ACCESS_KEY_ID,
            aws_secret_access_key=R2_SECRET_ACCESS_KEY,
            config=Config(signature_version='s3v4')
        )
        self.bucket = R2_BUCKET

    def find_tar_file(self, video_id: str, use_regex: bool = False) -> Optional[str]:
        """
        Find a tar file for the given video ID in R2.

        Args:
            video_id: The YouTube video ID to search for
            use_regex: If True, use regex matching (fallback for non-exact matches)

        Returns:
            The S3 key of the found tar file, or None if not found
        """
        # Primary search: exact match with video_id in filename
        paginator = self.s3_client.get_paginator('list_objects_v2')

        # Try exact prefix match first
        exact_key = f"{video_id}.tar"
        try:
            self.s3_client.head_object(Bucket=self.bucket, Key=exact_key)
            print(f"[R2] Found exact match: {exact_key}")
            return exact_key
        except:
            pass

        # Search with video_id as prefix
        for page in paginator.paginate(Bucket=self.bucket, Prefix=video_id):
            for obj in page.get('Contents', []):
                key = obj['Key']
                if key.endswith('.tar'):
                    print(f"[R2] Found tar file with prefix: {key}")
                    return key

        # Fallback: regex search if enabled
        if use_regex:
            pattern = re.compile(rf".*{re.escape(video_id)}.*\.tar$", re.IGNORECASE)
            for page in paginator.paginate(Bucket=self.bucket):
                for obj in page.get('Contents', []):
                    key = obj['Key']
                    if pattern.match(key):
                        print(f"[R2] Found via regex: {key}")
                        return key

        print(f"[R2] No tar file found for video_id: {video_id}")
        return None

    def download_tar(self, s3_key: str, local_path: str) -> str:
        """
        Download a tar file from R2 to local storage.

        Args:
            s3_key: The S3 key of the tar file
            local_path: Local directory to download to

        Returns:
            Path to the downloaded tar file
        """
        os.makedirs(local_path, exist_ok=True)
        filename = os.path.basename(s3_key)
        local_file = os.path.join(local_path, filename)

        print(f"[R2] Downloading {s3_key} to {local_file}...")
        self.s3_client.download_file(self.bucket, s3_key, local_file)
        print(f"[R2] Download complete: {os.path.getsize(local_file) / 1024 / 1024:.2f} MB")

        return local_file

    def extract_tar(self, tar_path: str, extract_to: str) -> Tuple[str, dict]:
        """
        Extract a tar file and return paths to metadata and segments.

        Args:
            tar_path: Path to the tar file
            extract_to: Directory to extract to

        Returns:
            Tuple of (segments_dir, metadata_dict)
        """
        os.makedirs(extract_to, exist_ok=True)

        print(f"[R2] Extracting {tar_path}...")
        with tarfile.open(tar_path, 'r') as tar:
            tar.extractall(extract_to)

        # Find metadata.json
        metadata_path = None
        segments_dir = None

        for root, dirs, files in os.walk(extract_to):
            if 'metadata.json' in files:
                metadata_path = os.path.join(root, 'metadata.json')
            if 'segments' in dirs:
                segments_dir = os.path.join(root, 'segments')

        # Load metadata
        metadata = {}
        if metadata_path:
            with open(metadata_path, 'r') as f:
                metadata = json.load(f)
            print(f"[R2] Loaded metadata: {len(metadata.get('segments', []))} segments defined")
        else:
            print("[R2] Warning: metadata.json not found")

        if segments_dir and os.path.exists(segments_dir):
            flac_files = list(Path(segments_dir).glob('*.flac'))
            print(f"[R2] Found {len(flac_files)} FLAC segment files")
        else:
            print("[R2] Warning: segments directory not found")
            # Try to find segments in the extracted root
            flac_files = list(Path(extract_to).glob('**/*.flac'))
            if flac_files:
                segments_dir = str(flac_files[0].parent)
                print(f"[R2] Found segments at: {segments_dir}")

        return segments_dir, metadata

def download_video_segments(
    video_id: str,
    work_dir: Optional[str] = None,
    use_regex_fallback: bool = True,
    cleanup_tar: bool = True
) -> Tuple[str, dict]:
    """
    Main function to download and extract audio segments for a video.

    Args:
        video_id: YouTube video ID
        work_dir: Working directory for downloads (default from config)
        use_regex_fallback: Try regex matching if exact match fails
        cleanup_tar: Remove tar file after extraction

    Returns:
        Tuple of (segments_directory, metadata_dict)
    """
    if work_dir is None:
        work_dir = DEFAULT_SETTINGS["work_dir"]

    video_work_dir = os.path.join(work_dir, video_id)
    download_dir = os.path.join(video_work_dir, "download")
    extract_dir = os.path.join(video_work_dir, "extracted")

    # Clean up any previous runs
    if os.path.exists(video_work_dir):
        shutil.rmtree(video_work_dir)

    client = R2StorageClient()

    # Find the tar file
    s3_key = client.find_tar_file(video_id, use_regex=use_regex_fallback)
    if not s3_key:
        raise FileNotFoundError(f"No tar file found for video_id: {video_id}")

    # Download
    tar_path = client.download_tar(s3_key, download_dir)

    # Extract
    segments_dir, metadata = client.extract_tar(tar_path, extract_dir)

    # Cleanup tar if requested
    if cleanup_tar and os.path.exists(tar_path):
        os.remove(tar_path)
        print(f"[R2] Cleaned up tar file")

    return segments_dir, metadata

if __name__ == "__main__":
    # Test download for the sample video
    test_video_id = "pF_BQpHaIdU"
    try:
        segments_dir, metadata = download_video_segments(test_video_id)
        print(f"\nSegments directory: {segments_dir}")
        print(f"Metadata keys: {list(metadata.keys())}")
    except Exception as e:
        print(f"Error: {e}")


**Preparing to read audio modules**


sed -n '1,340p' src/backend/audio_processor.py


sed -n '1,320p' src/backend/audio_polisher.py


Output:
Chunk ID: 6a8813
Wall time: 0.0504 seconds
Process exited with code 0
Original token count: 2916
Output:
"""
Audio processing module for handling segment duration and chunking.

Splitting strategy: VAD-aware energy-based cutting.
When segments >max_duration_sec, finds the lowest-energy point in a window
around the target split time (8-13s) to avoid cutting mid-speech.
Sequential chunks track their original segment + cut points for later merging.
"""
import os
import math
import numpy as np
from pathlib import Path
from typing import List, Dict, Optional
from dataclasses import dataclass
from pydub import AudioSegment

@dataclass
class AudioChunk:
    """Represents an audio chunk ready for transcription."""
    file_path: str
    original_segment: str  # Original segment filename
    chunk_index: int       # Index within the segment (0 if not split)
    total_chunks: int      # Total chunks for this segment
    start_ms: int          # Start time in milliseconds within original
    end_ms: int            # End time in milliseconds within original
    duration_sec: float    # Duration in seconds

    @property
    def is_split(self) -> bool:
        return self.total_chunks > 1

class AudioProcessor:
    """Handles audio segment processing with duration controls."""

    def __init__(
        self,
        max_duration_sec: float = 10.0,
        min_duration_sec: float = 2.0,
        overlap_sec: float = 0.0,
        output_format: str = "flac"
    ):
        """
        Initialize the audio processor.

        Args:
            max_duration_sec: Maximum allowed segment duration (hard limit)
            min_duration_sec: Minimum segment duration to process
            overlap_sec: Overlap between chunks when splitting (helps with word boundaries)
            output_format: Output format for split chunks (flac, wav, mp3)
        """
        self.max_duration_sec = max_duration_sec
        self.min_duration_sec = min_duration_sec
        self.overlap_sec = overlap_sec
        self.output_format = output_format

    def get_audio_duration(self, file_path: str) -> float:
        """Get duration of an audio file in seconds."""
        audio = AudioSegment.from_file(file_path)
        return len(audio) / 1000.0

    def needs_splitting(self, file_path: str) -> bool:
        """Check if a file needs to be split based on duration."""
        duration = self.get_audio_duration(file_path)
        return duration > self.max_duration_sec

    def _find_energy_valley(self, samples: np.ndarray, sr: int,
                            target_ms: int, search_window_ms: int = 3000
                            ) -> int:
        """
        Find the lowest-energy point near target_ms for VAD-aware splitting.
        Searches +/- search_window_ms around target for a silence/low-energy valley.
        Returns the best cut point in milliseconds.
        """
        # RMS energy in 20ms windows
        frame_ms = 20
        frame_samples = int(sr * frame_ms / 1000)
        total_ms = int(len(samples) / sr * 1000)

        search_start = max(0, target_ms - search_window_ms)
        search_end = min(total_ms, target_ms + search_window_ms)

        best_energy = float('inf')
        best_ms = target_ms  # fallback to target if no valley found

        for t_ms in range(search_start, search_end, frame_ms):
            idx = int(t_ms * sr / 1000)
            frame = samples[idx:idx + frame_samples]
            if len(frame) < frame_samples // 2:
                continue
            rms = np.sqrt(np.mean(frame ** 2)) + 1e-10
            energy_db = 20 * np.log10(rms)
            if energy_db < best_energy:
                best_energy = energy_db
                best_ms = t_ms

        return best_ms

    def split_audio(
        self,
        file_path: str,
        output_dir: str,
        chunk_duration_sec: Optional[float] = None
    ) -> List[AudioChunk]:
        """
        Split audio using VAD-aware energy-based cutting.

        Instead of chopping at fixed intervals, finds the lowest-energy point
        in a window around the target split time to avoid cutting mid-speech.
        Target: 8-13s chunks. Searches +/- 3s around target for silence valleys.

        Sequential chunks track their original segment + cut points so they
        can be merged back later (transcripts appended, original audio retained).

        Args:
            file_path: Path to the audio file
            output_dir: Directory to save chunks
            chunk_duration_sec: Target chunk duration (defaults to max_duration_sec)

        Returns:
            List of AudioChunk objects with sequential chunk indices
        """
        if chunk_duration_sec is None:
            chunk_duration_sec = self.max_duration_sec

        audio = AudioSegment.from_file(file_path)
        total_duration_ms = len(audio)
        total_duration_sec = total_duration_ms / 1000.0

        os.makedirs(output_dir, exist_ok=True)
        original_name = Path(file_path).stem

        # If already short enough, return as single chunk
        if total_duration_sec <= self.max_duration_sec:
            return [AudioChunk(
                file_path=file_path,
                original_segment=Path(file_path).name,
                chunk_index=0,
                total_chunks=1,
                start_ms=0,
                end_ms=total_duration_ms,
                duration_sec=total_duration_sec
            )]

        # Convert to numpy for energy analysis
        samples = np.array(audio.get_array_of_samples(), dtype=np.float32)
        if audio.channels > 1:
            samples = samples.reshape(-1, audio.channels).mean(axis=1)
        samples = samples / (2**15)  # normalize int16 to float
        sr = audio.frame_rate

        # Find VAD-aware split points using energy valleys
        target_ms = int(chunk_duration_sec * 1000)
        split_points = [0]  # start of first chunk
        cursor = 0

        while cursor + target_ms < total_duration_ms:
            # Look for lowest energy near the target split point
            candidate = cursor + target_ms
            cut_ms = self._find_energy_valley(samples, sr, candidate)
            # Don't cut too short (min 5s) or too long (max 15s)
            chunk_len = cut_ms - cursor
            if chunk_len < 5000:
                cut_ms = cursor + 5000
            elif chunk_len > 15000:
                cut_ms = cursor + int(chunk_duration_sec * 1000)
            split_points.append(cut_ms)
            cursor = cut_ms

        split_points.append(total_duration_ms)  # end of last chunk

        # Build chunks from split points
        chunks = []
        num_chunks = len(split_points) - 1

        for i in range(num_chunks):
            start_ms = split_points[i]
            end_ms = split_points[i + 1]
            chunk_audio = audio[start_ms:end_ms]
            chunk_duration = len(chunk_audio) / 1000.0

            # Skip tiny trailing chunks
            if chunk_duration < self.min_duration_sec and i > 0:
                continue

            chunk_filename = f"{original_name}_chunk{i:03d}.{self.output_format}"
            chunk_path = os.path.join(output_dir, chunk_filename)
            chunk_audio.export(chunk_path, format=self.output_format)

            chunks.append(AudioChunk(
                file_path=chunk_path,
                original_segment=Path(file_path).name,
                chunk_index=i,
                total_chunks=num_chunks,
                start_ms=start_ms,
                end_ms=end_ms,
                duration_sec=chunk_duration
            ))

        return chunks

    def process_segments_directory(
        self,
        segments_dir: str,
        output_dir: Optional[str] = None,
        max_segments: Optional[int] = None,
        skip_short: bool = True
    ) -> List[AudioChunk]:
        """
        Process all segments in a directory, splitting as needed.

        Args:
            segments_dir: Directory containing audio segments
            output_dir: Directory for split chunks (default: segments_dir/chunks)
            max_segments: Maximum number of segments to process (for testing)
            skip_short: Skip segments shorter than min_duration_sec

        Returns:
            List of AudioChunk objects ready for transcription
        """
        if output_dir is None:
            output_dir = os.path.join(segments_dir, "chunks")

        # Find all audio files
        audio_extensions = {'.flac', '.wav', '.mp3', '.ogg', '.m4a'}
        segment_files = []

        for ext in audio_extensions:
            segment_files.extend(Path(segments_dir).glob(f"*{ext}"))

        # Sort by filename for consistent ordering
        segment_files = sorted(segment_files, key=lambda x: x.name)

        if max_segments:
            segment_files = segment_files[:max_segments]

        print(f"[Audio] Processing {len(segment_files)} segments...")

        all_chunks = []
        split_count = 0
        skipped_count = 0

        for segment_path in segment_files:
            duration = self.get_audio_duration(str(segment_path))

            # Skip very short segments
            if skip_short and duration < self.min_duration_sec:
                skipped_count += 1
                continue

            # Split if needed
            if duration > self.max_duration_sec:
                chunks = self.split_audio(str(segment_path), output_dir)
                split_count += 1
                print(f"[Audio] Split {segment_path.name}: {duration:.1f}s -> {len(chunks)} chunks")
            else:
                # Use original file
                chunks = [AudioChunk(
                    file_path=str(segment_path),
                    original_segment=segment_path.name,
                    chunk_index=0,
                    total_chunks=1,
                    start_ms=0,
                    end_ms=int(duration * 1000),
                    duration_sec=duration
                )]

            all_chunks.extend(chunks)

        print(f"[Audio] Processed {len(segment_files)} segments -> {len(all_chunks)} chunks")
        print(f"[Audio] Split: {split_count}, Skipped (too short): {skipped_count}")

        return all_chunks

def get_segment_stats(segments_dir: str) -> Dict:
    """
    Get statistics about segments in a directory.

    Args:
        segments_dir: Directory containing audio segments

    Returns:
        Dictionary with duration statistics
    """
    processor = AudioProcessor()

    audio_extensions = {'.flac', '.wav', '.mp3', '.ogg', '.m4a'}
    durations = []

    for ext in audio_extensions:
        for f in Path(segments_dir).glob(f"*{ext}"):
            try:
                durations.append(processor.get_audio_duration(str(f)))
            except Exception as e:
                print(f"Error reading {f}: {e}")

    if not durations:
        return {"error": "No audio files found"}

    return {
        "total_files": len(durations),
        "total_duration_sec": sum(durations),
        "avg_duration_sec": sum(durations) / len(durations),
        "min_duration_sec": min(durations),
        "max_duration_sec": max(durations),
        "over_10s_count": sum(1 for d in durations if d > 10),
        "under_1s_count": sum(1 for d in durations if d < 1)
    }

if __name__ == "__main__":
    # Test with a sample directory
    import sys
    if len(sys.argv) > 1:
        segments_dir = sys.argv[1]
        stats = get_segment_stats(segments_dir)
        print("\nSegment Statistics:")
        for key, value in stats.items():
            print(f"  {key}: {value}")