Eason Lu commited on
Commit
84e3cbf
·
2 Parent(s): 3cc60a3 a4b45a2

solving conflict

Browse files

Former-commit-id: 5b68d833e807397faf6f220c02d03b1df6cb2c83

Files changed (2) hide show
  1. SRT.py +33 -21
  2. pipeline.py +13 -16
SRT.py CHANGED
@@ -114,51 +114,63 @@ class SRT_script():
114
  start_seg_id = id_range[0]
115
  end_seg_id = id_range[1]
116
 
117
- def inner_func(input_str):
118
  response = openai.ChatCompletion.create(
119
- model=model,
 
120
  messages = [
121
- {"role": "system", "content": "You are a helpful assistant that help calibrates English to Chinese subtitle translations in starcraft2."},
122
- {"role": "system", "content": "You are provided with a translated Chinese transcript; you must modify or split the Chinese sentence to match the meaning and the number of the English transcript exactly one by one. You must not merge ANY Chinese lines, you can only split them but the total Chinese lines MUST equals to number of English lines."},
123
- {"role": "system", "content": "There is no need for you to add any comments or notes, and do not modify the English transcript."},
124
- {"role": "user", "content": 'You are given the English transcript and line number, your task is to merge or split the Chinese to match the exact number of lines in English transcript, no more no less. For example, if there are more Chinese lines than English lines, merge some the Chinese lines to match the number of English lines. If Chinese lines is less than English lines, split some Chinese lines to match the english lines: "{}"'.format(input_str)}
 
 
 
125
  ],
126
- temperature=0.7
 
127
  )
128
  return response['choices'][0]['message']['content'].strip()
129
 
 
130
  lines = translate.split('\n\n')
131
  if len(lines) < (end_seg_id - start_seg_id + 1):
132
  count = 0
133
- solved = False
134
  while count<5 and len(lines) != (end_seg_id - start_seg_id + 1):
135
-
136
  count += 1
137
  print("Solving Unmatched Lines|iteration {}".format(count))
138
- input_str = "\n"
139
  #initialize GPT input
140
- for i, seg in enumerate(self.segments[start_seg_id-1:end_seg_id]):
141
- input_str += 'Sentence %d: ' %(i+1)+ seg.source_text + '\n'
142
- #Append to prompt string
143
- #Adds sentence index let GPT keep track of sentence breaks
144
- input_str += translate
145
  #append translate to prompt
146
-
147
  flag = True
148
  while flag:
149
  flag = False
 
 
150
  try:
151
- translate = inner_func(input_str)
 
 
152
  except Exception as e:
153
  print("An error has occurred during solving unmatched lines:",e)
154
  print("Retrying...")
155
  flag = True
156
-
157
- lines = translate.split('\n\n')
 
 
158
  if len(lines) < (end_seg_id - start_seg_id + 1):
 
159
  print("Failed Solving unmatched lines, Manually parse needed")
160
 
161
-
 
162
  if video_link:
163
  log_file = "./logs/log_link.csv"
164
  log_exist = os.path.exists(log_file)
@@ -192,7 +204,7 @@ class SRT_script():
192
  if i == len(lines) - 1:
193
  break
194
  try:
195
- seg.translation = lines[i].split(":" or ":")[1]
196
  except:
197
  seg.translation = lines[i]
198
 
 
114
  start_seg_id = id_range[0]
115
  end_seg_id = id_range[1]
116
 
117
+ def inner_func(target,input_str):
118
  response = openai.ChatCompletion.create(
119
+ #model=model,
120
+ model = "gpt-3.5-turbo",
121
  messages = [
122
+ #{"role": "system", "content": "You are a helpful assistant that help calibrates English to Chinese subtitle translations in starcraft2."},
123
+ #{"role": "system", "content": "You are provided with a translated Chinese transcript; you must modify or split the Chinese sentence to match the meaning and the number of the English transcript exactly one by one. You must not merge ANY Chinese lines, you can only split them but the total Chinese lines MUST equals to number of English lines."},
124
+ #{"role": "system", "content": "There is no need for you to add any comments or notes, and do not modify the English transcript."},
125
+ #{"role": "user", "content": 'You are given the English transcript and line number, your task is to merge or split the Chinese to match the exact number of lines in English transcript, no more no less. For example, if there are more Chinese lines than English lines, merge some the Chinese lines to match the number of English lines. If Chinese lines is less than English lines, split some Chinese lines to match the english lines: "{}"'.format(input_str)}
126
+ {"role": "system", "content": "你的任务是按照要求合并或拆分句子到指定行数,你需要尽可能保证句意,但必要时可以将一句话分为两行输出"},
127
+ {"role": "system", "content": "注意:你只需要输出处理过的中文句子,如果你要输出序号,请使用冒号隔开"},
128
+ {"role": "user", "content": '请将下面的句子拆分或组合为{}句:\n{}'.format(target,input_str)}
129
  ],
130
+ #temperature=0.7
131
+ temperature = 0.15
132
  )
133
  return response['choices'][0]['message']['content'].strip()
134
 
135
+
136
  lines = translate.split('\n\n')
137
  if len(lines) < (end_seg_id - start_seg_id + 1):
138
  count = 0
139
+ solved = True
140
  while count<5 and len(lines) != (end_seg_id - start_seg_id + 1):
 
141
  count += 1
142
  print("Solving Unmatched Lines|iteration {}".format(count))
143
+ #input_str = "\n"
144
  #initialize GPT input
145
+ #for i, seg in enumerate(self.segments[start_seg_id-1:end_seg_id]):
146
+ # input_str += 'Sentence %d: ' %(i+1)+ seg.source_text + '\n'
147
+ # #Append to prompt string
148
+ # #Adds sentence index let GPT keep track of sentence breaks
149
+ #input_str += translate
150
  #append translate to prompt
 
151
  flag = True
152
  while flag:
153
  flag = False
154
+ #print("translate:")
155
+ #print(translate)
156
  try:
157
+ #print("target")
158
+ #print(end_seg_id - start_seg_id + 1)
159
+ translate = inner_func(end_seg_id - start_seg_id + 1,translate)
160
  except Exception as e:
161
  print("An error has occurred during solving unmatched lines:",e)
162
  print("Retrying...")
163
  flag = True
164
+ lines = translate.split('\n')
165
+ #print("result")
166
+ #print(len(lines))
167
+
168
  if len(lines) < (end_seg_id - start_seg_id + 1):
169
+ solved = False
170
  print("Failed Solving unmatched lines, Manually parse needed")
171
 
172
+ if not os.path.exists("./logs"):
173
+ os.mkdir("./logs")
174
  if video_link:
175
  log_file = "./logs/log_link.csv"
176
  log_exist = os.path.exists(log_file)
 
204
  if i == len(lines) - 1:
205
  break
206
  try:
207
+ seg.translation = lines[i].split(":" or ":" or ".")[1]
208
  except:
209
  seg.translation = lines[i]
210
 
pipeline.py CHANGED
@@ -5,6 +5,8 @@ import os
5
  from tqdm import tqdm
6
  from SRT import SRT_script
7
  import stable_whisper
 
 
8
  import subprocess
9
 
10
  import time
@@ -47,7 +49,7 @@ if args.video_name == 'placeholder' :
47
  elif args.audio_file is not None:
48
  VIDEO_NAME = args.audio_file.split('/')[-1].split('.')[0]
49
  elif args.srt_file is not None:
50
- VIDEO_NAME = args.srt_file.split('/')[-1].split('.')[0]
51
  else:
52
  VIDEO_NAME = args.video_name
53
  else:
@@ -95,14 +97,7 @@ elif args.video_file is not None:
95
  audio_file= open(args.audio_file, "rb")
96
  audio_path = args.audio_file
97
  else:
98
- # escaped_video_path = args.video_file.replace('(', '\(').replace(')', '\)').replace(' ', '\ ')
99
- # print(escaped_video_path)
100
- # os.system(f'ffmpeg -i {escaped_video_path} -f mp3 -ab 192000 -vn {DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3')
101
- # audio_file= open(f'{DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3', "rb")
102
- # audio_path = f'{DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3'
103
  output_audio_path = f'{DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3'
104
- # print(video_path)
105
- # print(output_audio_path)
106
  subprocess.run(['ffmpeg', '-i', video_path, '-f', 'mp3', '-ab', '192000', '-vn', output_audio_path])
107
  audio_file = open(output_audio_path, "rb")
108
  audio_path = output_audio_path
@@ -133,15 +128,17 @@ else:
133
 
134
  # use stable-whisper
135
  model = stable_whisper.load_model('base')
136
- transcript = model.transcribe(audio_path)
137
- # (
138
- # transcript
139
- # .split_by_punctuation(['.', '。', '?'])
140
- # .merge_by_gap(.15, max_words=3)
141
- # .merge_by_punctuation([' '])
142
- # .split_by_punctuation(['.', '。', '?'])
143
- # )
 
144
  transcript = transcript.to_dict()
 
145
  srt = SRT_script(transcript['segments']) # read segments to SRT class
146
 
147
  else:
 
5
  from tqdm import tqdm
6
  from SRT import SRT_script
7
  import stable_whisper
8
+ import whisper
9
+
10
  import subprocess
11
 
12
  import time
 
49
  elif args.audio_file is not None:
50
  VIDEO_NAME = args.audio_file.split('/')[-1].split('.')[0]
51
  elif args.srt_file is not None:
52
+ VIDEO_NAME = args.srt_file.split('/')[-1].split('.')[0].split("_")[:-1]
53
  else:
54
  VIDEO_NAME = args.video_name
55
  else:
 
97
  audio_file= open(args.audio_file, "rb")
98
  audio_path = args.audio_file
99
  else:
 
 
 
 
 
100
  output_audio_path = f'{DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3'
 
 
101
  subprocess.run(['ffmpeg', '-i', video_path, '-f', 'mp3', '-ab', '192000', '-vn', output_audio_path])
102
  audio_file = open(output_audio_path, "rb")
103
  audio_path = output_audio_path
 
128
 
129
  # use stable-whisper
130
  model = stable_whisper.load_model('base')
131
+ transcript = model.transcribe(audio_path, regroup = False, initial_prompt="Hello, welcome to my lecture. Are you good my friend?")
132
+ (
133
+ transcript
134
+ .split_by_punctuation(['.', '。', '?'])
135
+ .merge_by_gap(.15, max_words=3)
136
+ .merge_by_punctuation([' '])
137
+ .split_by_punctuation(['.', '。', '?'])
138
+ )
139
+ # transcript.to_srt_vtt(srt_file_en)
140
  transcript = transcript.to_dict()
141
+ # print(transcript)
142
  srt = SRT_script(transcript['segments']) # read segments to SRT class
143
 
144
  else: