Lenylvt commited on
Commit
9c9b591
β€’
1 Parent(s): e595915

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -3,6 +3,7 @@ import pandas as pd
3
  import gradio as gr
4
  from transformers import MarianMTModel, MarianTokenizer
5
  import io
 
6
 
7
  # Fetch and parse language options
8
  url = "https://huggingface.co/Lenylvt/LanguageISO/resolve/main/iso.md"
@@ -35,21 +36,23 @@ def translate_text(text, source_language_code, target_language_code):
35
 
36
  return translated_text
37
 
38
- def translate_srt(file_info, source_language_code, target_language_code):
39
- srt_content = file_info["content"].decode("utf-8")
40
- translated_lines = []
41
- for line in srt_content.split("\n"):
42
- if line.isdigit() or "-->" in line:
43
- translated_lines.append(line)
44
- elif line.strip() == "":
45
- translated_lines.append(line)
46
- else:
47
- translated_text = translate_text(line, source_language_code, target_language_code)
48
- translated_lines.append(translated_text)
49
- translated_srt_content = "\n".join(translated_lines)
 
50
  output_path = "/mnt/data/translated_srt.srt"
51
  with open(output_path, "w", encoding="utf-8") as file:
52
- file.write(translated_srt_content)
 
53
  return output_path
54
 
55
  source_language_dropdown = gr.Dropdown(choices=language_options, label="Source Language")
 
3
  import gradio as gr
4
  from transformers import MarianMTModel, MarianTokenizer
5
  import io
6
+ import pysrt
7
 
8
  # Fetch and parse language options
9
  url = "https://huggingface.co/Lenylvt/LanguageISO/resolve/main/iso.md"
 
36
 
37
  return translated_text
38
 
39
+ def translate_srt(file_content, source_language_code, target_language_code):
40
+ # Load the SRT content using pysrt and io.BytesIO
41
+ subs = pysrt.from_string(file_content.decode("utf-8"))
42
+
43
+ # Translate each subtitle
44
+ for sub in subs:
45
+ translated_text = translate_text(sub.text, source_language_code, target_language_code)
46
+ sub.text = translated_text
47
+
48
+ # Convert the subtitles back to an SRT string
49
+ srt_content = '\n'.join(subs.to_string())
50
+
51
+ # Save the translated SRT content to a temporary file
52
  output_path = "/mnt/data/translated_srt.srt"
53
  with open(output_path, "w", encoding="utf-8") as file:
54
+ file.write(srt_content)
55
+
56
  return output_path
57
 
58
  source_language_dropdown = gr.Dropdown(choices=language_options, label="Source Language")