Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 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
|
| 10 |
+
|
| 11 |
+
# Load the text generation model
|
| 12 |
+
generator = pipeline('text-generation', model='distilgpt2')
|
| 13 |
+
|
| 14 |
+
def generate_text(prompt):
|
| 15 |
+
response = generator(prompt, max_length=150, num_return_sequences=1)
|
| 16 |
+
return response[0]['generated_text']
|
| 17 |
+
|
| 18 |
+
# Text-to-speech conversion
|
| 19 |
+
def text_to_speech(text, filename='output_audio.mp3'):
|
| 20 |
+
tts = gTTS(text)
|
| 21 |
+
tts.save(filename)
|
| 22 |
+
return filename
|
| 23 |
+
|
| 24 |
+
# Generate animation using AnimateDiffPipeline
|
| 25 |
+
def create_animation(prompt, output_file='animation.gif'):
|
| 26 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 27 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 28 |
+
step = 4
|
| 29 |
+
repo = "ByteDance/AnimateDiff-Lightning"
|
| 30 |
+
ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
|
| 31 |
+
base = "emilianJR/epiCRealism"
|
| 32 |
+
|
| 33 |
+
# Load adapter and pipeline
|
| 34 |
+
adapter = MotionAdapter().to(device, dtype)
|
| 35 |
+
adapter.load_state_dict(load_file(hf_hub_download(repo, ckpt), device=device))
|
| 36 |
+
pipe = AnimateDiffPipeline.from_pretrained(base, motion_adapter=adapter, torch_dtype=dtype).to(device)
|
| 37 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")
|
| 38 |
+
|
| 39 |
+
# Generate animation based on prompt
|
| 40 |
+
output = pipe(prompt=prompt, guidance_scale=1.0, num_inference_steps=step)
|
| 41 |
+
export_to_gif(output.frames[0], output_file)
|
| 42 |
+
|
| 43 |
+
return output_file
|
| 44 |
+
|
| 45 |
+
# Combine animation and audio into a video
|
| 46 |
+
def create_video(animation_file, audio_file, output_file='output_video.mp4'):
|
| 47 |
+
clip = VideoFileClip(animation_file)
|
| 48 |
+
audio = AudioFileClip(audio_file)
|
| 49 |
+
clip = clip.set_audio(audio)
|
| 50 |
+
clip.write_videofile(output_file, fps=24)
|
| 51 |
+
|
| 52 |
+
def generate_educational_video(prompt):
|
| 53 |
+
# Step 1: Generate text from prompt
|
| 54 |
+
generated_text = generate_text(prompt)
|
| 55 |
+
|
| 56 |
+
# Step 2: Convert text to speech
|
| 57 |
+
audio_file = text_to_speech(generated_text)
|
| 58 |
+
|
| 59 |
+
# Step 3: Create animation based on prompt
|
| 60 |
+
animation_file = create_animation(prompt)
|
| 61 |
+
|
| 62 |
+
# Step 4: Assemble the video
|
| 63 |
+
create_video(animation_file, audio_file)
|
| 64 |
+
|
| 65 |
+
# Return the path to the video
|
| 66 |
+
return 'output_video.mp4'
|
| 67 |
+
|
| 68 |
+
# Streamlit UI
|
| 69 |
+
st.title("Educational Video Generator")
|
| 70 |
+
|
| 71 |
+
# User input for prompt
|
| 72 |
+
prompt = st.text_input("Enter your prompt here:")
|
| 73 |
+
|
| 74 |
+
if st.button("Generate Video"):
|
| 75 |
+
if prompt:
|
| 76 |
+
st.write("Generating video, please wait...")
|
| 77 |
+
|
| 78 |
+
# Generate the video
|
| 79 |
+
video_path = generate_educational_video(prompt)
|
| 80 |
+
|
| 81 |
+
# Display the video in Streamlit
|
| 82 |
+
st.video(video_path)
|
| 83 |
+
else:
|
| 84 |
+
st.warning("Please enter a prompt to generate the video.")
|