feat(models): add Google Cloud Translation v2 and v3 as LID models - #18
Conversation
6af68ac to
a182c83
Compare
Closes #11. Registers `GoogleTranslate`, wrapping Cloud Translation's `detect` method. The API key is read from `GOOGLE_TRANSLATE_API_KEY` in the process environment, which the caller is expected to have set; the model does not read `.env` files itself. Uses Cloud Translation v2, not v3. v3 `detectLanguage` refuses API-key authentication outright ("401 API keys are not supported by this API") on both its gRPC and REST transports and wants OAuth2 / ADC plus a project id. v2 accepts an API key, needs no project id, and detects a whole list of texts per request, where v3 takes a single string per call and `batchTranslateText` covers translation only. Batching turns a 373K-sample benchmark from 373K HTTP calls into a few thousand. The wrapper re-splits each batch to stay under the per-request segment and size caps, sends the pieces concurrently, retries 429 and transient errors with bounded backoff, and short-circuits blank text so a rejected request is never billed. Detected BCP-47 codes are cut at the first `-` before the existing ISO 639-3 conformance pass, so `zh-CN` becomes `zho`. The API's `confidence` is kept as the prediction score. New optional extra `commonlid[google-translate]`, named after the service rather than the vendor so a future Google model gets its own.
a182c83 to
e2d87e1
Compare
|
The CommonLID nano results are already on the leaderboard: https://huggingface.co/spaces/commoncrawl/commonlid |
|
Maybe we should put an explicit reference in the table to the fact that it is V2 and not V3? I'm having trouble finding the list of languages by version, but my understanding was that V3 had more coverage. It's sad we cannot use it, but we should make it explicit which version it is |
|
I think v2 just refers to the API version. I couldn't find any information on the actual model. |
|
I've run a quick test:
|
Makes the Cloud Translation edition explicit ahead of a v3 sibling model. The model id becomes `GoogleTranslate-v2`, the class `GoogleTranslateV2Model` and the module `google_translate_v2`. Behaviour is unchanged. The id is persisted in result folder names, the summary's `model_id` and the prediction cache keys, so the one published result (commonlid_nano/GoogleTranslate) is migrated separately in the results dataset rather than recomputed.
Adds `GoogleTranslate-v3` alongside `GoogleTranslate-v2`, which is unchanged. Both ship so the two editions can be compared; neither replaces the other. The editions differ in three ways that matter for detection, all per the official reference: - Auth. Basic "accept[s] API keys ... as well as service accounts"; Advanced "requires service account authentication that's integrated with IAM roles" and "does not support API keys". So v3 uses Application Default Credentials and a project id, resolved from GOOGLE_TRANSLATE_PROJECT_ID, then GOOGLE_CLOUD_PROJECT, then whatever ADC reports (its own project for a service account, else its quota project). - Batching. v2's `q` is repeatable, so one request detects a whole list. v3's `content` is a single string, so a batch is a thread pool over one request per text. - Confidence. v2 documents both `isReliable` and `confidence` as deprecated and advises against basing decisions on them; v3 carries no such notice. The README score table now records this. Measured on 300 samples of commonlid_nano, the two agree on 297 (99.0%) and report identical confidences to 1e-6 on 298. Every disagreement is the same labelling difference, not a detection one: v2 returns `fil` for Tagalog where v3 returns `tl`, with identical confidence, conforming to `fil` and `tgl` respectively. CommonLID's gold label is `tgl`, so v3 scored 76.67% against v2's 75.67% on that slice. Also switches the v2 wrapper to `import google.cloud.translate_v2 as ...`. `translate_v2` ships no py.typed, so once a typed `google.cloud.*` module is in play (v3 imports one) mypy reports it as a missing attribute of the namespace package. Behaviour is unchanged.
Closes #11.
Why
Adds Google Cloud Translation's language detection as two models, one per API edition, so the commercial detector can be compared against the open models on the leaderboard:
model_idGoogleTranslate-v2detectGoogleTranslate-v3detectLanguageBuilds on #17: both wrappers return the API's
confidenceas the prediction score.How the editions differ
Per the official reference, three differences matter for detection:
Auth. Basic accepts API keys and service accounts. Advanced "requires service account authentication that's integrated with IAM roles" and "does not support API keys". Trying anyway fails on both transports:
Batching. v2's
qis repeatable, so one request detects a whole list. v3'scontentis a single string, so v3 needs one request per text.Confidence. v2 documents both
isReliableandconfidenceas deprecated and recommends "not basing any decisions or thresholds" on them. v3 carries no such notice. The README score table records this.Do they agree?
On 300 samples of
commonlid_nano, the two editions return the same prediction for 297 (99.0%) and identical confidences to 1e-6 for 298. Every disagreement is the same labelling difference, not a detection one: for Tagalog, v2 returnsfiland v3 returnstl, with identical confidence.Those codes conform to two distinct ISO 639-3 languages, Filipino
filand Tagalogtgl, and the benchmark's gold labels use both. This is deliberately not remapped for Google alone: GlotLID, OpenLID-v2, cld3 and funlangid also predictfiland get none of thetglsamples right.Rename:
GoogleTranslatetoGoogleTranslate-v2An earlier revision of this PR registered the v2 model as
GoogleTranslate. It is renamed so the edition is explicit. The id is persisted in result paths, summaries and cache keys, so the one published result is migrated rather than recomputed in https://huggingface.co/datasets/commoncrawl/commonlid-results/discussions/7. There,predictions.jsonlis byte-identical andsummary.jsondiffers only inmodel_id. Merge that PR together with this one.Credentials
Both models read only the process environment; neither loads
.envfiles.GoogleTranslate-v2needsGOOGLE_TRANSLATE_API_KEY.GoogleTranslate-v3needs Application Default Credentials, viagcloud auth application-default loginorGOOGLE_APPLICATION_CREDENTIALS. Its project id resolves fromGOOGLE_TRANSLATE_PROJECT_ID, thenGOOGLE_CLOUD_PROJECT, then the ADC project, then the ADC quota project.Missing credentials fail at
load()with a message naming what to set.Cost
Detection is billed per character. v2 detection costs $20 per million characters, with the first 500K each month free. Measured on the cleaned text that is actually sent, since
predict()applies the OpenLID normer first:commonlid_nanocommonlidThe character counts are the same for both editions, but v3 sends one request per text.
commonlid_nanohas been evaluated with v2 and published. No fullcommonlidrun has been done, and none should be without agreeing the spend first. Re-runs are free because the evaluator'sPredictionCachecovers them.Implementation notes
-before the existing ISO 639-3 conformance pass, sozh-CNbecomeszho.discover_supported_languages()asks the API. v2 yields 187 conformed codes from 195 raw, v3 188 from 196, with nothing dropped by conformance.client_options={"api_key": ...}and falls through to ADC, so the key is passed asgoogle.auth.api_key.Credentials.commonlid[google-translate]covers both. It is mirrored intodevso the tests exercise the real client, and added toall. Newmake install-google-translate.google.cloud.translate_v2as a module. It ships nopy.typed, so once v3's typed import is present, mypy otherwise reports it as a missing attribute.Testing
make checkpasses: 295 tests, 95.8% coverage, ruff and mypy strict clean.The model tests never touch the network. They swap in fake clients or monkeypatch the library out of
sys.modules. They cover missing extras and credentials, v3's project-id resolution order, code conformance, confidence surviving an unmappable code, blank-text short-circuiting with zero API calls, v2 chunking, order preservation across thread pools, and the retry and give-up paths.Verified against the live API with both editions: