Artificial-superintelligence commited on
Commit
157a5b0
·
verified ·
1 Parent(s): 4ff1681

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -3
app.py CHANGED
@@ -9,7 +9,7 @@ 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 Exception as e:
14
  st.error(f"Error loading Whisper model: {e}")
15
 
@@ -55,12 +55,31 @@ if video_file:
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 * whisper.audio.SAMPLE_RATE):int(end * whisper.audio.SAMPLE_RATE)] # 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)
@@ -68,7 +87,7 @@ if video_file:
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
 
9
 
10
  # Initialize Whisper model
11
  try:
12
+ whisper_model = whisper.load_model("base")
13
  except Exception as e:
14
  st.error(f"Error loading Whisper model: {e}")
15
 
 
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 * whisper.audio.SAMPLE_RATE):int(end * whisper.audio.SAMPLE_RATE)]
59
  result = model.transcribe(segment)
60
  segments.append(result['text'])
61
 
62
  return ' '.join(segments)
63
 
64
+ # Function to translate text in chunks
65
+ def translate_in_chunks(text, translator, max_length=500):
66
+ words = text.split()
67
+ chunks = []
68
+ current_chunk = ""
69
+
70
+ for word in words:
71
+ if len(current_chunk) + len(word) + 1 <= max_length:
72
+ current_chunk += " " + word if current_chunk else word
73
+ else:
74
+ chunks.append(current_chunk)
75
+ current_chunk = word
76
+
77
+ if current_chunk:
78
+ chunks.append(current_chunk)
79
+
80
+ translated_chunks = [translator.translate(chunk) for chunk in chunks]
81
+ return ' '.join(translated_chunks)
82
+
83
  # Transcribe audio using Whisper
84
  try:
85
  original_text = transcribe_audio_in_chunks(audio_path, whisper_model)
 
87
 
88
  # Translate text to the target language
89
  translator = Translator(to_lang=LANGUAGES[target_language])
90
+ translated_text = translate_in_chunks(original_text, translator)
91
  st.write(f"Translated Text ({target_language}):", translated_text)
92
 
93
  # Convert translated text to speech