Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
!pip install transformers diffusers gtts moviepy safetensors
|
2 |
+
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Continue with your code...
|
6 |
+
|
7 |
+
from diffusers import AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler
|
8 |
+
from diffusers.utils import export_to_gif
|
9 |
+
from huggingface_hub import hf_hub_download
|
10 |
+
from safetensors.torch import load_file
|
11 |
+
from transformers import pipeline
|
12 |
+
from gtts import gTTS
|
13 |
+
from moviepy.editor import *
|
14 |
+
from IPython.display import Video
|
15 |
+
|
16 |
+
# Load the text generation model
|
17 |
+
generator = pipeline('text-generation', model='distilgpt2')
|
18 |
+
|
19 |
+
def generate_text(prompt):
|
20 |
+
response = generator(prompt, max_length=150, num_return_sequences=1)
|
21 |
+
return response[0]['generated_text']
|
22 |
+
|
23 |
+
# Text-to-speech conversion
|
24 |
+
def text_to_speech(text, filename='output_audio.mp3'):
|
25 |
+
tts = gTTS(text)
|
26 |
+
tts.save(filename)
|
27 |
+
return filename
|
28 |
+
|
29 |
+
# Generate animation using AnimateDiffPipeline
|
30 |
+
def create_animation(prompt, output_file='animation.gif'):
|
31 |
+
device = "cuda"
|
32 |
+
dtype = torch.float16
|
33 |
+
step = 4
|
34 |
+
repo = "ByteDance/AnimateDiff-Lightning"
|
35 |
+
ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
|
36 |
+
base = "emilianJR/epiCRealism"
|
37 |
+
|
38 |
+
# Load adapter and pipeline
|
39 |
+
adapter = MotionAdapter().to(device, dtype)
|
40 |
+
adapter.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device))
|
41 |
+
pipe = AnimateDiffPipeline.from_pretrained(base, motion_adapter=adapter, torch_dtype=dtype).to(device)
|
42 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")
|
43 |
+
|
44 |
+
# Generate animation based on prompt
|
45 |
+
output = pipe(prompt=prompt, guidance_scale=1.0, num_inference_steps=step)
|
46 |
+
export_to_gif(output.frames[0], output_file)
|
47 |
+
|
48 |
+
return output_file
|
49 |
+
|
50 |
+
# Combine animation and audio into a video
|
51 |
+
def create_video(animation_file, audio_file, output_file='output_video.mp4'):
|
52 |
+
clip = VideoFileClip(animation_file)
|
53 |
+
audio = AudioFileClip(audio_file)
|
54 |
+
clip = clip.set_audio(audio)
|
55 |
+
clip.write_videofile(output_file, fps=24)
|
56 |
+
|
57 |
+
def generate_educational_video(prompt):
|
58 |
+
# Step 1: Generate text from prompt
|
59 |
+
generated_text = generate_text(prompt)
|
60 |
+
|
61 |
+
# Step 2: Convert text to speech
|
62 |
+
audio_file = text_to_speech(generated_text)
|
63 |
+
|
64 |
+
# Step 3: Create animation based on prompt
|
65 |
+
animation_file = create_animation(prompt)
|
66 |
+
|
67 |
+
# Step 4: Assemble the video
|
68 |
+
create_video(animation_file, audio_file)
|
69 |
+
|
70 |
+
# Step 5: Display the video
|
71 |
+
return Video("output_video.mp4", embed=True)
|
72 |
+
|
73 |
+
# Example usage
|
74 |
+
generate_educational_video("give me a jock?")
|