File size: 2,609 Bytes
5ab1003
c1f4ca4
8e900c3
 
f222f79
c1f4ca4
 
 
 
 
5ab1003
f222f79
c1f4ca4
 
 
 
 
 
 
 
 
 
 
 
 
 
f222f79
 
c1f4ca4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f222f79
c1f4ca4
 
 
 
 
f222f79
 
 
5ab1003
 
 
 
 
 
 
 
 
7d746a2
 
 
 
5ab1003
 
f21d34b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import streamlit as st
import torch
print("Torch version:", torch.__version__)

from transformers import pipeline
from diffusers import AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler
from diffusers.utils import export_to_gif
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
from gtts import gTTS
from moviepy.editor import VideoFileClip, AudioFileClip
import os

# Load the text generation model
generator = pipeline('text-generation', model='distilgpt2')

def generate_text(prompt):
    response = generator(prompt, max_length=150, num_return_sequences=1)
    return response[0]['generated_text']

def text_to_speech(text, filename='output_audio.mp3'):
    tts = gTTS(text)
    tts.save(filename)
    return filename

def create_animation(prompt, output_file='animation.gif'):
    device = "cuda" if torch.cuda.is_available() else "cpu"
    dtype = torch.float16 if device == "cuda" else torch.float32
    step = 4
    repo = "ByteDance/AnimateDiff-Lightning"
    ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
    base = "emilianJR/epiCRealism"

    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")

    output = pipe(prompt=prompt, guidance_scale=1.0, num_inference_steps=step)
    export_to_gif(output.frames[0], output_file)

    return output_file

def create_video(animation_file, audio_file, output_file='output_video.mp4'):
    clip = VideoFileClip(animation_file)
    audio = AudioFileClip(audio_file)
    clip = clip.set_audio(audio)
    clip.write_videofile(output_file, fps=24)
    return output_file

def generate_educational_video(prompt):
    generated_text = generate_text(prompt)
    audio_file = text_to_speech(generated_text)
    animation_file = create_animation(prompt)
    video_file = create_video(animation_file, audio_file)
    return video_file

# Streamlit UI
st.title("Educational Video Generator")

prompt = st.text_input("Enter your prompt here:")

if st.button("Generate Video"):
    if prompt:
        st.write("Generating video, please wait...")
        video_path = generate_educational_video(prompt)
        if os.path.exists(video_path):
            st.video(video_path)
        else:
            st.write("Video generation failed.")
    else:
        st.warning("Please enter a prompt.")