Synced repo using 'sync_with_huggingface' Github Action
Browse files- app.py +15 -0
- audioldm2.py +71 -0
app.py
CHANGED
@@ -3,6 +3,7 @@ import threading
|
|
3 |
|
4 |
import discord
|
5 |
import gradio as gr
|
|
|
6 |
from deepfloydif import deepfloydif_stage_1, deepfloydif_stage_2_react_check
|
7 |
from discord import app_commands
|
8 |
from discord.ext import commands
|
@@ -83,6 +84,20 @@ async def musicgen(ctx, prompt: str):
|
|
83 |
print(f"Error: {e}")
|
84 |
|
85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
@client.event
|
87 |
async def on_reaction_add(reaction, user):
|
88 |
"""Checks for a reaction in order to call dfif2"""
|
|
|
3 |
|
4 |
import discord
|
5 |
import gradio as gr
|
6 |
+
from audiodlm2 import audiodlm2_create
|
7 |
from deepfloydif import deepfloydif_stage_1, deepfloydif_stage_2_react_check
|
8 |
from discord import app_commands
|
9 |
from discord.ext import commands
|
|
|
84 |
print(f"Error: {e}")
|
85 |
|
86 |
|
87 |
+
@client.hybrid_command(
|
88 |
+
name="audiodlm2",
|
89 |
+
with_app_command=True,
|
90 |
+
description="Enter a prompt to generate music!",
|
91 |
+
)
|
92 |
+
@app_commands.guilds(MY_GUILD)
|
93 |
+
async def audiodlm2(ctx, prompt: str):
|
94 |
+
"""Audiodlm2 generation"""
|
95 |
+
try:
|
96 |
+
await audiodlm2_create(ctx, prompt)
|
97 |
+
except Exception as e:
|
98 |
+
print(f"Error: (app.py){e}")
|
99 |
+
|
100 |
+
|
101 |
@client.event
|
102 |
async def on_reaction_add(reaction, user):
|
103 |
"""Checks for a reaction in order to call dfif2"""
|
audioldm2.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import asyncio
|
2 |
+
import os
|
3 |
+
import random
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
import discord
|
7 |
+
from gradio_client import Client
|
8 |
+
from gradio_client.utils import QueueError
|
9 |
+
|
10 |
+
BOT_USER_ID = 1102236653545861151 # real
|
11 |
+
MUSIC_CHANNEL_ID = 1143183148881035365 # real
|
12 |
+
|
13 |
+
|
14 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
15 |
+
audioldm2 = Client("huggingface-projects/audioldm2-text2audio-text2music", HF_TOKEN)
|
16 |
+
|
17 |
+
|
18 |
+
def audioldm2_create_job(prompt):
|
19 |
+
"""Generates a sound or music based on a given prompt"""
|
20 |
+
try:
|
21 |
+
random.seed(datetime.now().timestamp())
|
22 |
+
guidance_scale = 6 # between 1-6, larger = better, smaller = diverser
|
23 |
+
seed = random.randint(1, 1000)
|
24 |
+
quality_control = 3 # between 1-3, higher = longer compute but better results
|
25 |
+
job = audioldm2.submit(prompt, guidance_scale, seed, quality_control, fn_index=0)
|
26 |
+
while not job.done():
|
27 |
+
pass
|
28 |
+
return job
|
29 |
+
|
30 |
+
except Exception as e:
|
31 |
+
print(f"audioldm2_create_job Error: {e}")
|
32 |
+
|
33 |
+
|
34 |
+
async def audioldm2_create(ctx, prompt):
|
35 |
+
"""Runs audioldm2_create_job in executor"""
|
36 |
+
try:
|
37 |
+
if ctx.author.id != BOT_USER_ID:
|
38 |
+
if ctx.channel.id == MUSIC_CHANNEL_ID:
|
39 |
+
if os.environ.get("TEST_ENV") == "True":
|
40 |
+
print("Safetychecks passed for audioldm2_create")
|
41 |
+
|
42 |
+
message = await ctx.send(f"**{prompt}** - {ctx.author.mention}")
|
43 |
+
if len(prompt) > 99:
|
44 |
+
small_prompt = prompt[:99]
|
45 |
+
else:
|
46 |
+
small_prompt = prompt
|
47 |
+
thread = await message.create_thread(name=small_prompt, auto_archive_duration=60)
|
48 |
+
|
49 |
+
await thread.send(
|
50 |
+
"[DISCLAIMER: HuggingBot is a beta feature; The AudioLDM2"
|
51 |
+
" model can be found here: https://huggingface.co/spaces/"
|
52 |
+
"haoheliu/audioldm2-text2audio-text2music]"
|
53 |
+
)
|
54 |
+
if os.environ.get("TEST_ENV") == "True":
|
55 |
+
print("Running audioldm2_create_job...")
|
56 |
+
|
57 |
+
loop = asyncio.get_running_loop()
|
58 |
+
job = await loop.run_in_executor(None, audioldm2_create_job, prompt)
|
59 |
+
|
60 |
+
try:
|
61 |
+
job.result()
|
62 |
+
video = job.outputs()[0]
|
63 |
+
except QueueError:
|
64 |
+
await thread.send("The gradio space powering this bot is really busy! Please try again later!")
|
65 |
+
|
66 |
+
with open(video, "rb") as file:
|
67 |
+
discord_file = discord.File(file)
|
68 |
+
await thread.send(file=discord_file)
|
69 |
+
|
70 |
+
except Exception as e:
|
71 |
+
print(f"audioldm2_create Error: {e}")
|