Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate code from googleapis/java-mediatranslation #7356

Merged
merged 62 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
fb49652
feat: initial generation
chingor13 Mar 18, 2020
85ce08b
samples: use the released artifact directly (#11)
chingor13 Mar 19, 2020
5bfdb9d
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Mar 25, 2020
0d88a70
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Mar 30, 2020
8fbe9c3
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Apr 6, 2020
2b88e65
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Apr 8, 2020
61811db
media-translation-sample: translate from file (#15)
munkhuushmgl Apr 13, 2020
e475c14
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Apr 16, 2020
b33073c
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Apr 17, 2020
640943e
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Apr 23, 2020
194ac9b
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Jun 10, 2020
ed09ea8
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Jun 26, 2020
be99513
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Sep 22, 2020
4eba25c
test(deps): update dependency junit:junit to v4.13.1 (#138)
renovate-bot Oct 30, 2020
8fbc44d
test(deps): update dependency com.google.truth:truth to v1.1 (#147)
renovate-bot Oct 30, 2020
bdbc749
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Oct 30, 2020
71a3110
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Nov 2, 2020
e7bcdb3
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Nov 19, 2020
c8e2f47
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Dec 14, 2020
e195a7d
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Jan 7, 2021
b2d7ed0
test(deps): update dependency com.google.truth:truth to v1.1.2 (#204)
renovate-bot Jan 25, 2021
1e545b5
test(deps): update dependency junit:junit to v4.13.2 (#220)
renovate-bot Feb 16, 2021
f2a776f
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Feb 24, 2021
1d1374e
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Mar 3, 2021
7568ce1
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Mar 23, 2021
e7a7395
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Apr 6, 2021
bd4c94f
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Apr 12, 2021
b2bec30
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Apr 30, 2021
22e3fe0
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot May 25, 2021
7ed6310
test(deps): update dependency com.google.truth:truth to v1.1.3 (#299)
renovate-bot May 26, 2021
34b5c47
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot May 26, 2021
a4e485b
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Jun 7, 2021
68ddc03
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Jun 17, 2021
ab70a03
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Jul 14, 2021
d51e861
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Aug 19, 2021
c11ed47
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Aug 31, 2021
e51c0fc
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Sep 1, 2021
7a21a3e
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Sep 2, 2021
9bc6120
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Sep 14, 2021
4ea9649
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Sep 24, 2021
ef49419
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Oct 19, 2021
b15a86b
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Dec 6, 2021
d5e482f
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Dec 6, 2021
080c155
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Jan 17, 2022
2ca0ad0
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Mar 16, 2022
1b04f74
docs(samples): remove unsupported field RecognitionResult (#631)
kweinmeister Mar 29, 2022
08b8fcc
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Apr 1, 2022
f430cbc
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Apr 18, 2022
b93b0c3
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot May 25, 2022
35f4278
feat: Enable REST transport for most of Java and Go clients (#658)
gcf-owl-bot[bot] Jun 23, 2022
da751d2
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Jun 24, 2022
0b7cca8
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Aug 2, 2022
480ae52
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Aug 16, 2022
6abcd33
chore(bazel): Update WORKSPACE files for rules_gapic, gax_java, gener…
gcf-owl-bot[bot] Sep 8, 2022
cbff39a
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Sep 9, 2022
857c865
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Sep 20, 2022
0cf8e40
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Oct 6, 2022
b84955c
chore(deps): update dependency com.google.cloud:google-cloud-mediatra…
renovate-bot Oct 6, 2022
b285b98
Merge remote-tracking branch 'migration/main' into java-mediatranslat…
anguillanneuf Oct 24, 2022
a45759b
fix(mediatranslation): remove debugging field
anguillanneuf Oct 25, 2022
ff32010
fix region tag product name
anguillanneuf Oct 25, 2022
7c81f83
remove generated samples from the migration
anguillanneuf Oct 25, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions mediatranslation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[//]: # "This README.md file is auto-generated, all changes to this file will be lost."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to decide if we want to use this readme and if we want to create sub-directories within the product directory to prepare for future (separate) samples

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack.

[//]: # "To regenerate it, use `python -m synthtool`."
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>

# [Cloud Media Translation: Java Samples](https://github.com/googleapis/java-mediatranslation)

[![Open in Cloud Shell][shell_img]][shell_link]



## Table of Contents

* [Build the sample](#build-the-sample)
* [Samples](#samples)


## Build the sample

Install [Maven](http://maven.apache.org/).

Build your project with:

```
mvn clean package -DskipTests
```

## Samples

Please follow the [Set Up Your Project](https://cloud.google.com/media-translation/docs/getting-started#set_up_your_project)
steps in the Quickstart doc to create a project and enable the Google Cloud
Media Translation API. Following those steps, make sure that you
[Set Up a Service Account](https://cloud.google.com/media-translation/docs/common/auth#set_up_a_service_account),
and export the following environment variable:

```
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json
```

After you have authorized, you can translate media.


## Run
Run all tests:
```
mvn clean verify
```

[shell_img]: https://gstatic.com/cloudssh/images/open-btn.png
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-mediatranslation&page=editor&open_in_editor=samples/snippets/README.md
[product-docs]: https://cloud.google.com/mediatranslation/docs/
47 changes: 47 additions & 0 deletions mediatranslation/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud</groupId>
<artifactId>mediatranslation-snippets</artifactId>
<packaging>jar</packaging>
<name>Google Media Translation API Snippets</name>
<url>https://github.com/googleapis/java-mediatranslation</url>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-mediatranslation</artifactId>
<version>0.9.6</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Binary file added mediatranslation/resources/audio.raw
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.mediatranslation;

// [START mediatranslation_translate_from_file]

import com.google.api.gax.rpc.BidiStream;
import com.google.cloud.mediatranslation.v1beta1.SpeechTranslationServiceClient;
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechConfig;
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechRequest;
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechResponse;
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechResult;
import com.google.cloud.mediatranslation.v1beta1.TranslateSpeechConfig;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class TranslateFromFile {

public static void translateFromFile() throws IOException {
// TODO(developer): Replace these variables before running the sample.
String filePath = "path/to/audio.raw";
translateFromFile(filePath);
}

public static void translateFromFile(String filePath) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (SpeechTranslationServiceClient client = SpeechTranslationServiceClient.create()) {
Path path = Paths.get(filePath);
byte[] content = Files.readAllBytes(path);
ByteString audioContent = ByteString.copyFrom(content);

TranslateSpeechConfig audioConfig =
TranslateSpeechConfig.newBuilder()
.setAudioEncoding("linear16")
.setSampleRateHertz(16000)
.setSourceLanguageCode("en-US")
.setTargetLanguageCode("fr-FR")
.build();

StreamingTranslateSpeechConfig config =
StreamingTranslateSpeechConfig.newBuilder()
.setAudioConfig(audioConfig)
.setSingleUtterance(true)
.build();

BidiStream<StreamingTranslateSpeechRequest, StreamingTranslateSpeechResponse> bidiStream =
client.streamingTranslateSpeechCallable().call();

// The first request contains the configuration.
StreamingTranslateSpeechRequest requestConfig =
StreamingTranslateSpeechRequest.newBuilder().setStreamingConfig(config).build();

// The second request contains the audio
StreamingTranslateSpeechRequest request =
StreamingTranslateSpeechRequest.newBuilder().setAudioContent(audioContent).build();

bidiStream.send(requestConfig);
bidiStream.send(request);

for (StreamingTranslateSpeechResponse response : bidiStream) {
// Once the transcription settles, the response contains the
// is_final result. The other results will be for subsequent portions of
// the audio.
StreamingTranslateSpeechResult res = response.getResult();
String translation = res.getTextTranslationResult().getTranslation();

if (res.getTextTranslationResult().getIsFinal()) {
System.out.println(String.format("\nFinal translation: %s", translation));
break;
}
System.out.println(String.format("\nPartial translation: %s", translation));
}
}
}
}
// [END mediatranslation_translate_from_file]
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.mediatranslation;

// [START mediatranslation_translate_from_mic]

import com.google.api.gax.rpc.ClientStream;
import com.google.api.gax.rpc.ResponseObserver;
import com.google.api.gax.rpc.StreamController;
import com.google.cloud.mediatranslation.v1beta1.SpeechTranslationServiceClient;
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechConfig;
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechRequest;
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechResponse;
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechResult;
import com.google.cloud.mediatranslation.v1beta1.TranslateSpeechConfig;
import com.google.protobuf.ByteString;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

public class TranslateFromMic {

public static void main(String[] args) throws IOException, LineUnavailableException {
translateFromMic();
}

public static void translateFromMic() throws IOException, LineUnavailableException {

ResponseObserver<StreamingTranslateSpeechResponse> responseObserver = null;

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (SpeechTranslationServiceClient client = SpeechTranslationServiceClient.create()) {
responseObserver =
new ResponseObserver<StreamingTranslateSpeechResponse>() {

@Override
public void onStart(StreamController controller) {}

@Override
public void onResponse(StreamingTranslateSpeechResponse response) {
StreamingTranslateSpeechResult res = response.getResult();
String translation = res.getTextTranslationResult().getTranslation();

if (res.getTextTranslationResult().getIsFinal()) {
System.out.println(String.format("\nFinal translation: %s", translation));
} else {
System.out.println(String.format("\nPartial translation: %s", translation));
}
}

@Override
public void onComplete() {}

public void onError(Throwable t) {
System.out.println(t);
}
};

ClientStream<StreamingTranslateSpeechRequest> clientStream =
client.streamingTranslateSpeechCallable().splitCall(responseObserver);

TranslateSpeechConfig audioConfig =
TranslateSpeechConfig.newBuilder()
.setAudioEncoding("linear16")
.setSourceLanguageCode("en-US")
.setTargetLanguageCode("es-ES")
.setSampleRateHertz(16000)
.build();

StreamingTranslateSpeechConfig streamingRecognitionConfig =
StreamingTranslateSpeechConfig.newBuilder().setAudioConfig(audioConfig).build();

StreamingTranslateSpeechRequest request =
StreamingTranslateSpeechRequest.newBuilder()
.setStreamingConfig(streamingRecognitionConfig)
.build(); // The first request in a streaming call has to be a config

clientStream.send(request);
// SampleRate:16000Hz, SampleSizeInBits: 16, Number of channels: 1, Signed: true,
// bigEndian: false
AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true, false);
DataLine.Info targetInfo =
new DataLine.Info(
TargetDataLine.class,
audioFormat); // Set the system information to read from the microphone audio stream

if (!AudioSystem.isLineSupported(targetInfo)) {
System.out.println("Microphone not supported");
System.exit(0);
}
// Target data line captures the audio stream the microphone produces.
TargetDataLine targetDataLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
System.out.println("Start speaking... Press Ctrl-C to stop");
long startTime = System.currentTimeMillis();
// Audio Input Stream
AudioInputStream audio = new AudioInputStream(targetDataLine);

while (true) {
byte[] data = new byte[6400];
audio.read(data);
request =
StreamingTranslateSpeechRequest.newBuilder()
.setAudioContent(ByteString.copyFrom(data))
.build();
clientStream.send(request);
}
}
}
}
// [END mediatranslation_translate_from_mic]
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.mediatranslation;

import static com.google.common.truth.Truth.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class TranslateFromFileTest {

private ByteArrayOutputStream bout;

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
System.setOut(new PrintStream(bout));
}

@After
public void tearDown() {
System.setOut(null);
bout.reset();
}

@Test
public void testTranslateFromFile() throws IOException {
// Call translateFromFile to print out the translated output.
TranslateFromFile.translateFromFile("resources/audio.raw");
String output = bout.toString();

// Check that the output contain some translation.
assertThat(output).contains("Partial translation");
}
}