JafarUruc/orange_cube
0
1import os2import random3import re4import sys5import time6from pathlib import Path7 8import huggingface_hub9from huggingface_hub.constants import HF_HOME10 11RESERVED_KEYS = ["project", "run", "timestamp", "step", "time"]12TRACKIO_DIR = os.path.join(HF_HOME, "trackio")13 14TRACKIO_LOGO_PATH = str(Path(__file__).parent.joinpath("trackio_logo.png"))15 16 17def generate_readable_name():18 """19 Generates a random, readable name like "dainty-sunset-1"20 """21 adjectives = [22 "dainty",23 "brave",24 "calm",25 "eager",26 "fancy",27 "gentle",28 "happy",29 "jolly",30 "kind",31 "lively",32 "merry",33 "nice",34 "proud",35 "quick",36 "silly",37 "tidy",38 "witty",39 "zealous",40 "bright",41 "shy",42 "bold",43 "clever",44 "daring",45 "elegant",46 "faithful",47 "graceful",48 "honest",49 "inventive",50 "jovial",51 "keen",52 "lucky",53 "modest",54 "noble",55 "optimistic",56 "patient",57 "quirky",58 "resourceful",59 "sincere",60 "thoughtful",61 "upbeat",62 "valiant",63 "warm",64 "youthful",65 "zesty",66 "adventurous",67 "breezy",68 "cheerful",69 "delightful",70 "energetic",71 "fearless",72 "glad",73 "hopeful",74 "imaginative",75 "joyful",76 "kindly",77 "luminous",78 "mysterious",79 "neat",80 "outgoing",81 "playful",82 "radiant",83 "spirited",84 "tranquil",85 "unique",86 "vivid",87 "wise",88 "zany",89 "artful",90 "bubbly",91 "charming",92 "dazzling",93 "earnest",94 "festive",95 "gentlemanly",96 "hearty",97 "intrepid",98 "jubilant",99 "knightly",100 "lively",101 "magnetic",102 "nimble",103 "orderly",104 "peaceful",105 "quick-witted",106 "robust",107 "sturdy",108 "trusty",109 "upstanding",110 "vibrant",111 "whimsical",112 ]113 nouns = [114 "sunset",115 "forest",116 "river",117 "mountain",118 "breeze",119 "meadow",120 "ocean",121 "valley",122 "sky",123 "field",124 "cloud",125 "star",126 "rain",127 "leaf",128 "stone",129 "flower",130 "bird",131 "tree",132 "wave",133 "trail",134 "island",135 "desert",136 "hill",137 "lake",138 "pond",139 "grove",140 "canyon",141 "reef",142 "bay",143 "peak",144 "glade",145 "marsh",146 "cliff",147 "dune",148 "spring",149 "brook",150 "cave",151 "plain",152 "ridge",153 "wood",154 "blossom",155 "petal",156 "root",157 "branch",158 "seed",159 "acorn",160 "pine",161 "willow",162 "cedar",163 "elm",164 "falcon",165 "eagle",166 "sparrow",167 "robin",168 "owl",169 "finch",170 "heron",171 "crane",172 "duck",173 "swan",174 "fox",175 "wolf",176 "bear",177 "deer",178 "moose",179 "otter",180 "beaver",181 "lynx",182 "hare",183 "badger",184 "butterfly",185 "bee",186 "ant",187 "beetle",188 "dragonfly",189 "firefly",190 "ladybug",191 "moth",192 "spider",193 "worm",194 "coral",195 "kelp",196 "shell",197 "pebble",198 "boulder",199 "cobble",200 "sand",201 "wavelet",202 "tide",203 "current",204 ]205 adjective = random.choice(adjectives)206 noun = random.choice(nouns)207 number = random.randint(1, 99)208 return f"{adjective}-{noun}-{number}"209 210 211def block_except_in_notebook():212 in_notebook = bool(getattr(sys, "ps1", sys.flags.interactive))213 if in_notebook:214 return215 try:216 while True:217 time.sleep(0.1)218 except (KeyboardInterrupt, OSError):219 print("Keyboard interruption in main thread... closing dashboard.")220 221 222def simplify_column_names(columns: list[str]) -> dict[str, str]:223 """224 Simplifies column names to first 10 alphanumeric or "/" characters with unique suffixes.225 226 Args:227 columns: List of original column names228 229 Returns:230 Dictionary mapping original column names to simplified names231 """232 simplified_names = {}233 used_names = set()234 235 for col in columns:236 alphanumeric = re.sub(r"[^a-zA-Z0-9/]", "", col)237 base_name = alphanumeric[:10] if alphanumeric else f"col_{len(used_names)}"238 239 final_name = base_name240 suffix = 1241 while final_name in used_names:242 final_name = f"{base_name}_{suffix}"243 suffix += 1244 245 simplified_names[col] = final_name246 used_names.add(final_name)247 248 return simplified_names249 250 251def print_dashboard_instructions(project: str) -> None:252 """253 Prints instructions for viewing the Trackio dashboard.254 255 Args:256 project: The name of the project to show dashboard for.257 """258 YELLOW = "\033[93m"259 BOLD = "\033[1m"260 RESET = "\033[0m"261 262 print("* View dashboard by running in your terminal:")263 print(f'{BOLD}{YELLOW}trackio show --project "{project}"{RESET}')264 print(f'* or by running in Python: trackio.show(project="{project}")')265 266 267def preprocess_space_and_dataset_ids(268 space_id: str | None, dataset_id: str | None269) -> tuple[str | None, str | None]:270 if space_id is not None and "/" not in space_id:271 username = huggingface_hub.whoami()["name"]272 space_id = f"{username}/{space_id}"273 if dataset_id is not None and "/" not in dataset_id:274 username = huggingface_hub.whoami()["name"]275 dataset_id = f"{username}/{dataset_id}"276 if space_id is not None and dataset_id is None:277 dataset_id = f"{space_id}_dataset"278 return space_id, dataset_id279 280 281def fibo():282 """Generator for Fibonacci backoff: 1, 1, 2, 3, 5, 8, ..."""283 a, b = 1, 1284 while True:285 yield a286 a, b = b, a + b287 