Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image, ImageDraw, ImageFont
|
4 |
+
import moviepy.editor as mpe
|
5 |
+
import tempfile
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Load TTS model
|
9 |
+
tts_model = pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech")
|
10 |
+
|
11 |
+
def generate_audio(text, output_path):
|
12 |
+
tts = tts_model(text)
|
13 |
+
with open(output_path, "wb") as f:
|
14 |
+
f.write(tts[0]["waveform"])
|
15 |
+
|
16 |
+
def generate_images(text, output_folder, num_images=5):
|
17 |
+
if not os.path.exists(output_folder):
|
18 |
+
os.makedirs(output_folder)
|
19 |
+
|
20 |
+
words = text.split()
|
21 |
+
chunk_size = max(1, len(words) // num_images)
|
22 |
+
chunks = [words[i:i + chunk_size] for i in range(0, len(words), chunk_size)]
|
23 |
+
|
24 |
+
for i, chunk in enumerate(chunks):
|
25 |
+
image = Image.new("RGB", (1280, 720), color=(255, 255, 255))
|
26 |
+
draw = ImageDraw.Draw(image)
|
27 |
+
font = ImageFont.load_default()
|
28 |
+
text = " ".join(chunk)
|
29 |
+
draw.text((10, 360), text, fill=(0, 0, 0), font=font)
|
30 |
+
image.save(os.path.join(output_folder, f"frame_{i:03d}.png"))
|
31 |
+
|
32 |
+
def create_video(image_folder, audio_path, output_path):
|
33 |
+
image_files = [os.path.join(image_folder, img) for img in sorted(os.listdir(image_folder)) if img.endswith(".png")]
|
34 |
+
clips = [mpe.ImageClip(img).set_duration(2) for img in image_files]
|
35 |
+
video = mpe.concatenate_videoclips(clips, method="compose")
|
36 |
+
audio = mpe.AudioFileClip(audio_path)
|
37 |
+
video = video.set_audio(audio)
|
38 |
+
video.write_videofile(output_path, fps=24)
|
39 |
+
|
40 |
+
def main():
|
41 |
+
st.title("Text to Video Generator")
|
42 |
+
|
43 |
+
text_input = st.text_area("Enter the text to generate video:", "")
|
44 |
+
|
45 |
+
if st.button("Generate Video"):
|
46 |
+
if text_input:
|
47 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
48 |
+
audio_path = os.path.join(temp_dir, "audio.wav")
|
49 |
+
image_folder = os.path.join(temp_dir, "images")
|
50 |
+
video_path = os.path.join(temp_dir, "video.mp4")
|
51 |
+
|
52 |
+
generate_audio(text_input, audio_path)
|
53 |
+
generate_images(text_input, image_folder)
|
54 |
+
create_video(image_folder, audio_path, video_path)
|
55 |
+
|
56 |
+
with open(video_path, "rb") as video_file:
|
57 |
+
st.video(video_file.read(), format="video/mp4")
|
58 |
+
else:
|
59 |
+
st.error("Please enter some text.")
|
60 |
+
|
61 |
+
if __name__ == "__main__":
|
62 |
+
main()
|