le quy don commited on
Commit
9de0758
·
verified ·
1 Parent(s): 2a190c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -39
app.py CHANGED
@@ -104,30 +104,24 @@ def add_time_to_subtitle(input_file, hours, minutes, seconds):
104
 
105
  subs = pysrt.open(input_file)
106
 
107
- # Chuyển đổi thời gian nhập vào thành mili giây
108
- total_milliseconds = (int(hours) * 3600 + int(minutes) * 60 + int(seconds)) * 1000
 
 
 
 
 
109
 
110
- # Thêm thời gian vào tất cả các phụ đề nếu có thời gian được chỉ định
111
  if total_milliseconds > 0:
112
  for sub in subs:
113
- # Thêm thời gian vào start
114
- start_ms = sub.start.ordinal + total_milliseconds
115
- sub.start.hours = start_ms // 3600000
116
- sub.start.minutes = (start_ms % 3600000) // 60000
117
- sub.start.seconds = (start_ms % 60000) // 1000
118
- sub.start.milliseconds = start_ms % 1000
119
-
120
- # Thêm thời gian vào end
121
- end_ms = sub.end.ordinal + total_milliseconds
122
- sub.end.hours = end_ms // 3600000
123
- sub.end.minutes = (end_ms % 3600000) // 60000
124
- sub.end.seconds = (end_ms % 60000) // 1000
125
- sub.end.milliseconds = end_ms % 1000
126
 
127
  # Lưu file tạm
128
  output_path = tempfile.NamedTemporaryFile(suffix=".srt", delete=False).name
129
  subs.save(output_path, encoding='utf-8')
130
- return output_path, f"Đã thêm {hours}h {minutes}m {seconds}s vào file gốc"
131
 
132
  except Exception as e:
133
  raise gr.Error(f"Có lỗi xảy ra khi thêm thời gian: {str(e)}")
@@ -147,27 +141,21 @@ def translate_subtitle(input_file, source_language, target_language, hours, minu
147
 
148
  subs = pysrt.open(input_file)
149
 
150
- # Chuyển đổi thời gian nhập vào thành mili giây
151
- total_milliseconds = (int(hours) * 3600 + int(minutes) * 60 + int(seconds)) * 1000
 
 
 
 
 
152
 
153
- # Thêm thời gian vào tất cả các phụ đề nếu có thời gian được chỉ định
154
  if total_milliseconds > 0:
155
  for sub in subs:
156
- # Thêm thời gian vào start
157
- start_ms = sub.start.ordinal + total_milliseconds
158
- sub.start.hours = start_ms // 3600000
159
- sub.start.minutes = (start_ms % 3600000) // 60000
160
- sub.start.seconds = (start_ms % 60000) // 1000
161
- sub.start.milliseconds = start_ms % 1000
162
-
163
- # Thêm thời gian vào end
164
- end_ms = sub.end.ordinal + total_milliseconds
165
- sub.end.hours = end_ms // 3600000
166
- sub.end.minutes = (end_ms % 3600000) // 60000
167
- sub.end.seconds = (end_ms % 60000) // 1000
168
- sub.end.milliseconds = end_ms % 1000
169
 
170
- # Xử lý khác nhau nếu là model đơn hay model kép (dịch qua tiếng Anh)
171
  if isinstance(model_info[0], tuple):
172
  # Dịch qua tiếng Anh
173
  model1, tokenizer1 = model_info[0]
@@ -175,9 +163,7 @@ def translate_subtitle(input_file, source_language, target_language, hours, minu
175
 
176
  for sub in tqdm(subs, desc="Đang dịch"):
177
  if sub.text.strip():
178
- # Dịch sang tiếng Anh trước
179
  en_text = translate_text(sub.text, model1, tokenizer1)
180
- # Dịch từ tiếng Anh sang ngôn ngữ đích
181
  sub.text = translate_text(en_text, model2, tokenizer2)
182
  else:
183
  # Dịch trực tiếp
@@ -189,7 +175,7 @@ def translate_subtitle(input_file, source_language, target_language, hours, minu
189
  # Lưu file tạm
190
  output_path = tempfile.NamedTemporaryFile(suffix=".srt", delete=False).name
191
  subs.save(output_path, encoding='utf-8')
192
- return output_path, f"Dịch từ {source_language} sang {target_language} thành công! Đã thêm {hours}h {minutes}m {seconds}s"
193
 
194
  except Exception as e:
195
  raise gr.Error(f"Có lỗi xảy ra: {str(e)}")
@@ -221,7 +207,7 @@ with gr.Blocks(title="Subtitle Translator Pro", theme="soft") as demo:
221
  with gr.Row():
222
  hours = gr.Number(label="Giờ", value=0, precision=0, minimum=0)
223
  minutes = gr.Number(label="Phút", value=0, precision=0, minimum=0, maximum=59)
224
- seconds = gr.Number(label="Giây", value=0, precision=0, minimum=0, maximum=59)
225
 
226
  with gr.Row():
227
  add_time_btn = gr.Button("Chỉ thêm thời gian", variant="secondary")
@@ -268,7 +254,7 @@ with gr.Blocks(title="Subtitle Translator Pro", theme="soft") as demo:
268
  - Tự động phát hiện ngôn ngữ nguồn
269
  - Dịch giữa 24 ngôn ngữ khác nhau
270
  - Hỗ trợ dịch qua tiếng Anh nếu không có model trực tiếp
271
- - Thêm thời gian vào tất cả phụ đề (giờ, phút, giây)
272
  - Có nút riêng để chỉ thêm thời gian trước khi dịch
273
  """)
274
 
 
104
 
105
  subs = pysrt.open(input_file)
106
 
107
+ # Chuyển đổi thời gian nhập vào thành mili giây (hỗ trợ số thập phân)
108
+ try:
109
+ seconds_float = float(seconds)
110
+ except ValueError:
111
+ seconds_float = 0
112
+
113
+ total_milliseconds = int((int(hours) * 3600 + int(minutes) * 60 + seconds_float) * 1000)
114
 
115
+ # Thêm thời gian vào tất cả các phụ đề
116
  if total_milliseconds > 0:
117
  for sub in subs:
118
+ sub.start.ordinal += total_milliseconds
119
+ sub.end.ordinal += total_milliseconds
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  # Lưu file tạm
122
  output_path = tempfile.NamedTemporaryFile(suffix=".srt", delete=False).name
123
  subs.save(output_path, encoding='utf-8')
124
+ return output_path, f"Đã thêm {hours}h {minutes}m {seconds_float}s vào file gốc"
125
 
126
  except Exception as e:
127
  raise gr.Error(f"Có lỗi xảy ra khi thêm thời gian: {str(e)}")
 
141
 
142
  subs = pysrt.open(input_file)
143
 
144
+ # Chuyển đổi thời gian nhập vào thành mili giây (hỗ trợ số thập phân)
145
+ try:
146
+ seconds_float = float(seconds)
147
+ except ValueError:
148
+ seconds_float = 0
149
+
150
+ total_milliseconds = int((int(hours) * 3600 + int(minutes) * 60 + seconds_float) * 1000)
151
 
152
+ # Thêm thời gian vào tất cả các phụ đề
153
  if total_milliseconds > 0:
154
  for sub in subs:
155
+ sub.start.ordinal += total_milliseconds
156
+ sub.end.ordinal += total_milliseconds
 
 
 
 
 
 
 
 
 
 
 
157
 
158
+ # Xử lý dịch thuật
159
  if isinstance(model_info[0], tuple):
160
  # Dịch qua tiếng Anh
161
  model1, tokenizer1 = model_info[0]
 
163
 
164
  for sub in tqdm(subs, desc="Đang dịch"):
165
  if sub.text.strip():
 
166
  en_text = translate_text(sub.text, model1, tokenizer1)
 
167
  sub.text = translate_text(en_text, model2, tokenizer2)
168
  else:
169
  # Dịch trực tiếp
 
175
  # Lưu file tạm
176
  output_path = tempfile.NamedTemporaryFile(suffix=".srt", delete=False).name
177
  subs.save(output_path, encoding='utf-8')
178
+ return output_path, f"Dịch từ {source_language} sang {target_language} thành công! Đã thêm {hours}h {minutes}m {seconds_float}s"
179
 
180
  except Exception as e:
181
  raise gr.Error(f"Có lỗi xảy ra: {str(e)}")
 
207
  with gr.Row():
208
  hours = gr.Number(label="Giờ", value=0, precision=0, minimum=0)
209
  minutes = gr.Number(label="Phút", value=0, precision=0, minimum=0, maximum=59)
210
+ seconds = gr.Number(label="Giây", value=0, minimum=0, step=0.1)
211
 
212
  with gr.Row():
213
  add_time_btn = gr.Button("Chỉ thêm thời gian", variant="secondary")
 
254
  - Tự động phát hiện ngôn ngữ nguồn
255
  - Dịch giữa 24 ngôn ngữ khác nhau
256
  - Hỗ trợ dịch qua tiếng Anh nếu không có model trực tiếp
257
+ - Thêm thời gian vào tất cả phụ đề (hỗ trợ giây thập phân)
258
  - Có nút riêng để chỉ thêm thời gian trước khi dịch
259
  """)
260