# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# +
import requests
import json
import codecs
import sys, time
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# -

username = "xxxxxxxxxxx"
password = "<PASSWORD>"
headers = {'Content-Type' : "application/json"}

# +
print "\nCreating custom mmodel..."
data = {"name" : "Custom model #2", "base_model_name" : "ja-JP_BroadbandModel", "description" : "300312-01 custom model and Add a single user word"}
uri = "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations"
jsonObject = json.dumps(data).encode('utf-8')
resp = requests.post(uri, auth=(username,password), verify=False, headers=headers, data=jsonObject)

print "Model creation returns: ", resp.status_code
if resp.status_code != 201:
   print "Failed to create model"
   print resp.text
# -

respJson = resp.json()
customID = respJson['customization_id']
print "Model customization_id: ", customID

corpus_file = "../corpus_utf8.txt"
corpus_name = "corpus1"

# +
print "\nAdding corpus file..."
uri = "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/"+customID+"/corpora/"+corpus_name
with open(corpus_file, 'rb') as f:
   r = requests.post(uri, auth=(username,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

# +
print "Checking status of corpus analysis..."
uri = "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/"+customID+"/corpora/"+corpus_name
r = requests.get(uri, auth=(username,password), verify=False, headers=headers)
respJson = r.json()
status = respJson['status']
time_to_run = 10
while (status != 'analyzed'):
    time.sleep(10)
    r = requests.get(uri, auth=(username,password), verify=False, headers=headers)
    respJson = r.json()
    status = respJson['status']
    print "status: ", status, "(", time_to_run, ")"
    time_to_run += 10

print "Corpus analysis done!"
# -

print "\nListing words..."
uri = "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/"+customID+"/words?sort=count"
r = requests.get(uri, auth=(username,password), verify=False, headers=headers)
print "Listing words returns: ", r.status_code
file=codecs.open(customID+".OOVs.corpus", 'wb', 'utf-8')
file.write(r.text)
print "Words list from added corpus saved in file: "+customID+".OOVs.corpus"

# +
print "\nAdding multiple words..."
data = {"words" : [{"word" : "ごばん", "sounds_like" : ["ゴバン"], "display_as" : "5番"},{"word" : "にじゅよんごう", "sounds_like" : ["ニジュヨンゴウ"], "display_as" : "二十四号"}, {"word" : "ふくむらのりちか", "sounds_like" : ["フクムラノリチカ"], "display_as" : "福村教親"}]}
uri = "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/"+customID+"/words"
jsonObject = json.dumps(data).encode('utf-8')
r = requests.post(uri, auth=(username,password), verify=False, headers=headers, data=jsonObject)

print "Adding multiple words returns: ", r.status_code

# +
uri = "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/"+customID
r = requests.get(uri, auth=(username,password), verify=False, headers=headers)
respJson = r.json()
status = respJson['status']
print "Checking status of model for multiple words..."
time_to_run = 10
while (status != 'ready'):
    time.sleep(10)
    r = requests.get(uri, auth=(username,password), verify=False, headers=headers)
    respJson = r.json()
    status = respJson['status']
    print "status: ", status, "(", time_to_run, ")"
    time_to_run += 10

print "Multiple words added!"

# +
print "\nListing words..."
uri = "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/"+customID+"/words?word_type=user&sort=alphabetical"
r = requests.get(uri, auth=(username,password), verify=False, headers=headers)

print "Listing user-added words returns: ", r.status_code
file=codecs.open(customID+".OOVs.user", 'wb', 'utf-8')
file.write(r.text)
print "Words list from user-added words saved in file: "+customID+".OOVs.user"

# +
print "\nTraining custom model..."
uri = "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/"+customID+"/train"
data = {}
jsonObject = json.dumps(data).encode('utf-8')
r = requests.post(uri, auth=(username,password), verify=False, data=jsonObject)

print "Training request returns: ", r.status_code
if r.status_code != 200:
   print "Training failed to start - exiting!"

# +
uri = "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/"+customID
r = requests.get(uri, auth=(username,password), verify=False, headers=headers)
respJson = r.json()
status = respJson['status']
time_to_run = 10
while (status != 'available'):
    time.sleep(10)
    r = requests.get(uri, auth=(username,password), verify=False, headers=headers)
    respJson = r.json()
    status = respJson['status']
    print "status: ", status, "(", time_to_run, ")"
    time_to_run += 10

print "Training complete!"

print "\nGetting custom models..."
uri = "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations"
r = requests.get(uri, auth=(username,password), verify=False, headers=headers)

print "Get models returns: ", r.status_code
print r.text
# -

print "\nGetting custom models..."
uri = "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations"
r = requests.get(uri, auth=(username,password), verify=False, headers=headers)
print "Get models returns: ", r.status_code
print r.text



StarcoderdataJupyterScriptsDedupFiltered