Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Create civitai curl api for load model safetensor and others using unr:air code, also…

  1. User

    Create civitai curl api for load model safetensor and others using unr:air code, also include json response

  2. Assistant

    Sure! I can help you create a curl command to interact with the Civitai API for downloading models like .safetensor files and others. I'll also include examples of JSON responses. Below is a step-by-step guide.


    1. Understanding the Civitai API

    Civitai provides an API that allows users to access model information and download links programmatically. You can perform the following actions:

    • Retrieve model information: Get details about a specific model, including available versions and download URLs.
    • Download model files: Use the download URLs to fetch the actual model files.

    2. Retrieving Model Information

    To get information about a specific model, use the model's ID with the /models/{modelId} endpoint.

    Example Request

    curl -X GET "https://civitai.com/api/v1/models/{modelId}"
    

    Replace {modelId} with the actual ID of the model you're interested in.

    Sample JSON Response

    {
      "id": 1234,
      "name": "Sample Model",
      "type": "Checkpoint",
      "description": "This is a sample model.",
      "creator": {
        "username": "creatorUser",
        "image": "https://civitai.com/avatars/creatorUser.png"
      },
      "modelVersions": [
        {
          "id": 5678,
          "name": "v1.0",
          "description": "First version of the model.",
          "createdAt": "2023-10-01T12:34:56.789Z",
          "downloadUrl": "https://civitai.com/api/download/models/5678",
          "files": [
            {
              "name": "sample_model.safetensors",
              "sizeKB": 204800,
              "type": "Model",
              "format": "SafeTensor",
              "downloadUrl": "https://civitai.com/api/download/models/5678"
            }
          ],
          "images": [],
          "trainedWords": ["sample", "model"]
        }
      ]
    }
    

    3. Downloading Model Files

    After retrieving the model information, you can use the downloadUrl to fetch the model file.

    Example Download Command

    curl -L -o sample_model.safetensors "https://civitai.com/api/download/models/{modelVersionId}"
    

    Replace {modelVersionId} with the actual version ID of the model.

    • -L: Follow redirects.
    • -o: Specify the output file name.

    4. Full Workflow Example

    Let's put it all together in a script that:

    1. Fetches the model information.
    2. Parses the JSON to find the download URL.
    3. Downloads the model file.

    Bash Script Example

    #!/bin/bash
    
    # Set the model ID
    MODEL_ID=1234  # Replace with your model ID
    
    # Fetch model information
    MODEL_INFO=$(curl -s "https://civitai.com/api/v1/models/$MODEL_ID")
    
    # Parse JSON to get the download URL and file name
    DOWNLOAD_URL=$(echo "$MODEL_INFO" | jq -r '.modelVersions[0].files[0].downloadUrl')
    FILE_NAME=$(echo "$MODEL_INFO" | jq -r '.modelVersions[0].files[0].name')
    
    # Download the model file
    curl -L -o "$FILE_NAME" "$DOWNLOAD_URL"
    
    echo "Downloaded $FILE_NAME"
    

    Notes:

    • This script uses jq to parse JSON. Ensure jq is installed (sudo apt-get install jq).
    • It downloads the first file of the first model version. Modify the script if you need a specific version or file.

    5. Handling Different File Formats

    If you want to download files with a specific format (e.g., SafeTensor, CKPT), you can modify the script to filter based on the format field.

    Modified Script for Specific Format

    #!/bin/bash
    
    # Set the model ID and desired format
    MODEL_ID=1234  # Replace with your model ID
    DESIRED_FORMAT="SafeTensor"  # Replace with the desired format
    
    # Fetch model information
    MODEL_INFO=$(curl -s "https://civitai.com/api/v1/models/$MODEL_ID")
    
    # Parse JSON to find the download URL and file name for the desired format
    DOWNLOAD_URL=$(echo "$MODEL_INFO" | jq -r --arg format "$DESIRED_FORMAT" '
      .modelVersions[].files[] | select(.format == $format) | .downloadUrl' | head -n 1)
    FILE_NAME=$(echo "$MODEL_INFO" | jq -r --arg format "$DESIRED_FORMAT" '
      .modelVersions[].files[] | select(.format == $format) | .name' | head -n 1)
    
    # Check if the download URL was found
    if [ -z "$DOWNLOAD_URL" ]; then
      echo "No files found with format $DESIRED_FORMAT"
      exit 1
    fi
    
    # Download the model file
    curl -L -o "$FILE_NAME" "$DOWNLOAD_URL"
    
    echo "Downloaded $FILE_NAME"
    

    6. Example JSON Response Explanation

    Let's break down parts of the sample JSON response:

    • Model Details
      • id: Unique identifier for the model.
      • name: Name of the model.
      • type: Type of model (e.g., Checkpoint, TextualInversion).
      • description: Text description of the model.
      • creator: Information about the model's creator.
    • Model Versions
      • Each model can have multiple versions under modelVersions.
      • id: Unique identifier for the version.
      • name: Version name (e.g., "v1.0").
      • downloadUrl: API endpoint to download the version.
      • files: Array of files associated with the version.
        • name: Filename.
        • sizeKB: Size of the file in kilobytes.
        • type: Type of file (usually "Model").
        • format: File format (e.g., "SafeTensor", "CKPT").
        • downloadUrl: Direct URL to download the file.

    7. Additional Tips

    • Authentication: If the API requires authentication, include headers with your API key or token.
      curl -H "Authorization: Bearer YOUR_API_KEY" ...
      
    • Error Handling: Always check for errors in responses. The API might return error messages if something goes wrong.
    • Rate Limiting: Be mindful of any rate limits imposed by the API to avoid being blocked.

    8. Conclusion

    You can use the above methods to create curl commands or scripts to download models from Civitai using their API. Adjust the scripts as needed for your specific use case.


    Let me know if you need further assistance or have any questions!