diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index ed35149..0000000 Binary files a/.DS_Store and /dev/null differ diff --git a/.gitignore b/.gitignore deleted file mode 100644 index fde543a..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -.DS_Store -services.py diff --git a/README.md b/README.md index 05012b9..49da84b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,74 @@ -# slashml-python-client +# SLASHML Python client -to be updated to guide users +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 +``` +import speechtotext + +speect_to_text = speechtotext.SpeechToText() +transcribe_id= speect_to_text.transcribe(audio_url,service_provider="aws") +status=speect_to_text.status(transcribe_id,service_provider=service_provider) + +``` +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/). + +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): +``` +export SLASHML_API_KEY=[token] +``` +or including it in your code as follows: +``` +import speechtotext +API_KEY="your_api_key" +speect_to_text = speechtotext.SpeechToText(API_KEY) +transcribe_id= speect_to_text.transcribe(audio_url,service_provider="aws") +status=speect_to_text.status(transcribe_id,service_provider=service_provider) + +``` + +-- update from this part, include examples, sign up, token, service providers, type of servies, benchmarking, link to pricing, Tutorial examples/examples + + +SDK for SlashML documentation: +- methods: upload_audio, transcribe, status + +Steps to Integrate +1 - (Optional) Upload files where the data points to your audio file +``` +# call the class +speect_to_text = speechtotext.SpeechToText() +file_location="path/to/your/file.mp3" +# when +API_KEY="SLASH_ML_API_KEY" +model_choice="assembly" +result_upload = speect_to_text.upload_audio(file_location,API_KEY, model_choice) +print(result_upload) +``` +Save the upload_url. You can use this url link in the rest of the calls. + + +2- Submit your audio file for transcription +``` +upload_url=upload_url # you can skip step 1 and just input the accessible link of your # file) + +result_transcribe = speect_to_text.transcribe(upload_url,API_KEY, model_choice) + +print(result_transcribe) +``` +Save the id in the response object. + + +3 - Check the status and get the text result of the transcription +``` +job_id= id +result_status = speect_to_text.status(job_id,API_KEY, model_choice=model_choice) + +### get the full details of the result +print(result_status) +### get the text reulst only +print(json.loads(result)["text"]) +``` + + +et voilĂ , Next steps: +- pip install slashml +- add SLASH_API_KEY to sys path diff --git a/SDK_v1/.DS_Store b/SDK_v1/.DS_Store deleted file mode 100644 index e6517a4..0000000 Binary files a/SDK_v1/.DS_Store and /dev/null differ diff --git a/SDK_v1/__pycache__/speechtotext.cpython-310.pyc b/SDK_v1/__pycache__/speechtotext.cpython-310.pyc deleted file mode 100644 index 180fbdc..0000000 Binary files a/SDK_v1/__pycache__/speechtotext.cpython-310.pyc and /dev/null differ diff --git a/SDK_v1/readme.md b/SDK_v1/readme.md deleted file mode 100644 index 7321fe1..0000000 --- a/SDK_v1/readme.md +++ /dev/null @@ -1,44 +0,0 @@ -SDK for SlashML documentation: -- methods: upload_audio, transcribe, status - -Steps to Integrate -1 - (Optional) Upload files where the data points to your audio file -``` -# call the class -speect_to_text = speechtotext.SpeechToText() -file_location="path/to/your/file.mp3" -# when -API_KEY="SLASH_ML_API_KEY" -model_choice="assembly" -result_upload = speect_to_text.upload_audio(file_location,API_KEY, model_choice) -print(result_upload) -``` -Save the upload_url. You can use this url link in the rest of the calls. - - -2- Submit your audio file for transcription -``` -upload_url=upload_url # you can skip step 1 and just input the accessible link of your # file) - -result_transcribe = speect_to_text.transcribe(upload_url,API_KEY, model_choice) - -print(result_transcribe) -``` -Save the id in the response object. - - -3 - Check the status and get the text result of the transcription -``` -job_id= id -result_status = speect_to_text.status(job_id,API_KEY, model_choice=model_choice) - -### get the full details of the result -print(result_status) -### get the text reulst only -print(json.loads(result)["text"]) -``` - - -et voilĂ , Next steps: -- pip install slashml -- add SLASH_API_KEY to sys path diff --git a/SDK_v1/speechtotext.py b/SDK_v1/speechtotext.py deleted file mode 100644 index fc96cbe..0000000 --- a/SDK_v1/speechtotext.py +++ /dev/null @@ -1,58 +0,0 @@ -from django.http import JsonResponse -import requests -import os -from pathlib import Path - -class SpeechToText: - # TODO: make sure this is dynamic - SLASHML_BASE_URL = 'https://api.slashml.com/speech-to-text/v1' - SLASHML_UPLOAD_URL = SLASHML_BASE_URL+'/upload/' - SLASHML_TRANSCRIPT_URL = SLASHML_BASE_URL+'/transcribe/' - SLASHML_STATUS_URL = SLASHML_BASE_URL+'/transcription' - SLASHML_TRANSCRIPT_STATUS_URL = lambda self,id: f"{SpeechToText.SLASHML_STATUS_URL}/{id}/" - - HEADERS:dict = {} - ## add the api key to sys path envs - def __init__(self) -> None: - self.HEADERS = {'authorization': os.environ.get('SLASHML_API_KEY')} - - def upload_audio(self, file_location:str, API_KEY: str, model_choice: str ): - # here we can also add the service? assemblyai, aws, gcp? - - token="Token {0}".format(API_KEY) - headers = {'authorization': token} - payload = { 'model':model_choice } - files=[ - ('audio',('test_audio.mp3',open(file_location,'rb'),'audio/mpeg')) - ] - #import pdb - #pdb.set_trace() - response = requests.post(self.SLASHML_UPLOAD_URL, - headers=headers, - data=payload,files=files) - return response.text - - def transcribe(self,upload_url:str, API_KEY: str, model_choice: str ): - # here we can add more model params - transcript_request = {'audio_url': upload_url} - token="Token {0}".format(API_KEY) - headers = {'authorization': token} - payload = { - "uploaded_audio_url": upload_url, - "model": model_choice - } - - response = requests.post(self.SLASHML_TRANSCRIPT_URL, headers=headers, data=payload) - return response.text - - def status(self,job_id:str, API_KEY: str, model_choice: str ): - token="Token {0}".format(API_KEY) - headers = {'authorization': token} - payload = { - "model": model_choice - } - #import pdb - # pdb.set_trace() - response = requests.get(self.SLASHML_TRANSCRIPT_STATUS_URL(job_id) , headers=headers, data=payload) - return response.text - diff --git a/SDK_v1/test_speechtotext.py b/SDK_v1/test_speechtotext.py deleted file mode 100644 index 34bd9c6..0000000 --- a/SDK_v1/test_speechtotext.py +++ /dev/null @@ -1,51 +0,0 @@ -#### unit testing -import pytest - -import json -def test_upload(): - import speechtotext - # given: there is a local file - speect_to_text = speechtotext.SpeechToText() - file_location="/Users/JJneid/Desktop/SlashMl/s2t_experiments/api_tests/test_audio_1.mp3" - # when - API_KEY="1bd15e1c161ff6d4db2ea1d661d7468b4fa61ca9" - model_choice="assembly" - result = speect_to_text.upload_audio(file_location,API_KEY, model_choice) - print(result) - # 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="https://cdn.assemblyai.com/upload/f973cc7a-45f8-47b9-8ebc-20bd2f63a2bd" - # when - API_KEY="1bd15e1c161ff6d4db2ea1d661d7468b4fa61ca9" - model_choice="assembly" - result = speect_to_text.transcribe(upload_url,API_KEY, model_choice) - - print(result) - -def test_status(): - import speechtotext - # given: there is a local file - speect_to_text = speechtotext.SpeechToText() - upload_url="https://cdn.assemblyai.com/upload/28313e17-9a51-43cd-a29f-68dedd772f83" - # when - job_id= "r4ff9gd76c-530e-4e27-bed4-ce12b63b26b0" - API_KEY="1bd15e1c161ff6d4db2ea1d661d7468b4fa61ca9" - model_choice="assembly" - result = speect_to_text.status(job_id,API_KEY, model_choice=model_choice) - - print(json.loads(result)["text"]) - -test_upload() -#test_transcribe() -#test_status() -# API_KEY="1bd15e1c161ff6d4db2ea1d661d7468b4fa61ca9" -# token="Token {api}".format(api=API_KEY) -# print(token) - -# headers = {'authorization': token} -# print(headers) \ No newline at end of file diff --git a/src/__pycache__/speechtotext.cpython-310.pyc b/src/__pycache__/speechtotext.cpython-310.pyc new file mode 100644 index 0000000..44b34fe Binary files /dev/null and b/src/__pycache__/speechtotext.cpython-310.pyc differ diff --git a/src/requirements.txt b/src/requirements.txt new file mode 100644 index 0000000..d15ce5a --- /dev/null +++ b/src/requirements.txt @@ -0,0 +1 @@ +requests==2.28.1 diff --git a/src/speechtotext.py b/src/speechtotext.py new file mode 100644 index 0000000..3d055d7 --- /dev/null +++ b/src/speechtotext.py @@ -0,0 +1,75 @@ +import requests +import os +from pathlib import Path +import json + +class SpeechToText: + + SLASHML_BASE_URL = 'https://api.slashml.com/speech-to-text/v1' + SLASHML_UPLOAD_URL = SLASHML_BASE_URL+'/upload/' + SLASHML_TRANSCRIPT_URL = SLASHML_BASE_URL+'/transcribe/' + SLASHML_STATUS_URL = SLASHML_BASE_URL+'/transcription' + SLASHML_TRANSCRIPT_STATUS_URL = lambda self,id: f"{SpeechToText.SLASHML_STATUS_URL}/{id}/" + + HEADERS:dict = {} + ## add the api key to sys path envs + def __init__(self,API_KEY: str = None): + self.API_KEY=None + if API_KEY: + token="Token {0}".format(API_KEY) + self.HEADERS = {'authorization': token} + # print("Auth with "+API_KEY+"\nMake sure this matches your API Key generated in the dashboard settings") + elif os.environ.get('SLASHML_API_KEY'): + key_env = os.environ.get('SLASHML_API_KEY') + token="Token {0}".format(key_env) + self.HEADERS = {'authorization': token} + # print("Auth with environment variable SLASHML_API_KEY") + else: + self.HEADERS=None + print("No Auth, there are certain limites to the usage") + + def upload_audio(self, file_location:str,header=None): + headers = self.HEADERS + files=[ + ('audio',('test_audio.mp3',open(file_location,'rb'),'audio/mpeg')) + ] + response = requests.post(self.SLASHML_UPLOAD_URL, + headers=headers,files=files) + return response.json()["upload_url"] + + def transcribe(self,upload_url:str, service_provider: str,header=None ): + + transcript_request = {'audio_url': upload_url} + headers = self.HEADERS + payload = { + "uploaded_audio_url": upload_url, + "service_provider": service_provider + } + response = requests.post(self.SLASHML_TRANSCRIPT_URL, headers=headers, data=payload) + return response.json()["id"] + + def status(self,job_id:str, service_provider: str ,header=None): + headers = self.HEADERS + payload = { + "service_provider": service_provider + } + + response = requests.get(self.SLASHML_TRANSCRIPT_STATUS_URL(job_id) , headers=headers, data=payload) + + # Check the status code of the response + if response.status_code == 200: + return response.json()["transcription_data"]["transcription"] + + elif response.status_code == 404: + return "NOT FOUND" + + elif response.status_code == 500: + return "SERVER ERROR" + else: + + return "UNKNOWN ERROR" + + # return response.json()["transcription_data"]["transcription"] + + + #### chgeck the code of the reponse, ifit is not 200, print back the error. \ No newline at end of file diff --git a/src/status_test.py b/src/status_test.py new file mode 100644 index 0000000..ad1a39a --- /dev/null +++ b/src/status_test.py @@ -0,0 +1,24 @@ +import speechtotext +import os + +#API KEY (optional) +#API_KEY="0d91bfede9c5c9de6ff1d5610ef71c3b6d5be9ee" +# set environment path +os.environ["SLASHML_API_KEY"] = "0d91bfede9c5c9de6ff1d5610ef71c3b6d5be9ee" +# Initialize SlashML +speect_to_text = speechtotext.SpeechToText() +# optional local file to upload, if not an accessible url +# file_location="/Users/JJneid/Desktop/SlashMl/s2t_experiments/api_tests/test_french_english.mp3" +# # If your audio files aren't accessible via a URL already, you can upload your audio file using this API +# upload_url= speect_to_text.upload_audio(file_location) +# print(upload_url) +# choose your service provider: "asembly", "aws", "whisper" +service_provider="aws" +# transcribe +transcribe_id= "e5f43d08-6fa4-4cd7-86f9-2fb172004032" + +# get the status, the result will be out after the job is done, so we wait a bit :) +status=speect_to_text.status(transcribe_id,service_provider=service_provider) +# get the text result: status["transcription_data"]["transcription"] +print(status) +#print(status["transcription_data"]["transcription"]) \ No newline at end of file diff --git a/src/test_speechtotext_1.py b/src/test_speechtotext_1.py new file mode 100644 index 0000000..f6a3c70 --- /dev/null +++ b/src/test_speechtotext_1.py @@ -0,0 +1,21 @@ +import speechtotext +import os + +#API KEY (optional) + +# set environment path +os.environ["SLASHML_API_KEY"] = "0d91bfede9c5c9de6ff1d5610ef71c3b6d5be9ee" +# Initialize SlashML +speect_to_text = speechtotext.SpeechToText() +# optional local file to upload, if not an accessible url +file_location="/Users/JJneid/Desktop/SlashMl/s2t_experiments/api_tests/2test_audio.mp3" +# If your audio files aren't accessible via a URL already, you can upload your audio file using this API +upload_url= speect_to_text.upload_audio(file_location) +# choose your service provider: "asembly", "aws", "whisper" +service_provider="aws" +# transcribe +transcribe_id= speect_to_text.transcribe(upload_url,service_provider) +print(transcribe_id) + +status=speect_to_text.status(transcribe_id,service_provider=service_provider) +print(status) \ No newline at end of file