@@ -10,23 +10,35 @@ import requests from django.core.files import File from messages.models import Message +from ml_model.adapters.bytedance_model_ark import BytedanceContentType from ml_model.services.base import SimpleService -from ml_model.tasks import replicate_run +from ml_model.tasks import bytedance_model_ark_run, replicate_run class Reve(SimpleService): - PRICE = { - 'create': Decimal('12.5'), - 'edit-fast': Decimal('5'), - } + # Reve временно не работает на репликейте. Временно используем сидрим + TEMPORARY_PROVIDER_MODEL = 'seedream-5-0-260128' + + PRICE = Decimal('25') + # PRICE = { + # 'create': Decimal('12.5'), + # 'edit-fast': Decimal('5'), + # } @classmethod def predict_price(cls, content: str, file_exists: bool, info: dict[str, Any]) -> Decimal | None: - type_ = 'edit-fast' if file_exists else 'create' - return cls.PRICE[type_].quantize(Decimal('0.1'), rounding='ROUND_UP') + return cls.PRICE.quantize(Decimal('0.1'), rounding='ROUND_UP') + + def calculate_price(self) -> Decimal: + return self.PRICE.quantize(Decimal('0.1'), rounding='ROUND_UP') - def calculate_price(self, type: str) -> Decimal: - return self.PRICE[type].quantize(Decimal('0.1'), rounding='ROUND_UP') + # @classmethod + # def predict_price(cls, content: str, file_exists: bool, info: dict[str, Any]) -> Decimal | None: + # type_ = 'edit-fast' if file_exists else 'create' + # return cls.PRICE[type_].quantize(Decimal('0.1'), rounding='ROUND_UP') + # + # def calculate_price(self, type: str) -> Decimal: + # return self.PRICE[type].quantize(Decimal('0.1'), rounding='ROUND_UP') def save_results( self, @@ -53,18 +65,38 @@ class Reve(SimpleService): callback_data = { 'prompt': self.translate_prompt(input_message.content), **input_message.info, + 'size': '2K', + 'watermark': False, } - type = 'create' - if input_message.file: - kind = filetype.guess(input_message.file.read(20)) - mime = kind.mime if kind else 'application/octet-stream' - input_message.file.seek(0) - image = f'data:{mime};base64,{base64.b64encode(input_message.file.read()).decode("utf-8")}' - input_message.file.close() - callback_data.update({'image': image}) - type = 'edit-fast' - image = replicate_run(f'reve/{type}', callback_data) + if image := input_message.file: + callback_data.update({'image': image.url}) + image = bytedance_model_ark_run( + self.TEMPORARY_PROVIDER_MODEL, + callback_data, + content_type=BytedanceContentType.IMAGE, + )[0] process_time = timedelta(seconds=(time.time() - start_time)) - self.handle_invoice(input_message.content_object.model, type=type) + self.handle_invoice(input_message.content_object.model) msgs = self.save_results(input_message.content, image, process_time, save) return msgs + + # def make(self, input_message: Message, save: bool = True) -> list[Message]: + # start_time = time.time() + # callback_data = { + # 'prompt': self.translate_prompt(input_message.content), + # **input_message.info, + # } + # type = 'create' + # if input_message.file: + # kind = filetype.guess(input_message.file.read(20)) + # mime = kind.mime if kind else 'application/octet-stream' + # input_message.file.seek(0) + # image = f'data:{mime};base64,{base64.b64encode(input_message.file.read()).decode("utf-8")}' + # input_message.file.close() + # callback_data.update({'image': image}) + # type = 'edit-fast' + # image = replicate_run(f'reve/{type}', callback_data) + # process_time = timedelta(seconds=(time.time() - start_time)) + # self.handle_invoice(input_message.content_object.model, type=type) + # msgs = self.save_results(input_message.content, image, process_time, save) + # return msgs