Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,14 @@ import random
|
|
| 4 |
import sys
|
| 5 |
import torch
|
| 6 |
from PIL import Image, ImageDraw, ImageFont
|
| 7 |
-
from diffusers import DiffusionPipeline,
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
from gtts import gTTS
|
| 10 |
-
from moviepy.editor import ImageSequenceClip, concatenate_videoclips, AudioFileClip
|
| 11 |
import gradio as gr
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
num_inference_steps =
|
| 15 |
|
| 16 |
base_model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 17 |
repo_name = "ByteDance/Hyper-SD"
|
|
@@ -22,16 +22,18 @@ device = "cpu"
|
|
| 22 |
pipe = DiffusionPipeline.from_pretrained(base_model_id).to(device)
|
| 23 |
pipe.load_lora_weights(hf_hub_download(repo_name, ckpt_name))
|
| 24 |
pipe.fuse_lora()
|
| 25 |
-
pipe.scheduler =
|
| 26 |
|
| 27 |
-
def generate_image(prompt, step_count=
|
| 28 |
if seed is None:
|
| 29 |
seed = random.randint(0, sys.maxsize)
|
| 30 |
generator = torch.Generator(device).manual_seed(seed)
|
|
|
|
| 31 |
images = pipe(
|
| 32 |
prompt=prompt,
|
| 33 |
num_inference_steps=step_count,
|
| 34 |
-
guidance_scale=
|
|
|
|
| 35 |
generator=generator,
|
| 36 |
).images
|
| 37 |
return images[0]
|
|
@@ -86,7 +88,7 @@ def process_story(story):
|
|
| 86 |
for i, sentence in enumerate(sentences):
|
| 87 |
print(f"Sentence {i+1}: {sentence}\n")
|
| 88 |
seed = random.randint(0, sys.maxsize)
|
| 89 |
-
image = generate_image(sentence, step_count=
|
| 90 |
resized_image = image.resize((256, 256))
|
| 91 |
image_with_text = draw_text_on_image(resized_image, sentence)
|
| 92 |
|
|
@@ -113,6 +115,9 @@ def process_story(story):
|
|
| 113 |
|
| 114 |
video_clips.append(video_clip)
|
| 115 |
|
|
|
|
|
|
|
|
|
|
| 116 |
# Concatenate all video clips into a final video
|
| 117 |
final_video = concatenate_videoclips(video_clips)
|
| 118 |
final_video_path = "story_video.mp4"
|
|
|
|
| 4 |
import sys
|
| 5 |
import torch
|
| 6 |
from PIL import Image, ImageDraw, ImageFont
|
| 7 |
+
from diffusers import DiffusionPipeline, TCDScheduler
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
from gtts import gTTS
|
| 10 |
+
from moviepy.editor import ImageSequenceClip, VideoFileClip, concatenate_videoclips, AudioFileClip
|
| 11 |
import gradio as gr
|
| 12 |
|
| 13 |
+
# Choose among 1, 2, 4 and 8:
|
| 14 |
+
num_inference_steps = 8
|
| 15 |
|
| 16 |
base_model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 17 |
repo_name = "ByteDance/Hyper-SD"
|
|
|
|
| 22 |
pipe = DiffusionPipeline.from_pretrained(base_model_id).to(device)
|
| 23 |
pipe.load_lora_weights(hf_hub_download(repo_name, ckpt_name))
|
| 24 |
pipe.fuse_lora()
|
| 25 |
+
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
| 26 |
|
| 27 |
+
def generate_image(prompt, step_count=50, seed=None):
|
| 28 |
if seed is None:
|
| 29 |
seed = random.randint(0, sys.maxsize)
|
| 30 |
generator = torch.Generator(device).manual_seed(seed)
|
| 31 |
+
eta = 0.5
|
| 32 |
images = pipe(
|
| 33 |
prompt=prompt,
|
| 34 |
num_inference_steps=step_count,
|
| 35 |
+
guidance_scale=0.0,
|
| 36 |
+
eta=eta,
|
| 37 |
generator=generator,
|
| 38 |
).images
|
| 39 |
return images[0]
|
|
|
|
| 88 |
for i, sentence in enumerate(sentences):
|
| 89 |
print(f"Sentence {i+1}: {sentence}\n")
|
| 90 |
seed = random.randint(0, sys.maxsize)
|
| 91 |
+
image = generate_image(sentence, step_count=50, seed=seed) # Increase step count for better quality images
|
| 92 |
resized_image = image.resize((256, 256))
|
| 93 |
image_with_text = draw_text_on_image(resized_image, sentence)
|
| 94 |
|
|
|
|
| 115 |
|
| 116 |
video_clips.append(video_clip)
|
| 117 |
|
| 118 |
+
# Clear memory
|
| 119 |
+
del resized_image, image_with_text
|
| 120 |
+
|
| 121 |
# Concatenate all video clips into a final video
|
| 122 |
final_video = concatenate_videoclips(video_clips)
|
| 123 |
final_video_path = "story_video.mp4"
|