don0726 commited on
Commit
7b2e3f4
·
verified ·
1 Parent(s): 89ac88d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -48
app.py CHANGED
@@ -1,18 +1,15 @@
1
  import gradio as gr
2
  import re
3
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
- import torch
5
 
6
  # -------------------------
7
- # Load model
8
  # -------------------------
9
- MODEL_NAME = "facebook/nllb-200-distilled-600M"
10
 
11
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
12
 
13
- model = AutoModelForSeq2SeqLM.from_pretrained(
14
- MODEL_NAME
15
- )
16
 
17
  # -------------------------
18
  # Parse SRT
@@ -42,76 +39,78 @@ def parse_srt(srt_text):
42
 
43
 
44
  # -------------------------
45
- # Shorten text
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(-1)
53
  text = " ".join(words)
54
 
55
  return text
56
 
57
 
58
  # -------------------------
59
- # Translate line
60
  # -------------------------
61
- def translate_line(text, max_len):
62
-
63
- tokenizer.src_lang = "eng_Latn"
64
 
65
- encoded = tokenizer(
66
- text,
67
- return_tensors="pt"
 
 
68
  )
69
 
70
- generated_tokens = model.generate(
71
- **encoded,
72
- forced_bos_token_id=tokenizer.convert_tokens_to_ids("hin_Deva"),
73
- max_length=128
74
  )
75
 
76
- hindi = tokenizer.batch_decode(
77
- generated_tokens,
78
  skip_special_tokens=True
79
- )[0]
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 hindi
88
 
89
 
90
  # -------------------------
91
- # Main translation
92
  # -------------------------
93
  def translate_srt(srt_text):
94
 
95
  subtitles = parse_srt(srt_text)
96
 
97
- output = []
 
 
 
 
 
 
 
98
 
99
- for sub in subtitles:
100
 
101
- english = sub["text"]
102
 
103
- english_len = len(english)
104
 
105
- # Hindi max 130%
106
- max_hindi_len = int(english_len * 1.3)
 
 
107
 
108
- try:
109
- hindi = translate_line(
110
- english,
111
- max_hindi_len
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
- # Gradio UI
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="Translate English SRT subtitles to Hindi with timestamp preservation and subtitle length control."
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()