shaheerawan3 commited on
Commit
5140a89
Β·
verified Β·
1 Parent(s): 7601061

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +181 -0
app.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from pathlib import Path
3
+ import torch
4
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
5
+ from diffusers import StableDiffusionPipeline
6
+ from TTS.api import TTS
7
+ import cv2
8
+ import numpy as np
9
+ from PIL import Image
10
+ import tempfile
11
+ import os
12
+ from moviepy.editor import *
13
+ import base64
14
+
15
+ class VideoGenerator:
16
+ def __init__(self):
17
+ # Initialize text generation model
18
+ self.text_model = AutoModelForCausalLM.from_pretrained(
19
+ "facebook/opt-1.3b",
20
+ torch_dtype=torch.float16,
21
+ device_map="auto"
22
+ )
23
+ self.text_tokenizer = AutoTokenizer.from_pretrained("facebook/opt-1.3b")
24
+
25
+ # Initialize image generation model
26
+ self.image_generator = StableDiffusionPipeline.from_pretrained(
27
+ "runwayml/stable-diffusion-v1-5",
28
+ torch_dtype=torch.float16
29
+ ).to("cuda")
30
+
31
+ # Initialize TTS model
32
+ self.tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False)
33
+
34
+ # Create temp directory
35
+ self.temp_dir = Path(tempfile.mkdtemp())
36
+
37
+ def generate_script(self, prompt):
38
+ """Generate detailed script with facts and scenes"""
39
+ input_ids = self.text_tokenizer(
40
+ f"Generate a detailed video script with facts about: {prompt}. Include scene descriptions.",
41
+ return_tensors="pt"
42
+ ).input_ids.to("cuda")
43
+
44
+ outputs = self.text_model.generate(
45
+ input_ids,
46
+ max_length=500,
47
+ temperature=0.7,
48
+ num_return_sequences=1
49
+ )
50
+
51
+ script = self.text_tokenizer.decode(outputs[0], skip_special_tokens=True)
52
+ return script
53
+
54
+ def generate_scene_images(self, scene_descriptions):
55
+ """Generate images for each scene using Stable Diffusion"""
56
+ image_paths = []
57
+ for i, desc in enumerate(scene_descriptions):
58
+ image = self.image_generator(desc).images[0]
59
+ path = self.temp_dir / f"scene_{i}.png"
60
+ image.save(path)
61
+ image_paths.append(path)
62
+ return image_paths
63
+
64
+ def generate_voiceover(self, script):
65
+ """Generate voice narration using TTS"""
66
+ audio_path = self.temp_dir / "voiceover.wav"
67
+ self.tts.tts_to_file(script, file_path=str(audio_path))
68
+ return audio_path
69
+
70
+ def create_video(self, image_paths, audio_path, duration_per_image=5):
71
+ """Combine images and audio into video"""
72
+ clips = []
73
+ for img_path in image_paths:
74
+ clip = ImageClip(str(img_path)).set_duration(duration_per_image)
75
+ clips.append(clip)
76
+
77
+ video = concatenate_videoclips(clips)
78
+ audio = AudioFileClip(str(audio_path))
79
+
80
+ # Adjust video duration to match audio
81
+ video = video.set_duration(audio.duration)
82
+ final_video = video.set_audio(audio)
83
+
84
+ output_path = self.temp_dir / "output_video.mp4"
85
+ final_video.write_videofile(str(output_path), fps=24)
86
+ return output_path
87
+
88
+ def main():
89
+ st.set_page_config(page_title="AI Video Generator", layout="wide")
90
+ st.title("🎬 AI Text-to-Video Generator")
91
+
92
+ # Initialize session state
93
+ if 'video_generator' not in st.session_state:
94
+ st.session_state.video_generator = VideoGenerator()
95
+
96
+ # Input section
97
+ st.header("Enter Your Topic")
98
+ text_input = st.text_area(
99
+ "What would you like to create a video about?",
100
+ height=100,
101
+ placeholder="Example: Explain the process of photosynthesis in plants..."
102
+ )
103
+
104
+ # Generation settings
105
+ st.header("Video Settings")
106
+ col1, col2 = st.columns(2)
107
+ with col1:
108
+ video_length = st.slider("Approximate video length (seconds)", 30, 300, 60)
109
+ with col2:
110
+ style = st.selectbox(
111
+ "Video style",
112
+ ["Educational", "Documentary", "Engaging", "Professional"]
113
+ )
114
+
115
+ # Generate button
116
+ if st.button("πŸŽ₯ Generate Video"):
117
+ if text_input:
118
+ with st.spinner("πŸ€– Generating your video..."):
119
+ try:
120
+ # Progress bar
121
+ progress_bar = st.progress(0)
122
+ progress_text = st.empty()
123
+
124
+ # Generate script
125
+ progress_text.text("Generating script...")
126
+ script = st.session_state.video_generator.generate_script(text_input)
127
+ progress_bar.progress(25)
128
+
129
+ # Extract scene descriptions
130
+ progress_text.text("Processing scenes...")
131
+ scenes = [s.strip() for s in script.split("Scene:") if s.strip()]
132
+ progress_bar.progress(40)
133
+
134
+ # Generate images
135
+ progress_text.text("Creating visuals...")
136
+ image_paths = st.session_state.video_generator.generate_scene_images(scenes)
137
+ progress_bar.progress(60)
138
+
139
+ # Generate voiceover
140
+ progress_text.text("Generating voiceover...")
141
+ audio_path = st.session_state.video_generator.generate_voiceover(script)
142
+ progress_bar.progress(80)
143
+
144
+ # Create video
145
+ progress_text.text("Composing final video...")
146
+ video_path = st.session_state.video_generator.create_video(
147
+ image_paths,
148
+ audio_path,
149
+ duration_per_image=video_length/len(scenes)
150
+ )
151
+ progress_bar.progress(100)
152
+ progress_text.text("Video generation complete!")
153
+
154
+ # Display results
155
+ st.header("Generated Content")
156
+
157
+ # Show script
158
+ with st.expander("πŸ“ Generated Script"):
159
+ st.write(script)
160
+
161
+ # Show video
162
+ st.header("πŸŽ₯ Your Video")
163
+ video_file = open(str(video_path), 'rb')
164
+ video_bytes = video_file.read()
165
+ st.video(video_bytes)
166
+
167
+ # Download button
168
+ st.download_button(
169
+ label="Download Video",
170
+ data=video_bytes,
171
+ file_name="generated_video.mp4",
172
+ mime="video/mp4"
173
+ )
174
+
175
+ except Exception as e:
176
+ st.error(f"An error occurred: {str(e)}")
177
+ else:
178
+ st.warning("Please enter some text to generate a video!")
179
+
180
+ if __name__ == "__main__":
181
+ main()