Artificial-superintelligence commited on
Commit
04676e1
·
verified ·
1 Parent(s): 8eab835

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -24
app.py CHANGED
@@ -1,14 +1,17 @@
1
  import streamlit as st
2
  from moviepy.editor import VideoFileClip
3
  import whisper
4
- from googletrans import Translator
5
  from gtts import gTTS
6
  import tempfile
7
  import os
8
  import numpy as np
9
 
10
  # Initialize Whisper model
11
- whisper_model = whisper.load_model("base")
 
 
 
12
 
13
  # Language options
14
  LANGUAGES = {
@@ -44,22 +47,28 @@ if video_file:
44
  os.remove(temp_video_path)
45
  st.stop()
46
 
47
- # Transcribe audio using Whisper in chunks
48
- try:
49
- # Load the audio file with Whisper
50
- audio = whisper.load_audio(audio_path)
51
- audio_segments = split_audio(audio, segment_length=30) # Split into 30-second segments
 
 
 
 
 
 
52
 
53
- original_text = ""
54
- for segment in audio_segments:
55
- result = whisper_model.transcribe(segment)
56
- original_text += result["text"] + " " # Concatenate transcriptions
57
 
58
- st.write("Original Transcription:", original_text.strip())
 
 
 
59
 
60
  # Translate text to the target language
61
- translator = Translator()
62
- translated_text = translator.translate(original_text.strip(), dest=LANGUAGES[target_language]).text
63
  st.write(f"Translated Text ({target_language}):", translated_text)
64
 
65
  # Convert translated text to speech
@@ -77,13 +86,3 @@ if video_file:
77
  os.remove(temp_video_path)
78
  os.remove(audio_path)
79
  os.remove(audio_output_path)
80
-
81
- def split_audio(audio, segment_length=30):
82
- """Split audio into segments of specified length in seconds."""
83
- total_length = audio.shape[1] # Total length in seconds
84
- segments = []
85
- for start in range(0, total_length, segment_length):
86
- end = min(start + segment_length, total_length)
87
- segment = audio[:, start:end] # Append the segment
88
- segments.append(segment)
89
- return segments
 
1
  import streamlit as st
2
  from moviepy.editor import VideoFileClip
3
  import whisper
4
+ from translate import Translator
5
  from gtts import gTTS
6
  import tempfile
7
  import os
8
  import numpy as np
9
 
10
  # Initialize Whisper model
11
+ try:
12
+ whisper_model = whisper.load_model("base") # Ensure the model is installed from the correct Whisper library
13
+ except AttributeError:
14
+ st.error("Whisper model could not be loaded. Ensure that Whisper is installed from GitHub.")
15
 
16
  # Language options
17
  LANGUAGES = {
 
47
  os.remove(temp_video_path)
48
  st.stop()
49
 
50
+ # Function to transcribe audio in chunks
51
+ def transcribe_audio_in_chunks(audio_path, model, chunk_length=30):
52
+ audio_clip = whisper.load_audio(audio_path)
53
+ audio_duration = whisper.get_duration(audio_clip)
54
+ segments = []
55
+
56
+ for start in np.arange(0, audio_duration, chunk_length):
57
+ end = min(start + chunk_length, audio_duration)
58
+ segment = audio_clip[int(start * 16000):int(end * 16000)] # Convert to the right format
59
+ result = model.transcribe(segment)
60
+ segments.append(result['text'])
61
 
62
+ return ' '.join(segments)
 
 
 
63
 
64
+ # Transcribe audio using Whisper
65
+ try:
66
+ original_text = transcribe_audio_in_chunks(audio_path, whisper_model)
67
+ st.write("Original Transcription:", original_text)
68
 
69
  # Translate text to the target language
70
+ translator = Translator(to_lang=LANGUAGES[target_language])
71
+ translated_text = translator.translate(original_text)
72
  st.write(f"Translated Text ({target_language}):", translated_text)
73
 
74
  # Convert translated text to speech
 
86
  os.remove(temp_video_path)
87
  os.remove(audio_path)
88
  os.remove(audio_output_path)