lunarflu HF Staff commited on
Commit
fb3fc2e
·
1 Parent(s): 3b4bb8e

Create app.py

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