abdullah637 commited on
Commit
cb5705d
·
verified ·
1 Parent(s): baa0168

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -12
app.py CHANGED
@@ -1,11 +1,23 @@
1
  import gradio as gr
2
  from moviepy.editor import VideoFileClip
3
  import os
4
- from googletrans import Translator
5
-
6
  import whisper
7
- model = whisper.load_model("base")
 
 
 
 
 
 
 
 
 
8
 
 
 
 
 
 
9
  TRANSLATION_LANGUAGES = {
10
  "Urdu": "ur",
11
  "French": "fr",
@@ -16,7 +28,17 @@ TRANSLATION_LANGUAGES = {
16
  "Hindi": "hi"
17
  }
18
 
19
- translator = Translator()
 
 
 
 
 
 
 
 
 
 
20
 
21
  def generate_translated_subtitles(video_path, target_language):
22
  # Extract audio from video
@@ -24,7 +46,7 @@ def generate_translated_subtitles(video_path, target_language):
24
  audio_path = "temp_audio.wav"
25
  video.audio.write_audiofile(audio_path)
26
 
27
- # Transcribe (but do NOT translate) using Whisper
28
  result = model.transcribe(audio_path, language="en")
29
 
30
  # Clean up temporary audio file
@@ -33,15 +55,14 @@ def generate_translated_subtitles(video_path, target_language):
33
  # Extract all subtitle texts for batch translation
34
  texts = [segment['text'] for segment in result['segments']]
35
 
36
- # Perform batch translation (all at once)
37
- translations = translator.translate(texts, dest=TRANSLATION_LANGUAGES[target_language])
38
 
39
  # Format subtitles in .srt format
40
  srt_content = ""
41
- for index, (segment, translation) in enumerate(zip(result['segments'], translations)):
42
  start_time = segment['start']
43
  end_time = segment['end']
44
- translated_text = translation.text
45
 
46
  # Convert seconds to SRT time format (HH:MM:SS,mmm)
47
  def format_time(seconds):
@@ -77,8 +98,8 @@ iface = gr.Interface(
77
  gr.Textbox(label="Translated Subtitles", lines=10),
78
  gr.File(label="Download Translated .srt File") # Correct file download
79
  ],
80
- title="Video to Translated Subtitles",
81
- description="Upload an English video, select a language, and get translated subtitles."
82
  )
83
 
84
- iface.launch(share=True, debug=True)
 
1
  import gradio as gr
2
  from moviepy.editor import VideoFileClip
3
  import os
 
 
4
  import whisper
5
+ import torch
6
+ from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
7
+
8
+ # Load Whisper model (use 'small' for faster transcription)
9
+ model = whisper.load_model("small")
10
+
11
+ # Load M2M-100 model & tokenizer
12
+ m2m_model_name = "facebook/m2m100_418M" # Use "facebook/m2m100_1.2B" for better accuracy
13
+ tokenizer = M2M100Tokenizer.from_pretrained(m2m_model_name)
14
+ translator_model = M2M100ForConditionalGeneration.from_pretrained(m2m_model_name)
15
 
16
+ # Move model to GPU if available
17
+ device = "cuda" if torch.cuda.is_available() else "cpu"
18
+ translator_model.to(device)
19
+
20
+ # Supported languages for translation (must match M2M-100 language codes)
21
  TRANSLATION_LANGUAGES = {
22
  "Urdu": "ur",
23
  "French": "fr",
 
28
  "Hindi": "hi"
29
  }
30
 
31
+ def translate_text_m2m(text_list, target_lang):
32
+ """ Translates a list of English texts into the target language using M2M-100. """
33
+ tokenizer.src_lang = "en"
34
+
35
+ # Tokenize and translate in batches
36
+ inputs = tokenizer(text_list, return_tensors="pt", padding=True, truncation=True).to(device)
37
+ outputs = translator_model.generate(**inputs, forced_bos_token_id=tokenizer.get_lang_id(target_lang))
38
+
39
+ # Decode output
40
+ translated_texts = tokenizer.batch_decode(outputs, skip_special_tokens=True)
41
+ return translated_texts
42
 
43
  def generate_translated_subtitles(video_path, target_language):
44
  # Extract audio from video
 
46
  audio_path = "temp_audio.wav"
47
  video.audio.write_audiofile(audio_path)
48
 
49
+ # Transcribe (without translation) using Whisper
50
  result = model.transcribe(audio_path, language="en")
51
 
52
  # Clean up temporary audio file
 
55
  # Extract all subtitle texts for batch translation
56
  texts = [segment['text'] for segment in result['segments']]
57
 
58
+ # Translate using M2M-100
59
+ translated_texts = translate_text_m2m(texts, TRANSLATION_LANGUAGES[target_language])
60
 
61
  # Format subtitles in .srt format
62
  srt_content = ""
63
+ for index, (segment, translated_text) in enumerate(zip(result['segments'], translated_texts)):
64
  start_time = segment['start']
65
  end_time = segment['end']
 
66
 
67
  # Convert seconds to SRT time format (HH:MM:SS,mmm)
68
  def format_time(seconds):
 
98
  gr.Textbox(label="Translated Subtitles", lines=10),
99
  gr.File(label="Download Translated .srt File") # Correct file download
100
  ],
101
+ title="Video to Translated Subtitles (Offline M2M-100)",
102
+ description="Upload an English video, select a language, and get translated subtitles offline."
103
  )
104
 
105
+ iface.launch(share=True)