Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,36 +3,33 @@ import requests
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
def send_audio_to_laravel(audio):
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
if
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 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 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 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"),
|
| 33 |
outputs="text",
|
| 34 |
title="Speech Translation",
|
| 35 |
-
description="Record speech and send it
|
| 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()
|