Lenylvt's picture
Create app.py
8c9741b verified
raw
history blame
1.43 kB
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()