npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gradio-bot

v0.1.10

Published

Turn any Hugging Face Space or Gradio application into a discord.js bot.

Downloads

51

Readme

Gradio Bot

Turn any Hugging Face Space or Gradio application into a discord.js bot.

sd-gif

Installation

pnpm i -g gradio-bot

For API usage, you can install it as a dependency in your project without the -g flag.

CLI Usage

For the CLI, it requires 2 environment variables to be set:

  • BOT_ID: The application ID of your bot.
  • BOT_TOKEN: The token of your bot.

You can also set them in a .env file.

To get the bot ID and token, you need to create a bot application in the Discord Developer Portal.

Then, simply run the following command:

gradio-bot 'user/repo'

The bot will automatically register the commands and start running.

You can also pre-configure the spaces by setting the SPACES environment variable like 'user/repo1,user/repo2'.

Example

gradio-bot 'stabilityai/stable-diffusion-3-medium'

The fields on Gradio will be automatically converted to Discord command options.

options

sd3

File uploads are also supported! 🎉

file

Dynamic Management

You can also dynamically manage the spaces by using the management commands:

  • management list: List all the spaces that the bot is currently serving.
  • management add: Add a new space to the bot.
  • management remove: Remove a space from the bot.

Dynamic management can be disabled by setting the DISABLE_MANAGEMENT environment variable to non-empty.

Options

Use --help to see all the available options:

gradio-bot --help

Some of the options include:

  • --hf-token (HF_TOKEN): The Hugging Face API token to use.

API Usage

As a standalone bot

You can start a bot with the GradioBot directly.

import { Client } from "discord.js";
import { GradioBot } from "gradio-bot";

const client = new Client({ intents: [] });
const gb = await GradioBot.from(space, client);
await gb.register();
await gb.start();

You can explicitly pass the token and application ID to .register and .start, or it will try to get them from the environment variables.

As a command builder and handler

The GradioBot class is inherited from SlashCommandBuilder in discord.js, so you can use it to add new powers to your existing bot too!

To register the new commands, you can use the toJSON method:

import { GradioBot } from "gradio-bot";

const gb = await GradioBot.from(space);

// Just like what you do before, but add gb.toJSON() to the array
const commands = [...others, gb.toJSON()];
const rest = new REST().setToken(token);
await rest.put(Routes.applicationCommands(id), { body: commands });

To handle the interaction, you can use the handle method:

client.on("interactionCreate", async (interaction) => {
    if (!interaction.isChatInputCommand()) return;

    const handled = await gb.handle(interaction);
    if (handled) return;

    // The command name is not matched, do something else
    // ...
});

If you want to have more control over the interaction, you can use the parse method and respond manually:

client.on("interactionCreate", async (interaction) => {
    if (!interaction.isChatInputCommand()) return;

    if (interaction.commandName === gb.name) {
        // Parse the interaction options
        const { route, data } = gb.parse(interaction);

        // Run the prediction by yourself
        const result = await gradio.predict(route, data);

        // Create attachments for sending files (optional)
        const files = await makeAttachments(result.data);

        // Respond to the interaction manually
        await interaction.reply({ content: "Got some cool stuff!", files });
    }
});

Checkout the example-bot for a 50-line multi-space bot!