Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import InferenceClient
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Initialize the InferenceClient with the Mixtral model
|
| 5 |
+
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 6 |
+
|
| 7 |
+
def translate_srt(srt_file, source_lang, target_lang):
|
| 8 |
+
srt_content = srt_file.read().decode("utf-8")
|
| 9 |
+
translated_lines = []
|
| 10 |
+
|
| 11 |
+
# Split the SRT file into lines
|
| 12 |
+
lines = srt_content.split("\n")
|
| 13 |
+
|
| 14 |
+
for line in lines:
|
| 15 |
+
# Check if the line is a timestamp or an empty line
|
| 16 |
+
if line.strip().isdigit() or "-->" in line or line == "":
|
| 17 |
+
translated_lines.append(line)
|
| 18 |
+
else:
|
| 19 |
+
# Translate the text line
|
| 20 |
+
response = client(text=line, parameters={"src_lang": source_lang, "tgt_lang": target_lang})
|
| 21 |
+
translated_text = response["generated_text"]
|
| 22 |
+
translated_lines.append(translated_text)
|
| 23 |
+
|
| 24 |
+
translated_srt = "\n".join(translated_lines)
|
| 25 |
+
return translated_srt
|
| 26 |
+
|
| 27 |
+
# Define the Gradio interface
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=translate_srt,
|
| 30 |
+
inputs=[
|
| 31 |
+
gr.inputs.File(label="Upload SRT File"),
|
| 32 |
+
gr.inputs.Textbox(label="Source Language (ISO Code)", placeholder="e.g., en for English"),
|
| 33 |
+
gr.inputs.Textbox(label="Target Language (ISO Code)", placeholder="e.g., es for Spanish")
|
| 34 |
+
],
|
| 35 |
+
outputs="text",
|
| 36 |
+
title="SRT Translator",
|
| 37 |
+
description="Translate SRT (SubRip subtitle) files from one language to another."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
iface.launch()
|