File size: 2,490 Bytes
da46e35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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}")