Skip to content

Commit 0c2f7ec

Browse files
committed
new update with response succeess/failed
1 parent b207443 commit 0c2f7ec

File tree

10 files changed

+140
-82
lines changed

10 files changed

+140
-82
lines changed

.DS_Store

6 KB
Binary file not shown.

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
# SLASHML Python client
22

3-
This is a Python client for SLASHML. It lets you run transcription jobs from your Python code or Jupyter notebook.
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/).
413

514
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):
615
```
716
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+
826
```
927

1028
-- update from this part, include examples, sign up, token, service providers, type of servies, benchmarking, link to pricing, Tutorial examples/examples

SDK_v1/speechtotext.py

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

SDK_v1/test_speechtotext_1.py

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

src/.DS_Store

6 KB
Binary file not shown.
2.31 KB
Binary file not shown.

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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
return response.json()["id"]
50+
51+
def status(self,job_id:str, service_provider: str ,header=None):
52+
headers = self.HEADERS
53+
payload = {
54+
"service_provider": service_provider
55+
}
56+
57+
response = requests.get(self.SLASHML_TRANSCRIPT_STATUS_URL(job_id) , headers=headers, data=payload)
58+
59+
# Check the status code of the response
60+
if response.status_code == 200:
61+
return response.json()["transcription_data"]["transcription"]
62+
63+
elif response.status_code == 404:
64+
return "NOT FOUND"
65+
66+
elif response.status_code == 500:
67+
return "SERVER ERROR"
68+
else:
69+
70+
return "UNKNOWN ERROR"
71+
72+
# return response.json()["transcription_data"]["transcription"]
73+
74+
75+
#### chgeck the code of the reponse, ifit is not 200, print back the error.

src/status_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import speechtotext
2+
import os
3+
4+
#API KEY (optional)
5+
#API_KEY="0d91bfede9c5c9de6ff1d5610ef71c3b6d5be9ee"
6+
# set environment path
7+
os.environ["SLASHML_API_KEY"] = "0d91bfede9c5c9de6ff1d5610ef71c3b6d5be9ee"
8+
# Initialize SlashML
9+
speect_to_text = speechtotext.SpeechToText()
10+
# optional local file to upload, if not an accessible url
11+
# file_location="/Users/JJneid/Desktop/SlashMl/s2t_experiments/api_tests/test_french_english.mp3"
12+
# # If your audio files aren't accessible via a URL already, you can upload your audio file using this API
13+
# upload_url= speect_to_text.upload_audio(file_location)
14+
# print(upload_url)
15+
# choose your service provider: "asembly", "aws", "whisper"
16+
service_provider="aws"
17+
# transcribe
18+
transcribe_id= "e5f43d08-6fa4-4cd7-86f9-2fb172004032"
19+
20+
# get the status, the result will be out after the job is done, so we wait a bit :)
21+
status=speect_to_text.status(transcribe_id,service_provider=service_provider)
22+
# get the text result: status["transcription_data"]["transcription"]
23+
print(status)
24+
#print(status["transcription_data"]["transcription"])

src/test_speechtotext_1.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import speechtotext
2+
import os
3+
4+
#API KEY (optional)
5+
6+
# set environment path
7+
os.environ["SLASHML_API_KEY"] = "0d91bfede9c5c9de6ff1d5610ef71c3b6d5be9ee"
8+
# Initialize SlashML
9+
speect_to_text = speechtotext.SpeechToText()
10+
# optional local file to upload, if not an accessible url
11+
file_location="/Users/JJneid/Desktop/SlashMl/s2t_experiments/api_tests/2test_audio.mp3"
12+
# If your audio files aren't accessible via a URL already, you can upload your audio file using this API
13+
upload_url= speect_to_text.upload_audio(file_location)
14+
# choose your service provider: "asembly", "aws", "whisper"
15+
service_provider="aws"
16+
# transcribe
17+
transcribe_id= speect_to_text.transcribe(upload_url,service_provider)
18+
print(transcribe_id)
19+
20+
status=speect_to_text.status(transcribe_id,service_provider=service_provider)
21+
print(status)

0 commit comments

Comments
 (0)