Create handler.py
Browse files- handler.py +26 -0
handler.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from typing import List
|
| 3 |
+
import requests as r
|
| 4 |
+
import base64
|
| 5 |
+
import mimetypes
|
| 6 |
+
|
| 7 |
+
ENDPOINT_URL=""
|
| 8 |
+
HF_TOKEN=""
|
| 9 |
+
|
| 10 |
+
def predict(path_to_audio:str=None):
|
| 11 |
+
# read audio file
|
| 12 |
+
with open(path_to_audio, "rb") as i:
|
| 13 |
+
b = i.read()
|
| 14 |
+
# get mimetype
|
| 15 |
+
content_type= mimetypes.guess_type(path_to_audio)[0]
|
| 16 |
+
|
| 17 |
+
headers= {
|
| 18 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 19 |
+
"Content-Type": content_type
|
| 20 |
+
}
|
| 21 |
+
response = r.post(ENDPOINT_URL, headers=headers, data=b)
|
| 22 |
+
return response.json()
|
| 23 |
+
|
| 24 |
+
prediction = predict(path_to_audio="sample.wav")
|
| 25 |
+
|
| 26 |
+
prediction
|