Skip to content

Commit e34ea27

Browse files
authored
Merge pull request #2 from slashml/first_release
second version sdk
2 parents 07c637f + b65c13c commit e34ea27

File tree

5 files changed

+84
-33
lines changed

5 files changed

+84
-33
lines changed

.DS_Store

0 Bytes
Binary file not shown.
2.11 KB
Binary file not shown.

SDK_v1/readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SDK for SlashML documentation:
2+
3+
- pip install slashml
4+
- add SLASH_API_KEY to sys path
5+
- methods: upload_audio, transcribe, status
6+
-

SDK_v1/speechtotext.py

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,57 @@
1-
class SpeechToText:
2-
SLASHML_BASE_URL = 'https://api.slashml.com/v1/speech-to-text'
3-
SLASHML_UPLOAD_URL = SLASHML_BASE_URL+'/upload'
4-
SLASHML_TRANSCRIPT_URL = SLASHML_BASE_URL+'/transcribe'
5-
SLASHML_TRANSCRIPT_STATUS_URL = lambda self,id: f"{SpeechToText.SLASHML_TRANSCRIPT_URL}/{id}"
1+
from django.http import JsonResponse
2+
import requests
3+
import os
4+
from pathlib import Path
65

6+
class SpeechToText:
7+
SLASHML_BASE_URL = 'https://api.slashml.com/speech-to-text/v1'
8+
SLASHML_UPLOAD_URL = SLASHML_BASE_URL+'/upload/'
9+
SLASHML_TRANSCRIPT_URL = SLASHML_BASE_URL+'/transcribe/'
10+
SLASHML_STATUS_URL = SLASHML_BASE_URL+'/transcription'
11+
SLASHML_TRANSCRIPT_STATUS_URL = lambda self,id: f"{SpeechToText.SLASHML_STATUS_URL}/{id}/"
12+
13+
HEADERS:dict = {}
14+
15+
def __init__(self) -> None:
16+
self.HEADERS = {'authorization': os.environ.get('SLASHML_API_KEY')}
717

8-
def upload_audio(self, file_location) :
18+
def upload_audio(self, file_location:str, API_KEY: str, model_choice: str ):
919
# here we can also add the service? assemblyai, aws, gcp?
10-
return self.SLASHML_UPLOAD_URL #response.json()
1120

12-
def transcribe(self,upload_url, model_params:dict()):
21+
token="Token {0}".format(API_KEY)
22+
headers = {'authorization': token}
23+
payload = { 'model':model_choice }
24+
files=[
25+
('audio',('test_audio.mp3',open(file_location,'rb'),'audio/mpeg'))
26+
]
27+
#import pdb
28+
#pdb.set_trace()
29+
response = requests.post(self.SLASHML_UPLOAD_URL,
30+
headers=headers,
31+
data=payload,files=files)
32+
return response.text
33+
34+
def transcribe(self,upload_url:str, API_KEY: str, model_choice: str ):
1335
# here we can add more model params
1436
transcript_request = {'audio_url': upload_url}
15-
#transcript_response = requests.post( self.SLASHML_TRANSCRIPT_URL, json=transcript_request)
16-
job_id=self.SLASHML_TRANSCRIPT_URL
17-
return job_id
37+
token="Token {0}".format(API_KEY)
38+
headers = {'authorization': token}
39+
payload = {
40+
"uploaded_audio_url": upload_url,
41+
"model": model_choice
42+
}
43+
44+
response = requests.post(self.SLASHML_TRANSCRIPT_URL, headers=headers, data=payload)
45+
return response.text
1846

19-
def status(self, job_id:str):
20-
#options={"status1"="queue","status2":"completed","status3":"deleted","text":"output_api_result"...}
21-
return self.SLASHML_TRANSCRIPT_STATUS_URL(job_id)
47+
def status(self,job_id:str, API_KEY: str, model_choice: str ):
48+
token="Token {0}".format(API_KEY)
49+
headers = {'authorization': token}
50+
payload = {
51+
"model": model_choice
52+
}
53+
#import pdb
54+
# pdb.set_trace()
55+
response = requests.get(self.SLASHML_TRANSCRIPT_STATUS_URL(job_id) , headers=headers, data=payload)
56+
return response.text
2257

SDK_v1/test_speechtotext.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,51 @@
11
#### unit testing
22
import pytest
33

4-
4+
import json
55
def test_upload():
66
import speechtotext
77
# given: there is a local file
88
speect_to_text = speechtotext.SpeechToText()
9-
file_location="local_path"
9+
file_location="/Users/JJneid/Desktop/SlashMl/s2t_experiments/api_tests/test_audio_1.mp3"
1010
# when
11-
result = speect_to_text.upload_audio(file_location)
12-
11+
API_KEY="1bd15e1c161ff6d4db2ea1d661d7468b4fa61ca9"
12+
model_choice="assembly"
13+
result = speect_to_text.upload_audio(file_location,API_KEY, model_choice)
14+
print(result)
1315
# then
14-
assert result== 'https://api.slashml.com/v1/speech-to-text/upload'
16+
# assert result== 'https://api.slashml.com/v1/speech-to-text/upload'
1517

1618
def test_transcribe():
1719
import speechtotext
1820
# given: there is a local file
1921
speect_to_text = speechtotext.SpeechToText()
20-
upload_url="local_path"
22+
upload_url="https://cdn.assemblyai.com/upload/f973cc7a-45f8-47b9-8ebc-20bd2f63a2bd"
2123
# when
22-
result = speect_to_text.transcribe(upload_url,dict())
23-
24-
# then
25-
assert result== 'https://api.slashml.com/v1/speech-to-text/transcribe'
24+
API_KEY="1bd15e1c161ff6d4db2ea1d661d7468b4fa61ca9"
25+
model_choice="assembly"
26+
result = speect_to_text.transcribe(upload_url,API_KEY, model_choice)
2627

28+
print(result)
2729

2830
def test_status():
2931
import speechtotext
3032
# given: there is a local file
3133
speect_to_text = speechtotext.SpeechToText()
34+
upload_url="https://cdn.assemblyai.com/upload/28313e17-9a51-43cd-a29f-68dedd772f83"
3235
# when
33-
job_id = '123'
34-
result = speect_to_text.status(job_id)
35-
36-
# then
37-
assert result== 'https://api.slashml.com/v1/speech-to-text/transcribe/{}'.format(job_id)
38-
39-
40-
# def test_status():
41-
# assert
36+
job_id= "r4ff9gd76c-530e-4e27-bed4-ce12b63b26b0"
37+
API_KEY="1bd15e1c161ff6d4db2ea1d661d7468b4fa61ca9"
38+
model_choice="assembly"
39+
result = speect_to_text.status(job_id,API_KEY, model_choice=model_choice)
40+
41+
print(json.loads(result)["text"])
42+
43+
#test_transcribe()
44+
#test_transcribe()
45+
test_status()
46+
# API_KEY="1bd15e1c161ff6d4db2ea1d661d7468b4fa61ca9"
47+
# token="Token {api}".format(api=API_KEY)
48+
# print(token)
49+
50+
# headers = {'authorization': token}
51+
# print(headers)

0 commit comments

Comments
 (0)