File size: 2,498 Bytes
5cbaf6a
 
 
 
003400c
5cbaf6a
 
 
 
 
 
 
 
 
 
 
 
 
 
003400c
 
 
 
5cbaf6a
 
543edfd
359fd1e
1454c3b
543edfd
1454c3b
5cbaf6a
 
359fd1e
5cbaf6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1454c3b
5cbaf6a
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
import gradio as gr
from transformers import GPT2LMHeadModel, GPT2Tokenizer
from diffusers import StableDiffusionPipeline
from moviepy.editor import ImageSequenceClip, AudioFileClip, concatenate_videoclips
from gtts import gTTS
import os

# Function to generate a script from a text prompt
def generate_script(prompt):
    model_name = "gpt2"
    tokenizer = GPT2Tokenizer.from_pretrained(model_name)
    model = GPT2LMHeadModel.from_pretrained(model_name)

    inputs = tokenizer.encode(prompt, return_tensors="pt")
    outputs = model.generate(inputs, max_length=500, num_return_sequences=1)

    script = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return script

# Function to convert text to speech using gTTS
def text_to_speech(text, output_path="output.mp3"):
    tts = gTTS(text=text, lang='en')
    tts.save(output_path)
    return output_path

# Function to generate images from a text prompt using Stable Diffusion (CPU)
def generate_images_from_prompt(prompt, num_images=3, num_inference_steps=20):
    model_id = "runwayml/stable-diffusion-v1-5"
    pipe = StableDiffusionPipeline.from_pretrained(model_id)
    pipe = pipe.to("cpu")  # Ensure we are using CPU
    images = []
    for i in range(num_images):
        image = pipe(prompt, num_inference_steps=num_inference_steps).images[0]
        image_path = f"image_{i}.png"
        image.save(image_path)
        images.append(image_path)
    return images

# Function to create a video from images and synthesized speech
def create_video(images, audio_path, output_video_path="output.mp4"):
    # Load the images
    image_clips = [ImageSequenceClip(images, fps=1)]
    
    # Concatenate the image clips
    video = concatenate_videoclips(image_clips)
    
    # Load the audio clip
    audio_clip = AudioFileClip(audio_path)

    # Set the audio to the video
    video = video.set_audio(audio_clip)

    # Export the video
    video.write_videofile(output_video_path, fps=24)

# Main function to generate video from prompt
def generate_video_from_prompt(prompt):
    script = generate_script(prompt)
    audio_path = text_to_speech(script)
    images = generate_images_from_prompt(prompt)
    create_video(images, audio_path)
    return "output.mp4"

# Gradio interface
iface = gr.Interface(
    fn=generate_video_from_prompt,
    inputs="text",
    outputs="video",
    title="Text to Video Generator",
    description="Enter a prompt to generate a high-definition video."
)

# Launch the interface
iface.launch()