artificialguybr commited on
Commit
275e48a
·
1 Parent(s): 2de3a57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -27
app.py CHANGED
@@ -1,21 +1,14 @@
1
  import gradio as gr
2
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
- from subprocess import run, CalledProcessError
4
  from faster_whisper import WhisperModel
5
  import json
6
  import tempfile
7
  import os
 
8
  from zipfile import ZipFile
9
  import stat
10
 
11
- def run_command(command):
12
- try:
13
- run(command, check=True)
14
- except CalledProcessError as e:
15
- print(f"Command failed with error: {e}")
16
- return False
17
- return True
18
-
19
  ZipFile("ffmpeg.zip").extractall()
20
  st = os.stat('ffmpeg')
21
  os.chmod('ffmpeg', st.st_mode | stat.S_IEXEC)
@@ -28,15 +21,13 @@ model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M"
28
  whisper_model = WhisperModel("large-v2", device="cuda", compute_type="float16")
29
 
30
  def process_video(Video, target_language):
 
31
  audio_file = tempfile.NamedTemporaryFile(suffix=".wav").name
32
-
33
- if not run_command(["ffmpeg", "-i", Video, audio_file]):
34
- print("FFmpeg command failed. Exiting.")
35
- return
36
-
37
  segments, _ = whisper_model.transcribe(audio_file, beam_size=5)
38
  segments = list(segments)
39
-
40
  temp_transcript_file = tempfile.NamedTemporaryFile(delete=False, suffix=".srt")
41
  with open(temp_transcript_file.name, "w", encoding="utf-8") as f:
42
  counter = 1
@@ -49,12 +40,10 @@ def process_video(Video, target_language):
49
  end_milliseconds = int((segment.end - int(segment.end)) * 1000)
50
  formatted_start = f"{start_minutes:02d}:{start_seconds:02d},{start_milliseconds:03d}"
51
  formatted_end = f"{end_minutes:02d}:{end_seconds:02d},{end_milliseconds:03d}"
52
-
53
  f.write(f"{counter}\n")
54
  f.write(f"{formatted_start} --> {formatted_end}\n")
55
  f.write(f"{segment.text}\n\n")
56
  counter += 1
57
-
58
  flores_code = lang_codes.get(target_language, "eng_Latn")
59
  temp_translated_file = tempfile.NamedTemporaryFile(delete=False, suffix=".srt")
60
  with open(temp_transcript_file.name, "r", encoding="utf-8") as infile, open(temp_translated_file.name, "w", encoding="utf-8") as outfile:
@@ -68,19 +57,11 @@ def process_video(Video, target_language):
68
  outfile.write(translated_text + "\n")
69
  else:
70
  outfile.write("\n")
71
-
72
- if not os.path.exists(temp_translated_file.name):
73
- print("Subtitle file does not exist. Exiting.")
74
- return
75
-
76
  output_video = "output_video.mp4"
77
- if not run_command(["ffmpeg", "-i", Video, "-vf", f"subtitles={temp_translated_file.name}", output_video]):
78
- print("FFmpeg command for embedding subtitles failed. Exiting.")
79
- return
80
-
81
  os.unlink(temp_transcript_file.name)
82
  os.unlink(temp_translated_file.name)
83
-
84
  return output_video
85
 
86
  iface = gr.Interface(
 
1
  import gradio as gr
2
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
+ from subprocess import run
4
  from faster_whisper import WhisperModel
5
  import json
6
  import tempfile
7
  import os
8
+ import ffmpeg
9
  from zipfile import ZipFile
10
  import stat
11
 
 
 
 
 
 
 
 
 
12
  ZipFile("ffmpeg.zip").extractall()
13
  st = os.stat('ffmpeg')
14
  os.chmod('ffmpeg', st.st_mode | stat.S_IEXEC)
 
21
  whisper_model = WhisperModel("large-v2", device="cuda", compute_type="float16")
22
 
23
  def process_video(Video, target_language):
24
+ print("Iniciando process_video")
25
  audio_file = tempfile.NamedTemporaryFile(suffix=".wav").name
26
+ print("Executando FFmpeg para extração de áudio")
27
+ run(["ffmpeg", "-i", Video, audio_file])
28
+ print("Iniciando transcrição com Whisper")
 
 
29
  segments, _ = whisper_model.transcribe(audio_file, beam_size=5)
30
  segments = list(segments)
 
31
  temp_transcript_file = tempfile.NamedTemporaryFile(delete=False, suffix=".srt")
32
  with open(temp_transcript_file.name, "w", encoding="utf-8") as f:
33
  counter = 1
 
40
  end_milliseconds = int((segment.end - int(segment.end)) * 1000)
41
  formatted_start = f"{start_minutes:02d}:{start_seconds:02d},{start_milliseconds:03d}"
42
  formatted_end = f"{end_minutes:02d}:{end_seconds:02d},{end_milliseconds:03d}"
 
43
  f.write(f"{counter}\n")
44
  f.write(f"{formatted_start} --> {formatted_end}\n")
45
  f.write(f"{segment.text}\n\n")
46
  counter += 1
 
47
  flores_code = lang_codes.get(target_language, "eng_Latn")
48
  temp_translated_file = tempfile.NamedTemporaryFile(delete=False, suffix=".srt")
49
  with open(temp_transcript_file.name, "r", encoding="utf-8") as infile, open(temp_translated_file.name, "w", encoding="utf-8") as outfile:
 
57
  outfile.write(translated_text + "\n")
58
  else:
59
  outfile.write("\n")
 
 
 
 
 
60
  output_video = "output_video.mp4"
61
+ run(["ffmpeg", "-i", Video, "-vf", f"subtitles={temp_translated_file.name}", output_video])
 
 
 
62
  os.unlink(temp_transcript_file.name)
63
  os.unlink(temp_translated_file.name)
64
+ print("process_video concluído com sucesso")
65
  return output_video
66
 
67
  iface = gr.Interface(