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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import discord
|
| 2 |
+
from discord import File
|
| 3 |
+
from discord.ext import commands
|
| 4 |
+
from gradio_client import Client
|
| 5 |
+
import os
|
| 6 |
+
import sys
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
with gr.Blocks() as demo:
|
| 10 |
+
gr.Markdown("NinoBot machine is working...")
|
| 11 |
+
|
| 12 |
+
ninoToken = os.getenv('ninoToken')
|
| 13 |
+
|
| 14 |
+
# Initialize the bot with the intents
|
| 15 |
+
intents = discord.Intents.default()
|
| 16 |
+
intents.members = True
|
| 17 |
+
intents.messages = True
|
| 18 |
+
|
| 19 |
+
#Pass the intent variables to the bot
|
| 20 |
+
bot = discord.Bot(intents=intents)
|
| 21 |
+
|
| 22 |
+
# Function that is called as soon as the bot is ready
|
| 23 |
+
@bot.event
|
| 24 |
+
async def on_ready():
|
| 25 |
+
global clientMAGNeT, magnetAvailable, clientLLaMa, llamaAvailable
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
clientMAGNeT = Client("https://fffiloni-magnet.hf.space/--replicas/58jap/")
|
| 29 |
+
magnetAvailable = True
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"Failed to connect to MAGNet: {str(e)}")
|
| 32 |
+
magnetAvailable = False
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
clientLLaMa = Client("https://ysharma-explore-llamav2-with-tgi.hf.space/--replicas/brc3o/")
|
| 36 |
+
llamaAvailable = True
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"Failed to connect to MAGNet: {str(e)}")
|
| 39 |
+
llamaAvailable = False
|
| 40 |
+
|
| 41 |
+
print(f'{bot.user} has connected to Discord!')
|
| 42 |
+
|
| 43 |
+
# Function to trigger the command /ninowrite
|
| 44 |
+
@bot.command(name="ninowrite", description="Send a message with Nino to a specific channel")
|
| 45 |
+
@commands.has_role(1121063615106142329)
|
| 46 |
+
# pycord will figure out the types for you
|
| 47 |
+
async def ninowrite(ctx, channel: discord.TextChannel, message: discord.Option(str, "Enter your message")):
|
| 48 |
+
await channel.send(message)
|
| 49 |
+
await ctx.respond(f'<:NinoBlue:1205472560842539028> successfuly sent the message `{message}` sent to <#{channel.id}> !')
|
| 50 |
+
|
| 51 |
+
# Check for errors in the /ninowrite command
|
| 52 |
+
@ninowrite.error
|
| 53 |
+
async def your_command_error(ctx, error):
|
| 54 |
+
if isinstance(error, commands.CheckFailure):
|
| 55 |
+
await ctx.respond("You can use the `/ninotalk` command only if you are part of <@&1121063615106142329>", ephemeral=True)
|
| 56 |
+
|
| 57 |
+
# Function to trigger the command /ninoplay
|
| 58 |
+
@bot.command(name="ninoplay", description="Generate audio tracks from text")
|
| 59 |
+
@commands.check_any(commands.has_role(1121063615106142329), commands.has_role(1204421985443258498))
|
| 60 |
+
async def ninoplay(ctx,
|
| 61 |
+
model: discord.Option(str, "Choose your model", choices=["facebook/magnet-small-10secs", "facebook/magnet-medium-10secs", "facebook/magnet-small-30secs", "facebook/magnet-medium-30secs", "facebook/audio-magnet-small", "facebook/audio-magnet-medium"]),
|
| 62 |
+
prompt: discord.Option(str, "Enter your prompt"),
|
| 63 |
+
temperature: discord.Option(float, "Temperature", choices=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])):
|
| 64 |
+
if magnetAvailable:
|
| 65 |
+
await ctx.respond(f'<:NinoBlue:1205472560842539028> successfuly added in queue!\nYour audio generation with the prompt `{prompt}` using the model `{model}` with the temperature `{temperature}` has started, in approx. 45s will be available!', ephemeral=True)
|
| 66 |
+
result = clientMAGNeT.predict(
|
| 67 |
+
model, # Literal['facebook/magnet-small-10secs', 'facebook/magnet-medium-10secs', 'facebook/magnet-small-30secs', 'facebook/magnet-medium-30secs', 'facebook/audio-magnet-small', 'facebook/audio-magnet-medium'] in 'Model' Radio component
|
| 68 |
+
"", # str in 'Model Path (custom models)' Textbox component
|
| 69 |
+
prompt, # str in 'Input Text' Textbox component
|
| 70 |
+
temperature, # float in 'Temperature' Number component
|
| 71 |
+
0.9, # float in 'Top-p' Number component
|
| 72 |
+
10, # float in 'Max CFG coefficient' Number component
|
| 73 |
+
1, # float in 'Min CFG coefficient' Number component
|
| 74 |
+
20, # float in 'Decoding Steps (stage 1)' Number component
|
| 75 |
+
10, # float in 'Decoding Steps (stage 2)' Number component
|
| 76 |
+
10, # float in 'Decoding Steps (stage 3)' Number component
|
| 77 |
+
10, # float in 'Decoding Steps (stage 4)' Number component
|
| 78 |
+
"prod-stride1 (new!)", # Literal['max-nonoverlap', 'prod-stride1 (new!)'] in 'Span Scoring' Radio component
|
| 79 |
+
api_name="/predict_full"
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
print(f"{prompt} from {ctx.author} generated!")
|
| 83 |
+
|
| 84 |
+
# Format the result paths
|
| 85 |
+
parent_folders = []
|
| 86 |
+
result_paths = []
|
| 87 |
+
if isinstance(result[0], dict):
|
| 88 |
+
for key, value in result[0].items():
|
| 89 |
+
if key != "subtitles":
|
| 90 |
+
os.remove(value)
|
| 91 |
+
os.rmdir(os.path.dirname(value))
|
| 92 |
+
for path in result[1:]:
|
| 93 |
+
result_paths.append(path)
|
| 94 |
+
|
| 95 |
+
try:
|
| 96 |
+
await ctx.respond(f"<:NinoBlue:1205472560842539028> `{prompt}` generated using the MAGNeT model `{model}` by <@{ctx.author.id}>")
|
| 97 |
+
for index, path in enumerate(result_paths):
|
| 98 |
+
with open(path, 'rb') as fp:
|
| 99 |
+
await ctx.followup.send(file=File(fp, f"{prompt}_{ctx.author}_output_{index}.wav"))
|
| 100 |
+
os.remove(path)
|
| 101 |
+
parent_folders.append(os.path.dirname(path))
|
| 102 |
+
|
| 103 |
+
for folder in parent_folders:
|
| 104 |
+
os.rmdir(folder)
|
| 105 |
+
|
| 106 |
+
except Exception as e:
|
| 107 |
+
await ctx.respond(f"Failed to send the audio file: {str(e)}", ephemeral=True)
|
| 108 |
+
else:
|
| 109 |
+
await ctx.respond("The MAGNeT service is currently unavailable, please try again later.", ephemeral=True)
|
| 110 |
+
|
| 111 |
+
# Check for errors in the /ninoplay command
|
| 112 |
+
@ninoplay.error
|
| 113 |
+
async def your_command_error(ctx, error):
|
| 114 |
+
if isinstance(error, commands.CheckFailure):
|
| 115 |
+
await ctx.respond("You can use the `/ninoaudio` command only if you are part of <@&1121063615106142329> or <@&1121063615106142329>", ephemeral=True)
|
| 116 |
+
|
| 117 |
+
# Function to trigger the command /ninochat
|
| 118 |
+
@bot.command(name="ninochat", description="Nino LLm chatbot based on LLaMa")
|
| 119 |
+
@commands.check_any(commands.has_role(1121063615106142329), commands.has_role(1204421985443258498))
|
| 120 |
+
async def ninochat(ctx,
|
| 121 |
+
model: discord.Option(str, "Choose your model", choices=["llama-2-70b-chat"]),
|
| 122 |
+
prompt: discord.Option(str, "Enter your prompt"),
|
| 123 |
+
temperature: discord.Option(float, "Temperature", choices=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])):
|
| 124 |
+
if llamaAvailable:
|
| 125 |
+
await ctx.respond(f'<:NinoBlue:1205472560842539028> successfuly added in queue!\nYour text with the prompt `{prompt}` using the model `{model}` and with the temperature `{temperature}` has started, in approx. 24s will be available!', ephemeral=True)
|
| 126 |
+
|
| 127 |
+
result = clientLLaMa.predict(
|
| 128 |
+
prompt, # str in 'parameter_7' Textbox component
|
| 129 |
+
"", # str in 'Optional system prompt' Textbox component
|
| 130 |
+
temperature, # float (numeric value between 0.0 and 1.0) in 'Temperature' Slider component
|
| 131 |
+
1024, # float (numeric value between 0 and 4096) in 'Max new tokens' Slider component
|
| 132 |
+
0.9, # float (numeric value between 0.0 and 1) in 'Top-p (nucleus sampling)' Slider component
|
| 133 |
+
1.2, # float (numeric value between 1.0 and 2.0) in 'Repetition penalty' Slider component
|
| 134 |
+
api_name="/chat"
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
print(f"{prompt} from {ctx.author} generated!")
|
| 138 |
+
|
| 139 |
+
try:
|
| 140 |
+
message_prefix = f"<:NinoBlue:1205472560842539028> the answer to the <@{ctx.author.id}>:\n"
|
| 141 |
+
max_length = 2000 - len(message_prefix) - 10
|
| 142 |
+
chunks = [result[i:i+max_length] for i in range(0, len(result), max_length)]
|
| 143 |
+
await ctx.respond(message_prefix)
|
| 144 |
+
for chunk in chunks:
|
| 145 |
+
await ctx.followup.send(f"{chunk}\n")
|
| 146 |
+
except Exception as e:
|
| 147 |
+
await ctx.respond(f"Failed to retrieve the answer: {str(e)}", ephemeral=True)
|
| 148 |
+
else:
|
| 149 |
+
await ctx.respond("The LLaMa service is currently unavailable, please try again later.", ephemeral=True)
|
| 150 |
+
|
| 151 |
+
# Check for errors in the /ninochat command
|
| 152 |
+
@ninochat.error
|
| 153 |
+
async def your_command_error(ctx, error):
|
| 154 |
+
if isinstance(error, commands.CheckFailure):
|
| 155 |
+
await ctx.respond("You can use the `/ninochat` command only if you are part of <@&1121063615106142329> or <@&1121063615106142329>", ephemeral=True)
|
| 156 |
+
|
| 157 |
+
# Function to trigger the command /restart
|
| 158 |
+
@bot.command(name="restart", description="Restart the bot")
|
| 159 |
+
@commands.has_role(1121063615106142329)
|
| 160 |
+
async def restart(ctx):
|
| 161 |
+
await ctx.send("I'm taking a power nap! <:NinoBlue:1205472560842539028>\nI'll be back in a moment!")
|
| 162 |
+
await bot.logout()
|
| 163 |
+
await bot.close()
|
| 164 |
+
python = sys.executable
|
| 165 |
+
os.execl(python, python, *sys.argv)
|
| 166 |
+
|
| 167 |
+
# Check for errors in the /ninochat command
|
| 168 |
+
@restart.error
|
| 169 |
+
async def your_command_error(ctx, error):
|
| 170 |
+
if isinstance(error, commands.CheckFailure):
|
| 171 |
+
await ctx.respond("You can use the `/restart` command only if you are part of <@&1121063615106142329>", ephemeral=True)
|
| 172 |
+
|
| 173 |
+
# Function that is triggered when a member joins the server
|
| 174 |
+
@bot.event
|
| 175 |
+
async def on_member_join(member):
|
| 176 |
+
channel = discord.utils.get(member.guild.text_channels, name="🤖┊bot")
|
| 177 |
+
|
| 178 |
+
if channel: # Ensure the channel was found
|
| 179 |
+
await channel.send(f"Ciao, {member.mention}! It's a pleasure to have you here! <:NinoBlue:1205472560842539028>")
|
| 180 |
+
else:
|
| 181 |
+
print("The channel 🤖┊bot was not found.")
|
| 182 |
+
|
| 183 |
+
# Function that is triggered when a member send a message
|
| 184 |
+
@bot.event
|
| 185 |
+
async def on_message(message):
|
| 186 |
+
# Don't let the bot react to its own messages.
|
| 187 |
+
if message.author == bot.user:
|
| 188 |
+
return
|
| 189 |
+
|
| 190 |
+
# Check if the message is in the specific channel.
|
| 191 |
+
if message.channel.name == "🌍┊risorse":
|
| 192 |
+
emoji = discord.utils.get(bot.emojis, id=1205472560842539028)
|
| 193 |
+
if emoji: # If the emoji was found
|
| 194 |
+
try:
|
| 195 |
+
await message.add_reaction(emoji)
|
| 196 |
+
except discord.HTTPException as e:
|
| 197 |
+
print(f"Failed to add reaction: {e}")
|
| 198 |
+
else:
|
| 199 |
+
print("Emoji not found.")
|
| 200 |
+
|
| 201 |
+
if message.channel.name == "🚀┊risultati":
|
| 202 |
+
await message.add_reaction("🚀")
|
| 203 |
+
|
| 204 |
+
demo.launch()
|
| 205 |
+
|
| 206 |
+
# -----------------------------------------------------------
|
| 207 |
+
# Run the bot in a loop
|
| 208 |
+
bot.run(ninoToken)
|