c3f1032e80
The notice could survive beside a finished transcript, leaving a take a moment message under a completed reply. The typing indicator covers the wait, so drop the separate notice and its cleanup task.
159 lines
9.1 KiB
Markdown
159 lines
9.1 KiB
Markdown
# Transcribe
|
|
|
|
Speech to text for voice messages, with optional translation into another language. Audio is sent to a local Whisper service; translation either uses Whisper's own translate task or a local NLLB service.
|
|
|
|
## Requirements
|
|
|
|
- A Whisper HTTP service on `127.0.0.1:8201`, accepting multipart uploads at `/transcribe`.
|
|
- An NLLB HTTP service on `127.0.0.1:8200`, accepting JSON at `/translate`, for any target other than English.
|
|
- The `langdetect` package for automatic source detection when translating text.
|
|
- Read Message History permission for the bot, required by the `transcribe` command.
|
|
|
|
Both service addresses are compiled into `transcribe/transcribe.py`. Changing hosts means editing `WHISPER_URL` or `NLLB_URL` and reloading the cog.
|
|
|
|
## Commands
|
|
|
|
| Command | Aliases | Arguments | Cooldown | Concurrency |
|
|
|---|---|---|---|---|
|
|
| `[p]transcribe` | `stt` | `[language]` | 2 per 60 seconds per user | 1 across the bot, queued |
|
|
| `[p]translate` | none | `<language> [text or from <source>]` | 4 per 60 seconds per user | 1 across the bot, queued |
|
|
| `[p]transcribeset` | `sttset` | none | none | none |
|
|
| `[p]transcribeset showoriginal` | none | `<true or false>` | none | none |
|
|
|
|
All commands are guild only. The settings group requires Manage Server or a Red admin role.
|
|
|
|
The single global concurrency slot means one transcription or translation runs at a time across every guild, and later requests wait in Red's queue rather than failing.
|
|
|
|
## Finding the audio
|
|
|
|
`[p]transcribe` looks in exactly three places, in order:
|
|
|
|
1. The message it is replying to.
|
|
2. The invoking message's own attachments.
|
|
3. The single message posted immediately before it.
|
|
|
|
It does not scan further back. This is intentional: an earlier version scanned 25 messages and would silently transcribe an unrelated older clip when the new message did not match the extension list.
|
|
|
|
A message qualifies when any attachment has a content type starting with `audio/` or `video/`, or a filename ending in one of:
|
|
|
|
```
|
|
.ogg .opus .wav .mp3 .m4a .flac .aac .wma
|
|
.mp4 .mov .m4v .mkv .webm .avi .mpeg .mpg
|
|
```
|
|
|
|
Video containers are accepted because Whisper extracts the audio track, and members send videos expecting them to be transcribed.
|
|
|
|
Forwarded messages have no top level attachments. Their files live in a message snapshot, and the same extension and content type checks run against each snapshot the message carries. Forwarding a voice message and then running `[p]transcribe` bare works because the forward is the previous message. Replying to a forward works too, as does attaching media to the command itself. Forwarded message support needs a bot running discord.py 2.5 or newer, which introduced message snapshots. On an older build, forwards still produce the usual no-media reply.
|
|
|
|
When no media is found, the reply explains the three places it checked.
|
|
|
|
Each reply also includes a small-print note naming the author of the transcribed message and linking to it, so a wrong pick is visible. When the audio came from a forwarded snapshot, the note says "forwarded message" and links to the forward rather than the original post. That author is the member who forwarded it, because snapshots do not carry the original author.
|
|
|
|
## Transcription paths
|
|
|
|
### No target language, or English
|
|
|
|
`[p]transcribe` and `[p]transcribe english` send `task=translate` to Whisper. One model pass produces English text directly, and NLLB is not used. This path never reports a source language or a confidence value.
|
|
|
|
### Any other target language
|
|
|
|
1. The audio is sent with `task=transcribe`, so Whisper returns text, the detected language, and `language_probability`.
|
|
2. If the detected language equals the target, the bot says the text is already in that language and posts the transcript.
|
|
3. If the confidence is below 0.5, the bot refuses to translate, states the detected language and confidence, and posts the transcript instead. Translating from a guessed language produces fluent nonsense, so this is a hard stop.
|
|
4. Otherwise the text is sent to NLLB with the detected language as source and the resolved target.
|
|
|
|
The result header names both languages in the form `Source to Target`. When the guild setting `show_original` is on, which is the default, the original transcript is appended in small print. The setting applies only to `transcribe`; `translate` never shows the source.
|
|
|
|
Replies are clipped near 1900 characters, with the note that the text was cut off.
|
|
|
|
## Errors
|
|
|
|
The command holds a typing indicator for the whole operation.
|
|
|
|
Service errors are prefixed with a warning symbol and include the specific cause:
|
|
|
|
| Message | Cause |
|
|
|---|---|
|
|
| "That audio is N MB, over the 20 MB limit." | Attachment size above 20 MiB |
|
|
| "Could not download the audio (HTTP N)." | Discord CDN refused the download |
|
|
| "The transcription service is not reachable on port 8201." | Connection refused |
|
|
| "The transcription service timed out." | No response within 300 seconds |
|
|
| "The translation service is not reachable on port 8200." | NLLB down |
|
|
| "The translation service timed out." | NLLB slow |
|
|
| "No speech detected in that message." | Whisper returned empty text |
|
|
| "That audio file is empty." | Zero byte download |
|
|
| "Something went wrong while transcribing that audio." | Any other exception, logged with a traceback |
|
|
|
|
Requests use a 300 second total timeout. Malformed JSON from a service is not caught specifically and falls into the generic message, because only connection and timeout errors are mapped.
|
|
|
|
## Text translation
|
|
|
|
`[p]translate <language>` translates a replied message or text typed after the language.
|
|
|
|
- A replied message's text is assembled from its content, embed titles, descriptions, footers and field values.
|
|
- Text typed after the target is used when there is no reply, or when the reply has no readable text.
|
|
- Adding `from <language>` at the start of the typed text states the source and skips detection. This form works with a reply too when the text is included.
|
|
- Input is truncated to 8000 characters with a note in the reply.
|
|
|
|
Automatic detection uses `langdetect` with a fixed seed for repeatable results. It refuses in three cases:
|
|
|
|
- Input shorter than 12 characters, because short strings produce confident nonsense such as "ok" reading as Slovak.
|
|
- Top guess below 0.70 probability.
|
|
- Top guess within 15 points of the runner up.
|
|
|
|
Every refusal names the `from <language>` form as the fix.
|
|
|
|
If the detected or stated source equals the target, the bot reports there is nothing to do. If NLLB returns text identical to the input, the bot says it already reads as the target. A reply longer than 1800 characters is clipped.
|
|
|
|
`translate` does not declare the Read Message History check that `transcribe` declares. When the replied message is cached it works normally; when it is not cached and the fetch is forbidden, the fetch returns nothing and the command falls back to typed text.
|
|
|
|
## Language input
|
|
|
|
`_resolve_language` accepts several forms, checked in this order:
|
|
|
|
1. An English language name, case-insensitive, with underscores treated as spaces. Examples: `french`, `brazilian portuguese` is not present, `haitian creole` is. Over 90 names are mapped.
|
|
2. A script variant, matched against the raw lowercased input: `zh-hant`, `zh-tw`, `zh-hk`, `traditional chinese`, `zh-hans`, `zh-cn`, `zh-sg`, `simplified chinese`, `cantonese traditional`.
|
|
3. A FLORES-200 tag matching `aaa_Aaaa`, such as `zho_Hans`, passed straight through.
|
|
4. An ISO code, taking the part before the first hyphen, such as `pt-BR` becoming `pt`, accepted when the code has a display label.
|
|
|
|
Anything else produces a message listing the accepted forms.
|
|
|
|
Unknown languages are rejected before any media is downloaded, so a typo costs nothing.
|
|
|
|
## Detection refinements
|
|
|
|
`langdetect` returns codes such as `zh-cn`. These are mapped to FLORES tags for Chinese so the translation service picks the right script. Other codes pass through unchanged.
|
|
|
|
The confidence threshold of 0.5 applies to Whisper's detection, not to `langdetect`. The two tools have separate thresholds in separate paths.
|
|
|
|
## Settings
|
|
|
|
`[p]transcribeset` with no subcommand prints whether the original transcript is shown. `[p]transcribeset showoriginal <value>` sets it. The value is parsed by Red's boolean converter, so `true`, `yes`, `on` and `1` work.
|
|
|
|
The setting affects only the `transcribe` command's non-English path. It has no effect on the English path, which has no source text to show, or on `translate`.
|
|
|
|
## Limits
|
|
|
|
| Limit | Value |
|
|
|---|---|
|
|
| Attachment size | 20 MiB |
|
|
| HTTP timeout | 300 seconds |
|
|
| Text translation input | 8000 characters |
|
|
| Language confidence for translation | 0.5 from Whisper |
|
|
| Text detection confidence | 0.70, and a 15 point lead |
|
|
| Minimum text for detection | 12 characters |
|
|
| Reply length | 1700 to 1900 characters depending on the branch |
|
|
| User cooldown, transcribe | 2 per 60 seconds |
|
|
| User cooldown, translate | 4 per 60 seconds |
|
|
| Global concurrency | 1 |
|
|
|
|
## Stored data
|
|
|
|
Guild config under identifier `481516234`:
|
|
|
|
| Key | Default |
|
|
|---|---|
|
|
| `show_original` | `true` |
|
|
|
|
Audio is downloaded into memory, forwarded to Whisper, and discarded. Nothing is written to disk. The transcript is sent to the local NLLB service when a non-English target is requested.
|