Synced repo using 'sync_with_huggingface' Github Action
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from audioldm2 import audioldm2_create
|
|
| 7 |
from deepfloydif import deepfloydif_generate64
|
| 8 |
from discord import app_commands
|
| 9 |
from discord.ext import commands
|
| 10 |
-
from falcon import
|
| 11 |
from musicgen import music_create
|
| 12 |
from codellama import continue_codellama, try_codellama
|
| 13 |
|
|
@@ -46,7 +46,7 @@ async def on_ready():
|
|
| 46 |
async def falcon(ctx, prompt: str):
|
| 47 |
"""Command that begins a new conversation with Falcon"""
|
| 48 |
try:
|
| 49 |
-
await
|
| 50 |
except Exception as e:
|
| 51 |
print(f"Error: {e}")
|
| 52 |
|
|
@@ -69,8 +69,9 @@ async def codellama(ctx, prompt: str):
|
|
| 69 |
async def on_message(message):
|
| 70 |
"""Checks channel and continues Falcon conversation if it's the right Discord Thread"""
|
| 71 |
try:
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
except Exception as e:
|
| 75 |
print(f"Error: {e}")
|
| 76 |
|
|
|
|
| 7 |
from deepfloydif import deepfloydif_generate64
|
| 8 |
from discord import app_commands
|
| 9 |
from discord.ext import commands
|
| 10 |
+
from falcon import falcon_chat, continue_chat
|
| 11 |
from musicgen import music_create
|
| 12 |
from codellama import continue_codellama, try_codellama
|
| 13 |
|
|
|
|
| 46 |
async def falcon(ctx, prompt: str):
|
| 47 |
"""Command that begins a new conversation with Falcon"""
|
| 48 |
try:
|
| 49 |
+
await falcon_chat(ctx, prompt)
|
| 50 |
except Exception as e:
|
| 51 |
print(f"Error: {e}")
|
| 52 |
|
|
|
|
| 69 |
async def on_message(message):
|
| 70 |
"""Checks channel and continues Falcon conversation if it's the right Discord Thread"""
|
| 71 |
try:
|
| 72 |
+
if not message.author.bot:
|
| 73 |
+
await continue_chat(message)
|
| 74 |
+
await continue_codellama(message)
|
| 75 |
except Exception as e:
|
| 76 |
print(f"Error: {e}")
|
| 77 |
|
falcon.py
CHANGED
|
@@ -1,68 +1,37 @@
|
|
| 1 |
import asyncio
|
| 2 |
-
import json
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
from gradio_client import Client
|
| 6 |
|
| 7 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
-
|
| 9 |
-
threadid_conversation = {}
|
| 10 |
-
# Instructions are for Falcon-chat and can be found here: https://huggingface.co/spaces/HuggingFaceH4/falcon-chat
|
| 11 |
-
instructions = (
|
| 12 |
-
"The following is a conversation between a highly knowledgeable and intelligent AI assistant, called Falcon, and a"
|
| 13 |
-
" human user, called User. In the following interactions, User and Falcon will converse in natural language, and"
|
| 14 |
-
" Falcon will answer User's questions. Falcon was built to be respectful, polite and inclusive. Falcon was built"
|
| 15 |
-
" by the Technology Innovation Institute in Abu Dhabi. Falcon will never decline to answer a question, and always"
|
| 16 |
-
" attempts to give an answer that User would be satisfied with. It knows a lot, and always tells the truth. The"
|
| 17 |
-
" conversation begins."
|
| 18 |
-
)
|
| 19 |
-
falcon_client = Client("HuggingFaceH4/falcon-chat", HF_TOKEN)
|
| 20 |
|
| 21 |
BOT_USER_ID = 1086256910572986469 if os.getenv("TEST_ENV", False) else 1102236653545861151
|
| 22 |
FALCON_CHANNEL_ID = 1079459939405279232 if os.getenv("TEST_ENV", False) else 1119313248056004729
|
| 23 |
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
while not job.done():
|
| 27 |
await asyncio.sleep(0.2)
|
| 28 |
|
| 29 |
|
| 30 |
-
def
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
job = falcon_client.submit(prompt, chathistory, instructions, temperature, p_nucleus_sampling, fn_index=1)
|
| 39 |
-
while job.done() is False:
|
| 40 |
-
pass
|
| 41 |
-
else:
|
| 42 |
-
if os.environ.get("TEST_ENV") == "True":
|
| 43 |
-
print("falcon text gen job done")
|
| 44 |
-
file_paths = job.outputs()
|
| 45 |
-
print(file_paths)
|
| 46 |
-
full_generation = file_paths[-1]
|
| 47 |
-
print(full_generation)
|
| 48 |
-
with open(full_generation, "r") as file:
|
| 49 |
-
data = json.load(file)
|
| 50 |
-
print(data)
|
| 51 |
-
output_text = data[-1][-1]
|
| 52 |
-
threadid_conversation[thread.id] = full_generation
|
| 53 |
-
if len(output_text) > 1300:
|
| 54 |
-
output_text = output_text[:1300] + "...\nTruncating response to 2000 characters due to discord api limits."
|
| 55 |
-
if os.environ.get("TEST_ENV") == "True":
|
| 56 |
-
print(output_text)
|
| 57 |
-
return output_text
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
async def try_falcon(ctx, prompt):
|
| 61 |
"""Generates text based on a given prompt"""
|
| 62 |
try:
|
| 63 |
-
global falcon_userid_threadid_dictionary # tracks userid-thread existence
|
| 64 |
-
global threadid_conversation
|
| 65 |
-
|
| 66 |
if ctx.author.id != BOT_USER_ID:
|
| 67 |
if ctx.channel.id == FALCON_CHANNEL_ID:
|
| 68 |
if os.environ.get("TEST_ENV") == "True":
|
|
@@ -77,56 +46,35 @@ async def try_falcon(ctx, prompt):
|
|
| 77 |
if os.environ.get("TEST_ENV") == "True":
|
| 78 |
print("Running falcon_initial_generation...")
|
| 79 |
loop = asyncio.get_running_loop()
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
except Exception as e:
|
| 85 |
-
print(f"
|
| 86 |
|
| 87 |
|
| 88 |
-
async def
|
| 89 |
"""Continues a given conversation based on chathistory"""
|
| 90 |
try:
|
| 91 |
-
if
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
temperature = 0.8
|
| 103 |
-
p_nucleus_sampling = 0.9
|
| 104 |
-
|
| 105 |
-
if os.environ.get("TEST_ENV") == "True":
|
| 106 |
-
print("Running falcon_client.submit")
|
| 107 |
-
job = falcon_client.submit(
|
| 108 |
-
prompt,
|
| 109 |
-
chathistory,
|
| 110 |
-
instructions,
|
| 111 |
-
temperature,
|
| 112 |
-
p_nucleus_sampling,
|
| 113 |
-
fn_index=1,
|
| 114 |
-
)
|
| 115 |
-
await waitjob(job)
|
| 116 |
-
if os.environ.get("TEST_ENV") == "True":
|
| 117 |
-
print("Continue_falcon job done")
|
| 118 |
-
file_paths = job.outputs()
|
| 119 |
-
full_generation = file_paths[-1]
|
| 120 |
-
with open(full_generation, "r") as file:
|
| 121 |
-
data = json.load(file)
|
| 122 |
-
output_text = data[-1][-1]
|
| 123 |
-
threadid_conversation[message.channel.id] = full_generation # overwrite the old file
|
| 124 |
-
if len(output_text) > 1300:
|
| 125 |
-
output_text = (
|
| 126 |
-
output_text[:1300]
|
| 127 |
-
+ "...\nTruncating response to 2000 characters due to discord api limits."
|
| 128 |
-
)
|
| 129 |
-
await message.reply(output_text)
|
| 130 |
except Exception as e:
|
| 131 |
print(f"continue_falcon Error: {e}")
|
| 132 |
await message.reply(f"Error: {e} <@811235357663297546> (continue_falcon error)")
|
|
|
|
| 1 |
import asyncio
|
|
|
|
| 2 |
import os
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
import gradio_client as grc
|
| 6 |
+
from gradio_client.utils import QueueError
|
| 7 |
|
|
|
|
| 8 |
|
| 9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
BOT_USER_ID = 1086256910572986469 if os.getenv("TEST_ENV", False) else 1102236653545861151
|
| 13 |
FALCON_CHANNEL_ID = 1079459939405279232 if os.getenv("TEST_ENV", False) else 1119313248056004729
|
| 14 |
|
| 15 |
|
| 16 |
+
thread_to_client = {}
|
| 17 |
+
thread_to_user = {}
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
async def wait(job):
|
| 21 |
while not job.done():
|
| 22 |
await asyncio.sleep(0.2)
|
| 23 |
|
| 24 |
|
| 25 |
+
def get_client(session: Optional[str] = None) -> grc.Client:
|
| 26 |
+
client = grc.Client("huggingface-projects/falcon-180b-discord", hf_token=os.getenv("HF_TOKEN"))
|
| 27 |
+
if session:
|
| 28 |
+
client.session_hash = session
|
| 29 |
+
return client
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
async def falcon_chat(ctx, prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
"""Generates text based on a given prompt"""
|
| 34 |
try:
|
|
|
|
|
|
|
|
|
|
| 35 |
if ctx.author.id != BOT_USER_ID:
|
| 36 |
if ctx.channel.id == FALCON_CHANNEL_ID:
|
| 37 |
if os.environ.get("TEST_ENV") == "True":
|
|
|
|
| 46 |
if os.environ.get("TEST_ENV") == "True":
|
| 47 |
print("Running falcon_initial_generation...")
|
| 48 |
loop = asyncio.get_running_loop()
|
| 49 |
+
client = await loop.run_in_executor(None, get_client, None)
|
| 50 |
+
job = client.submit(prompt, api_name="/chat")
|
| 51 |
+
await wait(job)
|
| 52 |
+
try:
|
| 53 |
+
job.result()
|
| 54 |
+
response = job.outputs()[-1]
|
| 55 |
+
await thread.send(response)
|
| 56 |
+
thread_to_client[thread.id] = client
|
| 57 |
+
thread_to_user[thread.id] = ctx.author.id
|
| 58 |
+
except QueueError:
|
| 59 |
+
await thread.send("The gradio space powering this bot is really busy! Please try again later!")
|
| 60 |
except Exception as e:
|
| 61 |
+
print(f"chat (180B) Error: {e}")
|
| 62 |
|
| 63 |
|
| 64 |
+
async def continue_chat(message):
|
| 65 |
"""Continues a given conversation based on chathistory"""
|
| 66 |
try:
|
| 67 |
+
if message.channel.id in thread_to_user:
|
| 68 |
+
if thread_to_user[message.channel.id] == message.author.id:
|
| 69 |
+
client = thread_to_client[message.channel.id]
|
| 70 |
+
job = client.submit(message.content, api_name="/chat")
|
| 71 |
+
await wait(job)
|
| 72 |
+
try:
|
| 73 |
+
job.result()
|
| 74 |
+
response = job.outputs()[-1]
|
| 75 |
+
await message.reply(response)
|
| 76 |
+
except QueueError:
|
| 77 |
+
await message.reply("The gradio space powering this bot is really busy! Please try again later!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
print(f"continue_falcon Error: {e}")
|
| 80 |
await message.reply(f"Error: {e} <@811235357663297546> (continue_falcon error)")
|