don0726 commited on
Commit
48e9bb2
·
verified ·
1 Parent(s): a3490b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -18
app.py CHANGED
@@ -1,13 +1,17 @@
1
  import gradio as gr
2
  import re
3
- from transformers import pipeline
 
4
 
5
  # -------------------------
6
- # Load Model
7
  # -------------------------
8
- translator = pipeline(
9
- "text2text-generation",
10
- model="facebook/nllb-200-distilled-600M"
 
 
 
11
  )
12
 
13
  # -------------------------
@@ -56,19 +60,25 @@ def shorten_text(text, max_len):
56
  # -------------------------
57
  def translate_line(text, max_len):
58
 
59
- prompt = f"""
60
- Translate this English text to spoken Hindi.
61
 
62
- English:
63
- {text}
64
- """
 
65
 
66
- result = translator(
67
- prompt,
68
- max_new_tokens=128
 
69
  )
70
 
71
- hindi = result[0]["generated_text"].strip()
 
 
 
 
 
72
 
73
  # Length control
74
  if len(hindi) > max_len:
@@ -78,7 +88,7 @@ English:
78
 
79
 
80
  # -------------------------
81
- # Main Translation
82
  # -------------------------
83
  def translate_srt(srt_text):
84
 
@@ -92,7 +102,7 @@ def translate_srt(srt_text):
92
 
93
  english_len = len(english)
94
 
95
- # Hindi can be max 130%
96
  max_hindi_len = int(english_len * 1.3)
97
 
98
  try:
@@ -115,7 +125,7 @@ def translate_srt(srt_text):
115
 
116
 
117
  # -------------------------
118
- # UI
119
  # -------------------------
120
  demo = gr.Interface(
121
  fn=translate_srt,
@@ -128,7 +138,7 @@ demo = gr.Interface(
128
  label="Hindi SRT"
129
  ),
130
  title="English → Hindi SRT Translator",
131
- description="Translate English subtitles to Hindi with timestamp preservation and length control."
132
  )
133
 
134
  demo.launch()
 
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
  # -------------------------
 
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:
 
88
 
89
 
90
  # -------------------------
91
+ # Main translation
92
  # -------------------------
93
  def translate_srt(srt_text):
94
 
 
102
 
103
  english_len = len(english)
104
 
105
+ # Hindi max 130%
106
  max_hindi_len = int(english_len * 1.3)
107
 
108
  try:
 
125
 
126
 
127
  # -------------------------
128
+ # Gradio UI
129
  # -------------------------
130
  demo = gr.Interface(
131
  fn=translate_srt,
 
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()