Hub documentation

Otomasi Webhooks

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Otomasi Webhooks

Webhooks memungkinkan Anda untuk mendengarkan perubahan baru pada repositori atau bucket tertentu, atau pada semua repositori yang dimiliki oleh kumpulan pengguna/organisasi tertentu (bukan hanya repositori Anda, tetapi repositori apa pun) di Hugging Face.

Gunakan create_webhook di klien Python huggingface_hub untuk membuat webhook yang memicu Job ketika terjadi perubahan di repositori Hugging Face:

from huggingface_hub import create_webhook

# Example: Creating a webhook that triggers a Job
webhook = create_webhook(
    job_id=job_id,
    watched=[{"type": "user", "name": "your-username"}, {"type": "org", "name": "your-org-name"}],
    domains=["repo", "discussion"],
    secret="your-secret"
)

Untuk menjalankan Job saat file ditambahkan atau dihapus dari bucket, pantau bucket tersebut:

webhook = create_webhook(
    job_id=job_id,
    watched=[{"type": "bucket", "name": "your-username/your-bucket"}],
    domains=["repo"],
    secrets={"HF_TOKEN": "hf_***"},
)

Untuk contoh lengkap, lihat Memproses file baru di bucket dengan Jobs.

Webhook memicu Job dengan variabel lingkungan berikut:

  • WEBHOOK_PAYLOAD: seluruh payload webhook sebagai string JSON
  • WEBHOOK_REPO_ID: nama repositori atau bucket (misalnya, user/repo-name)
  • WEBHOOK_REPO_TYPE: tipe repositori (model, dataset, space, atau bucket)
  • WEBHOOK_SECRET: rahasia webhook, jika ada yang dikonfigurasi
  • WEBHOOK_ID: pengidentifikasi unik untuk pengiriman, stabil di antara pengulangan pengiriman tersebut

Eksekusi webhook tidak menyimpan volume atau rahasia (secrets) dari Job. Sampaikan rahasia yang dibutuhkan Job, seperti HF_TOKEN, dengan secrets= di create_webhook. Untuk menjalankan skrip UV dari webhook, definisikan job Anda dengan hf jobs run <image> uv run <url>, karena jika tidak, hf jobs uv run akan mengunggah skrip ke volume, yang tidak dimiliki oleh eksekusi webhook.

Payload webhook berisi beberapa field, berikut adalah beberapa yang berguna:

- event:
  - action: one of "create", "delete", "move", "update"
  - scope: string
- repo:
  - owner: string
  - headSha: string (not sent for buckets)
  - name: string
  - type: one of "dataset", "model", "space", "bucket"
- updatedFiles: for bucket file changes, the list of added and deleted files

Anda dapat menemukan informasi lebih lanjut tentang webhooks dalam dokumentasi Webhooks huggingface_hub.

Update on GitHub