Importer des données pour la classification de texte à étiquette unique
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Importez des données pour la classification de texte à étiquette unique à l'aide de la méthode import_data.
En savoir plus
Pour obtenir une documentation détaillée incluant cet exemple de code, consultez la page suivante :
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],[],[],[],null,["Imports data for text classification single label using the import_data method.\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Create a dataset for training text classification models](/vertex-ai/docs/text-data/classification/create-dataset)\n\nCode sample \n\nJava\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[Vertex AI quickstart using\nclient libraries](/vertex-ai/docs/start/client-libraries).\n\n\nFor more information, see the\n[Vertex AI Java API\nreference documentation](/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1).\n\n\nTo authenticate to Vertex AI, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.api.gax.longrunning.OperationFuture;\n import com.google.cloud.aiplatform.v1.DatasetName;\n import com.google.cloud.aiplatform.v1.DatasetServiceClient;\n import com.google.cloud.aiplatform.v1.DatasetServiceSettings;\n import com.google.cloud.aiplatform.v1.GcsSource;\n import com.google.cloud.aiplatform.v1.ImportDataConfig;\n import com.google.cloud.aiplatform.v1.ImportDataOperationMetadata;\n import com.google.cloud.aiplatform.v1.ImportDataResponse;\n import java.io.IOException;\n import java.util.Collections;\n import java.util.List;\n import java.util.concurrent.ExecutionException;\n import java.util.concurrent.TimeUnit;\n import java.util.concurrent.TimeoutException;\n\n public class ImportDataTextClassificationSingleLabelSample {\n\n public static void main(String[] args)\n throws IOException, InterruptedException, ExecutionException, TimeoutException {\n // TODO(developer): Replace these variables before running the sample.\n String project = \"YOUR_PROJECT_ID\";\n String datasetId = \"YOUR_DATASET_ID\";\n String gcsSourceUri =\n \"gs://YOUR_GCS_SOURCE_BUCKET/path_to_your_text_source/[file.csv/file.jsonl]\";\n\n importDataTextClassificationSingleLabelSample(project, datasetId, gcsSourceUri);\n }\n\n static void importDataTextClassificationSingleLabelSample(\n String project, String datasetId, String gcsSourceUri)\n throws IOException, InterruptedException, ExecutionException, TimeoutException {\n DatasetServiceSettings datasetServiceSettings =\n DatasetServiceSettings.newBuilder()\n .setEndpoint(\"us-central1-aiplatform.googleapis.com:443\")\n .build();\n\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests. After completing all of your requests, call\n // the \"close\" method on the client to safely clean up any remaining background resources.\n try (DatasetServiceClient datasetServiceClient =\n DatasetServiceClient.create(datasetServiceSettings)) {\n String location = \"us-central1\";\n String importSchemaUri =\n \"gs://google-cloud-aiplatform/schema/dataset/ioformat/\"\n + \"text_classification_single_label_io_format_1.0.0.yaml\";\n\n GcsSource.Builder gcsSource = GcsSource.newBuilder();\n gcsSource.addUris(gcsSourceUri);\n DatasetName datasetName = DatasetName.of(project, location, datasetId);\n\n List\u003cImportDataConfig\u003e importDataConfigList =\n Collections.singletonList(\n ImportDataConfig.newBuilder()\n .setGcsSource(gcsSource)\n .setImportSchemaUri(importSchemaUri)\n .build());\n\n OperationFuture\u003cImportDataResponse, ImportDataOperationMetadata\u003e importDataResponseFuture =\n datasetServiceClient.importDataAsync(datasetName, importDataConfigList);\n System.out.format(\n \"Operation name: %s\\n\", importDataResponseFuture.getInitialFuture().get().getName());\n\n System.out.println(\"Waiting for operation to finish...\");\n ImportDataResponse importDataResponse = importDataResponseFuture.get(300, TimeUnit.SECONDS);\n System.out.format(\n \"Import Data Text Classification Response: %s\\n\", importDataResponse.toString());\n }\n }\n }\n\nNode.js\n\n\nBefore trying this sample, follow the Node.js setup instructions in the\n[Vertex AI quickstart using\nclient libraries](/vertex-ai/docs/start/client-libraries).\n\n\nFor more information, see the\n[Vertex AI Node.js API\nreference documentation](/nodejs/docs/reference/aiplatform/latest).\n\n\nTo authenticate to Vertex AI, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /**\n * TODO(developer): Uncomment these variables before running the sample.\\\n * (Not necessary if passing values as arguments)\n */\n\n // const datasetId = \"YOUR_DATASET_ID\";\n // const gcsSourceUri = \"YOUR_GCS_SOURCE_URI\";\n // eg. \"gs://\u003cyour-gcs-bucket\u003e/\u003cimport_source_path\u003e/[file.csv/file.jsonl]\"\n // const project = \"YOUR_PROJECT_ID\";\n // const location = 'YOUR_PROJECT_LOCATION';\n\n // Imports the Google Cloud Dataset Service Client library\n const {DatasetServiceClient} = require('@google-cloud/aiplatform');\n\n // Specifies the location of the api endpoint\n const clientOptions = {\n apiEndpoint: 'us-central1-aiplatform.googleapis.com',\n };\n const datasetServiceClient = new DatasetServiceClient(clientOptions);\n\n async function importDataTextClassificationSingleLabel() {\n const name = datasetServiceClient.datasetPath(project, location, datasetId);\n // Here we use only one import config with one source\n const importConfigs = [\n {\n gcsSource: {uris: [gcsSourceUri]},\n importSchemaUri:\n 'gs://google-cloud-aiplatform/schema/dataset/ioformat/text_classification_single_label_io_format_1.0.0.yaml',\n },\n ];\n const request = {\n name,\n importConfigs,\n };\n\n // Import data request\n const [response] = await datasetServiceClient.importData(request);\n console.log(`Long running operation : ${response.name}`);\n\n // Wait for operation to complete\n const [importDataResponse] = await response.promise();\n\n console.log(\n `Import data text classification single label response : \\\n ${JSON.stringify(importDataResponse.result)}`\n );\n }\n importDataTextClassificationSingleLabel();\n\nPython\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Vertex AI quickstart using\nclient libraries](/vertex-ai/docs/start/client-libraries).\n\n\nFor more information, see the\n[Vertex AI Python API\nreference documentation](/python/docs/reference/aiplatform/latest).\n\n\nTo authenticate to Vertex AI, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import aiplatform\n\n\n def import_data_text_classification_single_label_sample(\n project: str,\n dataset_id: str,\n gcs_source_uri: str,\n location: str = \"us-central1\",\n api_endpoint: str = \"us-central1-aiplatform.googleapis.com\",\n timeout: int = 1800,\n ):\n # The AI Platform services require regional API endpoints.\n client_options = {\"api_endpoint\": api_endpoint}\n # Initialize client that will be used to create and send requests.\n # This client only needs to be created once, and can be reused for multiple requests.\n client = aiplatform.gapic.DatasetServiceClient(client_options=client_options)\n import_configs = [\n {\n \"gcs_source\": {\"uris\": [gcs_source_uri]},\n \"import_schema_uri\": \"gs://google-cloud-aiplatform/schema/dataset/ioformat/text_classification_single_label_io_format_1.0.0.yaml\",\n }\n ]\n name = client.dataset_path(project=project, location=location, dataset=dataset_id)\n response = client.import_data(name=name, import_configs=import_configs)\n print(\"Long running operation:\", response.operation.name)\n import_data_response = response.result(timeout=timeout)\n print(\"import_data_response:\", import_data_response)\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=aiplatform)."]]