Search in sources :

Example 16 with IamAuthenticator

use of com.ibm.cloud.sdk.core.security.IamAuthenticator in project java-sdk by watson-developer-cloud.

the class RecognizeUsingWebSocketsWithSpeakerLabelsExample method main.

/**
 * The main method.
 *
 * @param args the arguments
 * @throws FileNotFoundException the file not found exception
 * @throws InterruptedException the interrupted exception
 */
public static void main(String[] args) throws FileNotFoundException, InterruptedException {
    FileInputStream audio = new FileInputStream("src/test/resources/speech_to_text/twospeakers.wav");
    Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
    SpeechToText service = new SpeechToText(authenticator);
    RecognizeWithWebsocketsOptions options = new RecognizeWithWebsocketsOptions.Builder().audio(audio).interimResults(true).speakerLabels(true).model(RecognizeOptions.Model.EN_US_NARROWBANDMODEL).contentType(HttpMediaType.AUDIO_WAV).build();
    RecoTokens recoTokens = new RecoTokens();
    service.recognizeUsingWebSocket(options, new BaseRecognizeCallback() {

        @Override
        public void onTranscription(SpeechRecognitionResults speechResults) {
            recoTokens.add(speechResults);
        }

        @Override
        public void onDisconnected() {
            lock.countDown();
        }
    });
    lock.await(1, TimeUnit.MINUTES);
}
Also used : IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) BaseRecognizeCallback(com.ibm.watson.speech_to_text.v1.websocket.BaseRecognizeCallback) RecognizeWithWebsocketsOptions(com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions) FileInputStream(java.io.FileInputStream) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) SpeechRecognitionResults(com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults)

Example 17 with IamAuthenticator

use of com.ibm.cloud.sdk.core.security.IamAuthenticator in project java-sdk by watson-developer-cloud.

the class SpeechToTextExample method main.

public static void main(String[] args) throws FileNotFoundException {
    Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
    SpeechToText service = new SpeechToText(authenticator);
    File audio = new File("src/test/resources/speech_to_text/sample1.wav");
    RecognizeOptions options = new RecognizeOptions.Builder().audio(audio).contentType(HttpMediaType.AUDIO_WAV).build();
    SpeechRecognitionResults transcript = service.recognize(options).execute().getResult();
    System.out.println(transcript);
}
Also used : IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) File(java.io.File) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) SpeechRecognitionResults(com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults) RecognizeOptions(com.ibm.watson.speech_to_text.v1.model.RecognizeOptions)

Example 18 with IamAuthenticator

use of com.ibm.cloud.sdk.core.security.IamAuthenticator in project java-sdk by watson-developer-cloud.

the class TextToSpeechExample method main.

public static void main(String[] args) {
    Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
    TextToSpeech service = new TextToSpeech(authenticator);
    Voices voices = service.listVoices().execute().getResult();
    System.out.println(voices);
}
Also used : IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) Voices(com.ibm.watson.text_to_speech.v1.model.Voices)

Example 19 with IamAuthenticator

use of com.ibm.cloud.sdk.core.security.IamAuthenticator in project java-sdk by watson-developer-cloud.

the class TranslateAndSynthesizeExample method main.

public static void main(String[] args) throws IOException {
    Authenticator ltAuthenticator = new IamAuthenticator("<iam_api_key>");
    LanguageTranslator translator = new LanguageTranslator("2019-11-22", ltAuthenticator);
    Authenticator ttsAuthenticator = new IamAuthenticator("<iam_api_key>");
    TextToSpeech synthesizer = new TextToSpeech(ttsAuthenticator);
    String text = "Greetings from Watson Developer Cloud";
    // translate
    TranslateOptions translateOptions = new TranslateOptions.Builder().addText(text).source(Language.ENGLISH).target(Language.SPANISH).build();
    TranslationResult translationResult = translator.translate(translateOptions).execute().getResult();
    String translation = translationResult.getTranslations().get(0).getTranslation();
    // synthesize
    SynthesizeOptions synthesizeOptions = new SynthesizeOptions.Builder().text(translation).voice(SynthesizeOptions.Voice.EN_US_LISAVOICE).accept(HttpMediaType.AUDIO_WAV).build();
    InputStream in = synthesizer.synthesize(synthesizeOptions).execute().getResult();
    writeToFile(WaveUtils.reWriteWaveHeader(in), new File("output.wav"));
}
Also used : LanguageTranslator(com.ibm.watson.language_translator.v3.LanguageTranslator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) InputStream(java.io.InputStream) TranslateOptions(com.ibm.watson.language_translator.v3.model.TranslateOptions) TranslationResult(com.ibm.watson.language_translator.v3.model.TranslationResult) File(java.io.File) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) SynthesizeOptions(com.ibm.watson.text_to_speech.v1.model.SynthesizeOptions)

Example 20 with IamAuthenticator

use of com.ibm.cloud.sdk.core.security.IamAuthenticator in project java-sdk by watson-developer-cloud.

the class DiscoveryQueryExample method main.

public static void main(String[] args) {
    Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
    Discovery discovery = new Discovery("2019-04-30", authenticator);
    String environmentId = null;
    String configurationId = null;
    String collectionId = null;
    String documentId = null;
    // See if an environment already exists
    System.out.println("Check if environment exists");
    ListEnvironmentsOptions listOptions = new ListEnvironmentsOptions.Builder().build();
    ListEnvironmentsResponse listResponse = discovery.listEnvironments(listOptions).execute().getResult();
    for (Environment environment : listResponse.getEnvironments()) {
        // look for an existing environment that isn't read only
        if (!environment.isReadOnly()) {
            environmentId = environment.getEnvironmentId();
            System.out.println("Found existing environment ID: " + environmentId);
            break;
        }
    }
    if (environmentId == null) {
        System.out.println("No environment found, creating new one...");
        // no environment found, create a new one (assuming we are a FREE plan)
        String environmentName = "watson_developer_cloud_test_environment";
        CreateEnvironmentOptions createOptions = new CreateEnvironmentOptions.Builder().name(environmentName).size(String.valueOf(0L)).build();
        Environment createResponse = discovery.createEnvironment(createOptions).execute().getResult();
        environmentId = createResponse.getEnvironmentId();
        System.out.println("Created new environment ID: " + environmentId);
        // wait for environment to be ready
        System.out.println("Waiting for environment to be ready...");
        boolean environmentReady = false;
        while (!environmentReady) {
            GetEnvironmentOptions getEnvironmentOptions = new GetEnvironmentOptions.Builder(environmentId).build();
            Environment getEnvironmentResponse = discovery.getEnvironment(getEnvironmentOptions).execute().getResult();
            environmentReady = getEnvironmentResponse.getStatus().equals(Environment.Status.ACTIVE);
            try {
                if (!environmentReady) {
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException("Interrupted", e);
            }
        }
        System.out.println("Environment Ready!");
    }
    // find the default configuration
    System.out.println("Finding the default configuration");
    ListConfigurationsOptions listConfigsOptions = new ListConfigurationsOptions.Builder(environmentId).build();
    ListConfigurationsResponse listConfigsResponse = discovery.listConfigurations(listConfigsOptions).execute().getResult();
    for (Configuration configuration : listConfigsResponse.getConfigurations()) {
        if (configuration.name().equals(DEFAULT_CONFIG_NAME)) {
            configurationId = configuration.configurationId();
            System.out.println("Found default configuration ID: " + configurationId);
            break;
        }
    }
    // create a new collection
    System.out.println("Creating a new collection...");
    String collectionName = "my_watson_developer_cloud_collection";
    CreateCollectionOptions createCollectionOptions = new CreateCollectionOptions.Builder(environmentId, collectionName).configurationId(configurationId).build();
    Collection collection = discovery.createCollection(createCollectionOptions).execute().getResult();
    collectionId = collection.getCollectionId();
    System.out.println("Created a collection ID: " + collectionId);
    // wait for the collection to be "available"
    System.out.println("Waiting for collection to be ready...");
    boolean collectionReady = false;
    while (!collectionReady) {
        GetCollectionOptions getCollectionOptions = new GetCollectionOptions.Builder(environmentId, collectionId).build();
        Collection getCollectionResponse = discovery.getCollection(getCollectionOptions).execute().getResult();
        collectionReady = getCollectionResponse.getStatus().equals(Collection.Status.ACTIVE);
        try {
            if (!collectionReady) {
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted", e);
        }
    }
    System.out.println("Collection Ready!");
    // add a document
    System.out.println("Creating a new document...");
    String documentJson = "{\"field\":\"value\"}";
    InputStream documentStream = new ByteArrayInputStream(documentJson.getBytes());
    AddDocumentOptions.Builder createDocumentBuilder = new AddDocumentOptions.Builder(environmentId, collectionId);
    createDocumentBuilder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
    DocumentAccepted createDocumentResponse = discovery.addDocument(createDocumentBuilder.build()).execute().getResult();
    documentId = createDocumentResponse.getDocumentId();
    System.out.println("Created a document ID: " + documentId);
    // wait for document to be ready
    System.out.println("Waiting for document to be ready...");
    boolean documentReady = false;
    while (!documentReady) {
        GetDocumentStatusOptions getDocumentStatusOptions = new GetDocumentStatusOptions.Builder(environmentId, collectionId, documentId).build();
        DocumentStatus getDocumentResponse = discovery.getDocumentStatus(getDocumentStatusOptions).execute().getResult();
        documentReady = !getDocumentResponse.getStatus().equals(DocumentStatus.Status.PROCESSING);
        try {
            if (!documentReady) {
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted");
        }
    }
    System.out.println("Document Ready!");
    // query document
    System.out.println("Querying the collection...");
    QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
    queryBuilder.query("field:value");
    QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute().getResult();
    // print out the results
    System.out.println("Query Results:");
    System.out.println(queryResponse);
    // cleanup the collection created
    System.out.println("Deleting the collection...");
    DeleteCollectionOptions deleteOptions = new DeleteCollectionOptions.Builder(environmentId, collectionId).build();
    discovery.deleteCollection(deleteOptions).execute();
    System.out.println("Collection deleted!");
    System.out.println("Discovery example finished");
}
Also used : Configuration(com.ibm.watson.discovery.v1.model.Configuration) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) QueryOptions(com.ibm.watson.discovery.v1.model.QueryOptions) GetEnvironmentOptions(com.ibm.watson.discovery.v1.model.GetEnvironmentOptions) GetCollectionOptions(com.ibm.watson.discovery.v1.model.GetCollectionOptions) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) DocumentAccepted(com.ibm.watson.discovery.v1.model.DocumentAccepted) ListConfigurationsResponse(com.ibm.watson.discovery.v1.model.ListConfigurationsResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ListConfigurationsOptions(com.ibm.watson.discovery.v1.model.ListConfigurationsOptions) GetDocumentStatusOptions(com.ibm.watson.discovery.v1.model.GetDocumentStatusOptions) DeleteCollectionOptions(com.ibm.watson.discovery.v1.model.DeleteCollectionOptions) ListEnvironmentsResponse(com.ibm.watson.discovery.v1.model.ListEnvironmentsResponse) DocumentStatus(com.ibm.watson.discovery.v1.model.DocumentStatus) ByteArrayInputStream(java.io.ByteArrayInputStream) AddDocumentOptions(com.ibm.watson.discovery.v1.model.AddDocumentOptions) QueryResponse(com.ibm.watson.discovery.v1.model.QueryResponse) CreateEnvironmentOptions(com.ibm.watson.discovery.v1.model.CreateEnvironmentOptions) Environment(com.ibm.watson.discovery.v1.model.Environment) CreateCollectionOptions(com.ibm.watson.discovery.v1.model.CreateCollectionOptions) Collection(com.ibm.watson.discovery.v1.model.Collection) ListEnvironmentsOptions(com.ibm.watson.discovery.v1.model.ListEnvironmentsOptions)

Aggregations

IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)32 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)30 Before (org.junit.Before)15 SpeechRecognitionResults (com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults)5 File (java.io.File)4 Response (com.ibm.cloud.sdk.core.http.Response)2 MessageInput (com.ibm.watson.assistant.v1.model.MessageInput)2 MessageOptions (com.ibm.watson.assistant.v1.model.MessageOptions)2 MessageResponse (com.ibm.watson.assistant.v1.model.MessageResponse)2 TranslateOptions (com.ibm.watson.language_translator.v3.model.TranslateOptions)2 TranslationResult (com.ibm.watson.language_translator.v3.model.TranslationResult)2 RecognizeWithWebsocketsOptions (com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions)2 BaseRecognizeCallback (com.ibm.watson.speech_to_text.v1.websocket.BaseRecognizeCallback)2 ToneAnalysis (com.ibm.watson.tone_analyzer.v3.model.ToneAnalysis)2 ToneOptions (com.ibm.watson.tone_analyzer.v3.model.ToneOptions)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 JsonObject (com.google.gson.JsonObject)1 HttpConfigOptions (com.ibm.cloud.sdk.core.http.HttpConfigOptions)1 ServiceCallback (com.ibm.cloud.sdk.core.http.ServiceCallback)1