Spaces:
Runtime error
Runtime error
with NamedTemporaryFile(delete=False) as tmp_file:
Browse files
main.py
CHANGED
|
@@ -2,6 +2,7 @@ from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
import requests
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
@@ -34,22 +35,23 @@ async def score_audio_plus(
|
|
| 34 |
url = "https://api.elsanow.io/api/v1/score_audio_plus"
|
| 35 |
headers = {"Authorization": f"ELSA {ELSA_API_TOKEN}"}
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
with
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
print(response)
|
| 53 |
|
| 54 |
if response.status_code == 200:
|
| 55 |
# Successfully made the request
|
|
|
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
import requests
|
| 4 |
import os
|
| 5 |
+
from tempfile import NamedTemporaryFile
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
|
|
| 35 |
url = "https://api.elsanow.io/api/v1/score_audio_plus"
|
| 36 |
headers = {"Authorization": f"ELSA {ELSA_API_TOKEN}"}
|
| 37 |
|
| 38 |
+
# Write file content to a temporary file
|
| 39 |
+
with NamedTemporaryFile(delete=False) as tmp_file:
|
| 40 |
+
tmp_file.write(await audio.read())
|
| 41 |
+
tmp_file_name = tmp_file.name
|
| 42 |
+
|
| 43 |
+
files = {
|
| 44 |
+
'audio_file': (os.path.basename(audio.filename), open(tmp_file_name, "rb"), 'audio/wav')
|
| 45 |
+
}
|
| 46 |
+
data = {
|
| 47 |
+
'api_plan': api_plan,
|
| 48 |
+
'return_json': "true" if return_json else "false"
|
| 49 |
+
}
|
| 50 |
|
| 51 |
+
# Send POST request
|
| 52 |
+
response = requests.post(url, files=files, data=data, headers=headers)
|
|
|
|
| 53 |
|
| 54 |
+
os.unlink(tmp_file_name) # Remove the temporary file
|
|
|
|
| 55 |
|
| 56 |
if response.status_code == 200:
|
| 57 |
# Successfully made the request
|