Skip to content

Commit fcfcb6c

Browse files
committed
speech to text sdk
1 parent 545a732 commit fcfcb6c

11 files changed

Lines changed: 201 additions & 155 deletions

.DS_Store

-6 KB
Binary file not shown.

README.md

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,74 @@
1-
# slashml-python-client
1+
# SLASHML Python client
22

3-
to be updated to guide users
3+
This is a Python client for SLASHML. It lets you run transcription jobs from your Python code or Jupyter notebook. Do a transcription job with three lines of code
4+
```
5+
import speechtotext
6+
7+
speect_to_text = speechtotext.SpeechToText()
8+
transcribe_id= speect_to_text.transcribe(audio_url,service_provider="aws")
9+
status=speect_to_text.status(transcribe_id,service_provider=service_provider)
10+
11+
```
12+
There is a daily limit (throttling) on the number of calls the user performs, transcription jobs can be done without specifying a token (API key). If the user intends on using the service more frequently, it is recommended to generate an token or API key from the dashboard @ [Slashml.com](https://www.slashml.com/).
13+
14+
Grab your token from [https://www.slashml.com/dashboard] (>settings> new api key) and authenticate by setting it as an environment variable (or when you initialize the service, see examples):
15+
```
16+
export SLASHML_API_KEY=[token]
17+
```
18+
or including it in your code as follows:
19+
```
20+
import speechtotext
21+
API_KEY="your_api_key"
22+
speect_to_text = speechtotext.SpeechToText(API_KEY)
23+
transcribe_id= speect_to_text.transcribe(audio_url,service_provider="aws")
24+
status=speect_to_text.status(transcribe_id,service_provider=service_provider)
25+
26+
```
27+
28+
-- update from this part, include examples, sign up, token, service providers, type of servies, benchmarking, link to pricing, Tutorial examples/examples
29+
30+
31+
SDK for SlashML documentation:
32+
- methods: upload_audio, transcribe, status
33+
34+
Steps to Integrate
35+
1 - (Optional) Upload files where the data points to your audio file
36+
```
37+
# call the class
38+
speect_to_text = speechtotext.SpeechToText()
39+
file_location="path/to/your/file.mp3"
40+
# when
41+
API_KEY="SLASH_ML_API_KEY"
42+
model_choice="assembly"
43+
result_upload = speect_to_text.upload_audio(file_location,API_KEY, model_choice)
44+
print(result_upload)
45+
```
46+
Save the upload_url. You can use this url link in the rest of the calls.
47+
48+
49+
2- Submit your audio file for transcription
50+
```
51+
upload_url=upload_url # you can skip step 1 and just input the accessible link of your # file)
52+
53+
result_transcribe = speect_to_text.transcribe(upload_url,API_KEY, model_choice)
54+
55+
print(result_transcribe)
56+
```
57+
Save the id in the response object.
58+
59+
60+
3 - Check the status and get the text result of the transcription
61+
```
62+
job_id= id
63+
result_status = speect_to_text.status(job_id,API_KEY, model_choice=model_choice)
64+
65+
### get the full details of the result
66+
print(result_status)
67+
### get the text reulst only
68+
print(json.loads(result)["text"])
69+
```
70+
71+
72+
et voilà, Next steps:
73+
- pip install slashml
74+
- add SLASH_API_KEY to sys path

SDK_v1/.DS_Store

-6 KB
Binary file not shown.
-2.11 KB
Binary file not shown.

SDK_v1/readme.md

Lines changed: 0 additions & 44 deletions
This file was deleted.

SDK_v1/speechtotext.py

Lines changed: 0 additions & 58 deletions
This file was deleted.

SDK_v1/test_speechtotext.py

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 SlashML
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

src/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests==2.28.1

src/speechtotext.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import requests
2+
import os
3+
from pathlib import Path
4+
import json
5+
6+
class SpeechToText:
7+
8+
SLASHML_BASE_URL = 'https://api.slashml.com/speech-to-text/v1'
9+
SLASHML_UPLOAD_URL = SLASHML_BASE_URL+'/upload/'
10+
SLASHML_TRANSCRIPT_URL = SLASHML_BASE_URL+'/transcribe/'
11+
SLASHML_STATUS_URL = SLASHML_BASE_URL+'/transcription'
12+
SLASHML_TRANSCRIPT_STATUS_URL = lambda self,id: f"{SpeechToText.SLASHML_STATUS_URL}/{id}/"
13+
14+
HEADERS:dict = {}
15+
## add the api key to sys path envs
16+
def __init__(self,API_KEY: str = None):
17+
self.API_KEY=None
18+
if API_KEY:
19+
token="Token {0}".format(API_KEY)
20+
self.HEADERS = {'authorization': token}
21+
# print("Auth with "+API_KEY+"\nMake sure this matches your API Key generated in the dashboard settings")
22+
elif os.environ.get('SLASHML_API_KEY'):
23+
key_env = os.environ.get('SLASHML_API_KEY')
24+
token="Token {0}".format(key_env)
25+
self.HEADERS = {'authorization': token}
26+
# print("Auth with environment variable SLASHML_API_KEY")
27+
else:
28+
self.HEADERS=None
29+
print("No Auth, there are certain limites to the usage")
30+
31+
def upload_audio(self, file_location:str,header=None):
32+
headers = self.HEADERS
33+
files=[
34+
('audio',('test_audio.mp3',open(file_location,'rb'),'audio/mpeg'))
35+
]
36+
response = requests.post(self.SLASHML_UPLOAD_URL,
37+
headers=headers,files=files)
38+
return response.json()["upload_url"]
39+
40+
def transcribe(self,upload_url:str, service_provider: str,header=None ):
41+
42+
transcript_request = {'audio_url': upload_url}
43+
headers = self.HEADERS
44+
payload = {
45+
"uploaded_audio_url": upload_url,
46+
"service_provider": service_provider
47+
}
48+
response = requests.post(self.SLASHML_TRANSCRIPT_URL, headers=headers, data=payload)
49+
# Check the status code of the response
50+
if response.status_code == 200:
51+
return response.json()["id"]
52+
53+
elif response.status_code == 429:
54+
return "THROTTLED"
55+
56+
elif response.status_code == 404:
57+
return "NOT FOUND"
58+
59+
elif response.status_code == 500:
60+
return "SERVER ERROR"
61+
else:
62+
return "UNKNOWN ERROR"
63+
64+
def status(self,job_id:str, service_provider: str ,header=None):
65+
headers = self.HEADERS
66+
payload = {
67+
"service_provider": service_provider
68+
}
69+
70+
response = requests.get(self.SLASHML_TRANSCRIPT_STATUS_URL(job_id) , headers=headers, data=payload)
71+
72+
# Check the status code of the response
73+
if response.status_code == 200:
74+
return response.json()["transcription_data"]["transcription"]
75+
76+
elif response.status_code == 429:
77+
return "THROTTLED"
78+
79+
elif response.status_code == 404:
80+
return "NOT FOUND"
81+
82+
elif response.status_code == 500:
83+
return "SERVER ERROR"
84+
else:
85+
return "UNKNOWN ERROR"

0 commit comments

Comments
 (0)