Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,13 @@
|
|
1 |
-
|
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 |
-
|
15 |
|
16 |
# Load the text generation model
|
17 |
generator = pipeline('text-generation', model='distilgpt2')
|
@@ -20,55 +16,54 @@ 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 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
|
74 |
-
generate_educational_video("give me a jock?")
|
|
|
1 |
+
import gradio as gr
|
|
|
2 |
import torch
|
3 |
+
from transformers import pipeline
|
|
|
|
|
4 |
from diffusers import AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler
|
5 |
from diffusers.utils import export_to_gif
|
6 |
from huggingface_hub import hf_hub_download
|
7 |
from safetensors.torch import load_file
|
|
|
8 |
from gtts import gTTS
|
9 |
+
from moviepy.editor import VideoFileClip, AudioFileClip, concatenate_videoclips
|
10 |
+
import os
|
11 |
|
12 |
# Load the text generation model
|
13 |
generator = pipeline('text-generation', model='distilgpt2')
|
|
|
16 |
response = generator(prompt, max_length=150, num_return_sequences=1)
|
17 |
return response[0]['generated_text']
|
18 |
|
|
|
19 |
def text_to_speech(text, filename='output_audio.mp3'):
|
20 |
tts = gTTS(text)
|
21 |
tts.save(filename)
|
22 |
return filename
|
23 |
|
|
|
24 |
def create_animation(prompt, output_file='animation.gif'):
|
25 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
26 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
27 |
step = 4
|
28 |
repo = "ByteDance/AnimateDiff-Lightning"
|
29 |
ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
|
30 |
base = "emilianJR/epiCRealism"
|
31 |
|
|
|
32 |
adapter = MotionAdapter().to(device, dtype)
|
33 |
adapter.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device))
|
34 |
pipe = AnimateDiffPipeline.from_pretrained(base, motion_adapter=adapter, torch_dtype=dtype).to(device)
|
35 |
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")
|
36 |
|
|
|
37 |
output = pipe(prompt=prompt, guidance_scale=1.0, num_inference_steps=step)
|
38 |
export_to_gif(output.frames[0], output_file)
|
39 |
|
40 |
return output_file
|
41 |
|
|
|
42 |
def create_video(animation_file, audio_file, output_file='output_video.mp4'):
|
43 |
clip = VideoFileClip(animation_file)
|
44 |
audio = AudioFileClip(audio_file)
|
45 |
clip = clip.set_audio(audio)
|
46 |
clip.write_videofile(output_file, fps=24)
|
47 |
+
return output_file
|
48 |
|
49 |
def generate_educational_video(prompt):
|
|
|
50 |
generated_text = generate_text(prompt)
|
|
|
|
|
51 |
audio_file = text_to_speech(generated_text)
|
|
|
|
|
52 |
animation_file = create_animation(prompt)
|
53 |
+
video_file = create_video(animation_file, audio_file)
|
54 |
+
return video_file
|
55 |
+
|
56 |
+
# Define Gradio Interface
|
57 |
+
def gradio_interface(prompt):
|
58 |
+
video_path = generate_educational_video(prompt)
|
59 |
+
return video_path
|
60 |
+
|
61 |
+
interface = gr.Interface(
|
62 |
+
fn=gradio_interface,
|
63 |
+
inputs="text",
|
64 |
+
outputs="video",
|
65 |
+
title="Educational Video Generator",
|
66 |
+
description="Enter a prompt to generate a video."
|
67 |
+
)
|
68 |
|
69 |
+
interface.launch()
|
|