Lenylvt commited on
Commit
37b15c7
·
verified ·
1 Parent(s): 3159abf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -49
app.py CHANGED
@@ -1,59 +1,39 @@
1
- from huggingface_hub import InferenceClient
2
  import gradio as gr
 
3
  import re
4
 
 
5
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
6
 
7
- def translate_subtitles(srt_content, target_language):
8
- """
9
- Translate the subtitles in the SRT file content to the target language.
10
- """
11
- # Split the SRT content into blocks
12
- blocks = srt_content.split('\n\n')
13
- translated_blocks = []
14
- for block in blocks:
15
- if block.strip() == "":
16
- continue
17
- lines = block.split('\n')
18
- if len(lines) >= 3:
19
- index = lines[0]
20
- time_range = lines[1]
21
- subtitle_text = '\n'.join(lines[2:])
22
- # Translate the subtitle text
23
- translation = client.translation(subtitle_text, target_language=target_language)
24
- translated_text = translation[0]['translation_text']
25
- translated_blocks.append(f"{index}\n{time_range}\n{translated_text}")
26
- return '\n\n'.join(translated_blocks)
27
-
28
- def read_srt_file(file_path):
29
- """
30
- Read SRT file content.
31
- """
32
- with open(file_path, 'r', encoding='utf-8') as file:
33
- return file.read()
34
-
35
- def save_translated_srt(content, output_path):
36
- """
37
- Save the translated subtitles to a new SRT file.
38
- """
39
- with open(output_path, 'w', encoding='utf-8') as file:
40
- file.write(content)
41
-
42
- def translate_srt_interface(srt_file, target_language):
43
- """
44
- Gradio interface function to translate SRT file content.
45
- """
46
- srt_content = srt_file.read()
47
- translated_content = translate_subtitles(srt_content, target_language)
48
- return translated_content
49
-
50
  iface = gr.Interface(
51
- fn=translate_srt_interface,
52
- inputs=[gr.File(label="Upload SRT File"), gr.Textbox(label="Target Language Code", placeholder="Enter language code here, e.g., 'en' for English")],
53
- outputs=gr.Textbox(),
54
  title="SRT File Translator",
55
- description="Translate SRT subtitle files to your desired language."
56
  )
57
 
58
-
59
  iface.launch()
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
  import re
4
 
5
+ # Initialize the InferenceClient with the Mixtral model
6
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
7
 
8
+ def translate_srt(file, target_language):
9
+ # Read the content of the SRT file
10
+ srt_content = file.read().decode("utf-8")
11
+ lines = srt_content.split('\n')
12
+
13
+ translated_lines = []
14
+ for line in lines:
15
+ # Check if the line is a timestamp or a subtitle number
16
+ if re.match(r"^\d+$", line) or re.match(r"^\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}$", line):
17
+ translated_lines.append(line) # Copy timestamps and numbers directly
18
+ elif line.strip() == "":
19
+ translated_lines.append(line) # Preserve empty lines for formatting
20
+ else:
21
+ # Translate the text line
22
+ response = client(inputs=line, parameters={"target_language": target_language})
23
+ translated_lines.append(response[0]["generated_text"])
24
+
25
+ # Join the translated lines back into a single string
26
+ translated_srt_content = "\n".join(translated_lines)
27
+ return translated_srt_content
28
+
29
+ # Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  iface = gr.Interface(
31
+ fn=translate_srt,
32
+ inputs=[gr.inputs.File(label="Upload SRT File"), gr.inputs.Dropdown(["fr", "en", "es", "de", "it", "pt"], label="Target Language")],
33
+ outputs="text",
34
  title="SRT File Translator",
35
+ description="Translate SRT files to the selected language using Mixtral model."
36
  )
37
 
38
+ # Launch the Gradio app
39
  iface.launch()