huggingface / discord-bots

48 stars 4 forks source link

Our bots 🤖

Bot Code Link Invite Link
CodeLlama 13B Code Invite Bot
DeepFloydIF Code Invite Bot
Falcon 180B Code Invite Bot
Wuerstchen Code Invite Bot
AudioLDM 2 Code -
MusicGen Code -

TLDR: How do our bots work ❓

Building blocks of a Discord Bot 🤖

  1. Create an application
  2. Create a Hugging Face Space
  3. Add commands

After that, we'll have a working discord bot. So how do we spice it up with machine learning?

Using ML demos in your bot 🧠

Here's an extremely simplified example 💻:

from gradio_client import Client

musicgen = Client("huggingface-projects/transformers-musicgen", hf_token=os.getenv("HF_TOKEN"))

# call this function when we use a command + prompt
async def music_create(ctx, prompt): 
    # run_in_executor for the blocking function
    loop = asyncio.get_running_loop()
    job = await loop.run_in_executor(None, music_create_job, prompt)

    # extract what we want from the outputs
    video = job.outputs()[0][0]

    # send what we want to discord
    await thread.send(video_file)

# submit as a Gradio job; this makes retrieving outputs simpler
def music_create_job(prompt):
    # pass prompt and other parameters if necessary
    job = musicgen.submit(prompt, api_name="/predict")
    return job

In summary, we:

  1. Use a command and specify a prompt ("piano music", for example)
  2. Query a specific Gradio Space as an API, and send it our prompt
  3. Retrieve the results once done and post them to discord

🎉 And voila! 🎉

For further explorations (depending on your needs), we can recommend checking these out 🧐: