@@ -9,7 +9,7 @@ from django.core.files import File from replicate.exceptions import ModelError from messages.models import Message -from ml_model.exceptions import GenerationException, RequestBlocked +from ml_model.exceptions import GenerationException, PromptLengthExceeded, RequestBlocked from ml_model.services.base import SimpleService from ml_model.tasks import replicate_run from payments.exceptions.insufficient_balance import InsufficientBalance @@ -18,10 +18,11 @@ from payments.selectors.payment_plan_selector import PaymentPlanSelector class Elevenlabs(SimpleService): TOKENS_PER_1K_CHARS = Decimal('6') + MAX_CONTENT_LENGTH = 2000 @classmethod def predict_price(cls, content: str, file_exists: bool, info: dict[str, Any]) -> Decimal | None: - chars = len(content) + chars = min(len(content), cls.MAX_CONTENT_LENGTH) price = cls.TOKENS_PER_1K_CHARS * Decimal(chars) / Decimal(1000) return price.quantize(Decimal('0.1'), rounding='ROUND_UP') @@ -42,6 +43,8 @@ class Elevenlabs(SimpleService): return [msg] def make(self, input_message: Message, save: bool = True) -> list[Message]: + if len(input_message.content) > self.MAX_CONTENT_LENGTH: + raise PromptLengthExceeded(max_length=self.MAX_CONTENT_LENGTH) cost = self.calculate_price(input_message.content) if (balance := PaymentPlanSelector(self.store.user).get_current_balance()) < cost: raise InsufficientBalance(balance, cost)