frogcho123 commited on
Commit
b480da2
·
1 Parent(s): 34bfd6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -8,13 +8,14 @@ from tempfile import NamedTemporaryFile
8
  # Define translation function
9
  def translate_audio(input_file, target_language):
10
  # Save uploaded audio file to a temporary file
11
- with NamedTemporaryFile(suffix=".mp3") as temp_audio:
12
  temp_audio.write(input_file.read())
13
  temp_audio.seek(0)
 
14
 
15
  # Auto to text (STT)
16
  model = whisper.load_model("base")
17
- audio = whisper.load_audio(temp_audio.name)
18
  audio = whisper.pad_or_trim(audio)
19
  mel = whisper.log_mel_spectrogram(audio).to(model.device)
20
  _, probs = model.detect_language(mel)
@@ -40,15 +41,15 @@ def translate_audio(input_file, target_language):
40
 
41
  # Define Gradio interface
42
  inputs = [
43
- gr.File(label="Upload Audio File"),
44
- gr.Dropdown(choices=['en', 'es', 'fr', 'de', 'ru'], label="Target Language")
45
  ]
46
 
47
  outputs = [
48
- gr.File(label="Translated Audio")
49
  ]
50
 
51
  title = "Audio Translation"
52
  description = "Upload an audio file, translate the speech to a target language, and download the translated audio."
53
 
54
- gr.Interface(fn=translate_audio, inputs=inputs, outputs=outputs, title=title, description=description).launch()
 
8
  # Define translation function
9
  def translate_audio(input_file, target_language):
10
  # Save uploaded audio file to a temporary file
11
+ with NamedTemporaryFile(suffix=".mp3", dir=".", delete=False) as temp_audio:
12
  temp_audio.write(input_file.read())
13
  temp_audio.seek(0)
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)
21
  _, probs = model.detect_language(mel)
 
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"
53
  description = "Upload an audio file, translate the speech to a target language, and download the translated audio."
54
 
55
+ gr.Interface(fn=translate_audio, inputs=inputs, outputs=outputs, title=title, description=description).launch()