-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslashml.py
More file actions
85 lines (70 loc) · 3.02 KB
/
slashml.py
File metadata and controls
85 lines (70 loc) · 3.02 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import requests
import os
from pathlib import Path
import json
class SpeechToText:
SLASHML_BASE_URL = 'https://api.slashml.com/speech-to-text/v1'
SLASHML_UPLOAD_URL = SLASHML_BASE_URL+'/upload/'
SLASHML_TRANSCRIPT_URL = SLASHML_BASE_URL+'/transcribe/'
SLASHML_STATUS_URL = SLASHML_BASE_URL+'/transcription'
SLASHML_TRANSCRIPT_STATUS_URL = lambda self,id: f"{SpeechToText.SLASHML_STATUS_URL}/{id}/"
HEADERS:dict = {}
## add the api key to sys path envs
def __init__(self,API_KEY: str = None):
self.API_KEY=None
if API_KEY:
token="Token {0}".format(API_KEY)
self.HEADERS = {'authorization': token}
# print("Auth with "+API_KEY+"\nMake sure this matches your API Key generated in the dashboard settings")
elif os.environ.get('SLASHML_API_KEY'):
key_env = os.environ.get('SLASHML_API_KEY')
token="Token {0}".format(key_env)
self.HEADERS = {'authorization': token}
# print("Auth with environment variable SLASHML_API_KEY")
else:
self.HEADERS=None
print("No Auth, there are certain limites to the usage")
def upload_audio(self, file_location:str,header=None):
headers = self.HEADERS
files=[
('audio',('test_audio.mp3',open(file_location,'rb'),'audio/mpeg'))
]
response = requests.post(self.SLASHML_UPLOAD_URL,
headers=headers,files=files)
return response.json()["upload_url"]
def transcribe(self,upload_url:str, service_provider: str,header=None ):
transcript_request = {'audio_url': upload_url}
headers = self.HEADERS
payload = {
"uploaded_audio_url": upload_url,
"service_provider": service_provider
}
response = requests.post(self.SLASHML_TRANSCRIPT_URL, headers=headers, data=payload)
# Check the status code of the response
if response.status_code == 200:
return response.json()["id"]
elif response.status_code == 429:
return "THROTTLED"
elif response.status_code == 404:
return "NOT FOUND"
elif response.status_code == 500:
return "SERVER ERROR"
else:
return "UNKNOWN ERROR"
def status(self,job_id:str, service_provider: str ,header=None):
headers = self.HEADERS
payload = {
"service_provider": service_provider
}
response = requests.get(self.SLASHML_TRANSCRIPT_STATUS_URL(job_id) , headers=headers, data=payload)
# Check the status code of the response
if response.status_code == 200:
return response.json()["transcription_data"]["transcription"]
elif response.status_code == 429:
return "THROTTLED"
elif response.status_code == 404:
return "NOT FOUND"
elif response.status_code == 500:
return "SERVER ERROR"
else:
return "UNKNOWN ERROR"