@@ -1,11 +1,9 @@ -import base64 import time from datetime import timedelta from decimal import Decimal from io import BytesIO from typing import Any -import filetype import requests from django.core.files import File from replicate.exceptions import ModelError @@ -17,15 +15,14 @@ from ml_model.tasks import replicate_run class Seedream(SimpleService): - PRICE = Decimal('9') + PRICE = Decimal('10.5') @classmethod def predict_price(cls, content: str, file_exists: bool, info: dict[str, Any]) -> Decimal | None: - max_images = 5 if info['story_mode'] else 1 - return cls.PRICE.quantize(Decimal('0.1'), rounding='ROUND_UP') * max_images + return cls.PRICE.quantize(Decimal('0.1'), rounding='ROUND_UP') - def calculate_price(self, max_images: int) -> Decimal: - return self.PRICE.quantize(Decimal('0.1'), rounding='ROUND_UP') * max_images + def calculate_price(self) -> Decimal: + return self.PRICE.quantize(Decimal('0.1'), rounding='ROUND_UP') def save_results(self, prompt: str, images: list, time: timedelta, save: bool = True) -> list[Message]: messages: list[Message] = [] @@ -44,28 +41,24 @@ class Seedream(SimpleService): def make(self, input_message: Message, save: bool = True) -> list[Message]: start_time = time.time() + raw_aspect_ratio = input_message.info.pop('aspect_ratio', 'Исходное изображение') + aspect_ratio = ( + 'match_input_image' if raw_aspect_ratio == 'Исходное изображение' else raw_aspect_ratio + ) callback_data = { - 'prompt': self.translate_prompt(input_message.content), - 'size': 'custom', + 'prompt': input_message.content, + 'aspect_ratio': aspect_ratio, **input_message.info, } - if input_message.info.get('story_mode', False): - callback_data.update({'sequential_image_generation': 'auto', 'max_images': 5}) - callback_data['prompt'] = f'Generate a sequence of multiple images. {callback_data["prompt"]}' - 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_input': [image]}) + if image := input_message.file: + callback_data.update({'image_input': [image.url]}) try: - images = replicate_run('bytedance/seedream-4', callback_data) + images = replicate_run('bytedance/seedream-5-lite', 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, max_images=len(images)) + self.handle_invoice(input_message.content_object.model) msgs = self.save_results(input_message.content, images, process_time, save) return msgs