Search in sources :

Example 1 with SpeechGeneratorConfiguration

use of com.amplifyframework.predictions.aws.configuration.SpeechGeneratorConfiguration in project amplify-android by aws-amplify.

the class AWSPollyService method synthesizeSpeech.

private InputStream synthesizeSpeech(String text, AWSVoiceType voiceType) throws PredictionsException {
    final String languageCode;
    final String voiceId;
    if (AWSVoiceType.UNKNOWN.equals(voiceType)) {
        // Obtain voice + language from plugin configuration by default
        SpeechGeneratorConfiguration config = pluginConfiguration.getSpeechGeneratorConfiguration();
        languageCode = config.getLanguage();
        voiceId = config.getVoice();
    } else {
        // Override configuration defaults if explicitly specified in the options
        languageCode = voiceType.getLanguageCode();
        voiceId = voiceType.getName();
    }
    SynthesizeSpeechRequest request = new SynthesizeSpeechRequest().withText(text).withTextType(TextType.Text).withLanguageCode(languageCode).withVoiceId(voiceId).withOutputFormat(OutputFormat.Mp3).withSampleRate(Integer.toString(MP3_SAMPLE_RATE));
    // Synthesize speech from given text via Amazon Polly
    final SynthesizeSpeechResult result;
    try {
        result = polly.synthesizeSpeech(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("AWS Polly encountered an error while synthesizing speech.", serviceException, "See attached service exception for more details.");
    }
    return result.getAudioStream();
}
Also used : SynthesizeSpeechRequest(com.amazonaws.services.polly.model.SynthesizeSpeechRequest) SpeechGeneratorConfiguration(com.amplifyframework.predictions.aws.configuration.SpeechGeneratorConfiguration) AmazonClientException(com.amazonaws.AmazonClientException) PredictionsException(com.amplifyframework.predictions.PredictionsException) SynthesizeSpeechResult(com.amazonaws.services.polly.model.SynthesizeSpeechResult)

Example 2 with SpeechGeneratorConfiguration

use of com.amplifyframework.predictions.aws.configuration.SpeechGeneratorConfiguration in project amplify-android by aws-amplify.

the class AWSPredictionsPluginConfigurationTest method testSpeechGeneratorConfiguration.

/**
 * Test that configuration with explicit "speechGenerator" section creates
 * customized text-to-speech configuration instance.
 * @throws Exception if configuration fails
 */
@Test
public void testSpeechGeneratorConfiguration() throws Exception {
    JSONObject json = Resources.readAsJson("configuration-with-text-to-speech.json");
    AWSPredictionsPluginConfiguration pluginConfig = AWSPredictionsPluginConfiguration.fromJson(json);
    // Custom text-to-speech configuration
    SpeechGeneratorConfiguration speechGeneratorConfig = pluginConfig.getSpeechGeneratorConfiguration();
    assertNotNull(speechGeneratorConfig);
    assertEquals("Aditi", speechGeneratorConfig.getVoice());
    assertEquals("en-IN", speechGeneratorConfig.getLanguage());
    assertEquals(NetworkPolicy.AUTO, speechGeneratorConfig.getNetworkPolicy());
}
Also used : JSONObject(org.json.JSONObject) SpeechGeneratorConfiguration(com.amplifyframework.predictions.aws.configuration.SpeechGeneratorConfiguration) Test(org.junit.Test)

Example 3 with SpeechGeneratorConfiguration

use of com.amplifyframework.predictions.aws.configuration.SpeechGeneratorConfiguration in project amplify-android by aws-amplify.

the class AWSPredictionsPluginConfiguration method fromJson.

/**
 * Constructs an instance of {@link AWSPredictionsPluginConfiguration} from
 * the plugin configuration JSON object.
 * @param configurationJson the plugin configuration
 * @return the configuration object for AWS Predictions Plugin
 * @throws PredictionsException if configuration is missing or malformed
 */
@NonNull
static AWSPredictionsPluginConfiguration fromJson(JSONObject configurationJson) throws PredictionsException {
    if (configurationJson == null) {
        throw new PredictionsException("Could not locate predictions configuration for AWS Predictions Plugin.", "Verify that amplifyconfiguration.json contains a section for \"awsPredictionsPlugin\".");
    }
    final Region defaultRegion;
    final SpeechGeneratorConfiguration speechGeneratorConfiguration;
    final TranslateTextConfiguration translateTextConfiguration;
    final IdentifyLabelsConfiguration identifyLabelsConfiguration;
    final IdentifyEntitiesConfiguration identifyEntitiesConfiguration;
    final IdentifyTextConfiguration identifyTextConfiguration;
    final InterpretTextConfiguration interpretConfiguration;
    try {
        // Get default region
        String regionString = configurationJson.getString(ConfigKey.DEFAULT_REGION.key());
        defaultRegion = Region.getRegion(regionString);
        if (configurationJson.has(ConfigKey.CONVERT.key())) {
            JSONObject convertJson = configurationJson.getJSONObject(ConfigKey.CONVERT.key());
            speechGeneratorConfiguration = SpeechGeneratorConfiguration.fromJson(convertJson);
            translateTextConfiguration = TranslateTextConfiguration.fromJson(convertJson);
        } else {
            speechGeneratorConfiguration = null;
            translateTextConfiguration = null;
        }
        if (configurationJson.has(ConfigKey.IDENTIFY.key())) {
            JSONObject identifyJson = configurationJson.getJSONObject(ConfigKey.IDENTIFY.key());
            identifyLabelsConfiguration = IdentifyLabelsConfiguration.fromJson(identifyJson);
            identifyEntitiesConfiguration = IdentifyEntitiesConfiguration.fromJson(identifyJson);
            identifyTextConfiguration = IdentifyTextConfiguration.fromJson(identifyJson);
        } else {
            identifyLabelsConfiguration = null;
            identifyEntitiesConfiguration = null;
            identifyTextConfiguration = null;
        }
        if (configurationJson.has(ConfigKey.INTERPRET.key())) {
            JSONObject interpretJson = configurationJson.getJSONObject(ConfigKey.INTERPRET.key());
            interpretConfiguration = InterpretTextConfiguration.fromJson(interpretJson);
        } else {
            interpretConfiguration = null;
        }
    } catch (JSONException | IllegalArgumentException exception) {
        throw new PredictionsException("Issue encountered while parsing configuration JSON", exception, "Check the attached exception for more details.");
    }
    return new AWSPredictionsPluginConfiguration(defaultRegion, speechGeneratorConfiguration, translateTextConfiguration, identifyLabelsConfiguration, identifyEntitiesConfiguration, identifyTextConfiguration, interpretConfiguration);
}
Also used : IdentifyEntitiesConfiguration(com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration) SpeechGeneratorConfiguration(com.amplifyframework.predictions.aws.configuration.SpeechGeneratorConfiguration) TranslateTextConfiguration(com.amplifyframework.predictions.aws.configuration.TranslateTextConfiguration) JSONException(org.json.JSONException) InterpretTextConfiguration(com.amplifyframework.predictions.aws.configuration.InterpretTextConfiguration) JSONObject(org.json.JSONObject) IdentifyLabelsConfiguration(com.amplifyframework.predictions.aws.configuration.IdentifyLabelsConfiguration) IdentifyTextConfiguration(com.amplifyframework.predictions.aws.configuration.IdentifyTextConfiguration) Region(com.amazonaws.regions.Region) PredictionsException(com.amplifyframework.predictions.PredictionsException) NonNull(androidx.annotation.NonNull)

Aggregations

SpeechGeneratorConfiguration (com.amplifyframework.predictions.aws.configuration.SpeechGeneratorConfiguration)3 PredictionsException (com.amplifyframework.predictions.PredictionsException)2 JSONObject (org.json.JSONObject)2 NonNull (androidx.annotation.NonNull)1 AmazonClientException (com.amazonaws.AmazonClientException)1 Region (com.amazonaws.regions.Region)1 SynthesizeSpeechRequest (com.amazonaws.services.polly.model.SynthesizeSpeechRequest)1 SynthesizeSpeechResult (com.amazonaws.services.polly.model.SynthesizeSpeechResult)1 IdentifyEntitiesConfiguration (com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration)1 IdentifyLabelsConfiguration (com.amplifyframework.predictions.aws.configuration.IdentifyLabelsConfiguration)1 IdentifyTextConfiguration (com.amplifyframework.predictions.aws.configuration.IdentifyTextConfiguration)1 InterpretTextConfiguration (com.amplifyframework.predictions.aws.configuration.InterpretTextConfiguration)1 TranslateTextConfiguration (com.amplifyframework.predictions.aws.configuration.TranslateTextConfiguration)1 JSONException (org.json.JSONException)1 Test (org.junit.Test)1