Baghdad99 commited on
Commit
c5fae6e
1 Parent(s): c21334d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -26
app.py CHANGED
@@ -15,13 +15,9 @@ tts = pipeline("text-to-speech", model="Baghdad99/english_voice_tts")
15
 
16
  def translate_speech(audio_input):
17
  print(f"Type of audio: {type(audio_input)}, Value of audio: {audio_input}") # Debug line
18
- # Check if the input is a tuple (recorded audio) or a string (uploaded file)
19
- if isinstance(audio_input, tuple):
20
- # Extract the audio data from the tuple
21
- sample_rate, audio_data = audio_input
22
- else:
23
- # Load the audio file as a floating point time series
24
- audio_data, sample_rate = librosa.load(audio_input, sr=None)
25
 
26
  # Normalize the audio data to the range [-1, 1]
27
  audio_data_normalized = audio_data / np.iinfo(audio_data.dtype).max
@@ -29,24 +25,20 @@ def translate_speech(audio_input):
29
  # Convert the normalized audio data to float64
30
  audio_data_float64 = audio_data_normalized.astype(np.float64)
31
 
32
- # Use the speech recognition pipeline to transcribe the audio
33
- output = pipe(audio_data_float64)
34
 
35
- print(f"Output: {output}") # Print the output to see what it contains
 
36
 
37
- # Check if the output contains 'text'
38
- if 'text' in output:
39
- transcription = output["text"]
40
- else:
41
- print("The output does not contain 'text'")
42
- return
43
 
44
- # Print the transcription
45
- print(f"Transcription: {transcription}")
46
 
47
  # Use the translation pipeline to translate the transcription
48
  translated_text = translator(transcription, return_tensors="pt")
49
- print(f"Translated text: {translated_text}") # Print the translated text to see what it contains
50
 
51
  # Check if the translated text contains 'generated_token_ids'
52
  if 'generated_token_ids' in translated_text[0]:
@@ -56,12 +48,8 @@ def translate_speech(audio_input):
56
  print("The translated text does not contain 'generated_token_ids'")
57
  return
58
 
59
- # Print the translated text string
60
- print(f"Translated text string: {translated_text_str}")
61
-
62
  # Use the text-to-speech pipeline to synthesize the translated text
63
  synthesised_speech = tts(translated_text_str)
64
- print(f"Synthesised speech: {synthesised_speech}") # Print the synthesised speech to see what it contains
65
 
66
  # Check if the synthesised speech contains 'audio'
67
  if 'audio' in synthesised_speech:
@@ -73,14 +61,12 @@ def translate_speech(audio_input):
73
  # Flatten the audio data
74
  synthesised_speech_data = synthesised_speech_data.flatten()
75
 
76
- # Print the shape and type of the synthesised speech data
77
- print(f"Synthesised speech data type: {type(synthesised_speech_data)}, Synthesised speech data shape: {synthesised_speech_data.shape}")
78
-
79
  # Scale the audio data to the range of int16 format
80
  synthesised_speech = (synthesised_speech_data * 32767).astype(np.int16)
81
 
82
  return 16000, synthesised_speech
83
 
 
84
  # Define the Gradio interface
85
  iface = gr.Interface(
86
  fn=translate_speech,
 
15
 
16
  def translate_speech(audio_input):
17
  print(f"Type of audio: {type(audio_input)}, Value of audio: {audio_input}") # Debug line
18
+ def translate_speech(audio_input):
19
+ # Load the audio file as a floating point time series
20
+ audio_data, sample_rate = librosa.load(audio_input, sr=None)
 
 
 
 
21
 
22
  # Normalize the audio data to the range [-1, 1]
23
  audio_data_normalized = audio_data / np.iinfo(audio_data.dtype).max
 
25
  # Convert the normalized audio data to float64
26
  audio_data_float64 = audio_data_normalized.astype(np.float64)
27
 
28
+ # Prepare the input dictionary
29
+ input_dict = pipe.tokenizer(audio_data_float64, return_tensors="pt", padding=True)
30
 
31
+ # Use the speech recognition model to get the logits
32
+ logits = pipe.model(input_dict.input_values.to("cuda")).logits
33
 
34
+ # Get the predicted IDs
35
+ pred_ids = torch.argmax(logits, dim=-1)[0]
 
 
 
 
36
 
37
+ # Decode the predicted IDs to get the transcription
38
+ transcription = pipe.tokenizer.decode(pred_ids)
39
 
40
  # Use the translation pipeline to translate the transcription
41
  translated_text = translator(transcription, return_tensors="pt")
 
42
 
43
  # Check if the translated text contains 'generated_token_ids'
44
  if 'generated_token_ids' in translated_text[0]:
 
48
  print("The translated text does not contain 'generated_token_ids'")
49
  return
50
 
 
 
 
51
  # Use the text-to-speech pipeline to synthesize the translated text
52
  synthesised_speech = tts(translated_text_str)
 
53
 
54
  # Check if the synthesised speech contains 'audio'
55
  if 'audio' in synthesised_speech:
 
61
  # Flatten the audio data
62
  synthesised_speech_data = synthesised_speech_data.flatten()
63
 
 
 
 
64
  # Scale the audio data to the range of int16 format
65
  synthesised_speech = (synthesised_speech_data * 32767).astype(np.int16)
66
 
67
  return 16000, synthesised_speech
68
 
69
+
70
  # Define the Gradio interface
71
  iface = gr.Interface(
72
  fn=translate_speech,