Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,42 +4,55 @@ import re
|
|
| 4 |
|
| 5 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
do_sample=True,
|
| 21 |
-
seed=42,
|
| 22 |
-
)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
def
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
return "\n".join(translated_lines)
|
| 38 |
|
| 39 |
-
gr.Interface(
|
| 40 |
-
fn=
|
| 41 |
-
inputs=gr.File(label="
|
| 42 |
-
outputs=
|
| 43 |
-
title="SRT Translator",
|
| 44 |
-
description="
|
| 45 |
-
)
|
|
|
|
|
|
|
|
|
| 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(file_type="srt"), gr.Textbox(label="Target Language Code")],
|
| 53 |
+
outputs="text",
|
| 54 |
+
title="SRT File Translator",
|
| 55 |
+
description="Translate SRT subtitle files to your desired language."
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
iface.launch()
|