| """
|
| Utility functions for generating slugs from names and names from slugs.
|
| """
|
|
|
| import re
|
| import unicodedata
|
|
|
|
|
| def generate_name_from_slug(slug: str) -> str:
|
| """
|
| Generate a human-readable name from a slug.
|
|
|
| Examples:
|
| - "llama-3.3-70b-versatile" -> "Llama 3.3 70B Versatile"
|
| - "openai/gpt-oss-20b" -> "GPT OSS 20B"
|
| - "nvidia/nemotron-3-super-120b-a12b:free" -> "Nemotron 3 Super 120B A12B"
|
| - "gpt-4-turbo" -> "GPT 4 Turbo"
|
|
|
| Args:
|
| slug: The slug to convert to a name
|
|
|
| Returns:
|
| A human-readable name
|
| """
|
|
|
| if "/" in slug:
|
| slug = slug.split("/")[-1]
|
|
|
|
|
| if ":" in slug:
|
| slug = slug.split(":")[0]
|
|
|
|
|
| name = slug.replace("-", " ").replace("_", " ")
|
|
|
|
|
| words = name.split()
|
|
|
|
|
| capitalized_words = []
|
| for word in words:
|
|
|
| if re.match(r"^\d+b$", word, re.IGNORECASE):
|
| capitalized_words.append(word.upper())
|
|
|
| elif len(word) <= 4 and word.replace(".", "").replace("-", "").isalnum():
|
| capitalized_words.append(word.upper())
|
|
|
| else:
|
| capitalized_words.append(word.capitalize())
|
|
|
| return " ".join(capitalized_words)
|
|
|
|
|
| def generate_slug(name: str) -> str:
|
| """
|
| Generate a URL-friendly slug from a name.
|
|
|
| Examples:
|
| - "GPT-4 Turbo" -> "gpt-4-turbo"
|
| - "Claude 3 Opus" -> "claude-3-opus"
|
| - "Llama 2 70B" -> "llama-2-70b"
|
| - "Gemini Pro 1.5" -> "gemini-pro-1-5"
|
| - "OpenAI Embeddings" -> "openai-embeddings"
|
|
|
| Args:
|
| name: The name to convert to a slug
|
|
|
| Returns:
|
| A URL-friendly slug
|
| """
|
|
|
| slug = name.lower()
|
|
|
|
|
| slug = unicodedata.normalize("NFKD", slug)
|
| slug = slug.encode("ascii", "ignore").decode("ascii")
|
|
|
|
|
| slug = re.sub(r"[^\w\s-]", "-", slug)
|
| slug = re.sub(r"[-\s]+", "-", slug)
|
|
|
|
|
| slug = slug.strip("-")
|
|
|
| return slug
|
|
|
|
|
| def generate_unique_slug(name: str, existing_slugs: list[str]) -> str:
|
| """
|
| Generate a unique slug by appending a number if necessary.
|
|
|
| Args:
|
| name: The name to convert to a slug
|
| existing_slugs: List of existing slugs to check against
|
|
|
| Returns:
|
| A unique URL-friendly slug
|
| """
|
| base_slug = generate_slug(name)
|
| slug = base_slug
|
| counter = 1
|
|
|
| while slug in existing_slugs:
|
| slug = f"{base_slug}-{counter}"
|
| counter += 1
|
|
|
| return slug
|
|
|