Download app.py from huggingface-projects/huggingbots: direct link, hf CLI and curl.
- Browser
- Download file 3.04 kB
-
https://huggingface.co/spaces/huggingface-projects/huggingbots/resolve/refs%2Fpr%2F17/app.py
- Command line
-
hf download hf://spaces/huggingface-projects/huggingbots@refs/pr/17/app.py
-
curl -L -o app.py https://huggingface.co/spaces/huggingface-projects/huggingbots/resolve/refs%2Fpr%2F17/app.py
3.04 kB
| import os | |
| import threading | |
| import discord | |
| import gradio as gr | |
| from deepfloydif import deepfloydif_stage_1, deepfloydif_stage_2_react_check | |
| from discord import app_commands | |
| from discord.ext import commands | |
| from falcon import continue_falcon, try_falcon | |
| from musicgen import music_create | |
| # HF GUILD SETTINGS | |
| MY_GUILD_ID = 1077674588122648679 if os.getenv("TEST_ENV", False) else 879548962464493619 | |
| MY_GUILD = discord.Object(id=MY_GUILD_ID) | |
| DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None) | |
| class Bot(commands.Bot): | |
| """This structure allows slash commands to work instantly.""" | |
| def __init__(self): | |
| super().__init__(command_prefix="/", intents=discord.Intents.all()) | |
| async def setup_hook(self): | |
| await self.tree.sync(guild=discord.Object(MY_GUILD_ID)) | |
| print(f"Synced slash commands for {self.user}.") | |
| client = Bot() | |
| async def on_ready(): | |
| print(f"Logged in as {client.user} (ID: {client.user.id})") | |
| print("------") | |
| async def falcon(ctx, prompt: str): | |
| """Command that begins a new conversation with Falcon""" | |
| try: | |
| await try_falcon(ctx, prompt) | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| async def on_message(message): | |
| """Checks channel and continues Falcon conversation if it's the right Discord Thread""" | |
| try: | |
| await continue_falcon(message) | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| async def deepfloydif(ctx, prompt: str): | |
| """DeepfloydIF stage 1 generation""" | |
| try: | |
| await deepfloydif_stage_1(ctx, prompt, client) | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| async def musicgen(ctx, prompt: str): | |
| """Generates music based on a prompt""" | |
| try: | |
| await music_create(ctx, prompt) | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| async def on_reaction_add(reaction, user): | |
| """Checks for a reaction in order to call dfif2""" | |
| try: | |
| await deepfloydif_stage_2_react_check(reaction, user) | |
| except Exception as e: | |
| print(f"Error: {e} (known error, does not cause issues, low priority)") | |
| def run_bot(): | |
| client.run(DISCORD_TOKEN) | |
| threading.Thread(target=run_bot).start() | |
| """This allows us to run the Discord bot in a Python thread""" | |
| with gr.Blocks() as demo: | |
| gr.Markdown(""" | |
| # Huggingbots Server | |
| This space hosts the huggingbots discord bot. | |
| Currently supported models are Falcon and DeepfloydIF | |
| """) | |
| demo.queue(concurrency_count=100) | |
| demo.launch() | |