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/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..9b50105 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 SlashML + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 05012b9..5cb926b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,66 @@ -# slashml-python-client +# SlashML Python client +[SlashML](https://www.slashml.com/) +# Introduction +## Overview +This is a Python client (SDK) for SlashML. It allows the user to use the apps available and active in [SlashML-Dashboard](https://www.slashml.com/dashboard). +The apps can be together in the same code. For example, if the user wants to transcribe an audio file and get a summary, they can call speechtotext followed by summarization. -to be updated to guide users +State-of-the-art AI models from several service providers are available. At SlashML, we also do the benchmarking on these models for the user. This will give them an idea of the best service provider for their application. For the full list of the available models thru SlashML, click [here](##availlable-service-providers) + + + +to lets you run transcription jobs from your Python code or Jupyter notebook. The transcription can be done with three lines of code +``` +import slashml + +speect_to_text = slashml.SpeechToText() +transcribe_id= speect_to_text.transcribe(audio_url,service_provider="aws") +status=speect_to_text.status(transcribe_id,service_provider=service_provider) + +``` + +## Set up and usage +There is a daily limit (throttling) on the number of calls the user performs, the code runs without specifying a token (API key), the throttling kicks in and prevents new jobs after exceeding 10 calls per minute. + +If the user intends on using the service more frequently, it is recommended to generate an token or API key from [SlashML-Dashboard](https://www.slashml.com/dashboard). This way, the throttling limit will increase. + +Sign up and Grab your token from [SlashML-Dashboard](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 Quickstart tutorial): + +In your terminal +``` +export SLASHML_API_KEY=[token] +``` +or including it in your python code as follows: +``` + +import os +os.environ["SLASHML_API_KEY"] = "slashml_api_token" + +``` + +If the user preferes using the API calls directly, the documentation is available [here](https://www.slashml.com/dashboard). +## Availlable service providers + +### Speech-to-text + +AssemblyAI +AWS +Whisper (OpenAI) + +### Summarization +hugging-face summarization based on Meta... include links +# Quickstart tutorial + +## Introduction + +### Start with initializing the service + +### Specify your service provider + +In this step, benchmarking will help you decide which service provider is the best for you. +For speech-to-text: "assembly", "aws", "whisper" +For summarization: "hugging-face", "openai" + + +et voilĂ , Next steps: +- pip install slashml 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/LICENSE.txt b/src/LICENSE.txt new file mode 100644 index 0000000..9b50105 --- /dev/null +++ b/src/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 SlashML + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. 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/slashml.py b/src/slashml.py new file mode 100644 index 0000000..481dbc7 --- /dev/null +++ b/src/slashml.py @@ -0,0 +1,85 @@ +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) + # Check the status code of the response + if response.status_code == 200: + return response.json()["id"] + + elif response.status_code == 429: + return "THROTTLED" + + elif response.status_code == 404: + return "NOT FOUND" + + elif response.status_code == 500: + return "SERVER ERROR" + else: + return "UNKNOWN ERROR" + + 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 == 429: + return "THROTTLED" + + elif response.status_code == 404: + return "NOT FOUND" + + elif response.status_code == 500: + return "SERVER ERROR" + else: + return "UNKNOWN ERROR" \ 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..ef6e690 --- /dev/null +++ b/src/test_speechtotext_1.py @@ -0,0 +1,22 @@ +import slashml +import os + +#API KEY (optional) + +# set environment path +import os +os.environ["SLASHML_API_KEY"] = "0d91bfede9c5c9de6ff1d5610ef71c3b6d5be9ee" +# Initialize SlashML +speect_to_text = slashml.SpeechToText() +# optional local file to upload, if not an accessible url +file_location="/Users/JJneid/Desktop/SlashMl/Benchmarking/podcast1/podcast1_long_trim.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: "assembly", "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