Lenylvt's picture
Update app.py
7c82987 verified
raw
history blame
1.54 kB
import gradio as gr
from huggingface_hub import InferenceClient
import re
# Initialize the InferenceClient with the Mixtral model
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
def translate_srt(file_info, target_language):
# file_info is a dictionary containing information about the file
# including its content
srt_content = file_info["content"].decode("utf-8")
lines = srt_content.split('\n')
translated_lines = []
for line in lines:
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):
translated_lines.append(line) # Copy timestamps and numbers directly
elif line.strip() == "":
translated_lines.append(line) # Preserve empty lines for formatting
else:
# Translate the text line
response = client(inputs={"inputs": line, "parameters": {"target_language": target_language}})
translated_lines.append(response[0]["generated_text"])
# Join the translated lines back into a single string
translated_srt_content = "\n".join(translated_lines)
return translated_srt_content
# Gradio interface
iface = gr.Interface(
fn=translate_srt,
inputs=[gr.File(label="Upload SRT File"), gr.Dropdown(["fr", "en", "es", "de", "it", "pt"], label="Target Language")],
outputs="text",
title="SRT File Translator",
description="Translate SRT files to the selected language using Mixtral model."
)
# Launch the Gradio app
iface.launch()