Baghdad99 commited on
Commit
9829b9c
1 Parent(s): 7e87039

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -4,7 +4,7 @@ import soundfile as sf
4
  import numpy as np
5
  import tempfile
6
  from pydub import AudioSegment
7
- import io # Add this line
8
 
9
  # Define the Hugging Face Inference API URLs and headers
10
  ASR_API_URL = "https://api-inference.huggingface.co/models/Baghdad99/saad-speech-recognition-hausa-audio-to-text"
@@ -29,8 +29,13 @@ def translate_speech(audio):
29
  sf.write(f, audio_data, sample_rate)
30
  audio_file = f.name
31
 
 
 
 
 
 
32
  # Use the ASR pipeline to transcribe the audio
33
- with open(audio_file, "rb") as f:
34
  data = f.read()
35
  response = requests.post(ASR_API_URL, headers=headers, data=data)
36
  output = response.json()
@@ -50,8 +55,7 @@ def translate_speech(audio):
50
  audio_bytes = response.content
51
 
52
  # Convert the audio bytes to an audio segment
53
- audio_segment = AudioSegment.from_mp3(io.BytesIO(audio_bytes))
54
-
55
 
56
  # Convert the audio segment to a numpy array
57
  audio_data = np.array(audio_segment.get_array_of_samples())
 
4
  import numpy as np
5
  import tempfile
6
  from pydub import AudioSegment
7
+ import io
8
 
9
  # Define the Hugging Face Inference API URLs and headers
10
  ASR_API_URL = "https://api-inference.huggingface.co/models/Baghdad99/saad-speech-recognition-hausa-audio-to-text"
 
29
  sf.write(f, audio_data, sample_rate)
30
  audio_file = f.name
31
 
32
+ # Convert the WAV file to MP3
33
+ audio_segment = AudioSegment.from_wav(audio_file)
34
+ mp3_file = audio_file.replace(".wav", ".mp3")
35
+ audio_segment.export(mp3_file, format="mp3")
36
+
37
  # Use the ASR pipeline to transcribe the audio
38
+ with open(mp3_file, "rb") as f: # Change this line
39
  data = f.read()
40
  response = requests.post(ASR_API_URL, headers=headers, data=data)
41
  output = response.json()
 
55
  audio_bytes = response.content
56
 
57
  # Convert the audio bytes to an audio segment
58
+ audio_segment = AudioSegment.from_mp3(io.BytesIO(audio_bytes)) # Change this line
 
59
 
60
  # Convert the audio segment to a numpy array
61
  audio_data = np.array(audio_segment.get_array_of_samples())