test commit: handling multiple video generations
Browse files
app.py
CHANGED
@@ -1,33 +1,42 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
from utils import get_translation, get_image_prompts, segments_to_chunks, generate_images, generate_video
|
3 |
import constants
|
4 |
from groq import Groq
|
5 |
|
6 |
client = Groq()
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
# Initialize state variables if not already set
|
9 |
-
if '
|
10 |
-
st.session_state
|
11 |
-
if '
|
12 |
-
st.session_state
|
13 |
-
if '
|
14 |
-
st.session_state
|
15 |
-
if '
|
16 |
-
st.session_state
|
17 |
-
if '
|
18 |
-
st.session_state
|
19 |
-
if '
|
20 |
-
st.session_state
|
21 |
-
if '
|
22 |
-
st.session_state
|
23 |
-
if '
|
24 |
-
st.session_state
|
25 |
-
if '
|
26 |
-
st.session_state
|
27 |
-
if '
|
28 |
-
st.session_state
|
29 |
-
if '
|
30 |
-
st.session_state
|
31 |
|
32 |
|
33 |
# Streamlit UI
|
@@ -35,7 +44,10 @@ st.markdown(
|
|
35 |
"<h1 style='text-align: center;'>AI Video Generator</h1>",
|
36 |
unsafe_allow_html=True
|
37 |
)
|
38 |
-
st.
|
|
|
|
|
|
|
39 |
|
40 |
# Upload audio file
|
41 |
audio_file = st.file_uploader("🔼 Upload your audio file:", type=constants.SUPPORTED_FORMATS)
|
@@ -45,15 +57,15 @@ print(audio_file,'is the upload')
|
|
45 |
|
46 |
if audio_file:
|
47 |
# Reset states only when a new file is uploaded
|
48 |
-
if st.session_state
|
49 |
-
st.session_state
|
50 |
-
st.session_state
|
51 |
-
st.session_state
|
52 |
-
st.session_state
|
53 |
-
st.session_state
|
54 |
-
st.session_state
|
55 |
-
st.session_state
|
56 |
-
st.session_state
|
57 |
|
58 |
st.info(f"Uploaded file: **{audio_file.name}**")
|
59 |
|
@@ -68,55 +80,55 @@ if audio_file:
|
|
68 |
response_format="verbose_json", # Return detailed JSON response
|
69 |
temperature=0.0, # Control randomness in the transcription output
|
70 |
)
|
71 |
-
st.session_state
|
72 |
-
st.session_state
|
73 |
|
74 |
# Translation logic
|
75 |
-
if st.session_state
|
76 |
with st.spinner("Generating translation... Please wait."):
|
77 |
-
st.session_state
|
78 |
|
79 |
-
st.audio(st.session_state
|
80 |
|
81 |
# Toggle transcript visibility
|
82 |
-
toggle_transcript = st.checkbox("Show Transcript", value=st.session_state
|
83 |
-
st.session_state
|
84 |
|
85 |
-
if st.session_state
|
86 |
st.write("### Transcription:")
|
87 |
-
st.write(st.session_state
|
88 |
|
89 |
# Toggle translation visibility
|
90 |
-
toggle_translation = st.checkbox("Show Translation", value=st.session_state
|
91 |
-
st.session_state
|
92 |
|
93 |
-
if st.session_state
|
94 |
st.write("### Translation:")
|
95 |
-
st.write(st.session_state
|
96 |
|
97 |
# Image generation logic
|
98 |
-
if st.session_state
|
99 |
with st.spinner("Generating image prompts... Please wait."):
|
100 |
-
if 'Already in English' in st.session_state
|
101 |
st.info("Audio is Already in English. Using Transcription to generate Image Prompts")
|
102 |
-
st.session_state
|
103 |
else:
|
104 |
-
st.session_state
|
105 |
|
106 |
-
print(st.session_state
|
107 |
# Ensure that generated_images is always a list
|
108 |
-
if '
|
109 |
-
st.session_state
|
110 |
|
111 |
# Generate images only if they have not been generated already
|
112 |
-
if st.session_state
|
113 |
progress_placeholder = st.empty()
|
114 |
progress_bar = st.progress(0)
|
115 |
-
total_images = len(st.session_state
|
116 |
progress_placeholder.text(f"Generating images. Please be patient...")
|
117 |
|
118 |
-
for idx, (prompt, image_path) in enumerate(generate_images(st.session_state
|
119 |
-
st.session_state.
|
120 |
progress = (idx + 1) / total_images
|
121 |
progress_bar.progress(progress)
|
122 |
progress_placeholder.text(f"Generated image {idx + 1} of {total_images}: {prompt[:50]}...")
|
@@ -125,29 +137,34 @@ if audio_file:
|
|
125 |
progress_bar.empty()
|
126 |
|
127 |
# Generate video when all images are generated
|
128 |
-
if st.session_state
|
129 |
with st.spinner("Generating video... Please wait."):
|
|
|
|
|
|
|
|
|
|
|
130 |
# Map images to segments
|
131 |
-
image_paths = [img[1] for img in st.session_state
|
132 |
generated_video_path = generate_video(
|
133 |
-
audio_file=st.session_state
|
134 |
images=image_paths,
|
135 |
-
segments=st.session_state
|
136 |
)
|
137 |
-
st.session_state
|
138 |
-
st.session_state
|
139 |
st.success("Video generated successfully!")
|
140 |
|
141 |
# Display the generated video
|
142 |
-
if st.session_state
|
143 |
-
st.video(st.session_state
|
144 |
|
145 |
# Add a download button for the generated video
|
146 |
-
with open(st.session_state
|
147 |
st.download_button(
|
148 |
label="Download Video",
|
149 |
data=file,
|
150 |
-
file_name="
|
151 |
mime="video/mp4"
|
152 |
)
|
153 |
|
|
|
1 |
import streamlit as st
|
2 |
+
import os
|
3 |
+
import tempfile
|
4 |
+
import uuid
|
5 |
from utils import get_translation, get_image_prompts, segments_to_chunks, generate_images, generate_video
|
6 |
import constants
|
7 |
from groq import Groq
|
8 |
|
9 |
client = Groq()
|
10 |
|
11 |
+
# Generate a unique session ID for each user
|
12 |
+
if 'session_id' not in st.session_state:
|
13 |
+
st.session_state.session_id = str(uuid.uuid4())
|
14 |
+
|
15 |
+
session_id = st.session_state.session_id
|
16 |
+
|
17 |
# Initialize state variables if not already set
|
18 |
+
if f'transcript_visible_{session_id}' not in st.session_state:
|
19 |
+
st.session_state[f'transcript_visible_{session_id}'] = False
|
20 |
+
if f'translation_visible_{session_id}' not in st.session_state:
|
21 |
+
st.session_state[f'translation_visible_{session_id}'] = False
|
22 |
+
if f'uploaded_file_name_{session_id}' not in st.session_state:
|
23 |
+
st.session_state[f'uploaded_file_name_{session_id}'] = None
|
24 |
+
if f'audio_{session_id}' not in st.session_state:
|
25 |
+
st.session_state[f'audio_{session_id}'] = None
|
26 |
+
if f'was_converted_{session_id}' not in st.session_state:
|
27 |
+
st.session_state[f'was_converted_{session_id}'] = False
|
28 |
+
if f'transcript_{session_id}' not in st.session_state:
|
29 |
+
st.session_state[f'transcript_{session_id}'] = None
|
30 |
+
if f'translation_{session_id}' not in st.session_state:
|
31 |
+
st.session_state[f'translation_{session_id}'] = None
|
32 |
+
if f'generated_video_{session_id}' not in st.session_state:
|
33 |
+
st.session_state[f'generated_video_{session_id}'] = None
|
34 |
+
if f'image_prompts_{session_id}' not in st.session_state:
|
35 |
+
st.session_state[f'image_prompts_{session_id}'] = None
|
36 |
+
if f'generated_images_{session_id}' not in st.session_state:
|
37 |
+
st.session_state[f'generated_images_{session_id}'] = None
|
38 |
+
if f'video_generated_{session_id}' not in st.session_state:
|
39 |
+
st.session_state[f'video_generated_{session_id}'] = False
|
40 |
|
41 |
|
42 |
# Streamlit UI
|
|
|
44 |
"<h1 style='text-align: center;'>AI Video Generator</h1>",
|
45 |
unsafe_allow_html=True
|
46 |
)
|
47 |
+
st.markdown("<p style='text-align: center;'>Leave a Like if it works for you! ❤️</p>", unsafe_allow_html=True)
|
48 |
+
st.info("**Video Generation Feature** - Functional But Can be Buggy")
|
49 |
+
|
50 |
+
# Encourage users to like the app
|
51 |
|
52 |
# Upload audio file
|
53 |
audio_file = st.file_uploader("🔼 Upload your audio file:", type=constants.SUPPORTED_FORMATS)
|
|
|
57 |
|
58 |
if audio_file:
|
59 |
# Reset states only when a new file is uploaded
|
60 |
+
if st.session_state[f'uploaded_file_name_{session_id}'] != audio_file.name:
|
61 |
+
st.session_state[f'uploaded_file_name_{session_id}'] = audio_file.name
|
62 |
+
st.session_state[f'audio_{session_id}'] = audio_file
|
63 |
+
st.session_state[f'transcript_{session_id}'] = None
|
64 |
+
st.session_state[f'translation_{session_id}'] = None
|
65 |
+
st.session_state[f'image_prompts_{session_id}'] = None
|
66 |
+
st.session_state[f'generated_images_{session_id}'] = None # Reset image generation state
|
67 |
+
st.session_state[f'generated_video_{session_id}'] = None # Reset generated video state
|
68 |
+
st.session_state[f'video_generated_{session_id}'] = False # Reset video generated flag
|
69 |
|
70 |
st.info(f"Uploaded file: **{audio_file.name}**")
|
71 |
|
|
|
80 |
response_format="verbose_json", # Return detailed JSON response
|
81 |
temperature=0.0, # Control randomness in the transcription output
|
82 |
)
|
83 |
+
st.session_state[f'transcript_{session_id}'] = result.text
|
84 |
+
st.session_state[f'segments_{session_id}'] = result.segments
|
85 |
|
86 |
# Translation logic
|
87 |
+
if st.session_state[f'transcript_{session_id}'] and st.session_state[f'translation_{session_id}'] is None:
|
88 |
with st.spinner("Generating translation... Please wait."):
|
89 |
+
st.session_state[f'translation_{session_id}'] = get_translation(st.session_state[f'transcript_{session_id}'])
|
90 |
|
91 |
+
st.audio(st.session_state[f'audio_{session_id}'], format=f"audio/{audio_file.type}")
|
92 |
|
93 |
# Toggle transcript visibility
|
94 |
+
toggle_transcript = st.checkbox("Show Transcript", value=st.session_state[f'transcript_visible_{session_id}'], key="toggle_transcript")
|
95 |
+
st.session_state[f'transcript_visible_{session_id}'] = toggle_transcript
|
96 |
|
97 |
+
if st.session_state[f'transcript_visible_{session_id}']:
|
98 |
st.write("### Transcription:")
|
99 |
+
st.write(st.session_state[f'transcript_{session_id}'])
|
100 |
|
101 |
# Toggle translation visibility
|
102 |
+
toggle_translation = st.checkbox("Show Translation", value=st.session_state[f'translation_visible_{session_id}'], key="toggle_translation")
|
103 |
+
st.session_state[f'translation_visible_{session_id}'] = toggle_translation
|
104 |
|
105 |
+
if st.session_state[f'translation_visible_{session_id}']:
|
106 |
st.write("### Translation:")
|
107 |
+
st.write(st.session_state[f'translation_{session_id}'])
|
108 |
|
109 |
# Image generation logic
|
110 |
+
if st.session_state[f'translation_{session_id}'] and st.session_state[f'image_prompts_{session_id}'] is None:
|
111 |
with st.spinner("Generating image prompts... Please wait."):
|
112 |
+
if 'Already in English' in st.session_state[f'translation_{session_id}']:
|
113 |
st.info("Audio is Already in English. Using Transcription to generate Image Prompts")
|
114 |
+
st.session_state[f'image_prompts_{session_id}'] = get_image_prompts(segments_to_chunks(st.session_state[f'segments_{session_id}']))['image_prompts']
|
115 |
else:
|
116 |
+
st.session_state[f'image_prompts_{session_id}'] = get_image_prompts(segments_to_chunks(st.session_state[f'segments_{session_id}']))['image_prompts']
|
117 |
|
118 |
+
print(st.session_state[f'image_prompts_{session_id}'])
|
119 |
# Ensure that generated_images is always a list
|
120 |
+
if f'generated_images_{session_id}' not in st.session_state or st.session_state[f'generated_images_{session_id}'] is None:
|
121 |
+
st.session_state[f'generated_images_{session_id}'] = []
|
122 |
|
123 |
# Generate images only if they have not been generated already
|
124 |
+
if st.session_state[f'image_prompts_{session_id}'] and not st.session_state[f'generated_images_{session_id}']:
|
125 |
progress_placeholder = st.empty()
|
126 |
progress_bar = st.progress(0)
|
127 |
+
total_images = len(st.session_state[f'image_prompts_{session_id}'])
|
128 |
progress_placeholder.text(f"Generating images. Please be patient...")
|
129 |
|
130 |
+
for idx, (prompt, image_path) in enumerate(generate_images(st.session_state[f'image_prompts_{session_id}'])):
|
131 |
+
st.session_state[f'generated_images_{session_id}'].append((prompt, image_path))
|
132 |
progress = (idx + 1) / total_images
|
133 |
progress_bar.progress(progress)
|
134 |
progress_placeholder.text(f"Generated image {idx + 1} of {total_images}: {prompt[:50]}...")
|
|
|
137 |
progress_bar.empty()
|
138 |
|
139 |
# Generate video when all images are generated
|
140 |
+
if st.session_state[f'generated_images_{session_id}'] and st.session_state[f'audio_{session_id}'] and not st.session_state[f'video_generated_{session_id}']:
|
141 |
with st.spinner("Generating video... Please wait."):
|
142 |
+
# Create a temporary directory for the video
|
143 |
+
temp_dir = tempfile.gettempdir()
|
144 |
+
video_filename = f"generated_video_{session_id}.mp4"
|
145 |
+
video_path = os.path.join(temp_dir, video_filename)
|
146 |
+
|
147 |
# Map images to segments
|
148 |
+
image_paths = [img[1] for img in st.session_state[f'generated_images_{session_id}']]
|
149 |
generated_video_path = generate_video(
|
150 |
+
audio_file=st.session_state[f'audio_{session_id}'],
|
151 |
images=image_paths,
|
152 |
+
segments=st.session_state[f'segments_{session_id}']
|
153 |
)
|
154 |
+
st.session_state[f'generated_video_{session_id}'] = generated_video_path
|
155 |
+
st.session_state[f'video_generated_{session_id}'] = True # Set the flag to True
|
156 |
st.success("Video generated successfully!")
|
157 |
|
158 |
# Display the generated video
|
159 |
+
if st.session_state[f'generated_video_{session_id}']:
|
160 |
+
st.video(st.session_state[f'generated_video_{session_id}'])
|
161 |
|
162 |
# Add a download button for the generated video
|
163 |
+
with open(st.session_state[f'generated_video_{session_id}'], "rb") as file:
|
164 |
st.download_button(
|
165 |
label="Download Video",
|
166 |
data=file,
|
167 |
+
file_name=f"generated_video_{session_id}.mp4",
|
168 |
mime="video/mp4"
|
169 |
)
|
170 |
|