import os import re import gradio as gr from huggingface_hub import get_collection os.environ["COLLECTION_SLUG_OR_URL"] = "https://huggingface.co/collections/PleIAs/finance-commons-66925e1095c7fa6e6828e26c" def extract_collection_id(input_text): if url_match := re.match(r"https://huggingface\.co/collections/(.+)$", input_text): return url_match[1] # Check if input is already in the correct format return input_text if re.match(r"^[\w-]+/[\w-]+", input_text) else None def load_collection(): collection_input = os.getenv("COLLECTION_SLUG_OR_URL") if not collection_input: raise ValueError("COLLECTION_SLUG_OR_URL environment variable is not set.") collection_id = extract_collection_id(collection_input) if not collection_id: raise ValueError( "Invalid collection ID or URL in COLLECTION_SLUG_OR_URL environment variable." ) collection = get_collection(collection_id) if dataset_ids := [ item.item_id for item in collection.items if item.item_type == "dataset" ]: return dataset_ids, collection_id else: raise ValueError("No datasets found in this collection.") def display_dataset(dataset_ids, index): dataset_id = dataset_ids[index] return gr.HTML(f"""""") def navigate_dataset(dataset_ids, index, direction): new_index = (index + direction) % len(dataset_ids) return ( new_index, f"Dataset {new_index + 1} of {len(dataset_ids)}: {dataset_ids[new_index]}", ) def get_display_name(collection_id): # Pattern to match username/repo-name with an optional ID of 16 or more hexadecimal characters pattern = r"^(.+?)-([a-f0-9]{16,})$" if match := re.match(pattern, collection_id): return match[1] else: # If no match, return the original return collection_id try: dataset_ids, collection_id = load_collection() display_name = get_display_name(collection_id) with gr.Blocks() as demo: gr.Markdown(f"