SohomToom's picture
Create app.py
68a7dd2 verified
import gradio as gr
from deep_translator import GoogleTranslator
import os
def translate_srt_file(file):
with open(file.name, "r", encoding="utf-8") as f:
content = f.read()
blocks = content.strip().split("\n\n")
translated_blocks = []
for block in blocks:
lines = block.strip().split("\n")
if len(lines) < 3:
continue
index = lines[0]
timecode = lines[1]
text_lines = lines[2:]
original_text = " ".join(text_lines)
try:
translated_text = GoogleTranslator(source='en', target='bn').translate(original_text)
except Exception as e:
translated_text = original_text
translated_block = f"{index}\n{timecode}\n{translated_text}"
translated_blocks.append(translated_block)
output_path = "translated_bn.srt"
with open(output_path, "w", encoding="utf-8") as f:
f.write("\n\n".join(translated_blocks))
return output_path
title = "English ➜ Bengali Subtitle Translator"
description = "Upload an `.srt` subtitle file in English. This app will return a translated Bengali version."
gr.Interface(
fn=translate_srt_file,
inputs=gr.File(file_types=[".srt"], label="Upload English .srt file"),
outputs=gr.File(label="Download Bengali .srt file"),
title=title,
description=description,
).launch()