Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -478,29 +478,38 @@ def translate_text(transcription_json, source_language, target_language):
|
|
478 |
|
479 |
def update_translations(file, edited_table, process_mode):
|
480 |
"""
|
481 |
-
Update the translations based on user edits in the Gradio Dataframe
|
|
|
482 |
"""
|
483 |
output_video_path = "output_video.mp4"
|
|
|
484 |
logger.debug(f"Editable Table: {edited_table}")
|
485 |
|
486 |
if file is None:
|
487 |
logger.info("No file uploaded. Please upload a video/audio file.")
|
488 |
-
return None,
|
489 |
-
|
490 |
try:
|
491 |
start_time = time.time() # Start the timer
|
492 |
|
493 |
-
# Convert the edited_table (
|
|
|
494 |
updated_translations = [
|
495 |
{
|
496 |
-
"start": row["start"],
|
497 |
"original": row["original"],
|
498 |
"translated": row["translated"],
|
499 |
-
"end": row["end"]
|
|
|
500 |
}
|
501 |
for _, row in edited_table.iterrows()
|
502 |
]
|
503 |
|
|
|
|
|
|
|
|
|
|
|
504 |
# Call the function to process the video with updated translations
|
505 |
add_transcript_voiceover(file.name, updated_translations, output_video_path, process_mode)
|
506 |
|
@@ -508,10 +517,12 @@ def update_translations(file, edited_table, process_mode):
|
|
508 |
elapsed_time = time.time() - start_time
|
509 |
elapsed_time_display = f"Updates applied successfully in {elapsed_time:.2f} seconds."
|
510 |
|
511 |
-
|
512 |
-
|
513 |
except Exception as e:
|
514 |
-
|
|
|
|
|
515 |
|
516 |
def create_subtitle_clip_pil(text, start_time, end_time, video_width, video_height, font_path):
|
517 |
try:
|
@@ -1336,13 +1347,16 @@ def build_interface():
|
|
1336 |
with gr.Row():
|
1337 |
with gr.Column(scale=4):
|
1338 |
file_input = gr.File(label="Upload Video/Audio File")
|
1339 |
-
language_input = gr.Dropdown(["en", "es", "fr", "zh"], label="Select Language") # Language codes
|
1340 |
-
process_mode = gr.Radio(choices=[("Transcription Only", 1),("Transcription with Premium Voice",2),("Transcription with Voice Clone", 3)],label="Choose Processing Type",value=1)
|
1341 |
-
submit_button = gr.Button("Post and Process")
|
1342 |
|
|
|
|
|
|
|
|
|
|
|
1343 |
with gr.Column(scale=8):
|
1344 |
gr.Markdown("## Edit Translations")
|
1345 |
-
|
1346 |
# Editable JSON Data
|
1347 |
editable_table = gr.Dataframe(
|
1348 |
value=[], # Default to an empty list to avoid undefined values
|
@@ -1358,7 +1372,6 @@ def build_interface():
|
|
1358 |
processed_video_output = gr.File(label="Download Processed Video", interactive=True) # Download button
|
1359 |
elapsed_time_display = gr.Textbox(label="Elapsed Time", lines=1, interactive=False)
|
1360 |
translated_json_download = gr.File(label="Download Translated JSON", interactive=True) # New: JSON download
|
1361 |
-
|
1362 |
with gr.Column(scale=1):
|
1363 |
gr.Markdown("**Feedback**")
|
1364 |
feedback_input = gr.Textbox(
|
@@ -1369,33 +1382,30 @@ def build_interface():
|
|
1369 |
feedback_btn = gr.Button("Submit Feedback")
|
1370 |
response_message = gr.Textbox(label=None, lines=1, interactive=False)
|
1371 |
db_download = gr.File(label="Download Database File", visible=False)
|
1372 |
-
|
1373 |
-
|
1374 |
-
|
1375 |
-
|
1376 |
-
|
1377 |
-
|
1378 |
-
|
1379 |
|
1380 |
save_changes_button.click(
|
1381 |
-
update_translations,
|
1382 |
inputs=[file_input, editable_table, process_mode],
|
1383 |
-
outputs=[processed_video_output, elapsed_time_display]
|
1384 |
)
|
1385 |
-
|
1386 |
submit_button.click(
|
1387 |
-
upload_and_manage,
|
1388 |
-
inputs=[file_input, language_input, process_mode],
|
1389 |
outputs=[editable_table, processed_video_output, translated_json_download, elapsed_time_display]
|
1390 |
)
|
1391 |
-
|
1392 |
# Connect submit button to save_feedback_db function
|
1393 |
feedback_btn.click(
|
1394 |
-
feedback_submission,
|
1395 |
-
inputs=[feedback_input],
|
1396 |
outputs=[response_message, db_download]
|
1397 |
)
|
1398 |
-
|
1399 |
return demo
|
1400 |
|
1401 |
tts_model = None
|
|
|
478 |
|
479 |
def update_translations(file, edited_table, process_mode):
|
480 |
"""
|
481 |
+
Update the translations based on user edits in the Gradio Dataframe
|
482 |
+
and allow the user to download the updated JSON.
|
483 |
"""
|
484 |
output_video_path = "output_video.mp4"
|
485 |
+
updated_json_path = "updated_translations.json" # Define the path for the updated JSON file
|
486 |
logger.debug(f"Editable Table: {edited_table}")
|
487 |
|
488 |
if file is None:
|
489 |
logger.info("No file uploaded. Please upload a video/audio file.")
|
490 |
+
return None, None, None
|
491 |
+
|
492 |
try:
|
493 |
start_time = time.time() # Start the timer
|
494 |
|
495 |
+
# Convert the edited_table (pandas DataFrame) back to list of dictionaries
|
496 |
+
# Ensure column names match the original structure
|
497 |
updated_translations = [
|
498 |
{
|
499 |
+
"start": row["start"],
|
500 |
"original": row["original"],
|
501 |
"translated": row["translated"],
|
502 |
+
"end": row["end"],
|
503 |
+
"speaker": row["speaker"] # Include speaker if it's part of your translation structure
|
504 |
}
|
505 |
for _, row in edited_table.iterrows()
|
506 |
]
|
507 |
|
508 |
+
# Save the updated translations to a JSON file
|
509 |
+
with open(updated_json_path, 'w', encoding='utf-8') as f:
|
510 |
+
json.dump(updated_translations, f, indent=4, ensure_ascii=False)
|
511 |
+
logger.info(f"Updated translations saved to: {updated_json_path}")
|
512 |
+
|
513 |
# Call the function to process the video with updated translations
|
514 |
add_transcript_voiceover(file.name, updated_translations, output_video_path, process_mode)
|
515 |
|
|
|
517 |
elapsed_time = time.time() - start_time
|
518 |
elapsed_time_display = f"Updates applied successfully in {elapsed_time:.2f} seconds."
|
519 |
|
520 |
+
# Return the path to the updated JSON file as well
|
521 |
+
return output_video_path, updated_json_path, elapsed_time_display
|
522 |
except Exception as e:
|
523 |
+
logger.error(f"Error updating translations: {e}")
|
524 |
+
# Return Nones for all outputs if an error occurs
|
525 |
+
return None, None, None
|
526 |
|
527 |
def create_subtitle_clip_pil(text, start_time, end_time, video_width, video_height, font_path):
|
528 |
try:
|
|
|
1347 |
with gr.Row():
|
1348 |
with gr.Column(scale=4):
|
1349 |
file_input = gr.File(label="Upload Video/Audio File")
|
1350 |
+
language_input = gr.Dropdown(["en", "es", "fr", "zh"], label="Select Language") # Language codes
|
|
|
|
|
1351 |
|
1352 |
+
process_mode = gr.Radio(choices=[("Transcription Only", 1),
|
1353 |
+
("Transcription with Premium Voice", 2),
|
1354 |
+
("Transcription with Voice Clone", 3)],
|
1355 |
+
label="Choose Processing Type", value=1)
|
1356 |
+
submit_button = gr.Button("Post and Process")
|
1357 |
with gr.Column(scale=8):
|
1358 |
gr.Markdown("## Edit Translations")
|
1359 |
+
|
1360 |
# Editable JSON Data
|
1361 |
editable_table = gr.Dataframe(
|
1362 |
value=[], # Default to an empty list to avoid undefined values
|
|
|
1372 |
processed_video_output = gr.File(label="Download Processed Video", interactive=True) # Download button
|
1373 |
elapsed_time_display = gr.Textbox(label="Elapsed Time", lines=1, interactive=False)
|
1374 |
translated_json_download = gr.File(label="Download Translated JSON", interactive=True) # New: JSON download
|
|
|
1375 |
with gr.Column(scale=1):
|
1376 |
gr.Markdown("**Feedback**")
|
1377 |
feedback_input = gr.Textbox(
|
|
|
1382 |
feedback_btn = gr.Button("Submit Feedback")
|
1383 |
response_message = gr.Textbox(label=None, lines=1, interactive=False)
|
1384 |
db_download = gr.File(label="Download Database File", visible=False)
|
1385 |
+
|
1386 |
+
# Link the feedback handling
|
1387 |
+
def feedback_submission(feedback):
|
1388 |
+
message, file_path = handle_feedback(feedback)
|
1389 |
+
if file_path:
|
1390 |
+
return message, gr.update(value=file_path, visible=True)
|
1391 |
+
return message, gr.update(visible=False)
|
1392 |
|
1393 |
save_changes_button.click(
|
1394 |
+
update_translations,
|
1395 |
inputs=[file_input, editable_table, process_mode],
|
1396 |
+
outputs=[processed_video_output, translated_json_download, elapsed_time_display]
|
1397 |
)
|
|
|
1398 |
submit_button.click(
|
1399 |
+
upload_and_manage,
|
1400 |
+
inputs=[file_input, language_input, process_mode],
|
1401 |
outputs=[editable_table, processed_video_output, translated_json_download, elapsed_time_display]
|
1402 |
)
|
|
|
1403 |
# Connect submit button to save_feedback_db function
|
1404 |
feedback_btn.click(
|
1405 |
+
feedback_submission,
|
1406 |
+
inputs=[feedback_input],
|
1407 |
outputs=[response_message, db_download]
|
1408 |
)
|
|
|
1409 |
return demo
|
1410 |
|
1411 |
tts_model = None
|