youngtsai commited on
Commit
eeda580
·
1 Parent(s): bab4ecf

@app .post("/score_audio_plus/")

Browse files
Files changed (1) hide show
  1. main.py +52 -2
main.py CHANGED
@@ -1,7 +1,57 @@
1
- from fastapi import FastAPI
 
 
 
2
 
3
  app = FastAPI()
4
 
 
 
 
 
 
 
5
  @app.get("/")
6
  def read_root():
7
- return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
 
8
+ PASSWORD = os.getenv("PASSWORD") # Read password from environment variable
9
+ ELSA_API_TOKEN = os.getenv("ELSA_API_TOKEN") # Make sure to set this environment variable
10
+
11
+ def verify_password(input_password: str):
12
+ return input_password == PASSWORD
13
+
14
  @app.get("/")
15
  def read_root():
16
+ return {"Hello": "World!"}
17
+
18
+ @app.post("/score_audio_plus/")
19
+ async def score_audio_plus(
20
+ password: str = Form(...),
21
+ audio: UploadFile = File(...),
22
+ api_plan: str = Form(...),
23
+ return_json: bool = Form(...)
24
+ ):
25
+ """
26
+ Call the ELSA 'score_audio_plus' endpoint with the provided audio file.
27
+ """
28
+ if not audio:
29
+ return JSONResponse(content={"message": "No audio file provided. Please upload an audio file."}, status_code=400)
30
+
31
+ if not verify_password(password):
32
+ raise HTTPException(status_code=401, detail="Unauthorized")
33
+
34
+ url = "https://api.elsanow.io/api/v1/score_audio_plus"
35
+ headers = {"Authorization": f"ELSA {ELSA_API_TOKEN}"}
36
+
37
+ # Read file content
38
+ file_content = await audio.read()
39
+
40
+ # Prepare files and data for POST request
41
+ files = {
42
+ 'audio_file': (audio.filename, file_content, audio.content_type)
43
+ }
44
+ data = {
45
+ 'api_plan': api_plan,
46
+ 'return_json': "true" if return_json else "false"
47
+ }
48
+
49
+ # Send POST request
50
+ response = requests.post(url, files=files, data=data, headers=headers)
51
+
52
+ if response.status_code == 200:
53
+ # Successfully made the request
54
+ return JSONResponse(content=response.json(), status_code=200)
55
+ else:
56
+ # Error handling
57
+ return JSONResponse(content={"error": response.text}, status_code=response.status_code)