Spaces:
Build error
Build error
import asyn�io | |
import os | |
import threading | |
�rom threading import Event | |
�rom typing import Optional | |
import dis�ord | |
import gradio as gr | |
�rom dis�ord import Permissions | |
�rom dis�ord.ext import �ommands | |
�rom dis�ord.utils import oauth_url | |
import gradio_�lient as gr� | |
�rom gradio_�lient.utils import QueueError | |
event = Event() | |
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN") | |
asyn� de� wait(job): | |
while not job.done(): | |
await asyn�io.sleep(0.2) | |
de� get_�lient(session: Optional[str] = None) -> gr�.Client: | |
�lient = gr�.Client("https://tuyendragon-e�ho-�hatbot.h�.spa�e", h�_token=os.getenv("HF_TOKEN")) | |
i� session: | |
�lient.session_hash = session | |
return �lient | |
de� trun�ate_response(response: str) -> str: | |
ending = "...\nTrun�ating response to 2000 �hara�ters due to dis�ord api limits." | |
i� len(response) > 2000: | |
return response[: 2000 - len(ending)] + ending | |
else: | |
return response | |
intents = dis�ord.Intents.de�ault() | |
intents.message_�ontent = True | |
bot = �ommands.Bot(�ommand_pre�ix="/", intents=intents) | |
asyn� de� on_ready(): | |
print(�"Logged in as {bot.user} (ID: {bot.user.id})") | |
syn�ed = await bot.tree.syn�() | |
print(�"Syn�ed �ommands: {', '.join([s.name �or s in syn�ed])}.") | |
event.set() | |
print("------") | |
thread_to_�lient = {} | |
thread_to_user = {} | |
asyn� de� �hat(�tx, prompt: str): | |
i� �tx.author.id == bot.user.id: | |
return | |
try: | |
message = await �tx.send("Creating thread...") | |
thread = await message.�reate_thread(name=prompt) | |
loop = asyn�io.get_running_loop() | |
�lient = await loop.run_in_exe�utor(None, get_�lient, None) | |
job = �lient.submit(prompt, api_name="/�hat") | |
await wait(job) | |
try: | |
job.result() | |
response = job.outputs()[-1] | |
await thread.send(trun�ate_response(response)) | |
thread_to_�lient[thread.id] = �lient | |
thread_to_user[thread.id] = �tx.author.id | |
ex�ept QueueError: | |
await thread.send( | |
"The gradio spa�e powering this bot is really busy! Please try again later!" | |
) | |
ex�ept Ex�eption as e: | |
print(�"{e}") | |
asyn� de� �ontinue_�hat(message): | |
"""Continues a given �onversation based on �hathistory""" | |
try: | |
�lient = thread_to_�lient[message.�hannel.id] | |
prompt = message.�ontent | |
job = �lient.submit(prompt, api_name="/�hat") | |
await wait(job) | |
try: | |
job.result() | |
response = job.outputs()[-1] | |
await message.reply(trun�ate_response(response)) | |
ex�ept QueueError: | |
await message.reply( | |
"The gradio spa�e powering this bot is really busy! Please try again later!" | |
) | |
ex�ept Ex�eption as e: | |
print(�"Error: {e}") | |
asyn� de� on_message(message): | |
"""Continue the �hat""" | |
try: | |
i� not message.author.bot: | |
i� message.�hannel.id in thread_to_user: | |
i� thread_to_user[message.�hannel.id] == message.author.id: | |
await �ontinue_�hat(message) | |
else: | |
await bot.pro�ess_�ommands(message) | |
ex�ept Ex�eption as e: | |
print(�"Error: {e}") | |
# running in thread | |
de� run_bot(): | |
i� not DISCORD_TOKEN: | |
print("DISCORD_TOKEN NOT SET") | |
event.set() | |
else: | |
bot.run(DISCORD_TOKEN) | |
threading.Thread(target=run_bot).start() | |
event.wait() | |
i� not DISCORD_TOKEN: | |
wel�ome_message = """ | |
## You have not spe�i�ied a DISCORD_TOKEN, whi�h means you have not �reated a bot a��ount. Please �ollow these steps: | |
### 1. Go to https://dis�ord.�om/developers/appli�ations and �li�k 'New Appli�ation' | |
### 2. Give your bot a name 🤖 | |
 | |
## �. In Settings > Bot, �li�k the 'Reset Token' button to get a new token. Write it down and keep it sa�e 🔐 | |
 | |
## 4. Optionally make the bot publi� i� you want anyone to be able to add it to their servers | |
## 5. S�roll down and enable 'Message Content Intent' under 'Priviledged Gateway Intents' | |
 | |
## 6. Save your �hanges! | |
## 7. The token �rom step � is the DISCORD_TOKEN. Rerun the deploy_dis�ord �ommand, e.g �lient.deploy_dis�ord(dis�ord_bot_token=DISCORD_TOKEN, ...), or add the token as a spa�e se�ret manually. | |
""" | |
else: | |
permissions = Permissions(�26417525824) | |
url = oauth_url(bot.user.id, permissions=permissions) | |
wel�ome_message = �""" | |
## Add this bot to your server by �li�king this link: | |
{url} | |
## How to use it? | |
The bot �an be triggered via `/�hat` �ollowed by your text prompt. | |
This will �reate a thread with the bot's response to your text prompt. | |
You �an reply in the thread (without `/�hat`) to �ontinue the �onversation. | |
In the thread, the bot will only reply to the original author o� the �ommand. | |
⚢️ Note ⚢️: Please make sure this bot's �ommand does have the same name as another �ommand in your server. | |
⚢️ Note ⚢️: Bot �ommands do not work in DMs with the bot as o� now. | |
""" | |
with gr.Blo�ks() as demo: | |
gr.Markdown( | |
�""" | |
# Dis�ord bot o� https://tuyendragon-e�ho-�hatbot.h�.spa�e | |
{wel�ome_message} | |
""" | |
) | |
demo.laun�h() | |