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:
@@ -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:
|
||||
|
||||
+3
-1
@@ -66,7 +66,9 @@ Replies are clipped near 1900 characters, with the note that the text was cut of
|
||||
|
||||
## Progress and errors
|
||||
|
||||
The command holds a typing indicator for the whole operation. Service errors are prefixed with a warning symbol and include the specific cause:
|
||||
The command holds a typing indicator for the whole operation. If a request is still running after three seconds, including the download and any translation, the bot posts a short notice that it is still transcribing. The notice is deleted when the request finishes, whether it succeeds or fails. Short clips complete before the delay and never show it.
|
||||
|
||||
Service errors are prefixed with a warning symbol and include the specific cause: Service errors are prefixed with a warning symbol and include the specific cause:
|
||||
|
||||
| Message | Cause |
|
||||
|---|---|
|
||||
|
||||
Reference in New Issue
Block a user