File size: 1,430 Bytes
8c9741b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from huggingface_hub import InferenceClient
import gradio as gr

# Initialize the InferenceClient with the Mixtral model
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")

def translate_srt(srt_file, source_lang, target_lang):
    srt_content = srt_file.read().decode("utf-8")
    translated_lines = []
    
    # Split the SRT file into lines
    lines = srt_content.split("\n")
    
    for line in lines:
        # Check if the line is a timestamp or an empty line
        if line.strip().isdigit() or "-->" in line or line == "":
            translated_lines.append(line)
        else:
            # Translate the text line
            response = client(text=line, parameters={"src_lang": source_lang, "tgt_lang": target_lang})
            translated_text = response["generated_text"]
            translated_lines.append(translated_text)
    
    translated_srt = "\n".join(translated_lines)
    return translated_srt

# Define the Gradio interface
iface = gr.Interface(
    fn=translate_srt,
    inputs=[
        gr.inputs.File(label="Upload SRT File"),
        gr.inputs.Textbox(label="Source Language (ISO Code)", placeholder="e.g., en for English"),
        gr.inputs.Textbox(label="Target Language (ISO Code)", placeholder="e.g., es for Spanish")
    ],
    outputs="text",
    title="SRT Translator",
    description="Translate SRT (SubRip subtitle) files from one language to another."
)

iface.launch()