younes21000 commited on
Commit
0e83a05
·
verified ·
1 Parent(s): 38fcd18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -21
app.py CHANGED
@@ -21,33 +21,53 @@ languages = {
21
  }
22
 
23
  def generate_subtitles(video_path, language_name):
24
- # Extract the target language code from the selected language name
25
- target_language = languages[language_name]
26
-
27
- # Extract audio from video
28
- video = mp.VideoFileClip(video_path)
29
- audio = video.audio
30
- audio.write_audiofile("temp_audio.wav", codec='pcm_s16le')
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- # Convert speech to text (ASR using Whisper)
33
- with open("temp_audio.wav", "rb") as audio_file:
34
- transcription = asr(audio_file)["text"]
35
 
36
- # Translate transcription to the target language using M2M100
37
- translation_pipeline = pipeline('translation', model='facebook/m2m100_418M')
38
- translated_subtitles = translation_pipeline(
39
- transcription,
40
- forced_bos_token_id=translation_pipeline.tokenizer.get_lang_id(target_language)
41
- )[0]["translation_text"]
42
 
43
- # Return subtitles (text for now)
44
- subtitles = f"Original: {transcription}\nTranslated: {translated_subtitles}"
45
- return subtitles
 
 
 
 
 
46
 
47
  # Define Gradio interface
48
  def subtitle_video(video_file, language_name):
49
- video_path = video_file.name
50
- return generate_subtitles(video_path, language_name)
 
 
 
 
 
51
 
52
  # Gradio app layout
53
  interface = gr.Interface(
 
21
  }
22
 
23
  def generate_subtitles(video_path, language_name):
24
+ try:
25
+ # Extract the target language code from the selected language name
26
+ target_language = languages[language_name]
27
+
28
+ # Log the file path for debugging
29
+ print(f"Received video file: {video_path}")
30
+
31
+ # Extract audio from video
32
+ video = mp.VideoFileClip(video_path)
33
+ audio = video.audio
34
+ audio.write_audiofile("temp_audio.wav", codec='pcm_s16le')
35
+
36
+ # Log transcription start
37
+ print("Starting speech-to-text transcription")
38
+
39
+ # Convert speech to text (ASR using Whisper)
40
+ with open("temp_audio.wav", "rb") as audio_file:
41
+ transcription = asr(audio_file)["text"]
42
 
43
+ # Log translation start
44
+ print("Starting translation")
 
45
 
46
+ # Translate transcription to the target language using M2M100
47
+ translation_pipeline = pipeline('translation', model='facebook/m2m100_418M')
48
+ translated_subtitles = translation_pipeline(
49
+ transcription,
50
+ forced_bos_token_id=translation_pipeline.tokenizer.get_lang_id(target_language)
51
+ )[0]["translation_text"]
52
 
53
+ # Return subtitles
54
+ subtitles = f"Original: {transcription}\nTranslated: {translated_subtitles}"
55
+ return subtitles
56
+
57
+ except Exception as e:
58
+ # Catch and log the error
59
+ print(f"Error occurred: {e}")
60
+ return f"Error occurred: {e}"
61
 
62
  # Define Gradio interface
63
  def subtitle_video(video_file, language_name):
64
+ try:
65
+ video_path = video_file.name
66
+ return generate_subtitles(video_path, language_name)
67
+ except Exception as e:
68
+ # Catch and display any high-level errors
69
+ print(f"Error in processing video: {e}")
70
+ return f"Error in processing video: {e}"
71
 
72
  # Gradio app layout
73
  interface = gr.Interface(