Update app.py
Browse files
app.py
CHANGED
@@ -166,8 +166,6 @@ def mock_analytics():
|
|
166 |
"Instagram": {"Views": random.randint(500, 3000), "Engagement Rate": f"{random.uniform(10, 20):.2f}%"},
|
167 |
}
|
168 |
|
169 |
-
import json
|
170 |
-
|
171 |
def update_translations(file, edited_table):
|
172 |
"""
|
173 |
Update the translations based on user edits in the Gradio Dataframe.
|
@@ -176,6 +174,8 @@ def update_translations(file, edited_table):
|
|
176 |
logger.debug(f"Editable Table: {edited_table}")
|
177 |
|
178 |
try:
|
|
|
|
|
179 |
# Convert the edited_table (list of lists) back to list of dictionaries
|
180 |
updated_translations = [
|
181 |
{
|
@@ -190,52 +190,53 @@ def update_translations(file, edited_table):
|
|
190 |
# Call the function to process the video with updated translations
|
191 |
add_transcript_to_video(file.name, updated_translations, output_video_path)
|
192 |
|
193 |
-
|
|
|
|
|
|
|
|
|
194 |
|
195 |
except Exception as e:
|
196 |
raise ValueError(f"Error updating translations: {e}")
|
197 |
|
198 |
-
# Core functionalities
|
199 |
def upload_and_manage(file, language):
|
200 |
if file is None:
|
201 |
-
return "Please upload a video/audio file."
|
202 |
|
203 |
-
|
204 |
-
|
205 |
-
output_video_path = "output_video.mp4"
|
206 |
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
|
211 |
-
|
212 |
-
|
213 |
-
# Add transcript to video based on timestamps
|
214 |
-
add_transcript_to_video(file.name, translated_json, output_video_path)
|
215 |
|
216 |
-
|
217 |
-
|
218 |
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
|
225 |
-
|
|
|
|
|
|
|
|
|
226 |
|
227 |
-
#
|
228 |
-
def time_execution(func):
|
229 |
-
def wrapper(*args, **kwargs):
|
230 |
-
start_time = time.time()
|
231 |
-
result = func(*args, **kwargs)
|
232 |
elapsed_time = time.time() - start_time
|
233 |
-
|
234 |
-
|
|
|
|
|
|
|
|
|
|
|
235 |
|
236 |
-
# Wrap the functions with the timer
|
237 |
-
timed_upload_and_manage = time_execution(upload_and_manage)
|
238 |
-
timed_update_translations = time_execution(update_translations)
|
239 |
|
240 |
# Gradio Interface with Tabs
|
241 |
def build_interface():
|
@@ -284,13 +285,13 @@ def build_interface():
|
|
284 |
return message, gr.update(visible=False)
|
285 |
|
286 |
save_changes_button.click(
|
287 |
-
|
288 |
inputs=[file_input, editable_table],
|
289 |
outputs=[processed_video_output, elapsed_time_display]
|
290 |
)
|
291 |
|
292 |
submit_button.click(
|
293 |
-
|
294 |
inputs=[file_input, language_input],
|
295 |
outputs=[editable_translations, editable_table, processed_video_output, elapsed_time_display]
|
296 |
)
|
|
|
166 |
"Instagram": {"Views": random.randint(500, 3000), "Engagement Rate": f"{random.uniform(10, 20):.2f}%"},
|
167 |
}
|
168 |
|
|
|
|
|
169 |
def update_translations(file, edited_table):
|
170 |
"""
|
171 |
Update the translations based on user edits in the Gradio Dataframe.
|
|
|
174 |
logger.debug(f"Editable Table: {edited_table}")
|
175 |
|
176 |
try:
|
177 |
+
start_time = time.time() # Start the timer
|
178 |
+
|
179 |
# Convert the edited_table (list of lists) back to list of dictionaries
|
180 |
updated_translations = [
|
181 |
{
|
|
|
190 |
# Call the function to process the video with updated translations
|
191 |
add_transcript_to_video(file.name, updated_translations, output_video_path)
|
192 |
|
193 |
+
# Calculate elapsed time
|
194 |
+
elapsed_time = time.time() - start_time
|
195 |
+
elapsed_time_display = f"Updates applied successfully in {elapsed_time:.2f} seconds."
|
196 |
+
|
197 |
+
return output_video_path, elapsed_time_display
|
198 |
|
199 |
except Exception as e:
|
200 |
raise ValueError(f"Error updating translations: {e}")
|
201 |
|
|
|
202 |
def upload_and_manage(file, language):
|
203 |
if file is None:
|
204 |
+
return None, [], None, "No file uploaded. Please upload a video/audio file."
|
205 |
|
206 |
+
try:
|
207 |
+
start_time = time.time() # Start the timer
|
|
|
208 |
|
209 |
+
# Define paths for audio and output files
|
210 |
+
audio_path = "audio.wav"
|
211 |
+
output_video_path = "output_video.mp4"
|
212 |
|
213 |
+
list_available_fonts()
|
|
|
|
|
|
|
214 |
|
215 |
+
# Transcribe audio from uploaded media file and get timestamps
|
216 |
+
transcrption_json = transcribe_video(file.name)
|
217 |
|
218 |
+
# Translate the transcription
|
219 |
+
translated_json = translate_text(transcrption_json, language)
|
220 |
+
|
221 |
+
# Add transcript to video based on timestamps
|
222 |
+
add_transcript_to_video(file.name, translated_json, output_video_path)
|
223 |
|
224 |
+
# Convert the translated JSON into a format for the editable table
|
225 |
+
editable_table = [
|
226 |
+
[float(entry["start"]), entry["original"], entry["translated"], float(entry["end"])]
|
227 |
+
for entry in translated_json
|
228 |
+
]
|
229 |
|
230 |
+
# Calculate elapsed time
|
|
|
|
|
|
|
|
|
231 |
elapsed_time = time.time() - start_time
|
232 |
+
elapsed_time_display = f"Processing completed in {elapsed_time:.2f} seconds."
|
233 |
+
|
234 |
+
return translated_json, editable_table, output_video_path, elapsed_time_display
|
235 |
+
|
236 |
+
except Exception as e:
|
237 |
+
# Handle errors gracefully
|
238 |
+
return None, [], None, f"An error occurred: {str(e)}"
|
239 |
|
|
|
|
|
|
|
240 |
|
241 |
# Gradio Interface with Tabs
|
242 |
def build_interface():
|
|
|
285 |
return message, gr.update(visible=False)
|
286 |
|
287 |
save_changes_button.click(
|
288 |
+
update_translations,
|
289 |
inputs=[file_input, editable_table],
|
290 |
outputs=[processed_video_output, elapsed_time_display]
|
291 |
)
|
292 |
|
293 |
submit_button.click(
|
294 |
+
upload_and_manage,
|
295 |
inputs=[file_input, language_input],
|
296 |
outputs=[editable_translations, editable_table, processed_video_output, elapsed_time_display]
|
297 |
)
|