fix: allow specifying transcription language

This commit is contained in:
k authored and Xubin Ren committed 2026-04-22 12:41:32 +08:00
1 parent 1826ab44fa
commit 123d69bfb7
6 files changed
+104 -12

No files matched your search

+15 -2
View File
@@ -10,13 +10,19 @@ from loguru import logger
class OpenAITranscriptionProvider:
"""Voice transcription provider using OpenAI's Whisper API."""
def __init__(self, api_key: str | None = None, api_base: str | None = None):
def __init__(
self,
api_key: str | None = None,
api_base: str | None = None,
language: str | None = None,
):
self.api_key = api_key or os.environ.get("OPENAI_API_KEY")
self.api_url = (
api_base
or os.environ.get("OPENAI_TRANSCRIPTION_BASE_URL")
or "https://api.openai.com/v1/audio/transcriptions"
)
self.language = language
async def transcribe(self, file_path: str | Path) -> str:
if not self.api_key:
@@ -30,6 +36,8 @@ class OpenAITranscriptionProvider:
async with httpx.AsyncClient() as client:
with open(path, "rb") as f:
files = {"file": (path.name, f), "model": (None, "whisper-1")}
if self.language:
files["language"] = (None, self.language)
headers = {"Authorization": f"Bearer {self.api_key}"}
response = await client.post(
self.api_url, headers=headers, files=files, timeout=60.0,
@@ -48,7 +56,12 @@ class GroqTranscriptionProvider:
Groq offers extremely fast transcription with a generous free tier.
"""
def __init__(self, api_key: str | None = None, api_base: str | None = None, language: str | None = None):
def __init__(
self,
api_key: str | None = None,
api_base: str | None = None,
language: str | None = None,
):
self.api_key = api_key or os.environ.get("GROQ_API_KEY")
self.api_url = api_base or os.environ.get("GROQ_BASE_URL") or "https://api.groq.com/openai/v1/audio/transcriptions"
self.language = language