Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import re
|
| 3 |
-
from transformers import
|
| 4 |
-
import torch
|
| 5 |
|
| 6 |
# -------------------------
|
| 7 |
-
#
|
| 8 |
# -------------------------
|
| 9 |
-
MODEL_NAME = "
|
| 10 |
|
| 11 |
-
tokenizer =
|
| 12 |
|
| 13 |
-
model =
|
| 14 |
-
MODEL_NAME
|
| 15 |
-
)
|
| 16 |
|
| 17 |
# -------------------------
|
| 18 |
# Parse SRT
|
|
@@ -42,76 +39,78 @@ def parse_srt(srt_text):
|
|
| 42 |
|
| 43 |
|
| 44 |
# -------------------------
|
| 45 |
-
# Shorten
|
| 46 |
# -------------------------
|
| 47 |
def shorten_text(text, max_len):
|
| 48 |
|
|
|
|
|
|
|
|
|
|
| 49 |
words = text.split()
|
| 50 |
|
| 51 |
while len(text) > max_len and len(words) > 1:
|
| 52 |
-
words.pop(
|
| 53 |
text = " ".join(words)
|
| 54 |
|
| 55 |
return text
|
| 56 |
|
| 57 |
|
| 58 |
# -------------------------
|
| 59 |
-
#
|
| 60 |
# -------------------------
|
| 61 |
-
def
|
| 62 |
-
|
| 63 |
-
tokenizer.src_lang = "eng_Latn"
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
return_tensors="pt"
|
|
|
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
**
|
| 72 |
-
|
| 73 |
-
max_length=128
|
| 74 |
)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
skip_special_tokens=True
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
hindi = hindi.strip()
|
| 82 |
-
|
| 83 |
-
# Length control
|
| 84 |
-
if len(hindi) > max_len:
|
| 85 |
-
hindi = shorten_text(hindi, max_len)
|
| 86 |
|
| 87 |
-
return
|
| 88 |
|
| 89 |
|
| 90 |
# -------------------------
|
| 91 |
-
# Main
|
| 92 |
# -------------------------
|
| 93 |
def translate_srt(srt_text):
|
| 94 |
|
| 95 |
subtitles = parse_srt(srt_text)
|
| 96 |
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
|
| 101 |
-
|
| 102 |
|
| 103 |
-
english_len = len(
|
| 104 |
|
| 105 |
-
#
|
| 106 |
-
max_hindi_len = int(
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
hindi
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
)
|
| 113 |
-
except:
|
| 114 |
-
hindi = english
|
| 115 |
|
| 116 |
block = (
|
| 117 |
f'{sub["index"]}\n'
|
|
@@ -125,7 +124,7 @@ def translate_srt(srt_text):
|
|
| 125 |
|
| 126 |
|
| 127 |
# -------------------------
|
| 128 |
-
#
|
| 129 |
# -------------------------
|
| 130 |
demo = gr.Interface(
|
| 131 |
fn=translate_srt,
|
|
@@ -137,8 +136,8 @@ demo = gr.Interface(
|
|
| 137 |
lines=20,
|
| 138 |
label="Hindi SRT"
|
| 139 |
),
|
| 140 |
-
title="English → Hindi SRT Translator",
|
| 141 |
-
description="
|
| 142 |
)
|
| 143 |
|
| 144 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import re
|
| 3 |
+
from transformers import MarianMTModel, MarianTokenizer
|
|
|
|
| 4 |
|
| 5 |
# -------------------------
|
| 6 |
+
# Fast Model
|
| 7 |
# -------------------------
|
| 8 |
+
MODEL_NAME = "Helsinki-NLP/opus-mt-en-hi"
|
| 9 |
|
| 10 |
+
tokenizer = MarianTokenizer.from_pretrained(MODEL_NAME)
|
| 11 |
|
| 12 |
+
model = MarianMTModel.from_pretrained(MODEL_NAME)
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# -------------------------
|
| 15 |
# Parse SRT
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
# -------------------------
|
| 42 |
+
# Shorten Hindi
|
| 43 |
# -------------------------
|
| 44 |
def shorten_text(text, max_len):
|
| 45 |
|
| 46 |
+
if len(text) <= max_len:
|
| 47 |
+
return text
|
| 48 |
+
|
| 49 |
words = text.split()
|
| 50 |
|
| 51 |
while len(text) > max_len and len(words) > 1:
|
| 52 |
+
words.pop()
|
| 53 |
text = " ".join(words)
|
| 54 |
|
| 55 |
return text
|
| 56 |
|
| 57 |
|
| 58 |
# -------------------------
|
| 59 |
+
# Batch Translate
|
| 60 |
# -------------------------
|
| 61 |
+
def batch_translate(texts):
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
inputs = tokenizer(
|
| 64 |
+
texts,
|
| 65 |
+
return_tensors="pt",
|
| 66 |
+
padding=True,
|
| 67 |
+
truncation=True
|
| 68 |
)
|
| 69 |
|
| 70 |
+
translated = model.generate(
|
| 71 |
+
**inputs,
|
| 72 |
+
max_new_tokens=64
|
|
|
|
| 73 |
)
|
| 74 |
|
| 75 |
+
outputs = tokenizer.batch_decode(
|
| 76 |
+
translated,
|
| 77 |
skip_special_tokens=True
|
| 78 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
return outputs
|
| 81 |
|
| 82 |
|
| 83 |
# -------------------------
|
| 84 |
+
# Main Function
|
| 85 |
# -------------------------
|
| 86 |
def translate_srt(srt_text):
|
| 87 |
|
| 88 |
subtitles = parse_srt(srt_text)
|
| 89 |
|
| 90 |
+
english_texts = [
|
| 91 |
+
sub["text"] for sub in subtitles
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
# FAST batch translation
|
| 95 |
+
hindi_texts = batch_translate(
|
| 96 |
+
english_texts
|
| 97 |
+
)
|
| 98 |
|
| 99 |
+
output = []
|
| 100 |
|
| 101 |
+
for sub, hindi in zip(subtitles, hindi_texts):
|
| 102 |
|
| 103 |
+
english_len = len(sub["text"])
|
| 104 |
|
| 105 |
+
# 130% rule
|
| 106 |
+
max_hindi_len = int(
|
| 107 |
+
english_len * 1.3
|
| 108 |
+
)
|
| 109 |
|
| 110 |
+
hindi = shorten_text(
|
| 111 |
+
hindi.strip(),
|
| 112 |
+
max_hindi_len
|
| 113 |
+
)
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
block = (
|
| 116 |
f'{sub["index"]}\n'
|
|
|
|
| 124 |
|
| 125 |
|
| 126 |
# -------------------------
|
| 127 |
+
# UI
|
| 128 |
# -------------------------
|
| 129 |
demo = gr.Interface(
|
| 130 |
fn=translate_srt,
|
|
|
|
| 136 |
lines=20,
|
| 137 |
label="Hindi SRT"
|
| 138 |
),
|
| 139 |
+
title="Fast English → Hindi SRT Translator",
|
| 140 |
+
description="Ultra-fast subtitle translation with timestamp preservation and subtitle length control."
|
| 141 |
)
|
| 142 |
|
| 143 |
demo.launch()
|