-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Language, Speech: Add generated code samples. #9153
Language, Speech: Add generated code samples. #9153
Conversation
– Use `include_samples=True` when calling gapic.py_library() – Copy the entire generated `samples/` directory from genfiles
– Use `include_samples=True` when calling gapic.py_library() – Copy the entire generated `samples/` directory from genfiles
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty reluctant to merge in the samples without also adding in the support in language/noxfile.py
/ speech/noxfile.py
to automate testing them. Given that sampe-tester
is pip-installable, the stanzas would look something like:
@nox.session(python=["2.7", "3.7"])
def samples(session):
"""Run the samples."""
# Sanity check: Only run tests if the environment variable is set.
if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", ""):
session.skip("Credentials must be set via environment variable")
samples_path = "samples"
if not os.path.exists(samples_path):
session.skip("Samples not found.")
session.install("pyyaml")
session.install("samples-tester")
for local_dep in LOCAL_DEPS:
session.install("-e", local_dep)
session.install("-e", ".")
session.run("sample-tester", samples_path, *session.posargs)
Then, one could test the samples locally by setting the environment variable and running:
$ nox -s samples
Thanks @tseaver! I made the change locally and couldn't get the session to run with
Here are my 💻 shell commands and output (with version numbers and whatnot) I pushed up a new commit with the change. [b7960f0e40] |
@beccasaurus I think you need to run $ cd speech
$ nox -s spamples or $ nox -r speech/noxfile.py -s samples |
- Only run these using one Python version by default (use 3.7) - Fix package name to install for sample-tester
Thanks! Obviously a Works perfectly, thanks! 🙏 @nox.session(python=["3.7"])
def samples(session):
"""Run the samples test suite.""" Because the samples are tested against the real API, we usually won't want the |
Looking at the label history, I'm not sure if this needs a The tests all passed except 1 Language lint error (since fixed) This is ready to run the tests again!
|
@beccasaurus I've applied the label again. Kokoro appears to be on a work stoppage today, though. :) |
Green is my favorite color. |
w00t ~ tagging @busunkim96 now that it's green |
W00t, I can see:
in the jobs for both speech and language. |
Looks great! We can add the |
✨ Updated!
This Pull Request adds GAPIC-generated code samples for Natural Language and Speech-to-Text.
synth.py
for Language and Speech libraries.synthtool
and add generatedsamples/
.👓 This still needs to be configured to run tests via Kokoro.
noxfile.py
for each library for sample tests)Right now => you can only run the tests for these samples locally (because Kokoro isn't configured).
☑️ Test output
📖 Natural Language
🗣 Speech-to-Text
@busunkim96 et al ~ ready for re-review! 🔍
✨ Using a brand new Pull Request to keep things clean. Original Pull Request: #8210
@googleapis/samplegen
📌 Notes
🐞 Tracking issue for cross-language samplegen support
🤖 Sample generation updates
Highlights since the original Pull Request:
$ sample-tester
or$ sample-tester [dirs or files..]
assert_contains_any
allows providing multiple values and the test passes if any one of those values is present (particularly useful for testing ML responses where the responses may change over time but DPEs can provide a list of sane values where, if none of those values are present, there's a very high likelihood that the API or sample is not working)📑 Internal document
💻 Try it yourself!
It's now easy to generate code samples demonstrating calling single API endpoints using Google Cloud client libraries.
🤖 Sample Generation Quickstart – 🐍 Python version
📂 Locations and Tools
Generated sample development follows the same pattern as generated library development:
googleapis/
googleapis/
1️⃣2️⃣3️⃣ Steps for generated sample authoring
Steps:
gapic.py_library(..., include_samples=True)
)googleapis/google/[api root]/[version]/samples/*.yaml
)samples/
(usingsynthtool
)googleapis/google/[api root]/[version]/samples/test/*.test.yaml
)$ sample-tester
)⌨️ Dependencies
🖥 Example: add new sample with tests
Example: Add a new sample demonstrating the AnalyzeEntities method of the Natural Language API.
1. Setup
googleapis
directoryYou will need a local copy of
googleapis
. Samples are configured ingoogleapis
, alongside the API definition proto files, service configuration yaml files, and existing library configuration files. (e.g. artman configuration yaml files)$ git clone https://github.com/googleapis/googleapis.git $ cd googleapis/
2. Browse existing sample configs in
googleapis
Browse to the directory in
googleapis
where the Natural Language files are located:$ cd google/cloud/language/
You can find the existing samples for Natural Language in
google/cloud/language/v1/samples/
$ tree v1/samples/ v1/samples/ ├── language_classify_gcs.yaml ├── language_classify_text.yaml ├── language_entities_gcs.yaml ├── language_entities_text.yaml ├── ... └── test ├── analyzing_entities.test.yaml ├── ... └── classifying_content.test.yaml
3. Create a new sample config in
googleapis
Create a new sample configuration:
v1/samples/my_entity_sample.yaml
4. Setup
google-cloud-python
directoryTo generate this code sample, you need to regenerate the client library using
synthtool
.In this example, we'll use Python client libraries which are hosted in the
google-cloud-python
repository.Locally, checkout the Google Cloud Python client library for Natural Language:
5. Configure
synth.py
for sample generationUpdate
synth.py
for the client library to generate samples by addinginclude_samples=True
.Edit
synth.py
:6. Generate new code sample with
synthtool
Run SynthTool to generate client libraries (using your
googleapis/
directory as the source for generation).First, you must set
SYNTHTOOL_GOOGLEAPIS
to the local path of yourgoogleapis
directory, which tells
synthtool
where to find the sources to use when generating theclient library. (.proto files and config files, including sample configs)
$ export SYNTHTOOL_GOOGLEAPIS=/path/to/googleapis
Now you're ready to run synth to generate your new code sample!
7. View newly generated Python code sample
New Python code sample should have been generated:
samples/v1/my_entity_sample.py
8. Run the generated Python code sample
Run the code sample:
If it worked OK you should see output:
Got a response from the API! Found entity: California (Wikipedia: https://en.wikipedia.org/wiki/California)
9. Add test configuration for new sample in
googleapis
Next, browse to the directory in
googleapis
where the Natural Language files are located again.This time, create a new sample test configuration file:
v1/samples/test/my_entity_sample.test.yaml
10. Run the sample test using
sample-tester
Time to run the test!
First, run
synthtool
again to pickup the new test file.$ cd google-cloud-python/language/ $ python3 -m synthtool
There should be a new file:
samples/v1/test/my_entity_sample.test.yaml
. Run it usingsample-tester
:If all went well, you should have a passing test 🎉
You can alternatively simply run
$ sample-tester
and it will run all tests:If you run sample-tester with
--verbosity detailed
, you can see the actual Pythoncommands that sample-tester executes to test each code sample: