Skip to content

Commit

Permalink
precommit
Browse files Browse the repository at this point in the history
  • Loading branch information
jmilldotdev committed Mar 25, 2024
1 parent d97d4cb commit adb1550
Show file tree
Hide file tree
Showing 83 changed files with 1,365 additions and 969 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pipeline-stg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ jobs:
push: true
tags:
${{ steps.prep.outputs.tagged_image }},${{ steps.prep.outputs.image
}}:latest
}}:latest
2 changes: 1 addition & 1 deletion .github/workflows/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ jobs:
push: true
tags:
${{ steps.prep.outputs.tagged_image }},${{ steps.prep.outputs.image
}}:latest
}}:latest
27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-docstring-first
- id: check-yaml
- id: debug-statements
# - repo: https://github.com/PyCQA/flake8
# rev: 3.9.2
# hooks:
# - id: flake8
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
# - repo: https://github.com/asottile/reorder_python_imports
# rev: v2.5.0
# hooks:
# - id: reorder-python-imports
# args: [--py39-plus]
- repo: https://github.com/asottile/add-trailing-comma
rev: v2.1.0
hooks:
- id: add-trailing-comma
args: [--py36-plus]
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# logos-svc

Install

```
rye sync
rye run pre-commit install
```

Run

`rye run uvicorn app.server:app --reload`
Expand All @@ -24,4 +31,3 @@ Send a command to the server
Tests

`rye run pytest -s tests`

3 changes: 2 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from dotenv import load_dotenv
load_dotenv()

load_dotenv()
2 changes: 1 addition & 1 deletion app/animations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from .story import animated_story
from .reel import animated_reel
from .comic import illustrated_comic
from .little_martians import little_martian_poster
from .little_martians import little_martian_poster
91 changes: 54 additions & 37 deletions app/animations/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def select_random_voice(character: Character = None):
prompt=prompt,
model="gpt-3.5-turbo",
params={"temperature": 0.0, "max_tokens": 10},
output_schema=gender_schema
output_schema=gender_schema,
)

try:
Expand All @@ -35,22 +35,19 @@ def select_random_voice(character: Character = None):

def talking_head(
character: Character,
text: str,
text: str,
width: Optional[int] = None,
height: Optional[int] = None,
gfpgan: bool = False,
gfpgan_upscale: int = 1
gfpgan_upscale: int = 1,
) -> str:
print("* talking head: {character.name} says {text}")
if character.voice:
voice_id = character.voice
else:
voice_id = select_random_voice(character)

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

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

audio_url = s3.upload(audio_bytes, "mp3")

Expand All @@ -64,7 +61,7 @@ def talking_head(
width=width,
height=height,
)

print(f"output: {output_url}")

return output_url, thumbnail_url
Expand All @@ -75,7 +72,7 @@ def screenplay_clip(
speech: str,
image_text: str,
width: Optional[int] = None,
height: Optional[int] = None
height: Optional[int] = None,
) -> str:
if not character:
voice_id = select_random_voice()
Expand All @@ -84,10 +81,7 @@ def screenplay_clip(
voice_id = character.voice
else:
voice_id = select_random_voice(character)
audio_bytes = elevenlabs.tts(
speech,
voice=voice_id
)
audio_bytes = elevenlabs.tts(speech, voice=voice_id)
audio_url = s3.upload(audio_bytes, "mp3")
video_url, thumbnail_url = replicate.txt2vid(
interpolation_texts=[image_text],
Expand All @@ -106,20 +100,20 @@ def comic_strip(
caption_padding_top: int = 10,
line_spacing: int = 1.3,
font_size: int = 48,
font_ttf: str = 'Raleway-Light.ttf'
font_ttf: str = "Raleway-Light.ttf",
):
font = get_font(font_ttf, font_size)
num_panels = len(images)
caption_box_height = 3 * int(1.5 * font.size)

width, height = 1024, 1024 #images[0].size
width, height = 1024, 1024 # images[0].size
total_width = width * 2 + margin
total_height = height * 2 + caption_box_height * 2 + margin

composite_image = Image.new('RGB', (total_width, total_height), color='white')
composite_image = Image.new("RGB", (total_width, total_height), color="white")

draw = ImageDraw.Draw(composite_image)
draw.rectangle([(0, 0), (total_width, total_height)], fill='black')
draw.rectangle([(0, 0), (total_width, total_height)], fill="black")

caption_box_height = 3 * int(1.5 * font.size) + 2 * caption_padding_top

Expand All @@ -133,32 +127,48 @@ def comic_strip(
else:
if num_panels == 3:
x = width + margin
y = ((i - 1) * (height + caption_box_height + margin)) if i == 1 else ((i - 1) * (height + caption_box_height))
y = (
((i - 1) * (height + caption_box_height + margin))
if i == 1
else ((i - 1) * (height + caption_box_height))
)
new_height = height
new_width = width
else:
x = (i % 2) * (width + margin) if i % 2 == 0 else (i % 2) * width + margin
y = (i // 2) * (height + caption_box_height) if i // 2 == 0 else (i // 2) * (height + caption_box_height + margin)
x = (
(i % 2) * (width + margin)
if i % 2 == 0
else (i % 2) * width + margin
)
y = (
(i // 2) * (height + caption_box_height)
if i // 2 == 0
else (i // 2) * (height + caption_box_height + margin)
)
new_height = height
new_width = width

resized_image = image.resize((new_width, new_height))

composite_image.paste(resized_image, (x, y))

caption_box = Image.new('RGB', (new_width, caption_box_height), color='black')
caption_box = Image.new("RGB", (new_width, caption_box_height), color="black")
draw = ImageDraw.Draw(caption_box)

wrapped_caption = wrap_text(draw, captions[i], font, new_width - 2 * padding)
caption_y = caption_padding_top
for line in wrapped_caption:
draw.text((padding, caption_y), line, fill='white', font=font)
draw.text((padding, caption_y), line, fill="white", font=font)
caption_y += int(line_spacing * font.size)

composite_image.paste(caption_box, (x, y + new_height))

if (num_panels == 4 and i == 0) or (num_panels == 3 and i == 1):
thumbnail = Image.new('RGB', (new_width, new_height + caption_box_height), color='white')
thumbnail = Image.new(
"RGB",
(new_width, new_height + caption_box_height),
color="white",
)
thumbnail.paste(resized_image, (0, 0))
thumbnail.paste(caption_box, (0, new_height))

Expand All @@ -172,46 +182,53 @@ def poster(
caption_padding_top: int = 10,
line_spacing: int = 1.3,
font_size: int = 36,
font_ttf: str = 'Raleway-Light.ttf',
font_ttf: str = "Raleway-Light.ttf",
shadow_offset: tuple = (1, 1.4),
font_color: str = '#e7e7e7',
shadow_color: str = '#d3d3d3'
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), (0, 0, 0)))
caption = caption.replace('\n', ' ')
draw = ImageDraw.Draw(Image.new("RGB", (width, height), (0, 0, 0)))
caption = caption.replace("\n", " ")
wrapped_caption = wrap_text(draw, caption, font, width - 2 * margin)
num_lines = len(wrapped_caption)

caption_box_height = num_lines * int(line_spacing * font.size) + 2 * caption_padding_top
caption_box_height = (
num_lines * int(line_spacing * font.size) + 2 * caption_padding_top
)

total_width = width + margin
total_height = height + caption_box_height + margin

composite_image = Image.new('RGB', (total_width, total_height), color='white')
composite_image = Image.new("RGB", (total_width, total_height), color="white")

draw = ImageDraw.Draw(composite_image)
draw.rectangle([(0, 0), (total_width, total_height)], fill='black')
draw.rectangle([(0, 0), (total_width, total_height)], fill="black")

resized_image = image.resize((width, height))
composite_image.paste(resized_image, (int(margin/2), int(margin/2)))
composite_image.paste(resized_image, (int(margin / 2), int(margin / 2)))

caption_box = Image.new('RGB', (total_width, caption_box_height), color='black')
caption_box = Image.new("RGB", (total_width, caption_box_height), color="black")
draw = ImageDraw.Draw(caption_box)

caption_y = caption_padding_top
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 + 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)
caption_y += int(line_spacing * font.size)

composite_image.paste(caption_box, (0, height))

thumbnail = Image.new('RGB', (width, height + caption_box_height), color='white')
thumbnail = Image.new("RGB", (width, height + caption_box_height), color="white")

thumbnail.paste(resized_image, (0, 0))
thumbnail.paste(caption_box, (0, height))

return composite_image, thumbnail
return composite_image, thumbnail
34 changes: 16 additions & 18 deletions app/animations/comic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,39 @@
def illustrated_comic(request: ComicRequest):
params = {"temperature": 1.0, "max_tokens": 2000, **request.params}
loras = {
"Verdelis": "https://edenartlab-prod-data.s3.us-east-1.amazonaws.com/f290723c93715a8eb14e589ca1eec211e10691f683d53cde37139bc7d3a91c22.tar"
"Verdelis": "https://edenartlab-prod-data.s3.us-east-1.amazonaws.com/f290723c93715a8eb14e589ca1eec211e10691f683d53cde37139bc7d3a91c22.tar",
}

comicwriter = LLM(
model=request.model,
system_message=comicwriter_system_template.template,
params=params,
)

comic_book = comicwriter(request.prompt, output_schema=ComicResult)

def run_panel(panel, idx):
# pick lora of character
# pick init image character x genre

return replicate.sdxl({
"text_input": panel['image'],
"lora": loras["Verdelis"],
"width": 512 if idx == 0 else 1024,
"height": 1024,
"n_samples": 1,
})

results = utils.process_in_parallel(
comic_book['panels'],
run_panel,
max_workers=4
)
return replicate.sdxl(
{
"text_input": panel["image"],
"lora": loras["Verdelis"],
"width": 512 if idx == 0 else 1024,
"height": 1024,
"n_samples": 1,
},
)

results = utils.process_in_parallel(comic_book["panels"], run_panel, max_workers=4)

image_urls = [image_url for image_url, thumbnail in results]
images = [utils.download_image(url) for url in image_urls]
captions = [panel['caption'] for panel in comic_book['panels']]
captions = [panel["caption"] for panel in comic_book["panels"]]

composite_image, thumbnail_image = comic_strip(images, captions)

img_bytes = utils.PIL_to_bytes(composite_image, ext="JPEG")
thumbnail_bytes = utils.PIL_to_bytes(thumbnail_image, ext="WEBP")

Expand Down
Loading

0 comments on commit adb1550

Please sign in to comment.