Ra-Is commited on
Commit
5445cc3
1 Parent(s): b9995a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -22
app.py CHANGED
@@ -3,36 +3,33 @@ import requests
3
  import os
4
 
5
  def send_audio_to_laravel(audio):
6
- url = os.getenv("BASE_URL")
 
7
 
8
- # Save audio to a temporary file if `audio` is a file-like object
9
- if isinstance(audio, str):
10
- # If the audio is a file path (string), use it directly
11
- audio_path = audio
12
- else:
13
- # Handle cases where `audio` is a file-like object
14
- with open("temp_audio.wav", "wb") as f:
15
- f.write(audio.read())
16
- audio_path = "temp_audio.wav"
17
 
18
  # Prepare form data for request
19
- files = {
20
- 'file': open(audio_path, 'rb'),
21
- }
22
- data = {
23
- 'lang': 'english-twi'
24
- }
25
-
26
- response = requests.post(url, files=files, data=data)
27
- return response.json()
28
 
29
  # Gradio interface for recording speech
30
  iface = gr.Interface(
31
  fn=send_audio_to_laravel,
32
- inputs=gr.Audio(type="filepath"), # Expect a file path as input
33
  outputs="text",
34
  title="Speech Translation",
35
- description="Record speech and send it to Laravel for processing.",
36
  )
37
 
38
- iface.launch()
 
3
  import os
4
 
5
  def send_audio_to_laravel(audio):
6
+ if audio is None:
7
+ return "No audio recorded. Please try again."
8
 
9
+ url = os.getenv("BASE_URL")
10
+ if not url:
11
+ return "BASE_URL environment variable is not set."
12
+
13
+ # audio is a tuple (file_path, sampling_rate) or None
14
+ audio_path, sampling_rate = audio
 
 
 
15
 
16
  # Prepare form data for request
17
+ try:
18
+ with open(audio_path, 'rb') as audio_file:
19
+ files = {'file': audio_file}
20
+ data = {'lang': 'english-twi'}
21
+ response = requests.post(url, files=files, data=data)
22
+ return response.json()
23
+ except Exception as e:
24
+ return f"An error occurred: {str(e)}"
 
25
 
26
  # Gradio interface for recording speech
27
  iface = gr.Interface(
28
  fn=send_audio_to_laravel,
29
+ inputs=gr.Audio(type="filepath"),
30
  outputs="text",
31
  title="Speech Translation",
32
+ description="Record speech and send it for processing.",
33
  )
34
 
35
+ iface.launch()