Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
if
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
return
|
| 27 |
-
|
| 28 |
-
|
| 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=
|
| 52 |
-
inputs=[gr.File(label="Upload SRT File"), gr.
|
| 53 |
-
outputs=
|
| 54 |
title="SRT File Translator",
|
| 55 |
-
description="Translate SRT
|
| 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()
|