# -*- coding: utf-8 -*- import requests import json import codecs import sys, time from requests.packages.urllib3.exceptions import InsecureRequestWarning import env requests.packages.urllib3.disable_warnings(InsecureRequestWarning) ########################################################################## # Initiates the training of a custom acoustic model with new audio resources # using a language model as the base # A status of available means that the custom model is trained and ready to use. ########################################################################## print "\nTrain custom acoustic model..." headers = {'Content-Type' : "application/json"} uri = "https://stream.watsonplatform.net/speech-to-text/api/v1/acoustic_customizations/"+env.get_acoustic_id()+"/train?custom_language_model_id="+env.get_language_id() r = requests.post(uri, auth=(env.get_username(),env.get_password()), verify=False, headers=headers) print "Train acoustic model returns: ", r.status_code sys.exit(0)
import requests import json import codecs import sys, time from requests.packages.urllib3.exceptions import InsecureRequestWarning import env requests.packages.urllib3.disable_warnings(InsecureRequestWarning) ########################################################################## # Delete a custom language model ########################################################################## print("\nDeleting custom language model: ") headers = {'Content-Type': "application/json"} uri = "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/" + env.get_language_id( ) resp = requests.delete(uri, auth=(env.get_username(), env.get_password()), verify=False, headers=headers) print("Delete language models returns: ", resp.status_code) if resp.status_code != 200: print("Failed to delete language model") print(resp.text) sys.exit(-1) sys.exit(0)
import json import codecs import sys, time from requests.packages.urllib3.exceptions import InsecureRequestWarning import env requests.packages.urllib3.disable_warnings(InsecureRequestWarning) ########################################################################## # Resets a custom language model by removing all corpora, grammars, and words from # the model. Resetting a custom language model initializes the model to its state # when it was first created. Metadata such as the name and language of the model # are preserved, but the model's words resource is removed and must be re-created. # You must use credentials for the instance of the service that owns a model to reset it. ########################################################################## print "\nResetting custom language model..." headers = {'Content-Type': "application/json"} uri = "https://gateway-lon.watsonplatform.net/speech-to-text/api/v1/customizations/" + env.get_language_id( ) + "/reset" r = requests.post(uri, auth=(env.get_username(), env.get_password()), verify=False, headers=headers) print "Reset model returns: ", r.status_code print r.text sys.exit(0)
import json import codecs import sys, time from requests.packages.urllib3.exceptions import InsecureRequestWarning import env requests.packages.urllib3.disable_warnings(InsecureRequestWarning) ########################################################################## # Resets a custom language model by removing all corpora, grammars, and words from # the model. Resetting a custom language model initializes the model to its state # when it was first created. Metadata such as the name and language of the model # are preserved, but the model's words resource is removed and must be re-created. # You must use credentials for the instance of the service that owns a model to reset it. ########################################################################## print("\nResetting custom language model...") headers = {'Content-Type': "application/json"} uri = env.get_endpoint() + "/v1/customizations/" + env.get_language_id( ) + "/reset" r = requests.post(uri, auth=(env.get_username(), env.get_password()), verify=False, headers=headers) print("Reset model returns: ", r.status_code) print(r.text) sys.exit(0)
# -*- coding: utf-8 -*- import requests import json import codecs import sys, time from requests.packages.urllib3.exceptions import InsecureRequestWarning import env requests.packages.urllib3.disable_warnings(InsecureRequestWarning) ########################################################################## # List the corpus for a custom langage model ########################################################################## print("\nGetting corpus ...") headers = {'Content-Type' : "application/json"} uri = env.get_endpoint() + "/v1/customizations/"+env.get_language_id()+"/corpora/" r = requests.get(uri, auth=(env.get_username(),env.get_password()), verify=False, headers=headers) print("Get corpus returns: ", r.status_code) if r.status_code != 200: print("Failed to get corpus") sys.exit(-1) else: print(r.text) sys.exit(0)
# -*- coding: utf-8 -*- import requests import json import codecs import sys, time from requests.packages.urllib3.exceptions import InsecureRequestWarning import env requests.packages.urllib3.disable_warnings(InsecureRequestWarning) ########################################################################## # Initiates the training of a custom acoustic model with new audio resources # using a language model as the base # A status of available means that the custom model is trained and ready to use. ########################################################################## print("\nTrain custom acoustic model...") headers = {'Content-Type' : "application/json"} uri = env.get_endpoint() + "/v1/acoustic_customizations/"+env.get_acoustic_id()+"/train?custom_language_model_id="+env.get_language_id() r = requests.post(uri, auth=(env.get_username(),env.get_password()), verify=False, headers=headers) print("Train acoustic model returns: ", r.status_code) sys.exit(0)
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) ########################################################################## # Step 1: Add a corpus file (plain text file) ########################################################################## # 'dictation_fixed.txt' is name of local file containing the corpus to be uploaded # 'dictation-1' is the name of the new corpus # >>>> REPLACE THE VALUES BELOW WITH YOUR OWN CORPUS FILE AND NAME #corpus_file = "dictation_fixed.txt" #corpus_name = "dictation-1" corpus_file = env.get_arg("corpus filename") print "\nAdding corpus file: " + corpus_file headers = {'Content-Type': "application/json"} uri = "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/" + env.get_language_id( ) + "/corpora/" + corpus_file with open(corpus_file, 'rb') as f: r = requests.post(uri, auth=(env.get_username(), env.get_password()), verify=False, headers=headers, data=f) print "Adding corpus file returns: ", r.status_code if r.status_code != 201: print "Failed to add corpus file" print r.text sys.exit(-1) ########################################################################## # Step 2: Get status of corpus file just added.
# -*- coding: utf-8 -*- import requests import json import codecs import sys, time from requests.packages.urllib3.exceptions import InsecureRequestWarning import env requests.packages.urllib3.disable_warnings(InsecureRequestWarning) ########################################################################## # Initiates the training of a custom acoustic model with new audio resources # using a language model as the base # A status of available means that the custom model is trained and ready to use. ########################################################################## print "\nTrain custom acoustic model..." headers = {'Content-Type': "application/json"} uri = "https://gateway-lon.watsonplatform.net/speech-to-text/api/v1/acoustic_customizations/" + env.get_acoustic_id( ) + "/train?custom_language_model_id=" + env.get_language_id() r = requests.post(uri, auth=(env.get_username(), env.get_password()), verify=False, headers=headers) print "Train acoustic model returns: ", r.status_code sys.exit(0)
import env requests.packages.urllib3.disable_warnings(InsecureRequestWarning) headers = {'Content-Type': "audio/wav"} ########################################################################## # Transcribe an audio file using a custom language and acoustic model # Need to specify in the API call: # - the base model # - the language_customization_id # - the acoustic_customization_id ########################################################################## print "\nTranscribe an audio using a custom language custom model..." audio_file = env.get_arg("audio file to transcribe") uri = "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?model=en-US_NarrowbandModel&language_customization_id=" + env.get_language_id( ) + "&acoustic_customization_id=" + env.get_acoustic_id() with open(audio_file, 'rb') as f: r = requests.post(uri, auth=(env.get_username(), env.get_password()), verify=False, headers=headers, data=f) print "Transcribe returns: ", r.status_code print r.text sys.exit(0)