patrickluan11 commited on
Commit
e2dfcd3
·
verified ·
1 Parent(s): 715ef3c

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -119
app.py DELETED
@@ -1,119 +0,0 @@
1
- import tempfile
2
- from pathlib import Path
3
-
4
- import gradio as gr
5
- import spaces
6
- import torch
7
- from diffusers import CogVideoXImageToVideoPipeline
8
- from diffusers.utils import export_to_video
9
- from PIL import Image
10
-
11
- MODEL_ID = "THUDM/CogVideoX1.5-5B-I2V"
12
- PIPE = None
13
-
14
-
15
- def get_pipeline():
16
- global PIPE
17
- if PIPE is None:
18
- print(f"Loading model: {MODEL_ID}", flush=True)
19
- PIPE = CogVideoXImageToVideoPipeline.from_pretrained(
20
- MODEL_ID,
21
- torch_dtype=torch.bfloat16,
22
- )
23
- PIPE.to("cuda")
24
- PIPE.enable_vae_slicing()
25
- PIPE.enable_vae_tiling()
26
- print("Model loaded on CUDA", flush=True)
27
- return PIPE
28
-
29
-
30
- def resize_image(image: Image.Image, profile: str):
31
- image = image.convert("RGB")
32
- if profile == "1080p (upscale final)":
33
- target_w, target_h = 1360, 768
34
- else:
35
- target_w, target_h = 1024, 576
36
-
37
- target_ratio = target_w / target_h
38
- source_ratio = image.width / image.height
39
- if source_ratio > target_ratio:
40
- crop_h = image.height
41
- crop_w = round(crop_h * target_ratio)
42
- else:
43
- crop_w = image.width
44
- crop_h = round(crop_w / target_ratio)
45
-
46
- left = max(0, (image.width - crop_w) // 2)
47
- top = max(0, (image.height - crop_h) // 2)
48
- image = image.crop((left, top, left + crop_w, top + crop_h))
49
- return image.resize((target_w, target_h), Image.Resampling.LANCZOS), target_w, target_h
50
-
51
-
52
- @spaces.GPU(duration=300)
53
- def generate_video(image, prompt, profile, seed):
54
- if image is None:
55
- raise gr.Error("Envie uma imagem antes de gerar o vídeo.")
56
- if not prompt or not prompt.strip():
57
- raise gr.Error("Escreva o movimento que você quer animar.")
58
-
59
- try:
60
- pipeline = get_pipeline()
61
- image, width, height = resize_image(image, profile)
62
- generator = torch.Generator(device="cuda").manual_seed(int(seed or 42))
63
- full_prompt = (
64
- "Animate the input image while preserving the exact identity, face, clothing, "
65
- "objects, composition and environment. Focus on natural physically plausible motion. "
66
- + prompt.strip()
67
- + ". Smooth cinematic movement, consistent character, no morphing, no extra limbs, "
68
- "no scene change, no camera cuts."
69
- )
70
-
71
- result = pipeline(
72
- prompt=full_prompt,
73
- image=image,
74
- height=height,
75
- width=width,
76
- num_frames=49,
77
- guidance_scale=6.0,
78
- num_inference_steps=20,
79
- generator=generator,
80
- )
81
- frames = result.frames[0]
82
- output_path = Path(tempfile.mkdtemp(prefix="motion-ai-")) / "motion.mp4"
83
- export_to_video(frames, str(output_path), fps=8)
84
- return str(output_path)
85
- except Exception as exc:
86
- print(f"Generation error: {type(exc).__name__}: {exc}", flush=True)
87
- raise gr.Error(f"Não foi possível gerar o vídeo: {type(exc).__name__}: {exc}") from exc
88
-
89
-
90
- with gr.Blocks(title="Motion AI Studio") as demo:
91
- gr.Markdown(
92
- "# Motion AI Studio\n"
93
- "Anime uma imagem com um comando de movimento. A GPU é solicitada somente ao gerar."
94
- )
95
- with gr.Row():
96
- with gr.Column():
97
- image = gr.Image(type="pil", label="Imagem de referência")
98
- prompt = gr.Textbox(
99
- label="Descreva o movimento",
100
- placeholder="Ex.: o homem começa a correr pela rua enquanto a câmera o acompanha lateralmente",
101
- lines=4,
102
- )
103
- profile = gr.Radio(
104
- ["720p", "1080p (upscale final)"],
105
- value="720p",
106
- label="Perfil",
107
- )
108
- seed = gr.Number(value=42, precision=0, label="Seed")
109
- generate = gr.Button("Gerar animação", variant="primary")
110
- with gr.Column():
111
- result = gr.Video(label="Resultado", format="mp4")
112
- gr.Markdown(
113
- "**Recomendação:** comece com 720p. O perfil 1080p gera a animação em resolução menor."
114
- )
115
-
116
- generate.click(generate_video, [image, prompt, profile, seed], result)
117
-
118
- if __name__ == "__main__":
119
- demo.queue(max_size=8).launch()