@@ -19,15 +19,10 @@ from payments.selectors.payment_plan_selector import PaymentPlanSelector class Ray(SimpleService): - """ - Ray Service - contains abstract method make, which makes a generation - """ + TOKENS_COST = {'ray-2-720p': Decimal('54'), 'ray-flash-2-540p': Decimal('9.9')} - TOKENS_COST = Decimal('135') - - def calculate_price(self) -> Decimal: - return self.TOKENS_COST.quantize(Decimal('0.1'), rounding='ROUND_UP') + def calculate_price(self, version: str, duration: int) -> Decimal: + return (self.TOKENS_COST[version] * duration).quantize(Decimal('0.1'), rounding='ROUND_UP') def save_results(self, content: str, t: timedelta, video: str, save: bool = True) -> list[Message]: msg = Message( @@ -41,8 +36,12 @@ class Ray(SimpleService): return [msg] def make(self, input_message: Message, save: bool = True) -> list[Message]: - if (balance := PaymentPlanSelector(self.store.user).get_current_balance()) < self.TOKENS_COST: - raise InsufficientBalance(balance, self.TOKENS_COST) + duration = input_message.info.get('duration', 5) + version = input_message.info.get('version', 'ray-2-720p') + if (balance := PaymentPlanSelector(self.store.user).get_current_balance()) < ( + cost := self.TOKENS_COST[version] * duration + ): + raise InsufficientBalance(balance, cost) callback_data = dict({'prompt': self.translate_prompt(input_message.content), **input_message.info}) if input_message.file: kind = filetype.guess(input_message.file.read(20)) @@ -53,12 +52,12 @@ class Ray(SimpleService): callback_data.update({'start_image': image}) start_time = time.time() try: - video = replicate_run('luma/ray', callback_data) + video = replicate_run(f'luma/{version}', callback_data) except ModelError as exc: if any(error in str(exc) for error in ('E005', 'E006', 'sexual')): raise RequestBlocked raise GenerationException from exc process_time = timedelta(seconds=(time.time() - start_time)) - self.handle_invoice(input_message.content_object.model) + self.handle_invoice(input_message.content_object.model, version=version, duration=duration) msgs = self.save_results(input_message.content, process_time, video, save) return msgs