-
Notifications
You must be signed in to change notification settings - Fork 1
Update sdk #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Update sdk #13
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wait for status to be |
||
| ``` | ||
| 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 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this code is outdated |
||
| 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 | ||
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| requests==2.28.1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import requests | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the *.pyc file above. |
||
| 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 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this variable is not required |
||
| 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") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comments |
||
| 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") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. limits typo
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. point them to the relevant webpage where they can get the API token |
||
|
|
||
| def upload_audio(self, file_location:str,header=None): | ||
| headers = self.HEADERS | ||
| files=[ | ||
| ('audio',('test_audio.mp3',open(file_location,'rb'),'audio/mpeg')) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. improve the formatting |
||
| ] | ||
| response = requests.post(self.SLASHML_UPLOAD_URL, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if the response is an error. |
||
| headers=headers,files=files) | ||
| return response.json()["upload_url"] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if the json() does not contain |
||
|
|
||
| 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, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. improve formatting. |
||
| "service_provider": service_provider | ||
| } | ||
| response = requests.post(self.SLASHML_TRANSCRIPT_URL, headers=headers, data=payload) | ||
| return response.json()["id"] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment as upload function, what if the |
||
|
|
||
| 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: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so we need a uniform pattern for how to deal with 400s, 500s. Either the same should be done for the other endpoints as well, or they should not be done for the other endpoints. |
||
| 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"] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comments |
||
|
|
||
|
|
||
| #### chgeck the code of the reponse, ifit is not 200, print back the error. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comments |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import speechtotext | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this file altogether. |
||
| 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"]) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import speechtotext | ||
| import os | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. either convert this file to proper test_cases, or remove this file altogether. If the purpose is to show how to use the API, move this file to a samples folder and create a sample application in there, along with the relevant file. |
||
|
|
||
| #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" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this location is specific to your computer, the file will not work on someone else's computer. |
||
| # 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) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.