CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 4d agoView on Hugging Face
0likes1.1kdownloads
hf-add-model-to-collection.py81 linesDownload Raw Back to utils
1#!/usr/bin/env python32 3from huggingface_hub import HfApi4import argparse5import sys6 7def add_model_to_collection(collection_slug, model_id, note=""):8    """9    Add a model to an existing collection10 11    Args:12        collection_slug: The slug of the collection (e.g., "username/collection-name-12345")13        model_id: The model repository ID (e.g., "username/model-name")14        note: Optional note about the model15 16    Returns:17        True if successful, False if failed18    """19 20    # Initialize API21    api = HfApi()22 23    try:24        user_info = api.whoami()25        print(f"โœ… Authenticated as: {user_info['name']}")26 27        # Verify the model exists28        print(f"๐Ÿ” Checking if model exists: {model_id}")29        try:30            model_info = api.model_info(model_id)31        except Exception as e:32            print(f"โŒ Model not found or not accessible: {model_id}")33            print(f"Error: {e}")34            return False35 36        print(f"๐Ÿ“š Adding model to collection...")37        api.add_collection_item(38            collection_slug=collection_slug,39            item_id=model_id,40            item_type="model",41            note=note42        )43 44        print(f"โœ… Model added to collection successfully!")45        print(f"๐Ÿ”— Collection URL: https://huggingface.co/collections/{collection_slug}")46 47        return True48 49    except Exception as e:50        print(f"โŒ Error adding model to collection: {e}")51        return False52 53def main():54    # This script requires that the environment variable HF_TOKEN is set with your55    # Hugging Face API token.56    api = HfApi()57 58    parser = argparse.ArgumentParser(description='Add model to a Huggingface Collection')59    parser.add_argument('--collection', '-c', help='The collection slug username/collection-hash', required=True)60    parser.add_argument('--model', '-m', help='The model to add to the Collection', required=True)61    parser.add_argument('--note', '-n', help='An optional note/description', required=False)62    args = parser.parse_args()63 64    collection = args.collection65    model = args.model66    note = args.note67 68    success = add_model_to_collection(69        collection_slug=collection,70        model_id=model,71        note=note72    )73 74    if success:75        print("\n๐ŸŽ‰ Model added successfully!")76    else:77        print("\nโŒ Failed to add model to collection")78        sys.exit(1)79if __name__ == "__main__":80    main()81