paulm0016 commited on
Commit
a69769d
1 Parent(s): 8c253c9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from diffusers import AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler
4
+ from diffusers.utils import export_to_gif
5
+ from huggingface_hub import hf_hub_download
6
+ from safetensors.torch import load_file
7
+
8
+ device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ dtype = torch.float16
10
+
11
+ # 加載模型
12
+ step = 4 # Options: [1, 2, 4, 8]
13
+ repo = "ByteDance/AnimateDiff-Lightning"
14
+ ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
15
+ base = "emilianJR/epiCRealism"
16
+
17
+ adapter = MotionAdapter().to(device, dtype)
18
+ adapter.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device))
19
+ pipe = AnimateDiffPipeline.from_pretrained(base, motion_adapter=adapter, torch_dtype=dtype).to(device)
20
+ pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")
21
+
22
+ # 定義生成 GIF 的函數
23
+ def text_to_gif(prompt):
24
+ output = pipe(prompt, guidance_scale=1.0, num_inference_steps=step)
25
+ gif_path = "animation.gif"
26
+ export_to_gif(output.frames[0], gif_path)
27
+ return gif_path
28
+
29
+ # 設置 Gradio 界面
30
+ with gr.Blocks() as demo:
31
+ gr.Markdown("# Text to GIF Generator using AnimateDiff")
32
+ prompt = gr.Textbox(label="Enter your prompt", placeholder="Describe the animation you want to create")
33
+ gif_output = gr.Image(label="Generated GIF")
34
+ generate_btn = gr.Button("Generate GIF")
35
+
36
+ generate_btn.click(fn=text_to_gif, inputs=prompt, outputs=gif_output)
37
+
38
+ # 啟動 Gradio 應用
39
+ demo.launch()