8000 feat(translate): add region tag 'import_client_library' and refactor sample by eapl-gemugami · Pull Request #13255 · GoogleCloudPlatform/python-docs-samples · GitHub
[go: up one dir, main page]

Skip to content

feat(translate): add region tag 'import_client_library' and refactor sample #13255

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

Conversation

eapl-gemugami
Copy link
Contributor
@eapl-gemugami eapl-gemugami commented Mar 25, 2025

Description

Fixes Internal:
b/405384416

  • Add region tag 'translate_v3_import_client_library'
  • Remove remaining Markdown files from previous repos 1 and 2
  • Fix styling of the sample and removed unnecessary dependencies
  • Refactor to improve sample objective of showing how to translate a simple text.

Checklist

@product-auto-label product-auto-label bot added api: translate Issues related to the Cloud Translation API. samples Issues that are directly related to samples. labels Mar 25, 2025
Copy link
Contributor
@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Hello @eapl-gemugami, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request addresses an internal issue (b/405384416) by adding a region tag, removing markdown files, and fixing sample styling. Specifically, it adds the 'translate_v3_import_client_library' region tag to the translate_v3_translate_text.py sample, removes the AUTHORING_GUIDE.md and CONTRIBUTING.md files, and improves the styling of the sample code.

Highlights

  • Region Tag: The pull request introduces a new region tag, 'translate_v3_import_client_library', to clearly mark the section where the Google Cloud Translation library is imported.
  • Markdown File Removal: Removes the AUTHORING_GUIDE.md and CONTRIBUTING.md files from the translate/samples directory, likely as part of a cleanup after a repository migration.
  • Sample Styling: The styling of the translate_v3_translate_text.py sample has been improved, including adding a comment to initialize the Translation client and reformatting comments for better readability.

Changelog

  • translate/samples/AUTHORING_GUIDE.md
    • Removed the file.
  • translate/samples/CONTRIBUTING.md
    • Removed the file.
  • translate/samples/snippets/translate_v3_translate_text.py
    • Added region tag 'translate_v3_import_client_library' around the import statement for the Google Cloud Translation library (lines 18-21).
    • Added a comment to initialize the Translation client (line 39).
    • Reformatted comments for better readability, including adding a newline after the language code description (lines 35-36) and changing 'Supported mime types' to 'Supported MIME types' (line 44).
    • Added a period to the end of the sentence 'Display the translation for each input text provided' (line 54).
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


Trivia time!

What company developed the Google Cloud Translation API?

Click here for the answer
Google developed the Google Cloud Translation API.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor
@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request adds a region tag, removes Markdown files, and fixes the styling of the sample. The changes look good overall, with a few minor suggestions for improvement.

Summary of Findings

  • Documentation Clarity: Clarify the purpose of initializing the Translation client with a comment before the initialization, to improve readability.
  • Comment Consistency: Ensure consistency in commenting style, such as using periods at the end of comments for clarity.

Merge Readiness

The pull request is almost ready for merging. Addressing the minor suggestions regarding documentation clarity and comment consistency would further enhance the code quality. I am unable to directly approve the pull request, and recommend that others review and approve this code before merging.

@eapl-gemugami eapl-gemugami added the snippet-bot:force-run Force snippet-bot runs its logic label Mar 25, 2025
@snippet-bot snippet-bot bot removed the snippet-bot:force-run Force snippet-bot runs its logic label Mar 25, 2025
Copy link
snippet-bot bot commented Mar 25, 2025

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@eapl-gemugami eapl-gemugami marked this pull request as ready for review March 25, 2025 18:25
@eapl-gemugami eapl-gemugami requested review from a team as code owners March 25, 2025 18:25
@@ -31,13 +32,17 @@ def translate_text(
text: The content to translate.
language_code: The language code for the translation.
Copy link
Contributor
@iennae iennae Mar 25, 2025

Choose a reason for hiding this comment

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

suggestion here: The ISO 639-1 language code of the target language (instead of "The language code for the translation")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with the suggestion, although documentation says:

Most language code parameters conform to ISO-639 identifiers, except where noted.

Leaving "The language code", and the link to the language models should be enough as there are many different variations and exceptions.

It's funny, the en-US used in source_language_code line 51 below, is not in the supported list, only en, although it works.

@eapl-gemugami eapl-gemugami changed the title feat(translate): add region tag 'import_client_library' and fix sample styling feat(translate): add region tag 'import_client_library' and refactor sample Mar 26, 2025
@eapl-gemugami
Copy link
Contributor Author

@iennae looking into your feedback I seized the opportunity to refactor the sample as David told me for previous cases.

I think the original sample was inconsistent on its purpose of 'translating text', for example only translating from English to another language, so I added the concept of source and target languages as parameters, which is like usually the translating platforms work.

I also removed the MIME type, not strictly required for this example, and cleaned it a bit the sample and the test.
As it is not a minor change in the sample, could you review it again please? Thanks!

https://cloud.google.com/translate/docs/languages#neural_machine_translation_model
"""

# Initialize Translation client.
client = translate_v3.TranslationServiceClient()
parent = f"projects/{PROJECT_ID}/locations/global"

# Translate text from English to chosen language.
# Supported MIME types:
# https://cloud.google.com/translate/docs/supported-formats
Copy link
Contributor
@glasnt glasnt Mar 26, 2025

Choose a reason for hiding this comment

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

mime_type defaults to text/plain (source), but I think it's important to still highlight that this API supports different mime types.

Copy link
Contributor Author
@eapl-gemugami eapl-gemugami Mar 26, 2025

Choose a reason for hiding this comment

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

I did think the same, although that link says:

If left blank, the MIME type defaults to "text/html".

Perhaps that Doc is outdated related to the client library? I'll take a look into it

@eapl-gemugami
Copy link
Contributor Author
eapl-gemugami commented Mar 26, 2025

@iennae @glasnt Thanks for your review! My thought process for this refactor starts at this page (sample appears in other pages as well): https://cloud.google.com/translate/docs/advanced/translating-text-v3#translating_text

Based on the title and context, I noticed the following:

  • I'd expect for this sample to receive a plain text or HTML and translate it from one language into another.
  • It shows how to translate a text from English to another (French as default). As the source language is not autodetected, it has to be supplied manually, I think it's better for the developer to supply the text, any source language and the target language.
  • A single text string is supplied, and then converted into a list of 1 element, it makes no sense to iterate over a list of results if a single one is expected -> I see two options, supplying a text and reading the first result, or supplying two texts in the sample and print all the results.
    Personally I'd vote for the second option, usually in i18n it's better to prepare the user to work on multiple elements instead of only one.
  • I agree with showing that the API supports many MIME types, although I think that concept is better suited for another sample translate_a_document_inline which uploads a document.
    As this sample submits a string which is limited to plain text or HTML, I'd add this note as a comment: the input can be plain text or HTML, and showing a comment for available MIME types, as in the Go and Node samples.

That said, these are suggestions based on my first look at the sample. I'd need to check all the translate samples in context, or know the intent behind this sample.

Please let me know if my suggestion would work, otherwise I can revert it.

Copy link
Contributor
@glasnt glasnt left a comment

Choose a reason for hiding this comment

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

Going by the source you linked, I've made some comments to address the additional changes made, while trying to keep in line with the sample style guide and similarity with other implementations (e.g. not all implementations take a source language in the function definition, but may use a default value).

@eapl-gemugami
Copy link
Contributor Author

@glasnt I have applied the feedback from your latest comment.

I only have a question on this approach for the MIME type.

If that looks correct, it's ready to be reviewed again. Thanks!

@eapl-gemugami
Copy link
Contributor Author

cc @alarconesparza @OremGLG @hivanalejandro (Team colleagues)

@OremGLG
Copy link
Contributor
OremGLG commented Mar 28, 2025

LGTM 👍

Copy link
Contributor
@glasnt glasnt left a comment

Choose a reason for hiding this comment

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

Pending doclink update

@glasnt glasnt merged commit 5ef6469 into GoogleCloudPlatform:main Mar 31, 2025
11 checks passed
9AA9
@eapl-gemugami eapl-gemugami deleted the paradalicea/feat/translate/add-region-tag-import-client-library branch March 31, 2025 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: translate Issues related to the Cloud Translation API. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0