frogcho123 commited on
Commit
e6cfad1
1 Parent(s): 97ed968

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -6
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import gradio as gr
2
- import os
3
  import whisper
4
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
5
  from gtts import gTTS
@@ -14,7 +13,7 @@ def translate_audio(input_file, target_language):
14
  temp_audio_path = temp_audio.name
15
 
16
  # Auto to text (STT)
17
- model = whisper.load_model("base")
18
  audio = whisper.load_audio(temp_audio_path)
19
  audio = whisper.pad_or_trim(audio)
20
  mel = whisper.log_mel_spectrogram(audio).to(model.device)
@@ -27,7 +26,8 @@ def translate_audio(input_file, target_language):
27
  # Translate
28
  tokenizer = AutoTokenizer.from_pretrained("alirezamsh/small100")
29
  model = AutoModelForSeq2SeqLM.from_pretrained("alirezamsh/small100")
30
- tokenizer.src_lang = target_language
 
31
  encoded_bg = tokenizer(text, return_tensors="pt")
32
  generated_tokens = model.generate(**encoded_bg)
33
  translated_text = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
@@ -41,12 +41,12 @@ def translate_audio(input_file, target_language):
41
 
42
  # Define Gradio interface
43
  inputs = [
44
- gr.inputs.File(label="Upload Audio File"),
45
- gr.inputs.Dropdown(choices=['en', 'es', 'fr', 'de', 'ru'], label="Target Language")
46
  ]
47
 
48
  outputs = [
49
- gr.outputs.File(label="Translated Audio")
50
  ]
51
 
52
  title = "Audio Translation"
 
1
  import gradio as gr
 
2
  import whisper
3
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
  from gtts import gTTS
 
13
  temp_audio_path = temp_audio.name
14
 
15
  # Auto to text (STT)
16
+ model = whisper.Whisper("base")
17
  audio = whisper.load_audio(temp_audio_path)
18
  audio = whisper.pad_or_trim(audio)
19
  mel = whisper.log_mel_spectrogram(audio).to(model.device)
 
26
  # Translate
27
  tokenizer = AutoTokenizer.from_pretrained("alirezamsh/small100")
28
  model = AutoModelForSeq2SeqLM.from_pretrained("alirezamsh/small100")
29
+ tokenizer.src_lang = lang
30
+ tokenizer.tgt_lang = target_language
31
  encoded_bg = tokenizer(text, return_tensors="pt")
32
  generated_tokens = model.generate(**encoded_bg)
33
  translated_text = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
 
41
 
42
  # Define Gradio interface
43
  inputs = [
44
+ gr.inputs.File(label="Upload Audio File"),
45
+ gr.inputs.Dropdown(choices=['en', 'es', 'fr', 'de', 'ru'], label="Target Language")
46
  ]
47
 
48
  outputs = [
49
+ gr.outputs.File(label="Translated Audio")
50
  ]
51
 
52
  title = "Audio Translation"