Spaces:
Paused
Paused
print("Started.") | |
import json | |
import os | |
import re | |
import asyncio | |
import random | |
import datetime | |
from threading import Thread | |
import discord | |
from discord.ext import tasks | |
from PIL import Image | |
from colorama import Fore, Style | |
import streamlit as st | |
from huggingface_hub import InferenceClient, login | |
try: | |
os.mkdir("data") | |
finally: | |
pass | |
HF_TOKEN = os.environ["HF_TOKEN"] | |
login(HF_TOKEN) | |
SD = InferenceClient(model="stabilityai/stable-diffusion-2-1") | |
LLM = InferenceClient(model="openchat/openchat-3.5-0106") | |
def ec(x, fd="<image>", sd="</image>"): | |
matches = re.findall(re.escape(fd) + "(.*?)" + re.escape(sd), x) | |
matches = matches if matches else [""] | |
return matches | |
intents = discord.Intents.default() | |
intents.message_content = True | |
client = discord.Client(intents=intents) | |
launch_time = datetime.datetime.utcnow() | |
ph = st.empty() | |
async def syncMessages(): | |
with ph.container(): | |
dirs = st.tabs(os.listdir("data")) | |
i = -1 | |
for dir in os.listdir("data"): | |
i += 1 | |
with dirs[i]: | |
files = st.tabs(os.listdir("data/" + dir)) | |
k = -1 | |
for file in os.listdir("data/" + dir): | |
k += 1 | |
with files[k]: | |
with open(f"data/{dir}/{file}", "r") as f: | |
o = f.read().split("<|end_of_turn|>") | |
for item in o: | |
o = o.split(": ", 1) | |
st.markdown(f":blue[{o[0]}]: {o[1]}") | |
async def on_ready(): | |
print(f"Logged in as {client.user}") | |
async def presence(): | |
delta_uptime = datetime.datetime.utcnow() - launch_time | |
hours, remainder = divmod(int(delta_uptime.total_seconds()), 3600) | |
minutes, seconds = divmod(remainder, 60) | |
days, hours = divmod(hours, 24) | |
print(f"Online Time: {days:02d}d | {hours:02d}h | {minutes:02d}m | {seconds:02d}s") | |
await client.change_presence( | |
status=discord.Status.dnd, | |
activity=discord.Activity( | |
type=discord.ActivityType.playing, | |
large_image="https://i.imgur.com/Kk2BvJg.jpg", | |
large_text="This is Game Icon", | |
name="Escaping the IRS.", | |
details="", | |
state=f"Running for {days:02d}d | {hours:02d}h | {minutes:02d}m", | |
), | |
) | |
await syncMessages() | |
async def on_message(message): | |
for user in message.mentions: | |
message.content = message.content.replace(f"<@{user.id}>", | |
f"@{str(user)}") | |
try: | |
msgchannel = message.channel | |
try: | |
msgchannel_name = msgchannel.name | |
guild = message.guild | |
guild_name = guild.name | |
except: | |
guild_name = "Direct" | |
msgchannel_name = message.author.display_name | |
s = f":green[{message.author}]: :violet[{message.content}] :blue[{msgchannel_name}] :orange[{guild_name}]" | |
if message.author == client.user: | |
return | |
sysp = """You are Lyre, a helpful discord chatbot. Respond in markdown. Your username is lyre#9828.""" | |
try: | |
os.mkdir("data/" + guild_name) | |
with open(f"{guild_name}/{msgchannel_name}", "w") as f: | |
f.write( | |
f"GPT4 Correct system: {sysp}\nGPT4 Correct {message.author}: {message.content}" | |
) | |
except: | |
f = open(f"data/{guild_name}/{msgchannel_name}", "w") | |
f.close() | |
with open(f"data/{guild_name}/{msgchannel_name}", "a") as f: | |
n = "\n" | |
f.write( | |
f"""GPT4 Correct {message.author.name}: {message.content.strip(n)}""" | |
) | |
finally: | |
with open(f"data/{guild_name}/{msgchannel_name}", "r") as f: | |
context = f.read() | |
context += f"GPT4 Correct Assistant: {message.content}\n" | |
title = ec(context, "<title>", "</title>")[0] | |
output = LLM.text_generation(context, | |
stop_sequences=[""], | |
max_new_tokens=4096) | |
with open(f"{guild_name}/{msgchannel_name}", "a") as f: | |
f.write(f"GPT4 Correct Assistant: {output}") | |
words = output.split() | |
embed = discord.Embed(title=title, | |
description=" ".join(words[:i * 10]).replace( | |
f"<title>{title}</title>", ""), | |
color=0x1E81B0) | |
embed.set_footer( | |
text= | |
"""Information or code generated by Lyre may not always be correct. Lyre was made by Araeyn.""" | |
) | |
e = await message.reply(embed=embed) | |
except Exception as e: | |
print(Fore.RED) | |
print(e) | |
print(Style.RESET_ALL) | |
token = os.environ["TOKEN"] | |
client.run(token) | |