0
# V3 AutoML Translation
1
2
Custom machine learning model training and management for specialized translation needs. AutoML Translation allows you to create custom translation models trained on your domain-specific data for improved accuracy in specialized contexts.
3
4
## Core Import
5
6
```python
7
from google.cloud import translate_v3
8
```
9
10
## Capabilities
11
12
### Dataset Management
13
14
Create and manage datasets for training custom translation models.
15
16
```python { .api }
17
def create_dataset(
18
self,
19
request=None,
20
*,
21
parent=None,
22
dataset=None,
23
retry=None,
24
timeout=None,
25
metadata=()
26
):
27
"""
28
Creates a Dataset.
29
30
Args:
31
request (CreateDatasetRequest): The request object
32
parent (str): Project/location resource name
33
dataset (Dataset): Dataset configuration
34
retry: Retry configuration
35
timeout (float): Request timeout in seconds
36
metadata: Additional metadata
37
38
Returns:
39
Operation: Long-running operation for dataset creation
40
"""
41
42
def list_datasets(
43
self,
44
request=None,
45
*,
46
parent=None,
47
page_size=None,
48
page_token=None,
49
retry=None,
50
timeout=None,
51
metadata=()
52
):
53
"""
54
Lists datasets.
55
56
Args:
57
request (ListDatasetsRequest): The request object
58
parent (str): Project/location resource name
59
page_size (int): Maximum number of datasets to return
60
page_token (str): Token for pagination
61
retry: Retry configuration
62
timeout (float): Request timeout in seconds
63
metadata: Additional metadata
64
65
Returns:
66
ListDatasetsResponse: Paginated list of datasets
67
"""
68
69
def get_dataset(
70
self,
71
request=None,
72
*,
73
name=None,
74
retry=None,
75
timeout=None,
76
metadata=()
77
):
78
"""
79
Gets a Dataset.
80
81
Args:
82
request (GetDatasetRequest): The request object
83
name (str): Dataset resource name
84
retry: Retry configuration
85
timeout (float): Request timeout in seconds
86
metadata: Additional metadata
87
88
Returns:
89
Dataset: Dataset resource
90
"""
91
92
def delete_dataset(
93
self,
94
request=None,
95
*,
96
name=None,
97
retry=None,
98
timeout=None,
99
metadata=()
100
):
101
"""
102
Deletes a dataset and all of its contents.
103
104
Args:
105
request (DeleteDatasetRequest): The request object
106
name (str): Dataset resource name
107
retry: Retry configuration
108
timeout (float): Request timeout in seconds
109
metadata: Additional metadata
110
111
Returns:
112
Operation: Long-running operation for dataset deletion
113
"""
114
```
115
116
### Data Import and Export
117
118
Manage training data within datasets through import and export operations.
119
120
```python { .api }
121
def import_data(
122
self,
123
request=None,
124
*,
125
dataset=None,
126
input_config=None,
127
retry=None,
128
timeout=None,
129
metadata=()
130
):
131
"""
132
Imports data into a dataset.
133
134
Args:
135
request (ImportDataRequest): The request object
136
dataset (str): Dataset resource name
137
input_config (DatasetInputConfig): Input configuration
138
retry: Retry configuration
139
timeout (float): Request timeout in seconds
140
metadata: Additional metadata
141
142
Returns:
143
Operation: Long-running operation for data import
144
"""
145
146
def export_data(
147
self,
148
request=None,
149
*,
150
dataset=None,
151
output_config=None,
152
retry=None,
153
timeout=None,
154
metadata=()
155
):
156
"""
157
Exports dataset's data to the provided output location.
158
159
Args:
160
request (ExportDataRequest): The request object
161
dataset (str): Dataset resource name
162
output_config (DatasetOutputConfig): Output configuration
163
retry: Retry configuration
164
timeout (float): Request timeout in seconds
165
metadata: Additional metadata
166
167
Returns:
168
Operation: Long-running operation for data export
169
"""
170
171
def list_examples(
172
self,
173
request=None,
174
*,
175
parent=None,
176
filter=None,
177
page_size=None,
178
page_token=None,
179
retry=None,
180
timeout=None,
181
metadata=()
182
):
183
"""
184
Lists sentence pairs in the dataset.
185
186
Args:
187
request (ListExamplesRequest): The request object
188
parent (str): Dataset resource name
189
filter (str): Filter expression for examples
190
page_size (int): Maximum number of examples to return
191
page_token (str): Token for pagination
192
retry: Retry configuration
193
timeout (float): Request timeout in seconds
194
metadata: Additional metadata
195
196
Returns:
197
ListExamplesResponse: Paginated list of training examples
198
"""
199
```
200
201
### Model Management
202
203
Train and manage custom translation models using your datasets.
204
205
```python { .api }
206
def create_model(
207
self,
208
request=None,
209
*,
210
parent=None,
211
model=None,
212
retry=None,
213
timeout=None,
214
metadata=()
215
):
216
"""
217
Creates a Model.
218
219
Args:
220
request (CreateModelRequest): The request object
221
parent (str): Project/location resource name
222
model (Model): Model configuration
223
retry: Retry configuration
224
timeout (float): Request timeout in seconds
225
metadata: Additional metadata
226
227
Returns:
228
Operation: Long-running operation for model creation
229
"""
230
231
def list_models(
232
self,
233
request=None,
234
*,
235
parent=None,
236
filter=None,
237
page_size=None,
238
page_token=None,
239
retry=None,
240
timeout=None,
241
metadata=()
242
):
243
"""
244
Lists models.
245
246
Args:
247
request (ListModelsRequest): The request object
248
parent (str): Project/location resource name
249
filter (str): Filter expression for models
250
page_size (int): Maximum number of models to return
251
page_token (str): Token for pagination
252
retry: Retry configuration
253
timeout (float): Request timeout in seconds
254
metadata: Additional metadata
255
256
Returns:
257
ListModelsResponse: Paginated list of models
258
"""
259
260
def get_model(
261
self,
262
request=None,
263
*,
264
name=None,
265
retry=None,
266
timeout=None,
267
metadata=()
268
):
269
"""
270
Gets a model.
271
272
Args:
273
request (GetModelRequest): The request object
274
name (str): Model resource name
275
retry: Retry configuration
276
timeout (float): Request timeout in seconds
277
metadata: Additional metadata
278
279
Returns:
280
Model: Model resource
281
"""
282
283
def delete_model(
284
self,
285
request=None,
286
*,
287
name=None,
288
retry=None,
289
timeout=None,
290
metadata=()
291
):
292
"""
293
Deletes a model.
294
295
Args:
296
request (DeleteModelRequest): The request object
297
name (str): Model resource name
298
retry: Retry configuration
299
timeout (float): Request timeout in seconds
300
metadata: Additional metadata
301
302
Returns:
303
Operation: Long-running operation for model deletion
304
"""
305
```
306
307
## Usage Examples
308
309
### Creating a Dataset
310
311
```python
312
from google.cloud import translate_v3
313
314
client = translate_v3.TranslationServiceClient()
315
316
parent = "projects/my-project/locations/us-central1"
317
318
dataset = {
319
"display_name": "My Translation Dataset",
320
"source_language_code": "en",
321
"target_language_code": "es",
322
"train_example_count": 1000,
323
"validate_example_count": 100,
324
}
325
326
operation = client.create_dataset(
327
request={
328
"parent": parent,
329
"dataset": dataset,
330
}
331
)
332
333
print(f"Operation name: {operation.name}")
334
335
# Wait for operation to complete
336
result = operation.result(timeout=300)
337
print(f"Created dataset: {result.name}")
338
print(f"Display name: {result.display_name}")
339
print(f"Source language: {result.source_language_code}")
340
print(f"Target language: {result.target_language_code}")
341
```
342
343
### Importing Training Data
344
345
```python
346
from google.cloud import translate_v3
347
348
client = translate_v3.TranslationServiceClient()
349
350
dataset_name = "projects/my-project/locations/us-central1/datasets/my-dataset"
351
352
input_config = {
353
"gcs_source": {
354
"input_uri": "gs://my-bucket/training-data.csv"
355
},
356
"mime_type": "text/csv"
357
}
358
359
operation = client.import_data(
360
request={
361
"dataset": dataset_name,
362
"input_config": input_config,
363
}
364
)
365
366
print(f"Import operation: {operation.name}")
367
368
# Wait for import to complete
369
result = operation.result(timeout=1800) # 30 minutes timeout
370
print(f"Import completed")
371
print(f"Imported example count: {result.example_count}")
372
```
373
374
### Training a Model
375
376
```python
377
from google.cloud import translate_v3
378
379
client = translate_v3.TranslationServiceClient()
380
381
parent = "projects/my-project/locations/us-central1"
382
dataset_name = f"{parent}/datasets/my-dataset"
383
384
model = {
385
"display_name": "My Custom Translation Model",
386
"dataset": dataset_name,
387
}
388
389
operation = client.create_model(
390
request={
391
"parent": parent,
392
"model": model,
393
}
394
)
395
396
print(f"Training operation: {operation.name}")
397
398
# Model training can take several hours
399
result = operation.result(timeout=7200) # 2 hours timeout
400
print(f"Model training completed: {result.name}")
401
print(f"Model state: {result.state}")
402
```
403
404
### Using Custom Model for Translation
405
406
```python
407
from google.cloud import translate_v3
408
409
client = translate_v3.TranslationServiceClient()
410
411
parent = "projects/my-project/locations/us-central1"
412
model_name = f"{parent}/models/my-model"
413
414
response = client.translate_text(
415
request={
416
"parent": parent,
417
"contents": ["Hello, world!", "How are you?"],
418
"mime_type": "text/plain",
419
"source_language_code": "en",
420
"target_language_code": "es",
421
"model": model_name,
422
}
423
)
424
425
for translation in response.translations:
426
print(f"Custom model translation: {translation.translated_text}")
427
```
428
429
### Managing Datasets and Models
430
431
```python
432
from google.cloud import translate_v3
433
434
client = translate_v3.TranslationServiceClient()
435
436
parent = "projects/my-project/locations/us-central1"
437
438
# List all datasets
439
datasets_response = client.list_datasets(
440
request={
441
"parent": parent,
442
"page_size": 10,
443
}
444
)
445
446
print("Datasets:")
447
for dataset in datasets_response.datasets:
448
print(f" {dataset.name}")
449
print(f" Display name: {dataset.display_name}")
450
print(f" Languages: {dataset.source_language_code} -> {dataset.target_language_code}")
451
print(f" Example count: {dataset.example_count}")
452
print(f" State: {dataset.state}")
453
print(" ---")
454
455
# List all models
456
models_response = client.list_models(
457
request={
458
"parent": parent,
459
"page_size": 10,
460
}
461
)
462
463
print("Models:")
464
for model in models_response.models:
465
print(f" {model.name}")
466
print(f" Display name: {model.display_name}")
467
print(f" Dataset: {model.dataset}")
468
print(f" State: {model.state}")
469
print(f" Create time: {model.create_time}")
470
print(" ---")
471
```
472
473
### Listing Training Examples
474
475
```python
476
from google.cloud import translate_v3
477
478
client = translate_v3.TranslationServiceClient()
479
480
dataset_name = "projects/my-project/locations/us-central1/datasets/my-dataset"
481
482
response = client.list_examples(
483
request={
484
"parent": dataset_name,
485
"page_size": 10,
486
}
487
)
488
489
print("Training examples:")
490
for example in response.examples:
491
print(f"Source: {example.source_text}")
492
print(f"Target: {example.target_text}")
493
print(f"Usage: {example.usage}") # TRAIN, VALIDATION, or TEST
494
print("---")
495
496
# Handle pagination
497
if response.next_page_token:
498
next_response = client.list_examples(
499
request={
500
"parent": dataset_name,
501
"page_token": response.next_page_token,
502
"page_size": 10,
503
}
504
)
505
```
506
507
### Exporting Dataset
508
509
```python
510
from google.cloud import translate_v3
511
512
client = translate_v3.TranslationServiceClient()
513
514
dataset_name = "projects/my-project/locations/us-central1/datasets/my-dataset"
515
516
output_config = {
517
"gcs_destination": {
518
"output_uri_prefix": "gs://my-bucket/exported-data/"
519
}
520
}
521
522
operation = client.export_data(
523
request={
524
"dataset": dataset_name,
525
"output_config": output_config,
526
}
527
)
528
529
print(f"Export operation: {operation.name}")
530
531
# Wait for export to complete
532
result = operation.result(timeout=600) # 10 minutes timeout
533
print(f"Export completed")
534
print(f"Exported example count: {result.example_count}")
535
```
536
537
### Model Evaluation and Metrics
538
539
```python
540
from google.cloud import translate_v3
541
542
client = translate_v3.TranslationServiceClient()
543
544
model_name = "projects/my-project/locations/us-central1/models/my-model"
545
546
# Get model details including evaluation metrics
547
model = client.get_model(
548
request={"name": model_name}
549
)
550
551
print(f"Model: {model.display_name}")
552
print(f"State: {model.state}")
553
print(f"Create time: {model.create_time}")
554
print(f"Update time: {model.update_time}")
555
556
# Check if model has evaluation results
557
if hasattr(model, 'evaluation_results') and model.evaluation_results:
558
for result in model.evaluation_results:
559
print(f"Evaluation metric: {result.metric}")
560
print(f"Score: {result.score}")
561
```