|
| 1 | +import base64 |
| 2 | +import time |
| 3 | +import os |
| 4 | +from datetime import datetime, timezone |
| 5 | +import uuid |
| 6 | +import json |
| 7 | +import logging |
| 8 | +# import cuid |
| 9 | +import random |
| 10 | +from pprint import pprint |
| 11 | + |
| 12 | +import requests |
| 13 | + |
| 14 | +from labelbox import Client, Project, DataRow, Label |
| 15 | +from labelbox.data.serialization.labelbox_v1 import LBV1Converter |
| 16 | +from labelbox.schema.annotation_import import LabelImport, MALPredictionImport |
| 17 | +from labelbox.schema.labeling_frontend import LabelingFrontend |
| 18 | +from labelbox.schema.ontology import Classification, OntologyBuilder, Ontology, Tool, Option |
| 19 | + |
| 20 | +# logging.basicConfig(level=logging.DEBUG) |
| 21 | + |
| 22 | +# ____________________________________________________________________________________ |
| 23 | +"""HELPER FUNCTIONS""" |
| 24 | + |
| 25 | + |
| 26 | +def cleanup_my_org(): |
| 27 | + from datetime import datetime, timezone |
| 28 | + date = datetime.strptime("2022-07-15", |
| 29 | + "%Y-%m-%d").replace(tzinfo=timezone.utc) |
| 30 | + |
| 31 | + for project in client.get_projects(): |
| 32 | + if project.created_at > date: |
| 33 | + print(project.name) |
| 34 | + project.delete() |
| 35 | + for dataset in client.get_datasets(): |
| 36 | + if dataset.created_at > date: |
| 37 | + print(dataset.name) |
| 38 | + dataset.delete() |
| 39 | + # for model in client.get_models(): |
| 40 | + # model.delete() |
| 41 | + |
| 42 | + |
| 43 | +def get_lb_client(environment: str = "prod"): |
| 44 | + if environment == "prod": |
| 45 | + API_KEY = os.environ.get('apikey') |
| 46 | + client = Client(API_KEY) |
| 47 | + elif environment == "staging": |
| 48 | + API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjbDN1OXJ3NzIwMDlvMHl4a2ViOHhkdml0Iiwib3JnYW5pemF0aW9uSWQiOiJjbDN1OXJ3Nm4wMDluMHl4azYzczVnNjZwIiwiYXBpS2V5SWQiOiJjbDZldGg2Zm0wZnBtMHkxZWJpazY2bTVlIiwic2VjcmV0IjoiYjhmNjcwZTFkYjdkODNhYzdkYzYzYjQzMjE5MTBkODQiLCJpYXQiOjE2NTk2MDQxNzYsImV4cCI6MjI5MDc1NjE3Nn0.zCYnfXQEQl8PwsJbsBvP3s_cDA-hQbiFcNgIy82uOrQ" |
| 49 | + client = Client(API_KEY, endpoint="https://api.lb-stage.xyz/graphql") |
| 50 | + elif environment == "local": |
| 51 | + API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjbDI5M2N2OWUwMDBicnRvdTBneWgyZ3RvIiwib3JnYW5pemF0aW9uSWQiOiJjbDI5M2N2NjAwMDBhcnRvdTl0M3JhMGpzIiwiYXBpS2V5SWQiOiJjbDN2c21icWgwMDBhbjdvdWFldWMwMmcyIiwic2VjcmV0IjoiYTQ4MmZjODU4OGU5YmI0NmJhZDU2YjljZDBhZDcyZTUiLCJpYXQiOjE2NTQxMDAzMTUsImV4cCI6MjI4NTI1MjMxNX0.UJC2uF8Cu6WwBSIZGUZUY8UznP7RCRsG4ns616OFXjI" |
| 52 | + client = Client(API_KEY, endpoint='http://localhost:8080/graphql') |
| 53 | + else: |
| 54 | + print("Invalid environment") |
| 55 | + exit(1) |
| 56 | + return client |
| 57 | + |
| 58 | + |
| 59 | +# ____________________________________________________________________________________ |
| 60 | + |
| 61 | +os.system('clear') |
| 62 | + |
| 63 | +client = get_lb_client("prod") |
| 64 | + |
| 65 | +# ____________________________________________________________________________________ |
| 66 | + |
| 67 | +project = client.get_project("cl6xntneb7t28072bggdydv7a") |
| 68 | +# organization_id = client.get_organization().uid |
| 69 | +# dataset = client.create_dataset(name="hello world") |
| 70 | +# dataset = client.get_dataset("cl6xy8bcw0g7v07068u9t5hiv") |
| 71 | +# print(dataset.uid) |
| 72 | +# file_path = "/Users/jonathantso/Downloads/sample_batch.txt" |
| 73 | +# dataset.create_data_rows(file_path) |
| 74 | +# ____________________________________________________________________________________ |
| 75 | +# annotations = [] |
| 76 | + |
| 77 | +# rows = list(project.batches())[0].export_data_rows() |
| 78 | + |
| 79 | +# for row in project.export_queued_data_rows(include_metadata=True): |
| 80 | +# print(f"row: {row['id']}, {row['externalId']}") |
| 81 | +# for i in range(4): |
| 82 | +# annotations.append({ |
| 83 | +# "uuid": str(uuid.uuid4()), |
| 84 | +# "name": "boxy", |
| 85 | +# "dataRow": {"id": row['id']}, |
| 86 | +# "bbox": {"top": round(random.uniform(0,300),2), "left": round(random.uniform(0,300),2), "height": round(random.uniform(200,500),2), "width": round(random.uniform(0,200),2)}, |
| 87 | +# "unit": "POINTS", |
| 88 | +# "page": random.randint(0,9) |
| 89 | +# }) |
| 90 | + |
| 91 | +# import_annotations = MALPredictionImport.create_from_objects(client=client, project_id = project.uid, name=f"import {str(uuid.uuid4())}", predictions=annotations) |
| 92 | +# import_annotations.wait_until_done() |
| 93 | + |
| 94 | +# print(f"\nerrors: {import_annotations.errors}") |
| 95 | +#assert should be that import_annotations.errors == [] |
| 96 | +#____________________________________________________________________________________ |
| 97 | +# labels = project.label_generator() |
| 98 | +with open("/Users/jonathantso/Downloads/export-2022-08-17T18_37_30.233Z.json", |
| 99 | + "r") as f: |
| 100 | + labels = json.load(f) |
| 101 | + print("\nnow deserializing..\n") |
| 102 | + labels = LBV1Converter.deserialize(labels) |
| 103 | + # for label in labels: |
| 104 | + # if label.annotations: |
| 105 | + # for obj in label.annotations: |
| 106 | + # print(f"\n\t{obj}") |
| 107 | + |
| 108 | + print("\nnow serializing..\n") |
| 109 | + labels = LBV1Converter.serialize(labels) |
| 110 | + # for label in labels: |
| 111 | + # print("serialized") |
| 112 | + # print(label, "\n") |
| 113 | + labels = LBV1Converter.deserialize(labels) |
| 114 | + for label in labels: |
| 115 | + if label.annotations: |
| 116 | + for obj in label.annotations: |
| 117 | + print(f"\n\t{obj}") |
0 commit comments