Spaces:
Runtime error
Runtime error
File size: 2,028 Bytes
eeda580 94ee50b bab4ecf eeda580 bab4ecf eeda580 94ee50b 3ebeb3e 94ee50b eeda580 94ee50b 4bd8415 eeda580 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
from fastapi.responses import JSONResponse
import requests
import os
from tempfile import NamedTemporaryFile
app = FastAPI()
PASSWORD = os.getenv("PASSWORD") # Read password from environment variable
ELSA_API_TOKEN = os.getenv("ELSA_API_TOKEN") # Make sure to set this environment variable
def verify_password(input_password: str):
return input_password == PASSWORD
@app.get("/")
def read_root():
return {"Hello": "World!"}
@app.post("/score_audio_plus/")
async def score_audio_plus(
password: str = Form(...),
audio: UploadFile = File(...),
api_plan: str = Form(...),
return_json: bool = Form(...)
):
"""
Call the ELSA 'score_audio_plus' endpoint with the provided audio file.
"""
if not audio:
return JSONResponse(content={"message": "No audio file provided. Please upload an audio file."}, status_code=400)
if not verify_password(password):
raise HTTPException(status_code=401, detail="Unauthorized")
url = "https://api.elsanow.io/api/v1/score_audio_plus"
headers = {"Authorization": f"ELSA {ELSA_API_TOKEN}"}
# Write file content to a temporary file
with NamedTemporaryFile(delete=False) as tmp_file:
tmp_file.write(await audio.read())
tmp_file_name = tmp_file.name
files = {
'audio_file': (os.path.basename(audio.filename), open(tmp_file_name, "rb"), 'audio/wav')
}
data = {
'api_plan': api_plan,
'return_json': "true" if return_json else "false"
}
# Send POST request
response = requests.post(url, files=files, data=data, headers=headers)
os.unlink(tmp_file_name) # Remove the temporary file
if response.status_code == 200:
# Successfully made the request
return JSONResponse(content=response.json(), status_code=200)
else:
# Error handling
return JSONResponse(content={"error": response.text}, status_code=response.status_code)
|