Jaward commited on
Commit
ee60cd3
1 Parent(s): f466968

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -36
app.py CHANGED
@@ -12,42 +12,45 @@ LANGUAGE_CODES = {
12
  "Chinese": "cmn"
13
  }
14
 
15
- def translate_speech(audio_file, target_language):
16
- """
17
- Translate input speech (audio file) to the specified target language.
18
-
19
- Args:
20
- audio_file (str): Path to the input audio file.
21
- target_language (str): The target language for translation.
22
-
23
- Returns:
24
- str: Path to the translated audio file.
25
- """
26
- language_code = LANGUAGE_CODES[target_language]
27
- output_file = "translated_audio.wav"
28
-
29
- command = [
30
- "expressivity_predict",
31
- audio_file,
32
- "--tgt_lang", language_code,
33
- "--model_name", "seamless_expressivity",
34
- "--vocoder_name", "vocoder_pretssel",
35
- "--gated-model-dir", "seamlessmodel",
36
- "--output_path", output_file
37
- ]
38
 
39
- subprocess.run(command, check=True)
 
 
 
 
40
 
41
- if os.path.exists(output_file):
42
- print(f"File created successfully: {output_file}")
43
- else:
44
- print(f"File not found: {output_file}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- return output_file
 
 
 
 
 
 
47
 
48
  def create_interface():
49
- """Create and configure the Gradio interface."""
50
-
51
  inputs = [
52
  gr.Audio(label="User", sources="microphone", type="filepath", waveform_options=False),
53
  gr.Dropdown(list(LANGUAGE_CODES.keys()), label="Target Language")
@@ -56,14 +59,11 @@ def create_interface():
56
  return gr.Interface(
57
  fn=translate_speech,
58
  inputs=inputs,
59
- outputs=gr.Audio(label="Translated Audio",
60
- interactive=False,
61
- autoplay=True,
62
- elem_classes="audio"),
63
  title="Seamless Expressive Speech-To-Speech Translator",
64
  description="Hear how you sound in another language.",
65
  )
66
 
67
- if name == "main":
68
  iface = create_interface()
69
  iface.launch()
 
12
  "Chinese": "cmn"
13
  }
14
 
15
+ def transcribe(audio):
16
+ if audio is None:
17
+ return "No audio input detected. Please record or upload an audio file."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ try:
20
+ text = model.stt_file(audio)[0]
21
+ return text
22
+ except Exception as e:
23
+ return f"Error transcribing audio: {str(e)}"
24
 
25
+ def translate_speech(audio_file, target_language):
26
+ if audio_file is None:
27
+ return "No audio input detected. Please record or upload an audio file."
28
+
29
+ try:
30
+ language_code = LANGUAGE_CODES[target_language]
31
+ output_file = "translated_audio.wav"
32
+
33
+ command = [
34
+ "expressivity_predict",
35
+ audio_file,
36
+ "--tgt_lang", language_code,
37
+ "--model_name", "seamless_expressivity",
38
+ "--vocoder_name", "vocoder_pretssel",
39
+ "--gated-model-dir", "seamlessmodel",
40
+ "--output_path", output_file
41
+ ]
42
+
43
+ subprocess.run(command, check=True)
44
 
45
+ if os.path.exists(output_file):
46
+ print(f"File created successfully: {output_file}")
47
+ return output_file
48
+ else:
49
+ return "Error: Translated audio file not found."
50
+ except Exception as e:
51
+ return f"Error translating speech: {str(e)}"
52
 
53
  def create_interface():
 
 
54
  inputs = [
55
  gr.Audio(label="User", sources="microphone", type="filepath", waveform_options=False),
56
  gr.Dropdown(list(LANGUAGE_CODES.keys()), label="Target Language")
 
59
  return gr.Interface(
60
  fn=translate_speech,
61
  inputs=inputs,
62
+ outputs=gr.Audio(label="Translated Audio", interactive=False, autoplay=True, elem_classes="audio"),
 
 
 
63
  title="Seamless Expressive Speech-To-Speech Translator",
64
  description="Hear how you sound in another language.",
65
  )
66
 
67
+ if __name__ == "__main__":
68
  iface = create_interface()
69
  iface.launch()