Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from deep_translator import GoogleTranslator
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def translate_srt_file(file):
|
| 6 |
+
with open(file.name, "r", encoding="utf-8") as f:
|
| 7 |
+
content = f.read()
|
| 8 |
+
|
| 9 |
+
blocks = content.strip().split("\n\n")
|
| 10 |
+
translated_blocks = []
|
| 11 |
+
|
| 12 |
+
for block in blocks:
|
| 13 |
+
lines = block.strip().split("\n")
|
| 14 |
+
if len(lines) < 3:
|
| 15 |
+
continue
|
| 16 |
+
index = lines[0]
|
| 17 |
+
timecode = lines[1]
|
| 18 |
+
text_lines = lines[2:]
|
| 19 |
+
|
| 20 |
+
original_text = " ".join(text_lines)
|
| 21 |
+
try:
|
| 22 |
+
translated_text = GoogleTranslator(source='en', target='bn').translate(original_text)
|
| 23 |
+
except Exception as e:
|
| 24 |
+
translated_text = original_text
|
| 25 |
+
|
| 26 |
+
translated_block = f"{index}\n{timecode}\n{translated_text}"
|
| 27 |
+
translated_blocks.append(translated_block)
|
| 28 |
+
|
| 29 |
+
output_path = "translated_bn.srt"
|
| 30 |
+
with open(output_path, "w", encoding="utf-8") as f:
|
| 31 |
+
f.write("\n\n".join(translated_blocks))
|
| 32 |
+
|
| 33 |
+
return output_path
|
| 34 |
+
|
| 35 |
+
title = "English ➜ Bengali Subtitle Translator"
|
| 36 |
+
description = "Upload an `.srt` subtitle file in English. This app will return a translated Bengali version."
|
| 37 |
+
|
| 38 |
+
gr.Interface(
|
| 39 |
+
fn=translate_srt_file,
|
| 40 |
+
inputs=gr.File(file_types=[".srt"], label="Upload English .srt file"),
|
| 41 |
+
outputs=gr.File(label="Download Bengali .srt file"),
|
| 42 |
+
title=title,
|
| 43 |
+
description=description,
|
| 44 |
+
).launch()
|