Skip to content

Commit 07c637f

Browse files
authored
Merge pull request #1 from slashml/first_release
First release
2 parents c097e0d + ffb8b81 commit 07c637f

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
services.py

SDK_v1/.DS_Store

6 KB
Binary file not shown.

SDK_v1/services.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from django.http import JsonResponse
2+
import requests
3+
import os
4+
from pathlib import Path
5+
6+
def upload_audio(*, audio_data, service_name) -> None:
7+
# with open('test.mp3', 'wb+') as mp3:
8+
# for chunk in audio_data.chunks():
9+
# mp3.write(chunk)
10+
11+
if service_name == 'assembly':
12+
Assembly().upload_audio(audio_file=audio_data)
13+
14+
15+
16+
class Assembly:
17+
ASSEMBLY_BASE_URL = 'https://api.assemblyai.com/v2'
18+
ASSEMBLY_UPLOAD_URL = ASSEMBLY_BASE_URL+'/upload'
19+
ASSEMBLY_TRANSCRIPT_URL = ASSEMBLY_BASE_URL+'/transcript'
20+
ASSEMBLY_TRANSCRIPT_STATUS_URL = lambda id: f"{Assembly.ASSEMBLY_BASE_URL}+/transcript/{id}"
21+
22+
HEADERS:dict = {}
23+
24+
def __init__(self) -> None:
25+
self.HEADERS = {'authorization': os.environ.get('ASSEMBLY_API_KEY')}
26+
27+
def _read_file(filename, chunk_size=5242880):
28+
with open(filename, 'rb') as _file:
29+
while True:
30+
data = _file.read(chunk_size)
31+
if not data:
32+
break
33+
yield data
34+
35+
def upload_audio(self, *, audio_file:Path, headers:dict=None) -> JsonResponse:
36+
import pdb
37+
pdb.set_trace()
38+
response = requests.post(self.ASSEMBLY_UPLOAD_URL,
39+
headers=headers if headers else self.HEADERS,
40+
data=self._read_file(audio_file))
41+
return response.json()
42+
43+
def request_transcript(self, *, upload_url:dict, headers:dict=None):
44+
45+
transcript_request = {
46+
'audio_url': upload_url['upload_url']
47+
}
48+
49+
transcript_response = requests.post(
50+
self.ASSEMBLY_TRANSCRIPT_URL,
51+
json=transcript_request,
52+
headers=headers if headers else self.HEADERS,
53+
)
54+
55+
return transcript_response.json()
56+
57+
def transcription_status(self, *, transcript_id:str, upload_url:dict, headers:dict=None):
58+
59+
transcript_request = {
60+
'audio_url': upload_url['upload_url']
61+
}
62+
63+
transcript_response = requests.post(
64+
self.ASSEMBLY_TRANSCRIPT_STATUS_URL(transcript_id),
65+
json=transcript_request,
66+
headers=headers if headers else self.HEADERS,
67+
)
68+
69+
return transcript_response.json()

SDK_v1/speechtotext.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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}"
6+
7+
8+
def upload_audio(self, file_location) :
9+
# here we can also add the service? assemblyai, aws, gcp?
10+
return self.SLASHML_UPLOAD_URL #response.json()
11+
12+
def transcribe(self,upload_url, model_params:dict()):
13+
# here we can add more model params
14+
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
18+
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)
22+

SDK_v1/test_speechtotext.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#### unit testing
2+
import pytest
3+
4+
5+
def test_upload():
6+
import speechtotext
7+
# given: there is a local file
8+
speect_to_text = speechtotext.SpeechToText()
9+
file_location="local_path"
10+
# when
11+
result = speect_to_text.upload_audio(file_location)
12+
13+
# then
14+
assert result== 'https://api.slashml.com/v1/speech-to-text/upload'
15+
16+
def test_transcribe():
17+
import speechtotext
18+
# given: there is a local file
19+
speect_to_text = speechtotext.SpeechToText()
20+
upload_url="local_path"
21+
# 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'
26+
27+
28+
def test_status():
29+
import speechtotext
30+
# given: there is a local file
31+
speect_to_text = speechtotext.SpeechToText()
32+
# 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

0 commit comments

Comments
 (0)