Spaces:
Running
Running
Create text-2-video
Browse files- text-2-video +66 -0
text-2-video
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from gtts import gTTS
|
4 |
+
import moviepy.editor as mp
|
5 |
+
from PIL import Image, ImageDraw, ImageFont
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Streamlit setup
|
9 |
+
st.title("AI Video Learning Chatbot")
|
10 |
+
|
11 |
+
# User input for the learning topic
|
12 |
+
prompt = st.text_input("Enter a learning topic:")
|
13 |
+
|
14 |
+
if st.button("Generate Video"):
|
15 |
+
try:
|
16 |
+
# Step 1: Generate Text from Prompt using GPT-2
|
17 |
+
st.write("Generating text response...")
|
18 |
+
text_generator = pipeline('text-generation', model='gpt2')
|
19 |
+
generated_text = text_generator(prompt, max_length=50)[0]['generated_text']
|
20 |
+
st.write(f"Generated Text: {generated_text}")
|
21 |
+
|
22 |
+
# Step 2: Convert Generated Text to Speech using gTTS
|
23 |
+
st.write("Converting text to speech...")
|
24 |
+
tts = gTTS(generated_text)
|
25 |
+
tts.save("audio.mp3")
|
26 |
+
|
27 |
+
# Step 3: Create Multiple Frames with Text Animation
|
28 |
+
st.write("Generating animated frames for video...")
|
29 |
+
num_frames = 30 # Number of frames in the video
|
30 |
+
frame_duration = 0.1 # Duration per frame in seconds
|
31 |
+
image_size = (640, 480)
|
32 |
+
frames = []
|
33 |
+
|
34 |
+
for i in range(num_frames):
|
35 |
+
image = Image.new('RGB', image_size, color='black')
|
36 |
+
draw = ImageDraw.Draw(image)
|
37 |
+
font = ImageFont.load_default()
|
38 |
+
|
39 |
+
# Animate text position
|
40 |
+
text = generated_text
|
41 |
+
text_position = (0, 20 + i * 10 % 400) # Move text down over frames
|
42 |
+
draw.text(text_position, text, fill="white", font=font)
|
43 |
+
frame_path = f"frame_{i:03d}.png"
|
44 |
+
image.save(frame_path)
|
45 |
+
frames.append(mp.ImageClip(frame_path).set_duration(frame_duration))
|
46 |
+
|
47 |
+
# Combine frames into a video
|
48 |
+
video_clip = mp.concatenate_videoclips(frames, method="compose")
|
49 |
+
audio_clip = mp.AudioFileClip("audio.mp3")
|
50 |
+
video_clip = video_clip.set_audio(audio_clip)
|
51 |
+
|
52 |
+
# Save the video with fps set to 24
|
53 |
+
video_clip.write_videofile("output.mp4", codec="libx264", fps=24)
|
54 |
+
|
55 |
+
# Display the video
|
56 |
+
st.write("Video generated successfully!")
|
57 |
+
st.video("output.mp4")
|
58 |
+
|
59 |
+
# Clean up temporary files
|
60 |
+
for frame_path in [f"frame_{i:03d}.png" for i in range(num_frames)]:
|
61 |
+
os.remove(frame_path)
|
62 |
+
os.remove("audio.mp3")
|
63 |
+
os.remove("output.mp4")
|
64 |
+
|
65 |
+
except Exception as e:
|
66 |
+
st.error(f"An error occurred: {e}")
|