artificialguybr commited on
Commit
d41a72f
·
verified ·
1 Parent(s): f00d512

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -29
app.py CHANGED
@@ -32,7 +32,7 @@ print("cwd", os.getcwd())
32
  print(os.listdir())
33
 
34
 
35
- def process_video(Video, target_language):
36
  current_path = os.getcwd()
37
  print("Iniciando process_video")
38
  common_uuid = uuid.uuid4()
@@ -40,16 +40,13 @@ def process_video(Video, target_language):
40
  run(["ffmpeg", "-version"])
41
  audio_file = f"{common_uuid}.wav"
42
  run(["ffmpeg", "-i", Video, audio_file])
43
- transcript_file = f"{common_uuid}.srt"
 
44
  # Transcription with Whisper.
45
  print("Iniciando transcrição com Whisper")
46
  segments, _ = whisper_model.transcribe(audio_file, beam_size=5)
47
  segments = list(segments)
48
- transcript_file = f"{current_path}/{common_uuid}.srt"
49
-
50
- # Create a list to hold the translated lines.
51
- translated_lines = []
52
-
53
  with open(transcript_file, "w+", encoding="utf-8") as f:
54
  counter = 1
55
  for segment in segments:
@@ -70,28 +67,25 @@ def process_video(Video, target_language):
70
  f.write(f"{formatted_start} --> {formatted_end}\n")
71
  f.write(f"{segment.text}\n\n")
72
  counter += 1
73
- # Move the file pointer to the beginning of the file.
74
- f.seek(0)
75
-
76
- # Translating the SRT from Whisper with Google Translate.
77
- target_language_code = google_lang_codes.get(target_language, "en")
78
- paragraph = ""
79
- for line in f:
80
- if line.strip().isnumeric() or "-->" in line:
81
- translated_lines.append(line)
82
- elif line.strip() != "":
83
- translated_text = translator.translate(line.strip(), dest=target_language_code).text
84
- translated_lines.append(translated_text + "\n")
85
- else:
86
- translated_lines.append("\n")
87
-
88
- # Move the file pointer to the beginning of the file and truncate it.
89
- f.seek(0)
90
- f.truncate()
91
-
92
- # Write the translated lines back into the original file.
93
- f.writelines(translated_lines)
94
- #return None, None
95
  output_video = f"{common_uuid}_output_video.mp4"
96
  # Debugging: Validate FFmpeg command for subtitle embedding
97
  print("Validating FFmpeg command for subtitle embedding...")
@@ -128,6 +122,7 @@ iface = gr.Interface(
128
  inputs=[
129
  gr.Video(),
130
  gr.Dropdown(choices=list(google_lang_codes.keys()), label="Target Language for Translation", value="English"),
 
131
  ],
132
  outputs=[
133
  gr.Video(),
 
32
  print(os.listdir())
33
 
34
 
35
+ def process_video(Video, target_language, translate_video):
36
  current_path = os.getcwd()
37
  print("Iniciando process_video")
38
  common_uuid = uuid.uuid4()
 
40
  run(["ffmpeg", "-version"])
41
  audio_file = f"{common_uuid}.wav"
42
  run(["ffmpeg", "-i", Video, audio_file])
43
+ transcript_file = f"{current_path}/{common_uuid}.srt"
44
+
45
  # Transcription with Whisper.
46
  print("Iniciando transcrição com Whisper")
47
  segments, _ = whisper_model.transcribe(audio_file, beam_size=5)
48
  segments = list(segments)
49
+
 
 
 
 
50
  with open(transcript_file, "w+", encoding="utf-8") as f:
51
  counter = 1
52
  for segment in segments:
 
67
  f.write(f"{formatted_start} --> {formatted_end}\n")
68
  f.write(f"{segment.text}\n\n")
69
  counter += 1
70
+
71
+ # Check if translation is needed
72
+ if translate_video:
73
+ # Translating the SRT from Whisper with Google Translate.
74
+ target_language_code = google_lang_codes.get(target_language, "en")
75
+ translated_lines = []
76
+ f.seek(0) # Move the file pointer to the beginning of the file.
77
+ for line in f:
78
+ if line.strip().isnumeric() or "-->" in line:
79
+ translated_lines.append(line)
80
+ elif line.strip() != "":
81
+ translated_text = translator.translate(line.strip(), dest=target_language_code).text
82
+ translated_lines.append(translated_text + "\n")
83
+ else:
84
+ translated_lines.append("\n")
85
+
86
+ f.seek(0) # Move the file pointer to the beginning of the file and truncate it.
87
+ f.truncate()
88
+ f.writelines(translated_lines) # Write the translated lines back into the original file.
 
 
 
89
  output_video = f"{common_uuid}_output_video.mp4"
90
  # Debugging: Validate FFmpeg command for subtitle embedding
91
  print("Validating FFmpeg command for subtitle embedding...")
 
122
  inputs=[
123
  gr.Video(),
124
  gr.Dropdown(choices=list(google_lang_codes.keys()), label="Target Language for Translation", value="English"),
125
+ gr.Checkbox(label="Translate Video", value=True, info="Check to translate the video to the selected language. Uncheck for transcription only."),
126
  ],
127
  outputs=[
128
  gr.Video(),