[BIG REVERT TO STABLE, RETESTING SPLITTING LATER]
Browse files
app.py
CHANGED
@@ -1,43 +1,389 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
@client.event
|
18 |
async def on_ready():
|
19 |
print(f'Logged in as {client.user} (ID: {client.user.id})')
|
20 |
print('------')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
#-------------------------------------------------------------------------------------------------------------------------------------
|
|
|
22 |
@client.tree.command()
|
23 |
-
@app_commands.describe(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
async def falcon(interaction: discord.Interaction, prompt: str):
|
25 |
-
"""Generates text based on a given prompt"""
|
26 |
try:
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
except Exception as e:
|
29 |
print(f"Error: {e}")
|
|
|
30 |
#-------------------------------------------------------------------------------------------------------------------------------------
|
31 |
@client.event
|
32 |
async def on_message(message):
|
33 |
-
"""detects messages and calls continue_falcon if we're in the right channel"""
|
34 |
try:
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
except Exception as e:
|
37 |
print(f"Error: {e}")
|
38 |
#-------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
39 |
def run_bot():
|
40 |
-
"""Runs the bot in a thread"""
|
41 |
client.run(DISCORD_TOKEN)
|
42 |
|
43 |
threading.Thread(target=run_bot).start()
|
|
|
1 |
+
import discord
|
2 |
+
from discord import app_commands #needed
|
3 |
+
import gradio_client
|
4 |
+
import gradio as gr
|
5 |
+
from gradio_client import Client #needed
|
6 |
+
import os #needed
|
7 |
+
import threading #needed
|
8 |
+
import json #needed
|
9 |
+
import random #dfif
|
10 |
+
from PIL import Image #dfif
|
11 |
+
import asyncio #dfif
|
12 |
+
import glob #needed, dfif
|
13 |
+
|
14 |
+
#-------------------------------------------------------------------------------------------------------------------------------------
|
15 |
+
|
16 |
+
|
17 |
+
MY_GUILD = discord.Object(id=1077674588122648679) # HF = 879548962464493619, test = 1077674588122648679
|
18 |
+
HF_TOKEN = os.getenv('HF_TOKEN')
|
19 |
+
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
20 |
+
|
21 |
+
falcon_userid_threadid_dictionary = {}
|
22 |
+
threadid_conversation = {}
|
23 |
+
GUILD_ID = 1077674588122648679 # 1077674588122648679 = test
|
24 |
+
FALCON_CHANNEL_ID = 1079459939405279232 # 1079459939405279232 = test
|
25 |
+
DEEPFLOYD_CHANNEL_ID = 1121834257959092234 # 1121834257959092234 = test
|
26 |
+
BOT_USER_ID = 1086256910572986469 # 1086256910572986469 = test
|
27 |
+
|
28 |
+
# HF = <a:loading:1114111677990981692>
|
29 |
+
# test = 🔁
|
30 |
+
|
31 |
+
# HF = <:agree:1098629085955113011>
|
32 |
+
# test = ✔️
|
33 |
+
|
34 |
+
# HF = <:disagree:1098628957521313892>
|
35 |
+
# test = ❌
|
36 |
+
|
37 |
+
df = Client("huggingface-projects/IF", HF_TOKEN)
|
38 |
+
falconclient = Client("HuggingFaceH4/falcon-chat", HF_TOKEN)
|
39 |
+
instructions = "The following is a conversation between a highly knowledgeable " \
|
40 |
+
"and intelligent AI assistant, called Falcon, and a human user, called User. In the " \
|
41 |
+
"following interactions, User and Falcon will converse in natural language, and Falcon " \
|
42 |
+
"will answer User's questions. Falcon was built to be respectful, polite and inclusive. " \
|
43 |
+
"Falcon was built by the Technology Innovation Institute in Abu Dhabi. Falcon will never " \
|
44 |
+
"decline to answer a question, and always attempts to give an answer that User would be satisfied " \
|
45 |
+
"with. It knows a lot, and always tells the truth. The conversation begins."
|
46 |
+
|
47 |
+
|
48 |
+
#-------------------------------------------------------------------------------------------------------------------------------------
|
49 |
+
# This structure allows commands to work instantly (instead of needing to sync global commands for up to an hour)
|
50 |
+
class MyClient(discord.Client):
|
51 |
+
def __init__(self, *, intents: discord.Intents):
|
52 |
+
super().__init__(intents=intents)
|
53 |
+
self.tree = app_commands.CommandTree(self)
|
54 |
+
|
55 |
+
async def setup_hook(self):
|
56 |
+
# This copies the global commands over to our guild
|
57 |
+
self.tree.copy_global_to(guild=MY_GUILD)
|
58 |
+
await self.tree.sync(guild=MY_GUILD)
|
59 |
+
|
60 |
+
#intents = discord.Intents.all()
|
61 |
+
client = MyClient(intents=discord.Intents.default())
|
62 |
+
#-------------------------------------------------------------------------------------------------------------------------------------
|
63 |
@client.event
|
64 |
async def on_ready():
|
65 |
print(f'Logged in as {client.user} (ID: {client.user.id})')
|
66 |
print('------')
|
67 |
+
#-------------------------------------------------------------------------------------------------------------------------------------
|
68 |
+
# deepfloydif stage 1 generation
|
69 |
+
def inference(prompt):
|
70 |
+
negative_prompt = ''
|
71 |
+
seed = random.randint(0, 1000)
|
72 |
+
#seed = 1
|
73 |
+
number_of_images = 4
|
74 |
+
guidance_scale = 7
|
75 |
+
custom_timesteps_1 = 'smart50'
|
76 |
+
number_of_inference_steps = 50
|
77 |
+
|
78 |
+
stage_1_results, stage_1_param_path, stage_1_result_path = df.predict(
|
79 |
+
prompt, negative_prompt, seed, number_of_images, guidance_scale, custom_timesteps_1, number_of_inference_steps, api_name='/generate64')
|
80 |
+
|
81 |
+
return [stage_1_results, stage_1_param_path, stage_1_result_path]
|
82 |
+
#-------------------------------------------------------------------------------------------------------------------------------------
|
83 |
+
# deepfloydif stage 2 upscaling
|
84 |
+
def inference2(index, stage_1_result_path):
|
85 |
+
selected_index_for_stage_2 = index
|
86 |
+
seed_2 = 0
|
87 |
+
guidance_scale_2 = 4
|
88 |
+
custom_timesteps_2 = 'smart50'
|
89 |
+
number_of_inference_steps_2 = 50
|
90 |
+
result_path = df.predict(stage_1_result_path, selected_index_for_stage_2, seed_2,
|
91 |
+
guidance_scale_2, custom_timesteps_2, number_of_inference_steps_2, api_name='/upscale256')
|
92 |
+
|
93 |
+
return result_path
|
94 |
+
#-------------------------------------------------------------------------------------------------------------------------------------
|
95 |
+
async def react1234(reaction_emojis, combined_image_dfif):
|
96 |
+
for emoji in reaction_emojis:
|
97 |
+
await combined_image_dfif.add_reaction(emoji)
|
98 |
#-------------------------------------------------------------------------------------------------------------------------------------
|
99 |
+
# deepfloydIF command (generate images with realistic text using slash commands)
|
100 |
@client.tree.command()
|
101 |
+
@app_commands.describe(
|
102 |
+
prompt='Enter a prompt to generate an image! Can generate realistic text, too!')
|
103 |
+
async def deepfloydif(interaction: discord.Interaction, prompt: str):
|
104 |
+
thread = None
|
105 |
+
try:
|
106 |
+
global BOT_USER_ID
|
107 |
+
global DEEPFLOYD_CHANNEL_ID
|
108 |
+
if interaction.user.id != BOT_USER_ID:
|
109 |
+
if interaction.channel.id == DEEPFLOYD_CHANNEL_ID:
|
110 |
+
await interaction.response.send_message(f"Working on it!")
|
111 |
+
channel = interaction.channel
|
112 |
+
message = await channel.send(f"DeepfloydIF Thread")
|
113 |
+
await message.add_reaction('🔁')
|
114 |
+
thread = await message.create_thread(name=f'{prompt}', auto_archive_duration=60)
|
115 |
+
await thread.send(f"[DISCLAIMER: HuggingBot is a **highly experimental** beta feature; Additional information" \
|
116 |
+
f" on the DeepfloydIF model can be found here: https://huggingface.co/spaces/DeepFloyd/IF")
|
117 |
+
|
118 |
+
dfif_command_message_id = message.id # used for updating the 'status' of our generations using reaction emojis
|
119 |
+
|
120 |
+
await thread.send(f'{interaction.user.mention}Generating images in thread, can take ~1 minute...')
|
121 |
+
|
122 |
+
# generation
|
123 |
+
loop = asyncio.get_running_loop()
|
124 |
+
result = await loop.run_in_executor(None, inference, prompt)
|
125 |
+
stage_1_results = result[0]
|
126 |
+
stage_1_result_path = result[2]
|
127 |
+
partialpath = stage_1_result_path[5:]
|
128 |
+
png_files = list(glob.glob(f"{stage_1_results}/**/*.png"))
|
129 |
+
|
130 |
+
img1 = None
|
131 |
+
img2 = None
|
132 |
+
img3 = None
|
133 |
+
img4 = None
|
134 |
+
|
135 |
+
if png_files:
|
136 |
+
png_file_index = 0
|
137 |
+
images = load_image(png_files, stage_1_results, png_file_index)
|
138 |
+
img1 = images[0]
|
139 |
+
img2 = images[1]
|
140 |
+
img3 = images[2]
|
141 |
+
img4 = images[3]
|
142 |
+
|
143 |
+
|
144 |
+
'''
|
145 |
+
first_png = png_files[0]
|
146 |
+
second_png = png_files[1]
|
147 |
+
third_png = png_files[2]
|
148 |
+
fourth_png = png_files[3]
|
149 |
+
|
150 |
+
first_png_path = os.path.join(stage_1_results, first_png)
|
151 |
+
second_png_path = os.path.join(stage_1_results, second_png)
|
152 |
+
third_png_path = os.path.join(stage_1_results, third_png)
|
153 |
+
fourth_png_path = os.path.join(stage_1_results, fourth_png)
|
154 |
+
|
155 |
+
img1 = Image.open(first_png_path)
|
156 |
+
img2 = Image.open(second_png_path)
|
157 |
+
img3 = Image.open(third_png_path)
|
158 |
+
img4 = Image.open(fourth_png_path)
|
159 |
+
'''
|
160 |
+
combined_image = Image.new('RGB', (img1.width * 2, img1.height * 2))
|
161 |
+
|
162 |
+
combined_image.paste(img1, (0, 0))
|
163 |
+
combined_image.paste(img2, (img1.width, 0))
|
164 |
+
combined_image.paste(img3, (0, img1.height))
|
165 |
+
combined_image.paste(img4, (img1.width, img1.height))
|
166 |
+
|
167 |
+
combined_image_path = os.path.join(stage_1_results, f'{partialpath}{dfif_command_message_id}.png')
|
168 |
+
combined_image.save(combined_image_path)
|
169 |
+
|
170 |
+
with open(combined_image_path, 'rb') as f:
|
171 |
+
combined_image_dfif = await thread.send(f'{interaction.user.mention}React with the image number you want to upscale!', file=discord.File(
|
172 |
+
f, f'{partialpath}{dfif_command_message_id}.png')) # named something like: tmpgtv4qjix1111269940599738479.png
|
173 |
+
|
174 |
+
emoji_list = ['↖️', '↗️', '↙️', '↘️']
|
175 |
+
await react1234(emoji_list, combined_image_dfif)
|
176 |
+
|
177 |
+
await message.remove_reaction('🔁', client.user)
|
178 |
+
await message.add_reaction('✔️')
|
179 |
+
|
180 |
+
except Exception as e:
|
181 |
+
print(f"Error: {e}")
|
182 |
+
await message.remove_reaction('🔁', client.user)
|
183 |
+
await message.add_reaction('❌')
|
184 |
+
#await thread.send(f"Error: {e} <@811235357663297546> (continue_falcon error)")
|
185 |
+
#-------------------------------------------------------------------------------------------------------------------------------------
|
186 |
+
def load_image(png_files, stage_1_results, png_file_index):
|
187 |
+
for file in png_files:
|
188 |
+
png_file = png_files[png_file_index]
|
189 |
+
png_path = os.path.join(stage_1_results, png_file)
|
190 |
+
if png_file_index == 0:
|
191 |
+
img1 = Image.open(png_path)
|
192 |
+
if png_file_index == 1:
|
193 |
+
img2 = Image.open(png_path)
|
194 |
+
if png_file_index == 2:
|
195 |
+
img3 = Image.open(png_path)
|
196 |
+
if png_file_index == 3:
|
197 |
+
img4 = Image.open(png_path)
|
198 |
+
png_file_index = png_file_index + 1
|
199 |
+
return [img1, img2, img3, img4]
|
200 |
+
#-------------------------------------------------------------------------------------------------------------------------------------
|
201 |
+
# upscaling function for images generated using /deepfloydif
|
202 |
+
async def dfif2(index: int, stage_1_result_path, thread, dfif_command_message_id):
|
203 |
+
try:
|
204 |
+
#await thread.send(f"inside dfif2")
|
205 |
+
parent_channel = thread.parent
|
206 |
+
dfif_command_message = await parent_channel.fetch_message(dfif_command_message_id)
|
207 |
+
await dfif_command_message.remove_reaction('✔️', client.user)
|
208 |
+
await dfif_command_message.add_reaction('🔁')
|
209 |
+
#await thread.send(f"getting index")
|
210 |
+
number = index + 1
|
211 |
+
if number == 1:
|
212 |
+
position = "top left"
|
213 |
+
elif number == 2:
|
214 |
+
position = "top right"
|
215 |
+
elif number == 3:
|
216 |
+
position = "bottom left"
|
217 |
+
elif number == 4:
|
218 |
+
position = "bottom right"
|
219 |
+
await thread.send(f"Upscaling the {position} image...")
|
220 |
+
|
221 |
+
# run blocking function in executor
|
222 |
+
loop = asyncio.get_running_loop()
|
223 |
+
result_path = await loop.run_in_executor(None, inference2, index, stage_1_result_path)
|
224 |
+
|
225 |
+
#await thread.send(f"✅upscale done")
|
226 |
+
with open(result_path, 'rb') as f:
|
227 |
+
await thread.send(f'Here is the upscaled image!', file=discord.File(f, 'result.png'))
|
228 |
+
|
229 |
+
await dfif_command_message.remove_reaction('🔁', client.user)
|
230 |
+
await dfif_command_message.add_reaction('✔️')
|
231 |
+
await thread.edit(archived=True)
|
232 |
+
|
233 |
+
except Exception as e:
|
234 |
+
print(f"Error: {e}")
|
235 |
+
parent_channel = thread.parent
|
236 |
+
dfif_command_message = await parent_channel.fetch_message(dfif_command_message_id)
|
237 |
+
await dfif_command_message.remove_reaction('🔁', client.user)
|
238 |
+
await dfif_command_message.add_reaction('❌')
|
239 |
+
await thread.send(f"Error during stage 2 upscaling, {e}")
|
240 |
+
await fullqueue(e, thread)
|
241 |
+
await thread.edit(archived=True)
|
242 |
+
#-------------------------------------------------------------------------------------------------------------------------------------
|
243 |
+
@client.event
|
244 |
+
async def on_reaction_add(reaction, user):
|
245 |
+
try:
|
246 |
+
global BOT_USER_ID
|
247 |
+
global DEEPFLOYD_CHANNEL_ID
|
248 |
+
channel = reaction.message.channel
|
249 |
+
#await channel.send("reaction detected")
|
250 |
+
if user.id != BOT_USER_ID: #
|
251 |
+
thread = reaction.message.channel
|
252 |
+
threadparentid = thread.parent.id
|
253 |
+
#await channel.send(f"threadparentid = {threadparentid}")
|
254 |
+
if threadparentid == DEEPFLOYD_CHANNEL_ID: # 1121834257959092234
|
255 |
+
#await channel.send(f"right parent")
|
256 |
+
if reaction.message.attachments:
|
257 |
+
#await channel.send(f"has attachments")
|
258 |
+
if user.id == reaction.message.mentions[0].id: # if user.id == reaction.message.mentions[0].id:
|
259 |
+
#await channel.send("checks passed for on_react")
|
260 |
+
#await reaction.message.channel.send("✅reaction detected")
|
261 |
+
attachment = reaction.message.attachments[0]
|
262 |
+
image_name = attachment.filename # named something like: tmpgtv4qjix1111269940599738479.png
|
263 |
+
# remove .png first
|
264 |
+
partialpathmessageid = image_name[:-4] # should be tmpgtv4qjix1111269940599738479
|
265 |
+
# extract partialpath, messageid
|
266 |
+
partialpath = partialpathmessageid[:11] # tmpgtv4qjix
|
267 |
+
messageid = partialpathmessageid[11:] # 1111269940599738479
|
268 |
+
# add /tmp/ to partialpath, save as new variable
|
269 |
+
fullpath = "/tmp/" + partialpath # should be /tmp/tmpgtv4qjix
|
270 |
+
#await reaction.message.channel.send(f"✅fullpath extracted, {fullpath}")
|
271 |
+
emoji = reaction.emoji
|
272 |
+
#await reaction.message.channel.send(f"emojis")
|
273 |
+
if emoji == "↖️":
|
274 |
+
index = 0
|
275 |
+
elif emoji == "↗️":
|
276 |
+
index = 1
|
277 |
+
elif emoji == "↙️":
|
278 |
+
index = 2
|
279 |
+
elif emoji == "↘️":
|
280 |
+
index = 3
|
281 |
+
|
282 |
+
#await reaction.message.channel.send(f"✅index extracted, {index}")
|
283 |
+
index = index
|
284 |
+
stage_1_result_path = fullpath
|
285 |
+
thread = reaction.message.channel
|
286 |
+
dfif_command_message_id = messageid
|
287 |
+
#await reaction.message.channel.send(f"✅calling dfif2")
|
288 |
+
await dfif2(index, stage_1_result_path, thread, dfif_command_message_id)
|
289 |
+
|
290 |
+
except Exception as e:
|
291 |
+
print(f"Error: {e} (known error, does not cause issues, low priority)")
|
292 |
+
#-------------------------------------------------------------------------------------------------------------------------------------
|
293 |
+
@client.tree.command()
|
294 |
+
@app_commands.describe(
|
295 |
+
prompt='Enter some text to chat with the bot! Like this: /falcon Hello, how are you?')
|
296 |
async def falcon(interaction: discord.Interaction, prompt: str):
|
|
|
297 |
try:
|
298 |
+
global falcon_userid_threadid_dictionary # tracks userid-thread existence
|
299 |
+
global instructions
|
300 |
+
global threadid_conversation
|
301 |
+
global BOT_USER_ID
|
302 |
+
global FALCON_CHANNEL_ID
|
303 |
+
|
304 |
+
if interaction.user.id != BOT_USER_ID:
|
305 |
+
if interaction.channel.id == FALCON_CHANNEL_ID:
|
306 |
+
await interaction.response.send_message(f"Working on it!")
|
307 |
+
channel = interaction.channel
|
308 |
+
# 1
|
309 |
+
message = await channel.send(f"Creating thread...")
|
310 |
+
thread = await message.create_thread(name=f'{prompt}', auto_archive_duration=60) # interaction.user
|
311 |
+
await thread.send(f"[DISCLAIMER: HuggingBot is a **highly experimental** beta feature; The Falcon " \
|
312 |
+
f"model and system prompt can be found here: https://huggingface.co/spaces/HuggingFaceH4/falcon-chat]")
|
313 |
+
# generation
|
314 |
+
chathistory = falconclient.predict(
|
315 |
+
fn_index=5
|
316 |
+
) # []
|
317 |
+
job = falconclient.submit(prompt, chathistory, instructions, 0.8, 0.9, fn_index=1) # This is not blocking, similar to run_in_executor (but better)
|
318 |
+
while job.done() == False:
|
319 |
+
status = job.status()
|
320 |
+
#print(status)
|
321 |
+
else:
|
322 |
+
file_paths = job.outputs()
|
323 |
+
full_generation = file_paths[-1] # tmp12345678.json
|
324 |
+
with open(full_generation, 'r') as file:
|
325 |
+
data = json.load(file)
|
326 |
+
output_text = data[-1][-1] # we output this as the bot
|
327 |
+
|
328 |
+
threadid_conversation[thread.id] = full_generation
|
329 |
+
|
330 |
+
falcon_userid_threadid_dictionary[thread.id] = interaction.user.id
|
331 |
+
|
332 |
+
print(output_text)
|
333 |
+
await thread.send(f"{output_text}")
|
334 |
+
|
335 |
+
except Exception as e:
|
336 |
+
print(f"Error: {e}")
|
337 |
+
#await thread.send(f"{e} cc <@811235357663297546> (falconprivate error)")
|
338 |
+
#-------------------------------------------------------------------------------------------------------------------------------------
|
339 |
+
async def continue_falcon(message):
|
340 |
+
try:
|
341 |
+
global instructions
|
342 |
+
global threadid_conversation
|
343 |
+
await message.add_reaction('🔁')
|
344 |
+
chathistory = threadid_conversation[message.channel.id]
|
345 |
+
prompt = message.content
|
346 |
+
# generation
|
347 |
+
job = falconclient.submit(prompt, chathistory, instructions, 0.8, 0.9, fn_index=1) # This is not blocking, similar to run_in_executor (but better)
|
348 |
+
while job.done() == False:
|
349 |
+
status = job.status()
|
350 |
+
#print(status)
|
351 |
+
else:
|
352 |
+
file_paths = job.outputs()
|
353 |
+
full_generation = file_paths[-1] # tmp12345678.json
|
354 |
+
with open(full_generation, 'r') as file:
|
355 |
+
data = json.load(file)
|
356 |
+
output_text = data[-1][-1] # we output this as the bot
|
357 |
+
|
358 |
+
threadid_conversation[message.channel.id] = full_generation # overwrite the old file
|
359 |
+
falcon_userid_threadid_dictionary[message.channel.id] = message.author.id
|
360 |
+
|
361 |
+
print(output_text)
|
362 |
+
await message.reply(f"{output_text}")
|
363 |
+
await message.remove_reaction('🔁', client.user) # test=🔁 hf=🔁
|
364 |
+
|
365 |
except Exception as e:
|
366 |
print(f"Error: {e}")
|
367 |
+
await message.reply(f"Error: {e} <@811235357663297546> (continue_falcon error)")
|
368 |
#-------------------------------------------------------------------------------------------------------------------------------------
|
369 |
@client.event
|
370 |
async def on_message(message):
|
|
|
371 |
try:
|
372 |
+
if not message.author.bot:
|
373 |
+
global falcon_userid_threadid_dictionary # tracks userid-thread existence
|
374 |
+
if message.channel.id in falcon_userid_threadid_dictionary: # is this a valid thread?
|
375 |
+
if falcon_userid_threadid_dictionary[message.channel.id] == message.author.id: # more than that - is this specifically the right user for this thread?
|
376 |
+
# call job for falcon
|
377 |
+
#await message.reply("checks succeeded, calling continue_falcon")
|
378 |
+
await continue_falcon(message)
|
379 |
+
|
380 |
except Exception as e:
|
381 |
print(f"Error: {e}")
|
382 |
#-------------------------------------------------------------------------------------------------------------------------------------
|
383 |
+
# running in thread
|
384 |
+
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
385 |
+
|
386 |
def run_bot():
|
|
|
387 |
client.run(DISCORD_TOKEN)
|
388 |
|
389 |
threading.Thread(target=run_bot).start()
|