Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
services.py
Binary file added SDK_v1/.DS_Store
Binary file not shown.
69 changes: 69 additions & 0 deletions SDK_v1/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from django.http import JsonResponse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we want to remove this file.

import requests
import os
from pathlib import Path

def upload_audio(*, audio_data, service_name) -> None:
# with open('test.mp3', 'wb+') as mp3:
# for chunk in audio_data.chunks():
# mp3.write(chunk)

if service_name == 'assembly':
Assembly().upload_audio(audio_file=audio_data)



class Assembly:
ASSEMBLY_BASE_URL = 'https://api.assemblyai.com/v2'
ASSEMBLY_UPLOAD_URL = ASSEMBLY_BASE_URL+'/upload'
ASSEMBLY_TRANSCRIPT_URL = ASSEMBLY_BASE_URL+'/transcript'
ASSEMBLY_TRANSCRIPT_STATUS_URL = lambda id: f"{Assembly.ASSEMBLY_BASE_URL}+/transcript/{id}"

HEADERS:dict = {}

def __init__(self) -> None:
self.HEADERS = {'authorization': os.environ.get('ASSEMBLY_API_KEY')}

def _read_file(filename, chunk_size=5242880):
with open(filename, 'rb') as _file:
while True:
data = _file.read(chunk_size)
if not data:
break
yield data

def upload_audio(self, *, audio_file:Path, headers:dict=None) -> JsonResponse:
import pdb
pdb.set_trace()
response = requests.post(self.ASSEMBLY_UPLOAD_URL,
headers=headers if headers else self.HEADERS,
data=self._read_file(audio_file))
return response.json()

def request_transcript(self, *, upload_url:dict, headers:dict=None):

transcript_request = {
'audio_url': upload_url['upload_url']
}

transcript_response = requests.post(
self.ASSEMBLY_TRANSCRIPT_URL,
json=transcript_request,
headers=headers if headers else self.HEADERS,
)

return transcript_response.json()

def transcription_status(self, *, transcript_id:str, upload_url:dict, headers:dict=None):

transcript_request = {
'audio_url': upload_url['upload_url']
}

transcript_response = requests.post(
self.ASSEMBLY_TRANSCRIPT_STATUS_URL(transcript_id),
json=transcript_request,
headers=headers if headers else self.HEADERS,
)

return transcript_response.json()
22 changes: 22 additions & 0 deletions SDK_v1/speechtotext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class SpeechToText:
SLASHML_BASE_URL = 'https://api.slashml.com/v1/speech-to-text'
SLASHML_UPLOAD_URL = SLASHML_BASE_URL+'/upload'
SLASHML_TRANSCRIPT_URL = SLASHML_BASE_URL+'/transcribe'
SLASHML_TRANSCRIPT_STATUS_URL = lambda self,id: f"{SpeechToText.SLASHML_TRANSCRIPT_URL}/{id}"


def upload_audio(self, file_location) :
# here we can also add the service? assemblyai, aws, gcp?
return self.SLASHML_UPLOAD_URL #response.json()

def transcribe(self,upload_url, model_params:dict()):
# here we can add more model params
transcript_request = {'audio_url': upload_url}
#transcript_response = requests.post( self.SLASHML_TRANSCRIPT_URL, json=transcript_request)
job_id=self.SLASHML_TRANSCRIPT_URL
return job_id

def status(self, job_id:str):
#options={"status1"="queue","status2":"completed","status3":"deleted","text":"output_api_result"...}
return self.SLASHML_TRANSCRIPT_STATUS_URL(job_id)

41 changes: 41 additions & 0 deletions SDK_v1/test_speechtotext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#### unit testing
import pytest


def test_upload():
import speechtotext
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this import can be outside.

# given: there is a local file
speect_to_text = speechtotext.SpeechToText()
file_location="local_path"
# when
result = speect_to_text.upload_audio(file_location)

# then
assert result== 'https://api.slashml.com/v1/speech-to-text/upload'

def test_transcribe():
import speechtotext
# given: there is a local file
speect_to_text = speechtotext.SpeechToText()
upload_url="local_path"
# when
result = speect_to_text.transcribe(upload_url,dict())

# then
assert result== 'https://api.slashml.com/v1/speech-to-text/transcribe'


def test_status():
import speechtotext
# given: there is a local file
speect_to_text = speechtotext.SpeechToText()
# when
job_id = '123'
result = speect_to_text.status(job_id)

# then
assert result== 'https://api.slashml.com/v1/speech-to-text/transcribe/{}'.format(job_id)


# def test_status():
# assert