generated from kyegomez/Python-Package-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kye
committed
Apr 16, 2024
1 parent
053627b
commit 99b15b2
Showing
3 changed files
with
81 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import torch | ||
from diffusers import ( | ||
AnimateDiffPipeline, | ||
EulerDiscreteScheduler, | ||
MotionAdapter, | ||
) | ||
from diffusers.utils import export_to_gif | ||
from dotenv import load_dotenv | ||
from huggingface_hub import hf_hub_download | ||
from safetensors.torch import load_file | ||
|
||
# Load environment variables from .env file | ||
load_dotenv() | ||
|
||
|
||
def text_to_video( | ||
task: str, | ||
model_name: str = "ByteDance/AnimateDiff-Lightning", | ||
guidance_scale: float = 1.0, | ||
inference_steps: int = 4, | ||
output_type: str = ".gif", | ||
output_path: str = "animation.gif", | ||
n: int = 1, | ||
length: int = 60, | ||
*args, | ||
**kwargs, | ||
): | ||
""" | ||
Converts a given text task into an animated video. | ||
Args: | ||
task (str): The text task to be converted into a video. | ||
Returns: | ||
str: The path to the exported GIF file. | ||
""" | ||
|
||
device = "cuda" | ||
dtype = torch.float16 | ||
|
||
repo = model_name | ||
ckpt = f"animatediff_lightning_{inference_steps}step_diffusers.safetensors" | ||
base = "emilianJR/epiCRealism" # Choose to your favorite base model. | ||
adapter = MotionAdapter().to(device, dtype) | ||
adapter.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device)) | ||
|
||
pipe = AnimateDiffPipeline.from_pretrained( | ||
base, motion_adapter=adapter, torch_dtype=dtype | ||
).to(device) | ||
|
||
pipe.scheduler = EulerDiscreteScheduler.from_config( | ||
pipe.scheduler.config, | ||
timestep_spacing="trailing", | ||
beta_schedule="linear", | ||
) | ||
|
||
# outputs = [] | ||
# for i in range(n): | ||
# output = pipe( | ||
# prompt=task, | ||
# guidance_scale=guidance_scale, | ||
# num_inference_steps=inference_steps, | ||
# ) | ||
# outputs.append(output) | ||
# if output_type == ".gif": | ||
# out = export_to_gif([output], f"{output_path}_{i}.gif") | ||
# else: | ||
# out = export_to_video([output], f"{output_path}_{i}.mp4") | ||
output = pipe( | ||
prompt=task, guidance_scale=guidance_scale, num_inference_steps=inference_steps | ||
) | ||
output = export_to_gif(output.frames[0], output_path) | ||
return output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters