feat: локальный Whisper (faster-whisper large-v3) вместо OpenAI API
Не зависит от OpenAI API/прокси. Модель large-v3 int8 на CPU. Лучшее качество распознавания русского языка. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -298,25 +298,35 @@ async def text_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
|
||||
# ── Voice message handling ────────────────────────────────────────────────
|
||||
|
||||
_whisper_model = None
|
||||
|
||||
|
||||
def _get_whisper():
|
||||
"""Lazy-load faster-whisper model (large-v3, CPU)."""
|
||||
global _whisper_model
|
||||
if _whisper_model is None:
|
||||
from faster_whisper import WhisperModel
|
||||
model_size = os.environ.get("WHISPER_MODEL", "large-v3")
|
||||
logger.info(f"Loading Whisper model: {model_size} ...")
|
||||
_whisper_model = WhisperModel(
|
||||
model_size, device="cpu", compute_type="int8",
|
||||
)
|
||||
logger.info("Whisper model loaded.")
|
||||
return _whisper_model
|
||||
|
||||
|
||||
async def transcribe_voice(file_path: str) -> str:
|
||||
"""Transcribe voice message using OpenAI Whisper API."""
|
||||
import httpx
|
||||
"""Transcribe voice message using local faster-whisper."""
|
||||
loop = asyncio.get_event_loop()
|
||||
return await loop.run_in_executor(None, _transcribe_sync, file_path)
|
||||
|
||||
api_key = os.environ.get("OPENAI_API_KEY", "")
|
||||
if not api_key:
|
||||
return ""
|
||||
|
||||
proxy = os.environ.get("OPENAI_PROXY") or os.environ.get("HTTPS_PROXY")
|
||||
async with httpx.AsyncClient(timeout=30, proxy=proxy) as client:
|
||||
with open(file_path, "rb") as f:
|
||||
resp = await client.post(
|
||||
"https://api.openai.com/v1/audio/transcriptions",
|
||||
headers={"Authorization": f"Bearer {api_key}"},
|
||||
files={"file": ("voice.ogg", f, "audio/ogg")},
|
||||
data={"model": "whisper-1", "language": "ru"},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json().get("text", "")
|
||||
def _transcribe_sync(file_path: str) -> str:
|
||||
model = _get_whisper()
|
||||
segments, info = model.transcribe(
|
||||
file_path, language="ru", beam_size=5, vad_filter=True,
|
||||
)
|
||||
return " ".join(seg.text.strip() for seg in segments)
|
||||
|
||||
|
||||
async def voice_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
|
||||
Reference in New Issue
Block a user