Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,33 +3,43 @@ import requests
|
|
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("TRANSLATION_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 |
-
|
|
|
|
|
|
|
17 |
try:
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
return response.json()
|
|
|
23 |
except Exception as e:
|
24 |
-
return
|
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()
|
|
|
3 |
import os
|
4 |
|
5 |
def send_audio_to_laravel(audio):
|
|
|
|
|
|
|
6 |
url = os.getenv("TRANSLATION_URL")
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
if audio is None:
|
9 |
+
return {"error": "No audio file provided"}
|
10 |
+
|
11 |
+
# If `audio` is a file-like object
|
12 |
try:
|
13 |
+
if isinstance(audio, bytes):
|
14 |
+
# If audio is a bytes object, save it to a temporary file
|
15 |
+
with open("temp_audio.wav", "wb") as f:
|
16 |
+
f.write(audio)
|
17 |
+
audio_path = "temp_audio.wav"
|
18 |
+
else:
|
19 |
+
# If audio is already a file path
|
20 |
+
audio_path = audio
|
21 |
+
|
22 |
+
# Prepare form data for request
|
23 |
+
files = {
|
24 |
+
'file': open(audio_path, 'rb'),
|
25 |
+
}
|
26 |
+
data = {
|
27 |
+
'lang': 'english-twi'
|
28 |
+
}
|
29 |
+
|
30 |
+
response = requests.post(url, files=files, data=data)
|
31 |
return response.json()
|
32 |
+
|
33 |
except Exception as e:
|
34 |
+
return {"error": str(e)}
|
35 |
|
36 |
# Gradio interface for recording speech
|
37 |
iface = gr.Interface(
|
38 |
fn=send_audio_to_laravel,
|
39 |
+
inputs=gr.Audio(type="filepath"), # Expect a file path or file-like object
|
40 |
outputs="text",
|
41 |
title="Speech Translation",
|
42 |
+
description="Record speech and send it to Laravel for processing.",
|
43 |
)
|
44 |
|
45 |
iface.launch()
|