FALCON.PY
Browse files
falcon.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import discord
|
| 2 |
+
from discord import app_commands
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from gradio_client import Client
|
| 5 |
+
import os
|
| 6 |
+
import threading
|
| 7 |
+
import json
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import asyncio
|
| 10 |
+
|
| 11 |
+
# HF GUILD SETTINGS
|
| 12 |
+
MY_GUILD_ID = 1077674588122648679 if os.getenv("TEST_ENV", False) else 879548962464493619
|
| 13 |
+
MY_GUILD = discord.Object(id=MY_GUILD_ID)
|
| 14 |
+
HF_TOKEN = os.getenv('HF_TOKEN')
|
| 15 |
+
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
| 16 |
+
|
| 17 |
+
falcon_userid_threadid_dictionary = {}
|
| 18 |
+
threadid_conversation = {}
|
| 19 |
+
instructions = "The following is a conversation between a highly knowledgeable " \
|
| 20 |
+
"and intelligent AI assistant, called Falcon, and a human user, called User. In the " \
|
| 21 |
+
"following interactions, User and Falcon will converse in natural language, and Falcon " \
|
| 22 |
+
"will answer User's questions. Falcon was built to be respectful, polite and inclusive. " \
|
| 23 |
+
"Falcon was built by the Technology Innovation Institute in Abu Dhabi. Falcon will never " \
|
| 24 |
+
"decline to answer a question, and always attempts to give an answer that User would be satisfied " \
|
| 25 |
+
"with. It knows a lot, and always tells the truth. The conversation begins."
|
| 26 |
+
threadid_conversation = {}
|
| 27 |
+
|
| 28 |
+
BOT_USER_ID = 1086256910572986469 # 1086256910572986469 = test
|
| 29 |
+
FALCON_CHANNEL_ID = 1079459939405279232 # 1079459939405279232 = test
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
async def try_falcon(interaction, prompt):
|
| 34 |
+
try:
|
| 35 |
+
global falcon_userid_threadid_dictionary # tracks userid-thread existence
|
| 36 |
+
global instructions
|
| 37 |
+
global threadid_conversation
|
| 38 |
+
global BOT_USER_ID
|
| 39 |
+
global FALCON_CHANNEL_ID
|
| 40 |
+
|
| 41 |
+
if interaction.user.id != BOT_USER_ID:
|
| 42 |
+
if interaction.channel.id == FALCON_CHANNEL_ID:
|
| 43 |
+
await interaction.response.send_message(f"Working on it!")
|
| 44 |
+
channel = interaction.channel
|
| 45 |
+
# 1
|
| 46 |
+
message = await channel.send(f"Creating thread...")
|
| 47 |
+
thread = await message.create_thread(name=f'{prompt}', auto_archive_duration=60) # interaction.user
|
| 48 |
+
await thread.send(f"[DISCLAIMER: HuggingBot is a **highly experimental** beta feature; The Falcon " \
|
| 49 |
+
f"model and system prompt can be found here: https://huggingface.co/spaces/HuggingFaceH4/falcon-chat]")
|
| 50 |
+
# generation
|
| 51 |
+
chathistory = falconclient.predict(
|
| 52 |
+
fn_index=5
|
| 53 |
+
) # []
|
| 54 |
+
job = falconclient.submit(prompt, chathistory, instructions, 0.8, 0.9, fn_index=1) # This is not blocking, similar to run_in_executor (but better)
|
| 55 |
+
while job.done() == False:
|
| 56 |
+
status = job.status()
|
| 57 |
+
#print(status)
|
| 58 |
+
else:
|
| 59 |
+
file_paths = job.outputs()
|
| 60 |
+
full_generation = file_paths[-1] # tmp12345678.json
|
| 61 |
+
with open(full_generation, 'r') as file:
|
| 62 |
+
data = json.load(file)
|
| 63 |
+
output_text = data[-1][-1] # we output this as the bot
|
| 64 |
+
|
| 65 |
+
threadid_conversation[thread.id] = full_generation
|
| 66 |
+
|
| 67 |
+
falcon_userid_threadid_dictionary[thread.id] = interaction.user.id
|
| 68 |
+
|
| 69 |
+
print(output_text)
|
| 70 |
+
await thread.send(f"{output_text}")
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
print(f"Error: {e}")
|
| 74 |
+
#await thread.send(f"{e} cc <@811235357663297546> (falconprivate error)")
|