File size: 27,584 Bytes
54389af f58bc10 54389af 3266be8 54389af f7d01ae 54389af 940edd3 a74043f 940edd3 54389af 8d13aff a1c5357 54389af 1a4a3a8 54389af 1a4a3a8 940edd3 54389af efd1fcc 7a977bf 49061dc 54389af 1a995d6 54389af f557fb9 1a995d6 f557fb9 1a995d6 f557fb9 54389af 6c72ff4 a7214ab 54389af 374981b 6c72ff4 940edd3 b6cce87 1a995d6 f557fb9 1a995d6 6c72ff4 54389af a7214ab 54389af b6cce87 14e1f11 b6cce87 14e1f11 b6cce87 1a995d6 f557fb9 1a995d6 b6cce87 a7214ab 8dea03e 54389af 8d13aff 54389af 940edd3 f58bc10 313dffd 6c72ff4 6d8cbbb b173336 6d8cbbb 940edd3 b173336 940edd3 8d13aff 313dffd 8d13aff 1a995d6 a5b1be0 8d13aff 9ef09ef 7b9201c 8d13aff 7b9201c 8d13aff 54389af 940edd3 54389af 6c72ff4 940edd3 54389af 940edd3 00920a1 940edd3 8d13aff 54389af 54c827e 313dffd 8e113ec fd6e72f a7a6a3d 8e113ec fd6e72f 54389af 313dffd 54389af 940edd3 6d8cbbb 49061dc 940edd3 8d13aff 54389af 8e113ec fd6e72f 54389af fd6e72f 54389af fd6e72f 54389af fd6e72f 54389af fd6e72f 54389af 8e113ec 3ad6f29 5c9ffae 8e113ec 54389af 5c9ffae fd6e72f 4a799bf fd6e72f 54389af 940edd3 6d8cbbb 49061dc 940edd3 54389af 8d13aff 7f192d1 54389af 8e113ec c36cd57 8e113ec 440b42e ac0690a 7f192d1 a5b1be0 7f192d1 5078b0f 7f192d1 6d8cbbb 49061dc 940edd3 7f192d1 54389af 58e68a4 49061dc 940edd3 54389af 1a995d6 54389af 7f192d1 1a995d6 2943c89 06c91bb 54389af 2943c89 6c72ff4 54389af 5078b0f 54389af 5078b0f 54389af 3ad6f29 54389af 3ad6f29 54389af 3ad6f29 54389af 3ad6f29 54389af 5078b0f 54389af 8d13aff 5078b0f 54ac175 54389af 940edd3 54389af 4eeea0f eab20c1 4eeea0f 9841fc8 458a3b3 946a765 458a3b3 f7d01ae eab20c1 f7d01ae eab20c1 ff02996 6c72ff4 eab20c1 f7d01ae eab20c1 e2040bb f7d01ae eab20c1 f7d01ae eab20c1 f7d01ae eab20c1 0f184ab 6c72ff4 0f184ab f7d01ae eab20c1 d8c1418 eab20c1 f7d01ae ad5f1ed 72e3d96 458a3b3 3e91574 e694d22 8c5f58c eab20c1 6c72ff4 eab20c1 940edd3 946a765 54389af 8ec4d2d ec6916e d57542d a7a6a3d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
import discord
import os
import threading
import gradio as gr
import requests
import json
import random
import time
import re
from discord import Embed, Color
from discord.ext import commands
# aa
from gradio_client import Client
from PIL import Image
#from ratelimiter import RateLimiter
import asyncio
import concurrent.futures
import multiprocessing
import shutil # for doing image movement magic
#import tempfile
#import glob
import uuid
import glob
#todo
# experiment with animeGANv2
#✅ tasks for concurrent coroutines (now jobs)
# ratelimits
# enlarge each of 4 images?
# Error: [Errno 104] Connection reset by peer?
# clean up old threads
# safety for on_reaction_add?
# could use one channel, use threads to organize it. Otherwise may be too split and harder to keep track of
# lock generation after ~120s, can change
# restructure using slash commands? generate -> deepfloydif -> prompt -> thread -> combined -> upscale -> thread
GRADIOTEST_TOKEN = os.getenv('HF_TOKEN')
DISCORD_TOKEN = os.environ.get("GRADIOTEST_TOKEN", None)
df = Client("huggingface-projects/IF", GRADIOTEST_TOKEN)
jojogan = Client("akhaliq/JoJoGAN", GRADIOTEST_TOKEN)
falconclient = Client("HuggingFaceH4/falcon-chat", GRADIOTEST_TOKEN)
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
#---------------------------------------------------------------------------------------------------------------------------------------------
@bot.event
async def on_ready():
print('Logged on as', bot.user)
bot.log_channel = bot.get_channel(1100458786826747945) # 1100458786826747945 = bot-test, 1107006391547342910 = lunarbot server
#---------------------------------------------------------------------------------------------------------------------------------------------
async def safetychecks(ctx):
failure_emoji = '<:disagree:1098628957521313892>'
try:
if ctx.author.bot:
print(f"Error: The bot is not allowed to use its own commands.")
await ctx.message.add_reaction(failure_emoji)
return False
#✅✅ check if the bot is offline
offline_bot_role_id = 1103676632667017266
bot_member = ctx.guild.get_member(bot.user.id)
if any(role.id == offline_bot_role_id for role in bot_member.roles):
print(f"Error: {ctx.author} The bot is offline or under maintenance. (Remove the offline-bot role to bring it online)")
thread = await ctx.message.create_thread(name=f'Offline Error')
await thread.send(f"Error: {ctx.author.mention} The bot is offline or under maintenance. (Remove the offline-bot role to bring it online)")
await ctx.message.add_reaction(failure_emoji)
return False
'''
# review this, may be able to remove
#✅✅ check if the command is in the allowed channel(s)
bot_test = 1100458786826747945
deepfloydif_channel = 1119313215675973714
falcon_channel = 1119313248056004729
channel_ids = [bot_test, deepfloydif_channel, jojo_channel, spidey_channel, sketch_channel, falcon_channel]
if ctx.channel.id not in channel_ids:
print(f"{ctx.author}, commands are not permitted in {ctx.channel}")
thread = await ctx.message.create_thread(name=f'Channel Error')
await thread.send(f"Error: {ctx.author.mention} commands are not permitted in {ctx.channel}")
await ctx.message.add_reaction(failure_emoji)
return False
'''
'''
#✅✅ check if the user has the required role(s)
guild_id = 879548962464493619
verified_role_id = 900063512829755413 # @verified = 900063512829755413, HF = 897376942817419265, fellows = 963431900825919498
huggingfolks_role_id = 897376942817419265
fellows_role_id = 963431900825919498
contentcreator_role_id = 928589475968323636
betatester_role_id = 1113511652990668893
allowed_role_ids = [huggingfolks_role_id, fellows_role_id, contentcreator_role_id, betatester_role_id]
guild = bot.get_guild(guild_id)
user_roles = ctx.author.roles
has_allowed_role = any(role.id in allowed_role_ids for role in user_roles)
if not has_allowed_role:
print(f"Error: {ctx.author} does not have any of the required roles to use that command.")
thread = await ctx.message.create_thread(name=f'Perms Error')
await thread.send(f"Error: {ctx.author.mention} does not have any of the required roles to use that command.")
await ctx.message.add_reaction(failure_emoji)
return False
'''
return True
# ping lunarflu if any safety check ever fails
except Exception as e:
print(f"Error: safetychecks failed somewhere, command will not continue, {e}")
await ctx.message.reply(f"❌ <@811235357663297546> SC failed somewhere ❌ {e}") # this will always ping, as long as the bot has access to the channel
await ctx.message.add_reaction(failure_emoji)
#----------------------------------------------------------------------------------------------------------------------------------------------
async def fullqueue(e, thread): # can improve this using jobs from gradio
error_message = str(e)
if "Error: Expecting value: line 1 column 1 (char 0)" in error_message:
await thread.send("Queue is full! Please try again.")
elif "Error: Queue is full! Please try again." in error_message:
await thread.send("Queue is full! Please try again.")
# raised when the space we depend on needs to build after some inactivity (~10h)
elif "local variable 'stage_1_results' referenced before assignment" in error_message:
await thread.send("Space is building! Please try again after a few minutes.")
#----------------------------------------------------------------------------------------------------------------------------------------------
# deepfloydif stage 1 generation ✅
def inference(prompt):
negative_prompt = ''
seed = random.randint(0, 1000)
#seed = 1
number_of_images = 4
guidance_scale = 7
custom_timesteps_1 = 'smart50'
number_of_inference_steps = 50
stage_1_results, stage_1_param_path, stage_1_result_path = df.predict(
prompt, negative_prompt, seed, number_of_images, guidance_scale, custom_timesteps_1, number_of_inference_steps, api_name='/generate64')
return [stage_1_results, stage_1_param_path, stage_1_result_path]
#----------------------------------------------------------------------------------------------------------------------------------------------
# deepfloydif stage 2 upscaling ✅
def inference2(index, stage_1_result_path):
selected_index_for_stage_2 = index
seed_2 = 0
guidance_scale_2 = 4
custom_timesteps_2 = 'smart50'
number_of_inference_steps_2 = 50
result_path = df.predict(stage_1_result_path, selected_index_for_stage_2, seed_2,
guidance_scale_2, custom_timesteps_2, number_of_inference_steps_2, api_name='/upscale256')
return result_path
#----------------------------------------------------------------------------------------------------------------------------------------------
# ✅
async def react1234(reaction_emojis, combined_image_dfif):
for emoji in reaction_emojis:
await combined_image_dfif.add_reaction(emoji)
#----------------------------------------------------------------------------------------------------------------------------------------------
# Stage 1 ✅
@bot.command()
async def deepfloydif(ctx, *, prompt: str):
thread = None
try:
try:
if await safetychecks(ctx): #✅
if ctx.channel.id == 1119313215675973714:
await ctx.message.add_reaction('<a:loading:1114111677990981692>')
dfif_command_message_id = ctx.message.id # we will use this in some magic later on
thread = await ctx.message.create_thread(name=f'DeepfloydIF | {prompt}', auto_archive_duration=60) # could also just use prompt, no deepfloydif
# create thread -> send new message inside thread + combined_image -> add reactions -> dfif2
#current_time = int(time.time())
#random.seed(current_time)
negative_prompt = ''
seed = random.randint(0, 1000)
#seed = 1
number_of_images = 4
guidance_scale = 7
custom_timesteps_1 = 'smart50'
number_of_inference_steps = 50
api_name = '/generate64'
await thread.send(f'{ctx.author.mention}Generating images in thread, can take ~1 minute...')
except Exception as e:
print(f"Error: {e}")
if thread is None:
thread = await ctx.message.create_thread(name=f'DFIF1 Error')
await thread.send(f"{ctx.author.mention} Error before stage 1 generation, {e}. If error code: 50035, upscale can still work.")
await fullqueue(e, thread)
await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
await ctx.message.add_reaction('<:disagree:1098628957521313892>')
await thread.edit(archived=True)
#generation✅-------------------------------------------------------
try:
#stage_1_results, stage_1_param_path, stage_1_result_path = df.predict(
# prompt, negative_prompt, seed, number_of_images, guidance_scale, custom_timesteps_1, number_of_inference_steps, api_name='/generate64')
# run blocking function in executor
#await thread.send(f'✅running blocking function in executor')
loop = asyncio.get_running_loop()
result = await loop.run_in_executor(None, inference, prompt)
#await thread.send(f'{ctx.author.mention}after executor')
#await thread.send(f'✅run_in_executor ran successfully')
stage_1_results = result[0]
stage_1_result_path = result[2]
partialpath = stage_1_result_path[5:] #magic for later
except Exception as e:
print(f"Error: {e}")
if thread is None:
thread = await ctx.message.create_thread(name=f'Generation Error')
await thread.send(f"{ctx.author.mention} Error during stage 1 generation, {e}")
await fullqueue(e, thread)
await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
await ctx.message.add_reaction('<:disagree:1098628957521313892>')
await thread.edit(archived=True)
#posting images✅----------------------------------------------------------------
try:
#await thread.send(f'✅combining images...')
#old, see: https://huggingface.co/spaces/DeepFloyd/IF/commit/fb79844b1d0b013a28ac435a36f804d8030fba50
#png_files = [f for f in os.listdir(stage_1_results) if f.endswith('.png')]
png_files = list(glob.glob(f"{stage_1_results}/**/*.png"))
if png_files:
first_png = png_files[0]
second_png = png_files[1]
third_png = png_files[2]
fourth_png = png_files[3]
first_png_path = os.path.join(stage_1_results, first_png)
second_png_path = os.path.join(stage_1_results, second_png)
third_png_path = os.path.join(stage_1_results, third_png)
fourth_png_path = os.path.join(stage_1_results, fourth_png)
img1 = Image.open(first_png_path)
img2 = Image.open(second_png_path)
img3 = Image.open(third_png_path)
img4 = Image.open(fourth_png_path)
combined_image = Image.new('RGB', (img1.width * 2, img1.height * 2))
combined_image.paste(img1, (0, 0))
combined_image.paste(img2, (img1.width, 0))
combined_image.paste(img3, (0, img1.height))
combined_image.paste(img4, (img1.width, img1.height))
combined_image_path = os.path.join(stage_1_results, f'{partialpath}{dfif_command_message_id}.png')
combined_image.save(combined_image_path)
with open(combined_image_path, 'rb') as f:
combined_image_dfif = await thread.send(f'{ctx.author.mention}React with the image number you want to upscale!', file=discord.File(
f, f'{partialpath}{dfif_command_message_id}.png')) # named something like: tmpgtv4qjix1111269940599738479.png
#await thread.send(f'✅reacting with 1234...')
emoji_list = ['↖️', '↗️', '↙️', '↘️']
await react1234(emoji_list, combined_image_dfif)
await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
await ctx.message.add_reaction('<:agree:1098629085955113011>')
''' individual images
if png_files:
for i, png_file in enumerate(png_files):
png_file_path = os.path.join(stage_1_results, png_file)
img = Image.open(png_file_path)
image_path = os.path.join(stage_1_results, f'{i+1}{partialpath}.png')
img.save(image_path)
with open(image_path, 'rb') as f:
await thread.send(f'{ctx.author.mention}Image {i+1}', file=discord.File(f, f'{i+1}{partialpath}.png'))
await asyncio.sleep(1)
'''
except Exception as e:
print(f"Error: {e}")
if thread is None:
thread = await ctx.message.create_thread(name=f'Posting Error')
await thread.send(f"{ctx.author.mention} Encountered error while posting combined image in thread, {e}")
await fullqueue(e, thread)
await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
await ctx.message.add_reaction('<:disagree:1098628957521313892>')
await thread.edit(archived=True)
#deepfloydif try/except
except Exception as e:
print(f"Error: {e}")
if thread is None:
thread = await ctx.message.create_thread(name=f'deepfloydif Error')
await thread.send(f"{ctx.author.mention} Overall error with deepfloydif, {e}")
await fullqueue(e, thread)
await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
await ctx.message.add_reaction('<:disagree:1098628957521313892>')
await thread.edit(archived=True)
#----------------------------------------------------------------------------------------------------------------------------
# Stage 2 ✅
async def dfif2(index: int, stage_1_result_path, thread, dfif_command_message_id): # add safetychecks
try:
parent_channel = thread.parent
dfif_command_message = await parent_channel.fetch_message(dfif_command_message_id)
await dfif_command_message.remove_reaction('<:agree:1098629085955113011>', bot.user)
await dfif_command_message.add_reaction('<a:loading:1114111677990981692>')
number = index + 1
if number == 1:
position = "top left"
elif number == 2:
position = "top right"
elif number == 3:
position = "bottom left"
elif number == 4:
position = "bottom right"
await thread.send(f"Upscaling the {position} image...")
# run blocking function in executor
loop = asyncio.get_running_loop()
result_path = await loop.run_in_executor(None, inference2, index, stage_1_result_path)
#await thread.send(f"✅upscale done")
with open(result_path, 'rb') as f:
await thread.send(f'Here is the upscaled image! :) ', file=discord.File(f, 'result.png'))
await dfif_command_message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
await dfif_command_message.add_reaction('<:agree:1098629085955113011>')
await thread.edit(archived=True)
except Exception as e:
print(f"Error: {e}")
parent_channel = thread.parent
dfif_command_message = await parent_channel.fetch_message(dfif_command_message_id)
await dfif_command_message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
await dfif_command_message.add_reaction('<:disagree:1098628957521313892>')
await thread.send(f"Error during stage 2 upscaling, {e}")
await fullqueue(e, thread)
await thread.edit(archived=True)
#----------------------------------------------------------------------------------------------------------------------------
# react detector for stage 2 ✅
@bot.event
async def on_reaction_add(reaction, user): # ctx = await bot.get_context(reaction.message)? could try later, might simplify
try:
#ctx = await bot.get_context(reaction.message)
# safety checks first ✅
if not user.bot:
thread = reaction.message.channel
threadparentid = thread.parent.id
if threadparentid == 1119313215675973714: # testing-the-bot, should be whatever the deepfloydif channel is
# 811235357663297546 = lunarflu
if reaction.message.attachments:
if user.id == reaction.message.mentions[0].id: # if user.id == reaction.message.mentions[0].id:
# magic begins
#await reaction.message.channel.send("✅reaction detected")
attachment = reaction.message.attachments[0]
image_name = attachment.filename # named something like: tmpgtv4qjix1111269940599738479.png
# remove .png first
partialpathmessageid = image_name[:-4] # should be tmpgtv4qjix1111269940599738479
# extract partialpath, messageid
partialpath = partialpathmessageid[:11] # tmpgtv4qjix
messageid = partialpathmessageid[11:] # 1111269940599738479
# add /tmp/ to partialpath, save as new variable
fullpath = "/tmp/" + partialpath # should be /tmp/tmpgtv4qjix
#await reaction.message.channel.send(f"✅fullpath extracted, {fullpath}")
emoji = reaction.emoji
if emoji == "↖️":
index = 0
elif emoji == "↗️":
index = 1
elif emoji == "↙️":
index = 2
elif emoji == "↘️":
index = 3
#await reaction.message.channel.send(f"✅index extracted, {index}")
index = index
stage_1_result_path = fullpath
thread = reaction.message.channel
dfif_command_message_id = messageid
ctx = await bot.get_context(reaction.message)
#await reaction.message.channel.send(f"✅calling dfif2")
await dfif2(index, stage_1_result_path, thread, dfif_command_message_id)
except Exception as e:
print(f"Error: {e} (known error, does not cause issues, fix later)")
#----------------------------------------------------------------------------------------------------------------------------
chathistory = None
falcon_users = []
#falcon_threads = []
falcon_dictionary = {}
falcon_userid_threadid_dictionary = {}
@bot.command()
async def falcon(ctx, *, prompt: str):
# todo: need to be careful with these, rework into something simpler
try:
global falcon_users
#global falcon_threads # deprecated
global falcon_dictionary
global falcon_userid_threadid_dictionary
# dict[generation, authorid]
# dict[threadid, authorid]
if not ctx.author.bot:
if ctx.channel.id == 1119313248056004729: # initial thread creation inside #falcon
#if ctx.author.id not in falcon_users: # deprecated
if ctx.author.id not in falcon_userid_threadid_dictionary:
await ctx.message.add_reaction('<a:loading:1114111677990981692>')
thread = await ctx.message.create_thread(name=f'{ctx.author}')
#falcon_users = [ctx.author.id] + falcon_users # deprecated
#falcon_threads = [thread.id] + falcon_threads # deprecated
await thread.send(f"[DISCLAIMER: HuggingBot is a **highly experimental** beta feature; The Falcon model and system prompt can be found here: https://huggingface.co/spaces/HuggingFaceH4/falcon-chat]")
# initial generation here
chathistory = falconclient.predict(
fn_index=5
) # []
instructions = "The following is a conversation between a highly knowledgeable and intelligent AI assistant, called Falcon, and a human user, called User. In the following interactions, User and Falcon will converse in natural language, and Falcon will answer User's questions. Falcon was built to be respectful, polite and inclusive. Falcon was built by the Technology Innovation Institute in Abu Dhabi. Falcon will never decline to answer a question, and always attempts to give an answer that User would be satisfied with. It knows a lot, and always tells the truth. The conversation begins."
job = falconclient.submit(prompt, chathistory, instructions, 0.8, 0.9, fn_index=1) # This is not blocking, similar to run_in_executor (but better)
while job.done() == False:
status = job.status() # could be spammy, let's test anyways
#print(status)
else:
file_paths = job.outputs()
full_generation = file_paths[-1] # tmp12345678.json
with open(full_generation, 'r') as file:
data = json.load(file)
output_text = data[-1][-1] # we output this as the bot
falcon_dictionary[ctx.author.id] = full_generation # 1234567890: tmp12345678.json
falcon_userid_threadid_dictionary[ctx.author.id] = thread.id
print(output_text)
await thread.send(f"{output_text}")
await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
#elif ctx.author.id in falcon_users: # deprecated
elif ctx.author.id in falcon_userid_threadid_dictionary:
# get the threadid, grab the last message in that thread, link to that message
thread_id = falcon_userid_threadid_dictionary[ctx.author.id]
thread_link = f"https://discord.com/channels/879548962464493619/1119313248056004729/{thread_id}"
await ctx.reply(f"{ctx.author.mention}, you already have an existing conversation here {thread_link}! Use !falconclear in the #falcon channel to start a new one.")
#------------------------------------
# post all other generations here
#if ctx.channel.id in falcon_threads: # deprecated
if ctx.channel.id in falcon_userid_threadid_dictionary.values():
if ctx.channel.id == falcon_userid_threadid_dictionary[ctx.author.id]:
await ctx.message.add_reaction('<a:loading:1114111677990981692>')
chathistory = falcon_dictionary[ctx.author.id]
instructions = "The following is a conversation between a highly knowledgeable and intelligent AI assistant, called Falcon, and a human user, called User. In the following interactions, User and Falcon will converse in natural language, and Falcon will answer User's questions. Falcon was built to be respectful, polite and inclusive. Falcon was built by the Technology Innovation Institute in Abu Dhabi. Falcon will never decline to answer a question, and always attempts to give an answer that User would be satisfied with. It knows a lot, and always tells the truth. The conversation begins."
job = falconclient.submit(prompt, chathistory, instructions, 0.8, 0.9, fn_index=1) # This is not blocking, similar to run_in_executor (but better)
while job.done() == False:
status = job.status() # could be spammy, let's test anyways
#print(status)
else:
file_paths = job.outputs()
full_generation = file_paths[-1] # tmp12345678.json
with open(full_generation, 'r') as file:
data = json.load(file)
output_text = data[-1][-1] # we output this as the bot
falcon_dictionary[ctx.author.id] = full_generation
print(output_text)
await ctx.reply(f"{output_text}")
await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
except Exception as e:
print(f"Error: {e}")
await ctx.reply(f"{e} cc <@811235357663297546> (falconprivate error)")
await ctx.message.remove_reaction('<a:loading:1114111677990981692>', bot.user)
await ctx.message.add_reaction('<:disagree:1098628957521313892>')
#----------------------------------------------------------------------------------------------------------------------------
@bot.command()
async def falconclear(ctx):
if not ctx.author.bot:
if ctx.channel.id == 1119313248056004729:
if ctx.author.id in falcon_userid_threadid_dictionary:
if ctx.author.id in falcon_dictionary:
del falcon_userid_threadid_dictionary[ctx.author.id]
del falcon_dictionary[ctx.author.id]
await ctx.reply(f"{ctx.author.mention}'s conversation has been cleared. Feel free to start a new one!")
#----------------------------------------------------------------------------------------------------------------------------
# hackerllama magic to run the bot in a Hugging Face Space
def run_bot():
bot.run(DISCORD_TOKEN)
threading.Thread(target=run_bot).start()
def greet(name):
return "Hello " + name + "!"
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
#demo.queue(concurrency_count=10)
demo.queue(concurrency_count=20)
demo.launch()
#----------------------------------------------------------------------------------------------------------------------------
|