app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import os
|
| 3 |
+
import threading
|
| 4 |
+
from threading import Event
|
| 5 |
+
from typing import Optional
|
| 6 |
+
|
| 7 |
+
import discord
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from discord import Permissions
|
| 10 |
+
from discord.ext import commands
|
| 11 |
+
from discord.utils import oauth_url
|
| 12 |
+
|
| 13 |
+
import gradio_client as grc
|
| 14 |
+
from gradio_client.utils import QueueError
|
| 15 |
+
|
| 16 |
+
event = Event()
|
| 17 |
+
|
| 18 |
+
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
| 19 |
+
|
| 20 |
+
async def wait(job):
|
| 21 |
+
while not job.done():
|
| 22 |
+
await asyncio.sleep(0.2)
|
| 23 |
+
|
| 24 |
+
def get_client(session: Optional[str] = None) -> grc.Client:
|
| 25 |
+
client = grc.Client("huggingface-projects/transformers-musicgen", hf_token=os.getenv("HF_TOKEN"))
|
| 26 |
+
if session:
|
| 27 |
+
client.session_hash = session
|
| 28 |
+
return client
|
| 29 |
+
|
| 30 |
+
intents = discord.Intents.default()
|
| 31 |
+
intents.message_content = True
|
| 32 |
+
bot = commands.Bot(command_prefix="/", intents=intents)
|
| 33 |
+
|
| 34 |
+
@bot.event
|
| 35 |
+
async def on_ready():
|
| 36 |
+
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
|
| 37 |
+
synced = await bot.tree.sync()
|
| 38 |
+
print(f"Synced commands: {', '.join([s.name for s in synced])}.")
|
| 39 |
+
event.set()
|
| 40 |
+
print("------")
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
#-----------------------------------------------------------------------
|
| 44 |
+
@client.hybrid_command(
|
| 45 |
+
name="testm",
|
| 46 |
+
description="Enter a prompt to generate music!",
|
| 47 |
+
)
|
| 48 |
+
async def musicgen_command(ctx, prompt: str, seed: int = None):
|
| 49 |
+
"""Generates music based on a prompt"""
|
| 50 |
+
if ctx.author.id == bot.user.id:
|
| 51 |
+
return
|
| 52 |
+
if seed is None:
|
| 53 |
+
seed = random.randint(1, 10000)
|
| 54 |
+
try:
|
| 55 |
+
await music_create(ctx, prompt, seed)
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"Error: {e}")
|
| 58 |
+
#-----------------------------------------------------------------------
|
| 59 |
+
async def music_create(ctx, prompt, seed):
|
| 60 |
+
"""Runs music_create_job in executor"""
|
| 61 |
+
try:
|
| 62 |
+
message = await ctx.send(f"**{prompt}** - {ctx.author.mention} Generating...")
|
| 63 |
+
|
| 64 |
+
loop = asyncio.get_running_loop()
|
| 65 |
+
job = await loop.run_in_executor(None, music_create_job, prompt, seed)
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
job.result()
|
| 69 |
+
files = job.outputs()
|
| 70 |
+
media_files = files[0]
|
| 71 |
+
except QueueError:
|
| 72 |
+
await ctx.send("The gradio space powering this bot is really busy! Please try again later!")
|
| 73 |
+
|
| 74 |
+
audio = media_files[0]
|
| 75 |
+
video = media_files[1]
|
| 76 |
+
short_filename = prompt[:20]
|
| 77 |
+
audio_filename = f"{short_filename}.mp3"
|
| 78 |
+
video_filename = f"{short_filename}.mp4"
|
| 79 |
+
|
| 80 |
+
with open(video, "rb") as file:
|
| 81 |
+
discord_video_file = discord.File(file, filename=video_filename)
|
| 82 |
+
await ctx.send(file=discord_video_file)
|
| 83 |
+
|
| 84 |
+
with open(audio, "rb") as file:
|
| 85 |
+
discord_audio_file = discord.File(file, filename=audio_filename)
|
| 86 |
+
await ctx.send(file=discord_audio_file)
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print(f"music_create Error: {e}")
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def music_create_job(prompt, seed):
|
| 93 |
+
"""Generates music based on a given prompt"""
|
| 94 |
+
try:
|
| 95 |
+
job = musicgen.submit(prompt, seed, api_name="/predict")
|
| 96 |
+
while not job.done():
|
| 97 |
+
pass
|
| 98 |
+
return job
|
| 99 |
+
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print(f"music_create_job Error: {e}")
|