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
|
| 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.ext import commands
|
| 10 |
+
import gradio_client as grc
|
| 11 |
+
from gradio_client.utils import QueueError
|
| 12 |
+
|
| 13 |
+
event = Event()
|
| 14 |
+
|
| 15 |
+
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
async def wait(job):
|
| 19 |
+
while not job.done():
|
| 20 |
+
await asyncio.sleep(0.2)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def get_client(session: Optional[str] = None) -> grc.Client:
|
| 24 |
+
client = grc.Client("https://tiiuae-falcon-180b-demo.hf.space", hf_token=os.getenv("HF_TOKEN"))
|
| 25 |
+
if session:
|
| 26 |
+
client.session_hash = session
|
| 27 |
+
return client
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def truncate_response(response: str) -> str:
|
| 31 |
+
ending = "...\nTruncating response to 2000 characters due to discord api limits."
|
| 32 |
+
if len(response) > 2000:
|
| 33 |
+
return response[: 2000 - len(ending)] + ending
|
| 34 |
+
else:
|
| 35 |
+
return response
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
intents = discord.Intents.default()
|
| 39 |
+
intents.message_content = True
|
| 40 |
+
bot = commands.Bot(command_prefix="/", intents=intents)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@bot.event
|
| 44 |
+
async def on_ready():
|
| 45 |
+
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
|
| 46 |
+
synced = await bot.tree.sync()
|
| 47 |
+
print(f"Synced commands: {', '.join([s.name for s in synced])}.")
|
| 48 |
+
event.set()
|
| 49 |
+
print("------")
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
thread_to_client = {}
|
| 53 |
+
thread_to_user = {}
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
@bot.hybrid_command(
|
| 57 |
+
name="falcon180",
|
| 58 |
+
description="Enter some text to chat with the bot! Like this: /falcon180 Hello, how are you?",
|
| 59 |
+
)
|
| 60 |
+
async def chat(ctx, prompt: str):
|
| 61 |
+
if ctx.author.id == bot.user.id:
|
| 62 |
+
return
|
| 63 |
+
try:
|
| 64 |
+
message = await ctx.send("Creating thread...")
|
| 65 |
+
|
| 66 |
+
thread = await message.create_thread(name=prompt[:100])
|
| 67 |
+
loop = asyncio.get_running_loop()
|
| 68 |
+
client = await loop.run_in_executor(None, get_client, None)
|
| 69 |
+
job = client.submit(prompt, "", 0.9, 256, 0.95, 1.0, api_name="/chat")
|
| 70 |
+
await wait(job)
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
job.result()
|
| 74 |
+
response = job.outputs()[-1]
|
| 75 |
+
await thread.send(truncate_response(response))
|
| 76 |
+
thread_to_client[thread.id] = client
|
| 77 |
+
thread_to_user[thread.id] = ctx.author.id
|
| 78 |
+
except QueueError:
|
| 79 |
+
await thread.send("The gradio space powering this bot is really busy! Please try again later!")
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print(f"{e}")
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
async def continue_chat(message):
|
| 86 |
+
"""Continues a given conversation based on chathistory"""
|
| 87 |
+
try:
|
| 88 |
+
client = thread_to_client[message.channel.id]
|
| 89 |
+
prompt = message.content
|
| 90 |
+
job = client.submit(prompt, "", 0.9, 256, 0.95, 1.0, api_name="/chat")
|
| 91 |
+
await wait(job)
|
| 92 |
+
try:
|
| 93 |
+
job.result()
|
| 94 |
+
response = job.outputs()[-1]
|
| 95 |
+
await message.reply(truncate_response(response))
|
| 96 |
+
except QueueError:
|
| 97 |
+
await message.reply("The gradio space powering this bot is really busy! Please try again later!")
|
| 98 |
+
|
| 99 |
+
except Exception as e:
|
| 100 |
+
print(f"Error: {e}")
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
@bot.event
|
| 104 |
+
async def on_message(message):
|
| 105 |
+
"""Continue the chat"""
|
| 106 |
+
try:
|
| 107 |
+
if not message.author.bot:
|
| 108 |
+
if message.channel.id in thread_to_user:
|
| 109 |
+
if thread_to_user[message.channel.id] == message.author.id:
|
| 110 |
+
await continue_chat(message)
|
| 111 |
+
else:
|
| 112 |
+
await bot.process_commands(message)
|
| 113 |
+
|
| 114 |
+
except Exception as e:
|
| 115 |
+
print(f"Error: {e}")
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
# running in thread
|
| 119 |
+
def run_bot():
|
| 120 |
+
if not DISCORD_TOKEN:
|
| 121 |
+
print("DISCORD_TOKEN NOT SET")
|
| 122 |
+
event.set()
|
| 123 |
+
else:
|
| 124 |
+
bot.run(DISCORD_TOKEN)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
threading.Thread(target=run_bot).start()
|
| 128 |
+
|
| 129 |
+
event.wait()
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
welcome_message = """
|
| 133 |
+
## Add this bot to your server by clicking this link:
|
| 134 |
+
|
| 135 |
+
https://discord.com/api/oauth2/authorize?client_id=1155169841276260546&permissions=326417516544&scope=bot
|
| 136 |
+
|
| 137 |
+
## How to use it?
|
| 138 |
+
|
| 139 |
+
The bot can be triggered via `/falcon180` followed by your text prompt.
|
| 140 |
+
|
| 141 |
+
This will create a thread with the bot's response to your text prompt.
|
| 142 |
+
You can reply in the thread (without `/falcon180`) to continue the conversation.
|
| 143 |
+
In the thread, the bot will only reply to the original author of the command.
|
| 144 |
+
|
| 145 |
+
⚠️ Note ⚠️: Please make sure this bot's command does have the same name as another command in your server.
|
| 146 |
+
|
| 147 |
+
⚠️ Note ⚠️: Bot commands do not work in DMs with the bot as of now.
|
| 148 |
+
"""
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
with gr.Blocks() as demo:
|
| 152 |
+
gr.Markdown(f"""
|
| 153 |
+
# Discord bot of https://tiiuae-falcon-180b-demo.hf.space
|
| 154 |
+
{welcome_message}
|
| 155 |
+
""")
|
| 156 |
+
|
| 157 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
discord.py
|
|
|
|
|
|
| 1 |
+
discord.py
|
| 2 |
+
gradio
|