Spaces:
Running
Running
import streamlit as st | |
from transformers import pipeline | |
from gtts import gTTS | |
import moviepy.editor as mp | |
from PIL import Image, ImageDraw, ImageFont | |
import os | |
# Streamlit setup | |
st.title("AI Video Learning Chatbot") | |
# User input for the learning topic | |
prompt = st.text_input("Enter a learning topic:") | |
if st.button("Generate Video"): | |
try: | |
# Step 1: Generate Text from Prompt using GPT-2 | |
st.write("Generating text response...") | |
text_generator = pipeline('text-generation', model='gpt2') | |
generated_text = text_generator(prompt, max_length=50)[0]['generated_text'] | |
st.write(f"Generated Text: {generated_text}") | |
# Step 2: Convert Generated Text to Speech using gTTS | |
st.write("Converting text to speech...") | |
tts = gTTS(generated_text) | |
tts.save("audio.mp3") | |
# Step 3: Create Multiple Frames with Text Animation | |
st.write("Generating animated frames for video...") | |
num_frames = 30 # Number of frames in the video | |
frame_duration = 0.1 # Duration per frame in seconds | |
image_size = (640, 480) | |
frames = [] | |
for i in range(num_frames): | |
image = Image.new('RGB', image_size, color='black') | |
draw = ImageDraw.Draw(image) | |
font = ImageFont.load_default() | |
# Animate text position | |
text = generated_text | |
text_position = (0, 20 + i * 10 % 400) # Move text down over frames | |
draw.text(text_position, text, fill="white", font=font) | |
frame_path = f"frame_{i:03d}.png" | |
image.save(frame_path) | |
frames.append(mp.ImageClip(frame_path).set_duration(frame_duration)) | |
# Combine frames into a video | |
video_clip = mp.concatenate_videoclips(frames, method="compose") | |
audio_clip = mp.AudioFileClip("audio.mp3") | |
video_clip = video_clip.set_audio(audio_clip) | |
# Save the video with fps set to 24 | |
video_clip.write_videofile("output.mp4", codec="libx264", fps=24) | |
# Display the video | |
st.write("Video generated successfully!") | |
st.video("output.mp4") | |
# Clean up temporary files | |
for frame_path in [f"frame_{i:03d}.png" for i in range(num_frames)]: | |
os.remove(frame_path) | |
os.remove("audio.mp3") | |
os.remove("output.mp4") | |
except Exception as e: | |
st.error(f"An error occurred: {e}") | |