@@ -34,6 +34,7 @@ from ml_model.services.runway import Runway from ml_model.services.ray import Ray from ml_model.services.recraft import Recraft from ml_model.services.sdxlemoji import Sdxlemoji +from ml_model.services.seedream import Seedream from ml_model.services.stablediffusion import Stablediffusion from ml_model.services.upscaleai import Upscaleai from ml_model.services.veo import Veo @@ -0,0 +1,60 @@ +import base64 +import logging +import time +from datetime import timedelta +from decimal import Decimal +from io import BytesIO + +import filetype +import requests +from django.core.files import File + +from messages.models import Message +from ml_model.models import ModelCategory, ModelInput, ModelParameter +from ml_model.services.base import SimpleService +from ml_model.tasks import replicate_run + + +class Seedream(SimpleService): + PRICE = Decimal('9') + + def calculate_price(self, max_images: int) -> Decimal: + return self.PRICE.quantize(Decimal('0.1'), rounding='ROUND_UP') * max_images + + def save_results(self, prompt: str, images: list, time: timedelta, save: bool = True) -> list[Message]: + messages: list[Message] = [] + for image in images: + messages.append( + Message( + content_object=self.store, + elapsed_time=time, + content=prompt, + file=File(BytesIO(requests.get(image).content), '.png'), + ) + ) + if save: + return Message.objects.bulk_create(messages) + return messages + + def make(self, input_message: Message, save: bool = True) -> list[Message]: + start_time = time.time() + callback_data = { + 'prompt': self.translate_prompt(input_message.content), + 'size': 'custom', + **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]}) + images = replicate_run('bytedance/seedream-4', callback_data) + process_time = timedelta(seconds=(time.time() - start_time)) + self.handle_invoice(input_message.content_object.model, max_images=len(images)) + msgs = self.save_results(input_message.content, images, process_time, save) + return msgs