transcribe: post a progress notice for long requests

Long clips and slow CPU inference leave users staring at a typing indicator.
Post a short note after three seconds of work and remove it when the request
finishes, so short messages stay quiet.
This commit is contained in:
2026-11-05 11:54:27 -06:00
parent f7538574c7
commit 809dcf5a87
2 changed files with 41 additions and 2 deletions
+38 -1
View File
@@ -2,8 +2,10 @@
from __future__ import annotations
import asyncio
import logging
import re
from contextlib import asynccontextmanager, suppress
from typing import Optional
import aiohttp
@@ -39,6 +41,11 @@ MEDIA_EXTENSIONS = (
# guessed language produces fluent nonsense, so we show the transcript instead.
MIN_LANGUAGE_CONFIDENCE = 0.5
# Post a progress note when a request is still running after this many seconds.
# Long voice messages and slow CPU inference both cross this line; short clips
# finish before the notice is sent, so they never see it.
SLOW_REQUEST_NOTICE_SECONDS = 3.0
# English language names -> ISO-639-1, so `[p]transcribe french` works.
LANGUAGE_NAMES = {
"afrikaans": "af", "albanian": "sq", "amharic": "am", "arabic": "ar",
@@ -330,6 +337,36 @@ class Transcribe(commands.Cog):
"zh-tw": "zho_Hant", "zh-hant": "zho_Hant",
}.get(code, code)
@asynccontextmanager
async def _slow_notice(self, ctx: commands.Context, delay: float = SLOW_REQUEST_NOTICE_SECONDS):
"""Tell the channel that a long transcription is still running.
Waits `delay` seconds before posting, and removes the notice when the
surrounding block exits, whether it succeeded, failed or returned early.
"""
holder: dict[str, Optional[discord.Message]] = {"message": None}
async def announce():
await asyncio.sleep(delay)
try:
holder["message"] = await ctx.send(
"Still transcribing. Longer recordings can take a moment."
)
except discord.HTTPException:
pass
task = asyncio.create_task(announce())
try:
yield
finally:
task.cancel()
with suppress(asyncio.CancelledError):
await task
message = holder["message"]
if message is not None:
with suppress(discord.HTTPException):
await message.delete()
# ----------------------------------------------------------------- commands
@commands.command(name="transcribe", aliases=["stt"])
@commands.guild_only()
@@ -363,7 +400,7 @@ class Transcribe(commands.Cog):
return
origin = self._origin_note(origin_message)
async with ctx.typing():
async with ctx.typing(), self._slow_notice(ctx):
try:
data = await self._download(attachment)
if not data: