lyre / app.py
Araeynn's picture
Update app.py
d14d99d verified
raw history blame
No virus
21.7 kB
import json
import os
import re
import asyncio
import random
import datetime
from threading import Thread
import discord
import time
import discord.utils
from discord.ext import tasks
from PIL import Image
import streamlit as st
from huggingface_hub import AsyncInferenceClient, login
import time
import os
from gradio_client import Client
async def C(x):
c = Client(src="ybelkada/llava-1-5-dlai")
print(c.view_api(return_format="dict"))
result = c.predict(
"Make a detailed description of the image. This would be sufficient for an LLM to answer questions about it with just your answer.",
"ip.png",
api_name="/predict"
)
launch_time = datetime.datetime.utcnow()
ph = st.empty()
def syncMessages():
with ph.container():
st.link_button(label="Invite the Bot", url="https://discord.com/api/oauth2/authorize?client_id=1116821362695221349&permissions=1067299753024&scope=bot", type="primary")
st.markdown(f"`Status:` :green[**Running**]")
if os.listdir("data") == []:
return 0
dirs = st.tabs(os.listdir("data"))
i = -1
for dir in os.listdir("data"):
i += 1
with dirs[i]:
if os.listdir(f"data/{dir}") == []:
return 0
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:
if item == "":
continue
item = item.split(": ", 1)
st.markdown(f":blue[{item[0].split('GPT4 Correct ')[1]}]: {item[1]}")
lock_file_path = "test.txt"
if not os.path.exists(lock_file_path):
with open(lock_file_path, "w") as f:
f.write("running")
clone = False
st.markdown("Bot is running, reload the page to see activity.")
else:
print("Streamlit app is already running, only streaming activity.")
clone = True
while True:
syncMessages()
time.sleep(0.5)
exit()
try:
os.mkdir("data")
except:
pass
HF_TOKEN = os.environ["HF_TOKEN"]
login(HF_TOKEN)
sd_turbo = "stabilityai/sd-turbo"
sdxl_turbo = "stabilityai/sd-turbo"
sdxl = "stabilityai/stable-diffusion-xl-base-1.0"
proteus = "dataautogpt3/ProteusV0.2"
sd_2_1 = "stabilityai/stable-diffusion-2-1"
open_journey = "prompthero/openjourney-v4"
SD = AsyncInferenceClient(model=sd_2_1)
SDXL = AsyncInferenceClient(model=sdxl)
SDXLT = AsyncInferenceClient(model=sdxl_turbo)
SDT = AsyncInferenceClient(model=sd_turbo)
PT = AsyncInferenceClient(model=proteus)
LLM = AsyncInferenceClient(model="openchat/openchat-3.5-0106")
RF = AsyncInferenceClient(model="stabilityai/stable-diffusion-xl-refiner-1.0")
UP = AsyncInferenceClient(model="radames/stable-diffusion-x4-upscaler-img2img")
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)
@client.event
async def on_ready():
if clone:
print(f"Clone started.")
syncMessages()
else:
print(f"Logged in as {client.user}")
presence.start()
@client.event
async def on_guild_join(guild):
await guild.system_channel.send("Hi! I'm Lyre! Use the `--help` command for instructions on setup!")
@tasks.loop(seconds=60)
async def presence():
if not clone:
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.idle,
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",
),
)
@client.event
async def on_disconnect():
print("Disconnected, Clone status:", clone)
setupCommand = """
## Command: `--setup`
The `--setup` command enables Lyre to communicate within a designated channel.
## Usage:
```css
--setup [channel]
```
## Parameters
- **[channel]**: Specify the target channel where you want the bot to be active. This is the channel name.
## Example
```arduino
--setup <#1234567890123456789>
```
or
```arduino
--setup #general
```
"""
revokeCommand = """
## Command: `--revoke`
The `--revoke` command prevents Lyre from communicating within a designated channel.
## Usage:
```css
--setup [channel]
```
## Parameters
- **[channel]**: Specify the target channel where you want the bot to be active. This is the channel name.
## Example
```arduino
--setup <#1234567890123456789>
```
or
```arduino
--setup #general
```"""
@client.event
async def on_message(message):
msgchannel = message.channel
if message.embeds != []:
x = ""
for field in message.embeds[0].fields:
x += f"{field.name} - {field.value}"
x += "\n"
message.content = f"""<|title|>{message.embeds[0].title}<|title|>\n{message.embeds[0].description}\n\n{x}"""
try:
msgchannel_name = msgchannel.name
guild = message.guild
guild_name = guild.name
except:
guild_name = "Direct"
msgchannel_name = str(message.author)
with open(f"{guild_name}.guild", "a") as f:
f.write("")
if message.content.startswith("--setup"):
args = message.content.split()
del args[0]
if len(args) == 0:
with open(f"{guild_name}.guild", "a") as f:
f.write(f"{msgchannel.id}\n")
embed = discord.Embed(title="Success!", description=f"You can now chat with the bot in <#{msgchannel.id}>")
await message.reply(embed=embed)
return 0
if args[0] == "":
embed = discord.Embed(title="Error", description="Parameter `[channel]` empty. Use `--help` for command help.")
await message.reply(embed=embed)
return 0
if args[0].startswith("<#") and args[0].endswith(">"):
try:
cid = int(args[0][2:-1])
except:
embed = discord.Embed(title="Error", description="Parameter `[channel]` invalid. Use `--help` for command help.")
await message.reply(embed=embed)
return 0
c = client.get_channel(cid)
if c in message.guild.text_channels:
with open(f"{guild_name}.guild", "a") as f:
f.write(f"{c.id}\n")
embed = discord.Embed(title="Success!", description=f"You can now chat with the bot in <#{cid}>")
await message.reply(embed=embed)
return 0
else:
embed = discord.Embed(title="Error", description="Parameter `[channel]` is not in this guild. Use `--help` for command help.")
await message.reply(embed=embed)
return 0
else:
embed = discord.Embed(title="Error", description="Parameter `[channel]` is not in valid forms. Use `--help` for command help.")
await message.reply(embed=embed)
return 0
if message.content.startswith("--revoke"):
args = message.content.split()
del args[0]
if len(args) == 0:
with open(f"{guild_name}.guild", "r") as f:
k = f.read().split("\n")
cid = message.channel.id
if str(cid) in k:
k.remove(str(cid))
with open(f"{guild_name}.guild", "w") as f:
f.write("\n".join(k))
embed = discord.Embed(title="Success", description=f"Lyre is now unable to chat in <#{msgchannel.id}>")
await message.reply(embed=embed)
return 0
else:
embed = discord.Embed(title="Error", description=f"Lyre was already unable to chat in <#{msgchannel.id}>")
await message.reply(embed=embed)
return 0
if args[0] == "":
embed = discord.Embed(title="Error", description="Parameter `[channel]` empty. Use `--help` for command help.")
await message.reply(embed=embed)
return 0
if args[0].startswith("<#") and args[0].endswith(">"):
try:
cid = int(args[0][2:-1])
except:
embed = discord.Embed(title="Error", description="Parameter `[channel]` invalid. Use `--help` for command help.")
await message.reply(embed=embed)
return 0
with open(f"{guild_name}.guild", "r") as f:
k = f.read().split("\n")
if str(cid) in k:
k.remove(str(cid))
with open(f"{guild_name}.guild", "w") as f:
f.write("\n".join(k))
embed = discord.Embed(title="Success", description=f"Lyre is now unable to chat in {args[0]}")
await message.reply(embed=embed)
return 0
else:
embed = discord.Embed(title="Error", description=f"Lyre was already unable to chat in {args[0]}")
await message.reply(embed=embed)
return 0
else:
embed = discord.Embed(title="Error", description="Parameter `[channel]` is not in valid forms, or does not exist. Use `--help` for command help.")
await message.reply(embed=embed)
return 0
if message.content.startswith("--reset"):
args = message.content.split()
del args[0]
if len(args) == 0:
try:
os.remove(f"data/{guild_name}/{msgchannel_name}")
embed = discord.Embed(title="Success", description=f"History reset in <#{msgchannel.id}>")
await message.reply(embed=embed)
return 0
except:
embed = discord.Embed(title="Error", description=f"No history in <#{msgchannel.id}>")
await message.reply(embed=embed)
return 0
if args[0] == "":
embed = discord.Embed(title="Error", description="Parameter `[channel]` empty. Use `--help` for command help.")
await message.reply(embed=embed)
return 0
if args[0].startswith("<#") and args[0].endswith(">"):
try:
cid = int(args[0][2:-1])
except:
embed = discord.Embed(title="Error", description="Parameter `[channel]` invalid. Use `--help` for command help.")
await message.reply(embed=embed)
return 0
try:
os.remove(f"data/{guild_name}/{msgchannel_name}")
embed = discord.Embed(title="Success", description=f"History rest in {args[0]}")
await message.reply(embed=embed)
return 0
except:
embed = discord.Embed(title="Error", description=f"No history in {args[0]}")
await message.reply(embed=embed)
return 0
else:
embed = discord.Embed(title="Error", description="Parameter `[channel]` is not in valid forms, or does not exist. Use `--help` for command help.")
await message.reply(embed=embed)
return 0
if message.content == "--help":
def check(reaction, user):
return reaction.message.id == msg.id and user == message.author
page = 0
titles = ["""--setup""", """--reset"""]
pages = [setupCommand, revokeCommand]
embed = discord.Embed(title=titles[page], description=pages[page])
embed.set_footer(text=f"Page {page + 1} of {len(pages)}")
msg = await message.reply(embed=embed)
await msg.add_reaction("◀️")
await msg.add_reaction("▶️")
while True:
try:
reaction, _ = await client.wait_for('reaction_add', timeout= 20.0, check=check)
if reaction.emoji == '◀️' and page > 0:
page -= 1
embed = discord.Embed(title=titles[page], description=pages[page])
embed.set_footer(text=f"Page {page + 1} of {len(pages)}")
await msg.edit(embed=embed)
if reaction.emoji == '▶️' and page < len(pages) -1:
page += 1
embed = discord.Embed(title=titles[page], description=pages[page])
embed.set_footer(text=f"Page {page + 1} of {len(pages)}")
await msg.edit(embed=embed)
except asyncio.TimeoutError:
await msg.remove_reaction("◀️")
await msg.remove_reaction("▶️")
return 0
for user in message.mentions:
if user.bot:
message.content = message.content.replace(f"<@{user.id}>",
f"<@{str(user)}[bot]>")
else:
message.content = message.content.replace(f"<@{user.id}>",
f"<@{str(user)}>")
try:
s = f":green[{message.author}]: :violet[{message.content}] :blue[{msgchannel_name}] :orange[{guild_name}]"
if message.author == client.user:
return
sysrp = f"""GPT4 Correct System:
You are lyre, a discord bot who can generate images. You were made by Araeyn.
Your username is lyre#9828.
You must use the markdown format for your responses.
Do not excessively use bullet points.
Use emojis at the start of your responses.
Use <|title|> at the start of your title for the response and <|title|> at the end of the title.
Your title is a conversation name that ChatGPT would make for the response and question.
Always include a title, both the start tag and the end tag.
If the user asks you to generate an image, use the start tag <|image|> and the end tag <|image|> around the prompt to generate it. Put this at the end of your response. Do not use a link for the image.
For example, if the user asks you to generate an image of a cat, you could say '<|title|>Cat Image<|title|>I hope you enjoy this image of a cat! If you have any other requests or questions, please don't hesitate to ask.<|image|>A cute cat with long fur that is looking out a window with curious eyes, volumetric lighting, 8k<|image|>'
Use relatively short prompts for images(20 words max), but still put details.
Do not generate images unless the user speciifes that they want an image.
If a user has [bot] next to their username, they are a bot.
If there is ImageParsed stuff at the end of the message, that is the text relative to the image."""
try:
os.mkdir("data/" + guild_name)
except:
pass
caption = ""
if message.reference is not None:
message.content = f"[Replied to: ({str(message.reference.cached_message.author)}: {message.reference.cached_message.content})]; {message.content}"
if len(message.attachments) > 0:
await message.attachments[0].save('ip.png')
caption = f"(ImageParsed:[{await C('ip.png')}])"
if os.path.exists(f"data/{guild_name}/{msgchannel_name}"):
with open(f"data/{guild_name}/{msgchannel_name}", "a") as f:
n = "\n"
if message.author.bot:
f.write(
f"""GPT4 Correct {message.author}[bot]: {message.content.strip(n)}{caption}<|end_of_turn|>"""
)
else:
f.write(
f"""GPT4 Correct {message.author}: {message.content.strip(n)}{caption}<|end_of_turn|>"""
)
else:
with open(f"data/{guild_name}/{msgchannel_name}", "w") as f:
if message.author.bot:
f.write(
f"GPT4 Correct system: {sysrp}<|end_of_turn|>GPT4 Correct {message.author}[bot]: {message.content}{caption}<|end_of_turn|>"
)
else:
f.write(
f"GPT4 Correct system: {sysrp}<|end_of_turn|>GPT4 Correct {message.author}: {message.content}{caption}<|end_of_turn|>"
)
with open(f"data/{guild_name}/{msgchannel_name}", "r") as f:
context = f.read()
with open(f"{guild_name}.guild", "r") as f:
o = f.read()
if str(message.channel.id) in o.split("\n") or guild_name == "Direct":
async with msgchannel.typing():
context += f"GPT4 Correct Assistant:"
load = random.choice(
[
"https://i.pinimg.com/originals/49/23/29/492329d446c422b0483677d0318ab4fa.gif"
]
)
output = await LLM.text_generation(context,
stop_sequences=["<|end_of_turn|>"],
max_new_tokens=2048)
title = ec(output, "<|title|>", "<|title|>")[0]
imgp = ec(output)[0]
with open(f"data/{guild_name}/{msgchannel_name}", "a") as f:
f.write(f"GPT4 Correct Assistant: {output}<|end_of_turn|>")
embed = discord.Embed(title=title,
description=output.replace(
f"<|title|>{title}<|title|>", "").replace(f"<|image|>{imgp}<|image|>", ""),
color=0x1E81B0)
if imgp != "":
embed.set_image(url=load)
embed.set_footer(
text=
"""Creating..."""
)
else:
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)
if imgp != "":
np = """lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature"""
st = time.time()
# image, m = (await SD.text_to_image(imgp, negative_prompt=np), "Stable Diffusion 2-1")
# image, m = (await SDXL.text_to_image(imgp, negative_prompt=np, num_inference_steps=8), "stable-diffusion-xl-base-1.0")
image, m = (await SDXLT.text_to_image(imgp, negative_prompt=np, num_inference_steps=1, guidance_scale=0.0), "sdxl-turbo")
# image, m = (await SDT.text_to_image(imgp, negative_prompt=np, num_inference_steps=1, guidance_scale=0.0), "sd-turbo")
# image, m = (await PT.text_to_image(imgp, negative_prompt=np), "Proteus v0.2")
image.save("image.png")
file = discord.File("image.png", filename="image.png", description=imgp)
embed.set_image(url="attachment://image.png")
embed.set_footer(
text=
"""Refining..."""
)
await e.edit(embed=embed, attachments=[file])
gt = time.time()
image, r = (await RF.image_to_image("image.png", num_inference_steps=1, prompt=imgp, negative_prompt=np), "stable-diffusion-xl-refiner-1.0")
rt = time.time()
embed.set_footer(
text=
f"Image generation model is {m}. Refiner model is {r}. Took {round((gt - st) * 10) / 10} seconds to generate. Took {round((rt - gt) * 10) / 10} seconds to refine."
)
image.save("image.png")
file = discord.File("image.png", filename="image.png")
embed.set_image(url="attachment://image.png")
await e.edit(embed=embed, attachments=[file])
except Exception as exc:
print(exc)
c = client.get_channel(1202160048126840882)
embed = discord.Embed(title="ERROR",
description=exc,
color=0xFF3348)
await c.send(embed=embed)
embed = discord.Embed(title="ERROR", color=0xFF3348)
await e.edit(embed=embed, attachments=[])
token = os.environ["TOKEN"]
client.run(token)