Skip to content

Commit 57c26ba

Browse files
committed
train, upgrade and reset customizations
1 parent 0642308 commit 57c26ba

File tree

3 files changed

+242
-2
lines changed

3 files changed

+242
-2
lines changed

Examples/ServiceExamples/Scripts/ExampleSpeechToText.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,24 @@ private void TestGetCustomization(string customizationID)
9696
m_SpeechToText.GetCustomization(HandleGetCustomization, customizationID);
9797
}
9898

99+
private void TestTrainCustomization(string customizationID)
100+
{
101+
Log.Debug("ExampleSpeechToText", "Attempting to train customization {0}", customizationID);
102+
m_SpeechToText.TrainCustomization(HandleTrainCustomization, customizationID);
103+
}
104+
105+
private void TestUpgradeCustomization(string customizationID)
106+
{
107+
Log.Debug("ExampleSpeechToText", "Attempting to upgrade customization {0}", customizationID);
108+
m_SpeechToText.UpgradeCustomization(HandleUpgradeCustomization, customizationID);
109+
}
110+
111+
private void TestResetCustomization(string customizationID)
112+
{
113+
Log.Debug("ExampleSpeechToText", "Attempting to reset customization {0}", customizationID);
114+
m_SpeechToText.ResetCustomization(HandleResetCustomization, customizationID);
115+
}
116+
99117
private void HandleGetModels(Model[] models)
100118
{
101119
if (models != null)
@@ -262,4 +280,52 @@ private void HandleGetCustomization(Customization customization, string customDa
262280
Log.Debug("ExampleSpeechToText", "Failed to get customization {0}!", m_CreatedCustomizationID);
263281
}
264282
}
283+
284+
private void HandleTrainCustomization(bool success, string customData)
285+
{
286+
if (!string.IsNullOrEmpty(customData))
287+
Log.Debug("ExampleSpeechToText", "custom data: {0}", customData);
288+
289+
if (success)
290+
{
291+
Log.Debug("ExampleSpeechToText", "Train customization {0}!", m_CreatedCustomizationID);
292+
Log.Debug("ExampleSpeechToText", "TrainCustomization() succeeded!");
293+
}
294+
else
295+
{
296+
Log.Debug("ExampleSpeechToText", "Failed to train customization!");
297+
}
298+
}
299+
300+
private void HandleUpgradeCustomization(bool success, string customData)
301+
{
302+
if (!string.IsNullOrEmpty(customData))
303+
Log.Debug("ExampleSpeechToText", "custom data: {0}", customData);
304+
305+
if (success)
306+
{
307+
Log.Debug("ExampleSpeechToText", "Upgrade customization {0}!", m_CreatedCustomizationID);
308+
Log.Debug("ExampleSpeechToText", "UpgradeCustomization() succeeded!");
309+
}
310+
else
311+
{
312+
Log.Debug("ExampleSpeechToText", "Failed to upgrade customization!");
313+
}
314+
}
315+
316+
private void HandleResetCustomization(bool success, string customData)
317+
{
318+
if (!string.IsNullOrEmpty(customData))
319+
Log.Debug("ExampleSpeechToText", "custom data: {0}", customData);
320+
321+
if (success)
322+
{
323+
Log.Debug("ExampleSpeechToText", "Reset customization {0}!", m_CreatedCustomizationID);
324+
Log.Debug("ExampleSpeechToText", "ResetCustomization() succeeded!");
325+
}
326+
else
327+
{
328+
Log.Debug("ExampleSpeechToText", "Failed to reset customization!");
329+
}
330+
}
265331
}

Scripts/Services/SpeechToText/DataModels.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,21 @@ public class CustomLanguage
532532
/// </summary>
533533
public string description { get; set; }
534534
}
535+
536+
/// <summary>
537+
/// The type of words from the custom model's words resource on which to train the model: all (the default) trains the model on all new words, regardless of whether they were extracted from corpora or were added or modified by the user. user trains the model only on new words that were added or modified by the user; the model is not trained on new words extracted from corpora.
538+
/// </summary>
539+
public class WordTypeToAdd
540+
{
541+
/// <summary>
542+
/// All word types.
543+
/// </summary>
544+
public const string ALL = "all";
545+
/// <summary>
546+
/// User word types.
547+
/// </summary>
548+
public const string USER = "user";
549+
}
535550
#endregion
536551

537552
#region Custom Corpora

Scripts/Services/SpeechToText/SpeechToText.cs

Lines changed: 161 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ private void OnDeleteCustomizationResp(RESTConnector.Request req, RESTConnector.
11521152
if (callback == null)
11531153
throw new ArgumentNullException("callback");
11541154
if (string.IsNullOrEmpty(customizationID))
1155-
throw new ArgumentNullException("A customizationID to get a custom voice model.");
1155+
throw new ArgumentNullException("A customizationID to get a custom language model.");
11561156

11571157
GetCustomizationRequest req = new GetCustomizationRequest();
11581158
req.Callback = callback;
@@ -1194,7 +1194,7 @@ private void OnGetCustomizationResp(RESTConnector.Request req, RESTConnector.Res
11941194
}
11951195
catch (Exception e)
11961196
{
1197-
Log.Error("Text To Speech", "CreateCustomization Exception: {0}", e.ToString());
1197+
Log.Error("Speech To Text", "GetCustomization Exception: {0}", e.ToString());
11981198
resp.Success = false;
11991199
}
12001200
}
@@ -1205,12 +1205,171 @@ private void OnGetCustomizationResp(RESTConnector.Request req, RESTConnector.Res
12051205
#endregion
12061206

12071207
#region Train Custom Model
1208+
/// <summary>
1209+
/// This callback is used by the TrainCustomization() function.
1210+
/// </summary>
1211+
/// <param name="success">The success of the call.</param>
1212+
/// <param name="data">Optional custom data.</param>
1213+
public delegate void TrainCustomizationCallback(bool success, string data);
1214+
/// <summary>
1215+
/// Initiates the training of a custom language model with new corpora, words, or both.After adding training data to the custom model with the corpora or words methods, use this method to begin the actual training of the model on the new data.You can specify whether the custom model is to be trained with all words from its words resources or only with words that were added or modified by the user.Only the owner of a custom model can use this method to train the model.
1216+
/// This method is asynchronous and can take on the order of minutes to complete depending on the amount of data on which the service is being trained and the current load on the service.The method returns an HTTP 200 response code to indicate that the training process has begun.
1217+
/// You can monitor the status of the training by using the GET /v1/customizations/{customization_id} method to poll the model's status. Use a loop to check the status every 10 seconds. The method returns a Customization object that includes status and progress fields. A status of available means that the custom model is trained and ready to use. If training is in progress, the progress field indicates the progress of the training as a percentage complete.
1218+
/// Note: For this beta release, the progress field does not reflect the current progress of the training. The field changes from 0 to 100 when training is complete.
1219+
/// Training can fail to start for the following reasons:
1220+
/// No training data (corpora or words) have been added to the custom model.
1221+
/// Pre-processing of corpora to generate a list of out-of-vocabulary (OOV) words is not complete.
1222+
/// Pre-processing of words to validate or auto-generate sounds-like pronunciations is not complete.
1223+
/// One or more words that were added to the custom model have invalid sounds-like pronunciations that you must fix.
1224+
/// Note: This method is currently a beta release that is available for US English only.
1225+
/// </summary>
1226+
/// <param name="callback">The callback.</param>
1227+
/// <param name="customizationID">The requested custom language model's identifier.</param>
1228+
/// <param name="customData">Optional custom data.</param>
1229+
/// <returns></returns>
1230+
public bool TrainCustomization(TrainCustomizationCallback callback, string customizationID, string wordTypeToAdd = WordTypeToAdd.ALL, string customData = default(string))
1231+
{
1232+
if (callback == null)
1233+
throw new ArgumentNullException("callback");
1234+
if (string.IsNullOrEmpty(customizationID))
1235+
throw new ArgumentNullException("A customizationID to train a custom language model.");
1236+
1237+
TrainCustomizationRequest req = new TrainCustomizationRequest();
1238+
req.Callback = callback;
1239+
req.CustomizationID = customizationID;
1240+
req.Data = customData;
1241+
req.Parameters["word_type_to_add"] = wordTypeToAdd;
1242+
req.Headers["Content-Type"] = "application/json";
1243+
req.Headers["Accept"] = "application/json";
1244+
req.Send = Encoding.UTF8.GetBytes("{}");
1245+
req.OnResponse = OnTrainCustomizationResp;
1246+
1247+
string service = "/v1/customizations/{0}/train";
1248+
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, string.Format(service, customizationID));
1249+
if (connector == null)
1250+
return false;
1251+
1252+
return connector.Send(req);
1253+
}
1254+
1255+
private class TrainCustomizationRequest : RESTConnector.Request
1256+
{
1257+
public TrainCustomizationCallback Callback { get; set; }
1258+
public string CustomizationID { get; set; }
1259+
public string Data { get; set; }
1260+
}
1261+
1262+
private void OnTrainCustomizationResp(RESTConnector.Request req, RESTConnector.Response resp)
1263+
{
1264+
if (((TrainCustomizationRequest)req).Callback != null)
1265+
((TrainCustomizationRequest)req).Callback(resp.Success, ((TrainCustomizationRequest)req).Data);
1266+
}
12081267
#endregion
12091268

12101269
#region Reset Custom Model
1270+
/// <summary>
1271+
/// This callback is used by the ResetCustomization() function.
1272+
/// </summary>
1273+
/// <param name="success">The success of the call.</param>
1274+
/// <param name="data">Optional custom data.</param>
1275+
public delegate void ResetCustomizationCallback(bool success, string data);
1276+
/// <summary>
1277+
/// Resets a custom language model by removing all corpora and words from the model.Resetting a custom model initializes the model to its state when it was first created. Metadata such as the name and language of the model are preserved.Only the owner of a custom model can use this method to reset the model.
1278+
/// Note: This method is currently a beta release that is available for US English only.
1279+
/// </summary>
1280+
/// <param name="callback">The callback.</param>
1281+
/// <param name="customizationID">The requested custom language model's identifier.</param>
1282+
/// <param name="customData">Optional custom data.</param>
1283+
/// <returns></returns>
1284+
public bool ResetCustomization(ResetCustomizationCallback callback, string customizationID, string customData = default(string))
1285+
{
1286+
if (callback == null)
1287+
throw new ArgumentNullException("callback");
1288+
if (string.IsNullOrEmpty(customizationID))
1289+
throw new ArgumentNullException("A customizationID to train a reset language model.");
1290+
1291+
ResetCustomizationRequest req = new ResetCustomizationRequest();
1292+
req.Callback = callback;
1293+
req.CustomizationID = customizationID;
1294+
req.Data = customData;
1295+
req.Headers["Content-Type"] = "application/json";
1296+
req.Headers["Accept"] = "application/json";
1297+
req.Send = Encoding.UTF8.GetBytes("{}");
1298+
req.OnResponse = OnResetCustomizationResp;
1299+
1300+
string service = "/v1/customizations/{0}/reset";
1301+
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, string.Format(service, customizationID));
1302+
if (connector == null)
1303+
return false;
1304+
1305+
return connector.Send(req);
1306+
}
1307+
1308+
private class ResetCustomizationRequest : RESTConnector.Request
1309+
{
1310+
public ResetCustomizationCallback Callback { get; set; }
1311+
public string CustomizationID { get; set; }
1312+
public string Data { get; set; }
1313+
}
1314+
1315+
private void OnResetCustomizationResp(RESTConnector.Request req, RESTConnector.Response resp)
1316+
{
1317+
if (((ResetCustomizationRequest)req).Callback != null)
1318+
((ResetCustomizationRequest)req).Callback(resp.Success, ((ResetCustomizationRequest)req).Data);
1319+
}
12111320
#endregion
12121321

12131322
#region Upgrade Custom Model
1323+
/// <summary>
1324+
/// This callback is used by the UpgradeCustomization() function.
1325+
/// </summary>
1326+
/// <param name="success">The success of the call.</param>
1327+
/// <param name="data">Optional custom data.</param>
1328+
public delegate void UpgradeCustomizationCallback(bool success, string data);
1329+
/// <summary>
1330+
/// Upgrades a custom language model to the latest release level of the Speech to Text service. The method bases the upgrade on the latest trained data stored for the custom model. If the corpora or words for the model have changed since the model was last trained, you must use the POST /v1/customizations/{customization_id}/train method to train the model on the new data. Only the owner of a custom model can use this method to upgrade the model.
1331+
/// Note: This method is not currently implemented.It will be added for a future release of the API.
1332+
/// </summary>
1333+
/// <param name="callback">The callback.</param>
1334+
/// <param name="customizationID">The requested custom language model's identifier.</param>
1335+
/// <param name="customData">Optional custom data.</param>
1336+
/// <returns></returns>
1337+
public bool UpgradeCustomization(UpgradeCustomizationCallback callback, string customizationID, string customData = default(string))
1338+
{
1339+
if (callback == null)
1340+
throw new ArgumentNullException("callback");
1341+
if (string.IsNullOrEmpty(customizationID))
1342+
throw new ArgumentNullException("A customizationID to upgrade a custom language model.");
1343+
1344+
UpgradeCustomizationRequest req = new UpgradeCustomizationRequest();
1345+
req.Callback = callback;
1346+
req.CustomizationID = customizationID;
1347+
req.Data = customData;
1348+
req.Headers["Content-Type"] = "application/json";
1349+
req.Headers["Accept"] = "application/json";
1350+
req.Send = Encoding.UTF8.GetBytes("{}");
1351+
req.OnResponse = OnResetCustomizationResp;
1352+
1353+
string service = "/v1/customizations/{0}/upgrade";
1354+
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, string.Format(service, customizationID));
1355+
if (connector == null)
1356+
return false;
1357+
1358+
return connector.Send(req);
1359+
}
1360+
1361+
private class UpgradeCustomizationRequest : RESTConnector.Request
1362+
{
1363+
public UpgradeCustomizationCallback Callback { get; set; }
1364+
public string CustomizationID { get; set; }
1365+
public string Data { get; set; }
1366+
}
1367+
1368+
private void OnUpgradeCustomizationResp(RESTConnector.Request req, RESTConnector.Response resp)
1369+
{
1370+
if (((UpgradeCustomizationRequest)req).Callback != null)
1371+
((UpgradeCustomizationRequest)req).Callback(resp.Success, ((UpgradeCustomizationRequest)req).Data);
1372+
}
12141373
#endregion
12151374

12161375
#region Custom Corpora

0 commit comments

Comments
 (0)