imorcillo commited on
Commit
b841d1b
·
verified ·
1 Parent(s): 9b706a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -12,27 +12,36 @@ def start_app():
12
  return gr.update(visible=False), gr.update(visible=True)
13
 
14
  def audio_to_bytes(audio):
15
- data, sr = sf.read(audio)
16
- audio_bytes = io.BytesIO()
17
- sf.write(audio_bytes, data, sr, format='WAV')
18
- audio_bytes.seek(0)
19
- return audio_bytes
 
 
 
 
20
 
21
  def langswitch_API_call(audio, language):
22
  audio_bytes = audio_to_bytes(audio)
23
  files = {'file': (f'audio_chunk.wav', audio_bytes, 'audio/wav')}
24
  api_url = os.getenv("api_url")
25
  response = requests.post(f"{api_url}/online/http?language={language}", files=files)
 
 
26
  if response.status_code != 200:
27
  print(response)
28
  raise Exception("API error")
29
- return "⚠️ Please wait until the audio is completely loaded."
30
  return response.json()
31
 
32
  def transcribe_base(audio, language):
33
  if not language: return "⚠️ Please select a language before transcribing."
34
- response = langswitch_API_call(audio, language)
35
- print(response)
 
 
 
 
36
  transcription = response["transcription"]
37
  is_new_speaker = response["is_new_speaker"]
38
  speaker = response["classified_speaker"]
 
12
  return gr.update(visible=False), gr.update(visible=True)
13
 
14
  def audio_to_bytes(audio):
15
+ try:
16
+ data, sr = sf.read(audio)
17
+ audio_bytes = io.BytesIO()
18
+ sf.write(audio_bytes, data, sr, format='WAV')
19
+ audio_bytes.seek(0)
20
+ return audio_bytes
21
+ except TypeError as e:
22
+ print("TypeError caught in audio_to_bytes:", e)
23
+ return None
24
 
25
  def langswitch_API_call(audio, language):
26
  audio_bytes = audio_to_bytes(audio)
27
  files = {'file': (f'audio_chunk.wav', audio_bytes, 'audio/wav')}
28
  api_url = os.getenv("api_url")
29
  response = requests.post(f"{api_url}/online/http?language={language}", files=files)
30
+ if audio_bytes is None:
31
+ raise Exception("Invalid audio input")
32
  if response.status_code != 200:
33
  print(response)
34
  raise Exception("API error")
 
35
  return response.json()
36
 
37
  def transcribe_base(audio, language):
38
  if not language: return "⚠️ Please select a language before transcribing."
39
+ try:
40
+ response = langswitch_API_call(audio, language)
41
+ print(response)
42
+ except Exception as e:
43
+ if "Invalid file: None" in str(e):
44
+ return "⚠️ Please wait for the audio to load completely."
45
  transcription = response["transcription"]
46
  is_new_speaker = response["is_new_speaker"]
47
  speaker = response["classified_speaker"]