-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathst.py
More file actions
260 lines (223 loc) · 8.5 KB
/
st.py
File metadata and controls
260 lines (223 loc) · 8.5 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import streamlit as st
import os, sys, time
from core.st_utils.imports_and_utils import *
from core.st_utils.task_runner import TaskRunner
from core import *
# SET PATH
current_dir = os.path.dirname(os.path.abspath(__file__))
os.environ["PATH"] += os.pathsep + current_dir
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
st.set_page_config(page_title="VideoLingo", page_icon="docs/logo.svg")
SUB_VIDEO = "output/output_sub.mp4"
DUB_VIDEO = "output/output_dub.mp4"
# ─── Task control UI (auto-refreshes every 1s while task is active) ───
@st.fragment(run_every=1)
def _task_control_panel(runner_key: str):
"""Renders progress bar + pause/stop buttons. Auto-refreshes every 1s."""
runner = TaskRunner.get(st.session_state, runner_key)
if runner.state == "idle":
return
# Progress
step_text = (
f"({runner.current_step + 1}/{runner.total_steps}) {runner.current_label}"
if runner.current_step >= 0
else ""
)
if runner.is_active:
if runner.state == "paused":
st.warning(f"⏸️ {t('Paused')} {step_text}")
else:
st.info(f"⏳ {t('Running...')} {step_text}")
st.progress(runner.progress)
# Control buttons
col1, col2 = st.columns(2)
with col1:
if runner.state == "paused":
if st.button(
f"▶️ {t('Resume')}",
key=f"{runner_key}_resume",
use_container_width=True,
):
runner.resume()
st.rerun()
else:
if st.button(
f"⏸️ {t('Pause')}",
key=f"{runner_key}_pause",
use_container_width=True,
):
runner.pause()
st.rerun()
with col2:
if st.button(
f"⏹️ {t('Stop')}",
key=f"{runner_key}_stop",
use_container_width=True,
type="primary",
):
runner.stop()
st.rerun()
elif runner.state == "completed":
st.success(t("Task completed!"))
st.progress(1.0)
runner.reset()
time.sleep(0.5)
st.rerun(scope="app")
elif runner.state == "stopped":
st.warning(f"⏹️ {t('Task stopped')} {step_text}")
if st.button(t("OK"), key=f"{runner_key}_ack_stop", use_container_width=True):
runner.reset()
st.rerun(scope="app")
elif runner.state == "error":
st.error(f"❌ {t('Task error')}: {runner.error_msg}")
if st.button(t("OK"), key=f"{runner_key}_ack_error", use_container_width=True):
runner.reset()
st.rerun(scope="app")
# ─── Text processing ───
def _get_text_steps():
"""Return the subtitle processing steps as (label, callable) list."""
steps = [
(t("WhisperX word-level transcription"), _2_asr.transcribe),
(
t("Sentence segmentation using NLP and LLM"),
lambda: (
_3_1_split_nlp.split_by_spacy(),
_3_2_split_meaning.split_sentences_by_meaning(),
),
),
(
t("Summarization and multi-step translation"),
lambda: (_4_1_summarize.get_summary(), _4_2_translate.translate_all()),
),
(
t("Cutting and aligning long subtitles"),
lambda: (
_5_split_sub.split_for_sub_main(),
_6_gen_sub.align_timestamp_main(),
),
),
(
t("Merging subtitles into the video"),
_7_sub_into_vid.merge_subtitles_to_video,
),
]
return steps
def text_processing_section():
st.header(t("b. Translate and Generate Subtitles"))
runner = TaskRunner.get(st.session_state, "_text_runner")
with st.container(border=True):
st.markdown(
f"""
<p style='font-size: 20px;'>
{t("This stage includes the following steps:")}
<p style='font-size: 20px;'>
1. {t("WhisperX word-level transcription")}<br>
2. {t("Sentence segmentation using NLP and LLM")}<br>
3. {t("Summarization and multi-step translation")}<br>
4. {t("Cutting and aligning long subtitles")}<br>
5. {t("Generating timeline and subtitles")}<br>
6. {t("Merging subtitles into the video")}
""",
unsafe_allow_html=True,
)
if not os.path.exists(SUB_VIDEO):
if runner.is_active:
_task_control_panel("_text_runner")
elif runner.is_done:
_task_control_panel("_text_runner")
else:
if st.button(
t("Start Processing Subtitles"), key="text_processing_button"
):
steps = _get_text_steps()
runner.start(steps)
st.rerun()
else:
if load_key("burn_subtitles"):
st.video(SUB_VIDEO)
download_subtitle_zip_button(text=t("Download All Srt Files"))
if st.button(t("Archive to 'history'"), key="cleanup_in_text_processing"):
cleanup()
st.rerun()
return True
# ─── Audio processing ───
def _get_audio_steps():
"""Return the audio/dubbing processing steps as (label, callable) list."""
steps = [
(
t("Generate audio tasks and chunks"),
lambda: (
_8_1_audio_task.gen_audio_task_main(),
_8_2_dub_chunks.gen_dub_chunks(),
),
),
(t("Extract reference audio"), _9_refer_audio.extract_refer_audio_main),
(t("Generate and merge audio files"), _10_gen_audio.gen_audio),
(t("Merge full audio"), _11_merge_audio.merge_full_audio),
(t("Merge final audio into video"), _12_dub_to_vid.merge_video_audio),
]
return steps
def audio_processing_section():
st.header(t("c. Dubbing"))
runner = TaskRunner.get(st.session_state, "_audio_runner")
with st.container(border=True):
st.markdown(
f"""
<p style='font-size: 20px;'>
{t("This stage includes the following steps:")}
<p style='font-size: 20px;'>
1. {t("Generate audio tasks and chunks")}<br>
2. {t("Extract reference audio")}<br>
3. {t("Generate and merge audio files")}<br>
4. {t("Merge final audio into video")}
""",
unsafe_allow_html=True,
)
if not os.path.exists(DUB_VIDEO):
if runner.is_active:
_task_control_panel("_audio_runner")
elif runner.is_done:
_task_control_panel("_audio_runner")
else:
if st.button(
t("Start Audio Processing"), key="audio_processing_button"
):
steps = _get_audio_steps()
runner.start(steps)
st.rerun()
else:
st.success(
t(
"Audio processing is complete! You can check the audio files in the `output` folder."
)
)
if load_key("burn_subtitles"):
st.video(DUB_VIDEO)
if st.button(t("Delete dubbing files"), key="delete_dubbing_files"):
delete_dubbing_files()
st.rerun()
if st.button(t("Archive to 'history'"), key="cleanup_in_audio_processing"):
cleanup()
st.rerun()
# ─── Main ───
def main():
logo_col, _ = st.columns([1, 1])
with logo_col:
st.image("docs/logo.png", width="stretch")
st.markdown(button_style, unsafe_allow_html=True)
welcome_text = t(
'Hello, welcome to VideoLingo. If you encounter any issues, feel free to get instant answers with our Free QA Agent <a href="https://share.fastgpt.in/chat/share?shareId=066w11n3r9aq6879r4z0v9rh" target="_blank">here</a>! You can also try out our SaaS website at <a href="https://videolingo.io" target="_blank">videolingo.io</a> for free!'
)
st.markdown(
f"<p style='font-size: 20px; color: #808080;'>{welcome_text}</p>",
unsafe_allow_html=True,
)
# add settings
with st.sidebar:
page_setting()
st.markdown(give_star_button, unsafe_allow_html=True)
download_video_section()
text_processing_section()
audio_processing_section()
if __name__ == "__main__":
main()