Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
# Translation model
|
| 6 |
+
translator = pipeline(
|
| 7 |
+
"translation",
|
| 8 |
+
model="google/translategemma-2b-it"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
# -----------------------------
|
| 12 |
+
# SRT PARSER
|
| 13 |
+
# -----------------------------
|
| 14 |
+
def parse_srt(srt_text):
|
| 15 |
+
pattern = re.compile(
|
| 16 |
+
r"(\d+)\s*\n"
|
| 17 |
+
r"(\d{2}:\d{2}:\d{2},\d{3}\s-->\s\d{2}:\d{2}:\d{2},\d{3})\s*\n"
|
| 18 |
+
r"(.*?)(?=\n\d+\n|\Z)",
|
| 19 |
+
re.DOTALL
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
matches = pattern.findall(srt_text.strip())
|
| 23 |
+
|
| 24 |
+
subtitles = []
|
| 25 |
+
|
| 26 |
+
for idx, timestamp, text in matches:
|
| 27 |
+
clean_text = text.replace("\n", " ").strip()
|
| 28 |
+
|
| 29 |
+
subtitles.append({
|
| 30 |
+
"index": idx,
|
| 31 |
+
"timestamp": timestamp,
|
| 32 |
+
"text": clean_text
|
| 33 |
+
})
|
| 34 |
+
|
| 35 |
+
return subtitles
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# -----------------------------
|
| 39 |
+
# SHORTEN HINDI TEXT
|
| 40 |
+
# -----------------------------
|
| 41 |
+
def shorten_text(text, max_len):
|
| 42 |
+
words = text.split()
|
| 43 |
+
|
| 44 |
+
while len(text) > max_len and len(words) > 1:
|
| 45 |
+
words.pop(-1)
|
| 46 |
+
text = " ".join(words)
|
| 47 |
+
|
| 48 |
+
return text
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# -----------------------------
|
| 52 |
+
# TRANSLATE SRT
|
| 53 |
+
# -----------------------------
|
| 54 |
+
def translate_srt(srt_text):
|
| 55 |
+
|
| 56 |
+
subtitles = parse_srt(srt_text)
|
| 57 |
+
|
| 58 |
+
output = []
|
| 59 |
+
|
| 60 |
+
for sub in subtitles:
|
| 61 |
+
|
| 62 |
+
english = sub["text"]
|
| 63 |
+
|
| 64 |
+
english_len = len(english)
|
| 65 |
+
|
| 66 |
+
# Max 130%
|
| 67 |
+
max_hindi_len = int(english_len * 1.3)
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
result = translator(
|
| 71 |
+
english,
|
| 72 |
+
max_new_tokens=128
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
hindi = result[0]["translation_text"].strip()
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
hindi = english
|
| 79 |
+
|
| 80 |
+
# Length control
|
| 81 |
+
if len(hindi) > max_hindi_len:
|
| 82 |
+
hindi = shorten_text(hindi, max_hindi_len)
|
| 83 |
+
|
| 84 |
+
block = (
|
| 85 |
+
f'{sub["index"]}\n'
|
| 86 |
+
f'{sub["timestamp"]}\n'
|
| 87 |
+
f'{hindi}\n'
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
output.append(block)
|
| 91 |
+
|
| 92 |
+
return "\n".join(output)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# -----------------------------
|
| 96 |
+
# GRADIO UI
|
| 97 |
+
# -----------------------------
|
| 98 |
+
demo = gr.Interface(
|
| 99 |
+
fn=translate_srt,
|
| 100 |
+
inputs=gr.Textbox(
|
| 101 |
+
lines=20,
|
| 102 |
+
label="English SRT"
|
| 103 |
+
),
|
| 104 |
+
outputs=gr.Textbox(
|
| 105 |
+
lines=20,
|
| 106 |
+
label="Hindi SRT"
|
| 107 |
+
),
|
| 108 |
+
title="English to Hindi SRT Translator",
|
| 109 |
+
description="Translate English SRT to Hindi with subtitle length control."
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
demo.launch()
|