Guest User

bad_is_date_sequential.py

a guest
Mar 23rd, 2021
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.33 KB | None | 0 0
  1. """
  2. Uses the Google Cloud Data Labeling API to ask human annotators whether the dates are sequential.
  3. Requires a billing account with an active credit card.
  4. You can use your mother's if you don't have one - just don't tell her about it.
  5. """
  6.  
  7. GCLOUD_PROJECT_ID = "<your google cloud project id>"
  8. INPUT_GCS_URI = "gs://<GCS resource file formatted as CSV, with the format below>.csv"
  9. """Sample format: (you don't really have to use a fixed format, just get every date on the same line and the human will figure it out)
  10. 2018-12-21 17:56:37;2018-09-11 19:41:17;2018-10-11 16:34:41;2020-07-27 03:10:17;2019-02-04 19:58:02
  11. ..."""
  12.  
  13. labeling_instruction_name = "Are the following dates in chronological order?"
  14. labeling_instruction_description = "Reply yes or no - if they are chronological reply yes, otherwise reply no."
  15. dataset_display_name = "List of dates"
  16. dataset_description = "A list of dates, you need to check if they are in chronological order."
  17. labels = ["Yes", "No"]
  18. labels_descs = ["Yes, they are in chronological order", "No, they are not in chronological order"]
  19.  
  20. def is_date_sequential():
  21.     dataset = create_dataset(GCLOUD_PROJECT_ID)
  22.     import_data(dataset.name, "TEXT", INPUT_GCS_URI)
  23.     spec_response = create_annotation_spec_set(GCLOUD_PROJECT_ID)
  24.     instruction = create_instruction(GCLOUD_PROJECT_ID, datalabeling.DataType.TEXT, INPUT_GCS_URI)
  25.     label_text(dataset.name, instruction.name, spec_response.name)
  26.     print("Submit complete! Please go to Google Cloud Console tomorrow morning to see the result.")
  27.  
  28. def create_instruction(project_id, data_type, instruction_gcs_uri):
  29.     """ Creates a data labeling PDF instruction for the given Google Cloud
  30.    project. The PDF file should be uploaded to the project in
  31.    Google Cloud Storage.
  32.    """
  33.     from google.cloud import datalabeling_v1beta1 as datalabeling
  34.  
  35.     client = datalabeling.DataLabelingServiceClient()
  36.  
  37.     project_path = f"projects/{project_id}"
  38.  
  39.     pdf_instruction = datalabeling.PdfInstruction(gcs_file_uri=instruction_gcs_uri)
  40.  
  41.     instruction = datalabeling.Instruction(
  42.         display_name=labeling_instruction_name,
  43.         description=labeling_instruction_description,
  44.         data_type=data_type,
  45.         pdf_instruction=pdf_instruction,
  46.     )
  47.  
  48.     operation = client.create_instruction(
  49.         request={"parent": project_path, "instruction": instruction}
  50.     )
  51.  
  52.     result = operation.result()
  53.  
  54.     # The format of the resource name:
  55.     # project_id/{project_id}/instruction/{instruction_id}
  56.     print("The instruction resource name: {}".format(result.name))
  57.     print("Display name: {}".format(result.display_name))
  58.     print("Description: {}".format(result.description))
  59.     print("Create time:")
  60.     print("\tseconds: {}".format(result.create_time.timestamp_pb().seconds))
  61.     print("\tnanos: {}".format(result.create_time.timestamp_pb().nanos))
  62.     print("Data type: {}".format(datalabeling.DataType(result.data_type).name))
  63.     print("Pdf instruction:")
  64.     print("\tGcs file uri: {}\n".format(result.pdf_instruction.gcs_file_uri))
  65.  
  66.     return result
  67.  
  68. def create_dataset(project_id):
  69.     """Creates a dataset for the given Google Cloud project."""
  70.     from google.cloud import datalabeling_v1beta1 as datalabeling
  71.  
  72.     client = datalabeling.DataLabelingServiceClient()
  73.  
  74.     formatted_project_name = f"projects/{project_id}"
  75.  
  76.     dataset = datalabeling.Dataset(
  77.         display_name=dataset_display_name, description=dataset_description
  78.     )
  79.  
  80.     response = client.create_dataset(
  81.         request={"parent": formatted_project_name, "dataset": dataset}
  82.     )
  83.  
  84.     # The format of resource name:
  85.     # project_id/{project_id}/datasets/{dataset_id}
  86.     print("The dataset resource name: {}".format(response.name))
  87.     print("Display name: {}".format(response.display_name))
  88.     print("Description: {}".format(response.description))
  89.     print("Create time:")
  90.     print("\tseconds: {}".format(response.create_time.timestamp_pb().seconds))
  91.     print("\tnanos: {}\n".format(response.create_time.timestamp_pb().nanos))
  92.  
  93.     return response
  94.  
  95. def create_annotation_spec_set(project_id):
  96.     """Creates a data labeling annotation spec set for the given
  97.    Google Cloud project.
  98.    """
  99.     from google.cloud import datalabeling_v1beta1 as datalabeling
  100.  
  101.     client = datalabeling.DataLabelingServiceClient()
  102.  
  103.     project_path = f"projects/{project_id}"
  104.  
  105.     annotation_spec_1 = datalabeling.AnnotationSpec(
  106.         display_name=labels[1], description=labels_descs[1]
  107.     )
  108.  
  109.     annotation_spec_2 = datalabeling.AnnotationSpec(
  110.         display_name=labels[2], description=labels_descs[2]
  111.     )
  112.  
  113.     annotation_spec_set = datalabeling.AnnotationSpecSet(
  114.         display_name=dataset_display_name,
  115.         description=dataset_description,
  116.         annotation_specs=[annotation_spec_1, annotation_spec_2],
  117.     )
  118.  
  119.     response = client.create_annotation_spec_set(
  120.         request={"parent": project_path, "annotation_spec_set": annotation_spec_set}
  121.     )
  122.  
  123.     # The format of the resource name:
  124.     # project_id/{project_id}/annotationSpecSets/{annotationSpecSets_id}
  125.     print("The annotation_spec_set resource name: {}".format(response.name))
  126.     print("Display name: {}".format(response.display_name))
  127.     print("Description: {}".format(response.description))
  128.     print("Annotation specs:")
  129.     for annotation_spec in response.annotation_specs:
  130.         print("\tDisplay name: {}".format(annotation_spec.display_name))
  131.         print("\tDescription: {}\n".format(annotation_spec.description))
  132.  
  133.     return response
  134.  
  135. def import_data(dataset_resource_name, data_type, input_gcs_uri):
  136.     """Imports data to the given Google Cloud project and dataset."""
  137.     from google.cloud import datalabeling_v1beta1 as datalabeling
  138.  
  139.     client = datalabeling.DataLabelingServiceClient()
  140.  
  141.     gcs_source = datalabeling.GcsSource(input_uri=input_gcs_uri, mime_type="text/csv")
  142.  
  143.     csv_input_config = datalabeling.InputConfig(
  144.         data_type=data_type, gcs_source=gcs_source
  145.     )
  146.  
  147.     response = client.import_data(
  148.         request={"name": dataset_resource_name, "input_config": csv_input_config}
  149.     )
  150.  
  151.     result = response.result()
  152.  
  153.     # The format of resource name:
  154.     # project_id/{project_id}/datasets/{dataset_id}
  155.     print("Dataset resource name: {}\n".format(result.dataset))
  156.  
  157.     return result
  158.  
  159. def label_text(
  160.     dataset_resource_name, instruction_resource_name, annotation_spec_set_resource_name
  161. ):
  162.     """Labels a text dataset."""
  163.     from google.cloud import datalabeling_v1beta1 as datalabeling
  164.  
  165.     client = datalabeling.DataLabelingServiceClient()
  166.  
  167.     basic_config = datalabeling.HumanAnnotationConfig(
  168.         instruction=instruction_resource_name,
  169.         annotated_dataset_display_name=dataset_display_name,
  170.         label_group="label group 1",
  171.         replica_count=1,
  172.     )
  173.  
  174.     feature = datalabeling.LabelTextRequest.Feature.TEXT_CLASSIFICATION
  175.  
  176.     config = datalabeling.TextEntityExtractionConfig(
  177.         annotation_spec_set=annotation_spec_set_resource_name
  178.     )
  179.  
  180.     response = client.label_text(
  181.         request={
  182.             "parent": dataset_resource_name,
  183.             "basic_config": basic_config,
  184.             "feature": feature,
  185.             "text_classification_config": config,
  186.         }
  187.     )
  188.  
  189.     print("Label_text operation name: {}".format(response.operation.name))
  190.     return response
  191.  
Advertisement
Add Comment
Please, Sign In to add comment