voice-dictation/src/voice_dictation/core/pricing.py
2026-10-20 11:45:49 +03:00

45 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Расчёт стоимости транскрипции Whisper API."""
from __future__ import annotations
import math
from collections.abc import Mapping
from decimal import Decimal
from types import MappingProxyType
from typing import Final
class PriceCalculator:
"""Вычисляет стоимость транскрипции по модели и длительности.
Цены — за минуту аудио, округление вверх до целой минуты (как у OpenAI).
"""
PRICES_PER_MINUTE: Final[Mapping[str, Decimal]] = MappingProxyType(
{
"gpt-transcribe": Decimal("0.0045"),
"gpt-4o-mini-transcribe": Decimal("0.003"),
"gpt-4o-transcribe": Decimal("0.006"),
"whisper-1": Decimal("0.006"),
}
)
@classmethod
def cost(cls, model: str, duration_sec: float) -> Decimal:
"""Рассчитывает стоимость транскрипции.
Args:
model: Идентификатор модели.
duration_sec: Длительность аудио в секундах.
Returns:
Стоимость в USD с точностью до 6 знаков.
Raises:
KeyError: Если модель не найдена в прайсе.
"""
price_per_minute = cls.PRICES_PER_MINUTE[model]
# OpenAI округляет вверх до целой секунды, минимум 1 сек
duration_sec_ceil = max(1.0, math.ceil(duration_sec))
minutes = Decimal(str(duration_sec_ceil)) / Decimal("60")
return (price_per_minute * minutes).quantize(Decimal("0.000001"))