<jupyter_start><jupyter_text>Introduction to Data Labeling with Gretel Cloud APIsThis blueprint gives a very basic introduction on how to utilize Gretel's Cloud APIs to label data in a publish / subscribe model. In the below code we start a producer thread that simply creates a fake record and sends it to a temporary Gretel Cloud Project. Meanwhile, the main thread will consume labeled records from the project and just print them to stdout.<jupyter_code>%%capture

!pip install -U gretel-client faker
import os
import getpass
import sys
import threading
import time

from faker import Faker

from gretel_client import get_cloud_client
from gretel_client.client import temporary_project
from gretel_client.projects import Project

API_KEY = os.getenv("GRETEL_API_KEY") or getpass.getpass("Gretel API Key:")
def subscriber(project: Project):
    # By default, ``iter_records()`` will block waiting for
    # new labeled records to be ready for consumption. It will
    # fetch in batches, so if there are already records in the
    # stream, we will immediately yield the most recent 200 records.
    for rec in project.iter_records():
        yield rec


def publish(project: Project, event: threading.Event):
    fake = Faker()
    while not event.is_set():
        rec = {
            "name": fake.name(),
            "phone": fake.phone_number()
        }
        project.send(rec)
        time.sleep(2)


def start(api_key: str):
    client = get_cloud_client("api", api_key)
    
    # NOTE: When this context handler exits, the project will
    # be automatically deleted.
    with temporary_project(client) as project:
        print(f"*** Console Project URL: {project.get_console_url()} ***\n")
        print("Visit the link above while this example runs to see your records in Gretel Cloud!\n\n")
        publish_event = threading.Event()
        publish_thread = threading.Thread(target=publish, args=(project, publish_event))
        publish_thread.start()
        try:
            for rec in subscriber(project):
                print(rec)
        except KeyboardInterrupt:
            print("Shutting down...")
        finally:
            publish_event.set()
            publish_thread.join()
    
start(API_KEY)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07791057990261177, 'fraction_numerical': 0.0017706949977866313, 'mean_word_length': 3.216417910447761, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '53138', 'license': 'Apache-2.0', 'n_tokens_mistral': 629, 'n_tokens_neox': 611, 'n_words': 235, 'path': 'gretel/gc-labeling_pub_sub_basic/blueprint.ipynb', 'repo_name': 'Ab-Abdurrahman/gretel-blueprints', 'size': '2259'}