Skip to content

Commit

Permalink
a bunch of stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
genekogan committed Feb 7, 2024
1 parent 5da7926 commit 0d76128
Show file tree
Hide file tree
Showing 36 changed files with 1,398 additions and 542 deletions.
64 changes: 52 additions & 12 deletions app/animations/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,53 @@
from PIL import Image, ImageDraw

from ..plugins import replicate, elevenlabs, s3
from ..character import EdenCharacter
from ..utils import combine_speech_video, wrap_text, get_font
from ..character import Character
from ..utils import combine_speech_video, wrap_text, get_font, create_dynamic_model
from ..scenarios.tasks import general_assistant
from ..models.tasks import SimpleAssistantRequest
from ..plugins import elevenlabs


def select_random_voice(character: Character = None):
if not character:
return elevenlabs.get_random_voice()

gender_schema = create_dynamic_model("gender", ["male", "female"])

prompt = f"What is the most likely gender of the following character, male or female?\n\nName: {character.name}\n\nDescription: {character.identity}"

request = SimpleAssistantRequest(
prompt=prompt,
model="gpt-3.5-turbo",
params={"temperature": 0.0, "max_tokens": 10},
output_schema=gender_schema
)

try:
gender = general_assistant(request)
voice_id = elevenlabs.get_random_voice(gender=gender)
except Exception as e:
voice_id = elevenlabs.get_random_voice()
finally:
return voice_id


def talking_head(
character: EdenCharacter,
character: Character,
text: str,
width: Optional[int] = None,
height: Optional[int] = None
) -> str:
if character.voice:
voice_id = character.voice
else:
voice_id = select_random_voice(character)

audio_bytes = elevenlabs.tts(
text,
voice=character.voice
voice=voice_id
)

audio_url = s3.upload(audio_bytes, "mp3")
output_url, thumbnail_url = replicate.wav2lip(
face_url=character.image,
Expand All @@ -29,15 +62,22 @@ def talking_head(


def screenplay_clip(
character: EdenCharacter,
character: Optional[Character],
speech: str,
image_text: str,
width: Optional[int] = None,
height: Optional[int] = None
) -> str:
if not character:
voice_id = select_random_voice()
else:
if character.voice:
voice_id = character.voice
else:
voice_id = select_random_voice(character)
audio_bytes = elevenlabs.tts(
speech,
voice=character.voice
voice=voice_id
)
audio_url = s3.upload(audio_bytes, "mp3")
video_url, thumbnail_url = replicate.txt2vid(
Expand All @@ -57,7 +97,7 @@ def comic_strip(
caption_padding_top: int = 10,
line_spacing: int = 1.3,
font_size: int = 48,
font_ttf: str = 'Roboto-Regular.ttf'
font_ttf: str = 'Raleway-Light.ttf'
):
font = get_font(font_ttf, font_size)
num_panels = len(images)
Expand Down Expand Up @@ -119,19 +159,19 @@ def comic_strip(
def poster(
image: Image.Image,
caption: str,
margin: int = 10,
margin: int = 32,
caption_padding_top: int = 10,
line_spacing: int = 1.3,
font_size: int = 48,
font_ttf: str = 'Roboto-Regular.ttf',
font_size: int = 36,
font_ttf: str = 'Raleway-Light.ttf',
shadow_offset: tuple = (1, 1.4),
font_color: str = '#e7e7e7',
shadow_color: str = '#d3d3d3'
):
font = get_font(font_ttf, font_size)
width, height = image.size

draw = ImageDraw.Draw(Image.new('RGB', (width, height))) # Temporary image for wrapping text
draw = ImageDraw.Draw(Image.new('RGB', (width, height)))
wrapped_caption = wrap_text(draw, caption, font, width - 2 * margin)
num_lines = len(wrapped_caption)

Expand All @@ -152,7 +192,7 @@ def poster(
caption_box = Image.new('RGB', (width, caption_box_height), color='black')
draw = ImageDraw.Draw(caption_box)

caption_y = caption_padding_top + int(margin/2)
caption_y = caption_padding_top + 0*margin/2
for line in wrapped_caption:
draw.text((margin + shadow_offset[0], caption_y + shadow_offset[1]), line, fill=shadow_color, font=font)
draw.text((margin, caption_y), line, fill=font_color, font=font)
Expand Down
3 changes: 3 additions & 0 deletions app/animations/dialogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def animated_dialogue(request: DialogueRequest):
characters[character_id].image
for character_id in request.character_ids
]
images = list(set(images))
width, height = utils.calculate_target_dimensions(images, MAX_PIXELS)

def run_talking_head_segment(message, idx):
Expand Down Expand Up @@ -61,6 +62,8 @@ def run_talking_head_segment(message, idx):
os.remove(video_file)

# generate thumbnail
print("DO IMAGES")
print(images)
thumbnail = utils.create_dialogue_thumbnail(*images, width, height)
thumbnail_url = s3.upload(thumbnail, "webp")

Expand Down
41 changes: 22 additions & 19 deletions app/animations/little_martians.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,30 @@ def little_martian_poster(request: LittleMartianRequest):
init_image = random.choice(data['init_images'])
init_image_strength = random_interval(*data['init_image_strength'])


print("go")


print(lora)
print(modifier)
print(lora_scale)


littlemartian_writer = LLM(
model=request.model,
system_message=littlemartians_poster_system.template,
params=params,
)
print("------")
print(request.prompt)



prompt = littlemartians_poster_prompt.substitute(
martian = request.martian.value,
setting = request.setting.value,
genre = request.genre.value,
premise = request.prompt,
)


print(prompt)
print("==============")
print("PROMPT:", prompt)

result = littlemartian_writer(prompt, output_schema=Poster)

print(result)
prompt = result['image']


text_input = f'{modifier}, {prompt}'
# text_input = f'{modifier}'

print(text_input)
print("RESULT:", prompt)
print("text_input:", text_input)

# def run_panel(panel, idx):
# # pick lora of character
Expand Down Expand Up @@ -94,6 +80,8 @@ def little_martian_poster(request: LittleMartianRequest):
"n_samples": 1,
}


print("config")
print(config)


Expand All @@ -102,9 +90,12 @@ def little_martian_poster(request: LittleMartianRequest):
print(image_url, thumbnail_url)

caption = result['caption']

print(caption)
image = utils.download_image(image_url)
composite_image, thumbnail_image = poster(image, caption)

print("-========-")
# results = utils.process_in_parallel(
# comic_book['panels'],
# run_panel,
Expand All @@ -123,4 +114,16 @@ def little_martian_poster(request: LittleMartianRequest):
output_url = s3.upload(img_bytes, "jpg")
thumbnail_url = s3.upload(thumbnail_bytes, "webp")

# download output_url
# martian, setting, genre, pr = request.martian.value, request.setting.value, request.genre.value, request.prompt

# filename = f'{martian}_{setting}_{genre}_{pr}.jpg'

# with open(filename, "wb") as f:
# f.write(requests.get(output_url).content)

# # save config as json file
# with open(f"{filename}.json", "w") as f:
# f.write(str(config))

return output_url, thumbnail_url
56 changes: 49 additions & 7 deletions app/animations/story.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from .. import utils
from ..plugins import replicate, elevenlabs, s3
from ..character import EdenCharacter
from ..character import Character, EdenCharacter
from ..scenarios import story
from ..models import StoryRequest
from .animation import screenplay_clip
Expand All @@ -15,6 +15,7 @@

def animated_story(request: StoryRequest):
screenplay = story(request)
#screenplay = {'clips': [{'voiceover': 'narrator', 'character': None, 'speech': 'In a quaint ski town replete with snowy peaks and the charm of winter, there lies a forgotten legend eager to reclaim his throne.', 'image_description': 'The sun glints off the snow-capped mountains, as a quaint alpine village stirs to life in the valley below.'}, {'voiceover': 'character', 'character': 'Viper', 'speech': "You see this medal? That's the mark of a champion. Sometimes I miss the cheers, the rush of the wind... the taste of victory.", 'image_description': 'A bronze-hued man, Viper, sits in a cozy ski lodge. Various medals and trophies litter the space around him. His eyes gleam with pride, yet carry a tinge of yearning.'}, {'voiceover': 'narrator', 'character': None, 'speech': 'But life, like the serpentine trails he once conquered, took an unexpected turn when a new star shot across his sky.', 'image_description': 'Laughter echoes through the local gym as a towering woman playfully scores basket after basket on an indoor court.'}, {'voiceover': 'character', 'character': 'Linda', 'speech': "I thought I knew what it was to be at the top, but Linda, she... she's a mountain of her own, you know?", 'image_description': "Viper gazes through the gym's glass doors, admiration painted on his features as he watches a nine-foot-tall woman, Linda, effortlessly dominate the basketball court."}, {'voiceover': 'narrator', 'character': None, 'speech': 'From the frozen slopes to the polished hardwood, it was as if fate had crafted a match not even the sports gods could have predicted.', 'image_description': 'A cosmic dance of stars and snowflakes swirls around the figures of Viper and Linda as they first meet, their differences dwarfed by the evident spark between them.'}, {'voiceover': 'character', 'character': 'Viper', 'speech': "Hey Linda, want to trade some stories? I've got a lifetime on the slopes and you've got... well, a view from the top. Literally.", 'image_description': 'Viper approaches Linda with a mischievous smirk, leaning on a basketball. Linda towers above him, her presence as commanding as her height.'}, {'voiceover': 'character', 'character': 'Linda', 'speech': "I am Linda I am so cool", 'image_description': 'Linda plays basketball at night'}]}
print(screenplay)

characters = {
Expand All @@ -27,20 +28,61 @@ def animated_story(request: StoryRequest):
for character_id, character in characters.items()
}

# images = [
# characters[character_id].image
# for character_id in request.character_ids
# ]
print("LETS MAKE ALL THE CHARACTERS")
print("LETS MAKE ALL THE CHARACTERS 2")
thechars = [clip['character'] for clip in screenplay['clips']]
print("LETS MAKE ALL THE CHARACTERS 3")
print(thechars)
print(character_name_lookup)
# if any character is new, assign a random voice
for clip in screenplay['clips']:
print(" --> ", clip['character'], clip['voiceover'])
if clip['character']:
character_name = clip['character']
print("ceck " , character_name)
print("ceck2 " , character_name)
print("look here", character_name_lookup)
print("look there", characters)
if character_name not in character_name_lookup:
print("MAKE A NEW CHARACTER", character_name)
characters[character_name] = Character(name=character_name)
character_name_lookup[character_name] = character_name
else:
print("CHARACTER IS FOUND", character_name)
print("check on voice...", character_name)
print("about to chekc ovice")
print("is it voice? 0")
print(characters)
character_id = character_name_lookup[character_name]
print("got id", character_id)
print(characters[character_id])
print("ok 3")
#print(characters[character_name])
print("ok 4")
print("is it voice?", characters[character_id].voice)
if not characters[character_id].voice:
print("NO VOICE, GET A RANDOM VOICE")
voice_id = elevenlabs.get_random_voice()
characters[character_id].voice = voice_id
print("is it voice now?", characters[character_id].voice)
print("------")

print("THIS IS THE END!")




# width, height = utils.calculate_target_dimensions(images, MAX_PIXELS)
width, height = 1024, 1024
width, height = 1792, 1024

def run_story_segment(clip, idx):
if clip['voiceover'] == 'character':
character_id = character_name_lookup[clip['character']]
character = characters[character_id]
# character_id = characters[clip['character']]
character = characters.get(character_id)
else:
character = characters[request.narrator_id]
#print("")
output_filename, thumbnail_url = screenplay_clip(
character,
clip['speech'],
Expand Down
Loading

0 comments on commit 0d76128

Please sign in to comment.