Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -40,33 +40,38 @@ from colorama import Fore, Back, Style
|
|
40 |
|
41 |
import re
|
42 |
|
43 |
-
def extract_content(input_string, first_delimiter="<image>", second_delimiter="</image>"):
|
44 |
-
# Define the pattern to match text inside delimiters
|
45 |
-
pattern = re.escape(first_delimiter) + '(.*?)' + re.escape(second_delimiter)
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
51 |
|
52 |
-
|
|
|
|
|
|
|
53 |
|
54 |
-
|
55 |
|
56 |
|
57 |
-
|
|
|
|
|
|
|
58 |
def main():
|
59 |
-
|
60 |
|
61 |
|
62 |
def run():
|
63 |
-
|
64 |
-
|
|
|
65 |
|
66 |
|
67 |
def ka():
|
68 |
-
|
69 |
-
|
70 |
|
71 |
|
72 |
HF_TOKEN = os.environ["HF_TOKEN"]
|
@@ -83,25 +88,30 @@ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
|
83 |
# )
|
84 |
# pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
85 |
|
|
|
86 |
def generate_latex_image(latex_code):
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
90 |
|
91 |
imgmodel = "stabilityai/stable-diffusion-2-1"
|
92 |
IMG_URL = "https://api-inference.huggingface.co/models/" + imgmodel
|
93 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
94 |
|
|
|
95 |
def generate(prompt):
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
99 |
|
100 |
def query(x):
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
105 |
|
106 |
def caption(filename):
|
107 |
with open(filename, "rb") as f:
|
@@ -120,59 +130,66 @@ client = discord.Client(intents=intents)
|
|
120 |
|
121 |
@tasks.loop(minutes=1)
|
122 |
async def presence():
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
@client.event
|
137 |
async def on_ready():
|
138 |
-
|
139 |
-
|
140 |
|
141 |
|
142 |
@client.event
|
143 |
-
async def on_message(message:discord.Message):
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
try:
|
149 |
-
msgchannel_name = msgchannel.name
|
150 |
-
guild = message.guild
|
151 |
-
guild_name = guild.name
|
152 |
-
except:
|
153 |
-
guild_name = "Direct"
|
154 |
-
msgchannel_name = message.author.display_name
|
155 |
-
# await msgchannel.typing()
|
156 |
-
s = f"{Fore.LIGHTGREEN_EX}{message.author.display_name}#{message.author.discriminator}: {Fore.WHITE}{message.content} {Fore.LIGHTBLUE_EX} {msgchannel_name} {Fore.LIGHTMAGENTA_EX} {guild_name}"
|
157 |
-
print(Style.RESET_ALL)
|
158 |
-
print(s)
|
159 |
-
if message.author == client.user:
|
160 |
-
return
|
161 |
-
ats = []
|
162 |
-
if len(message.attachments) > 0:
|
163 |
-
for i in message.attachments:
|
164 |
-
if i.content_type == "image":
|
165 |
-
Image.open(io.BytesIO(i.read())).save("image.png")
|
166 |
-
ats.append(f"ImageInput:{caption("image.png")}")
|
167 |
-
s = f"GPT4 Correct {message.author}: {message.content} Attachments: {" ".join(ats)}<|end_of_turn|>"
|
168 |
try:
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
You must use the markdown format for your responses.
|
177 |
Do not excessively use bullet points.
|
178 |
Use emojis at the start of your responses.
|
@@ -183,101 +200,126 @@ async def on_message(message:discord.Message):
|
|
183 |
Always include a title.
|
184 |
Do not generate images unless the user speciifes that they want an image.
|
185 |
<|end_of_turn|>"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
)
|
187 |
-
|
188 |
-
|
189 |
-
async with msgchannel.typing():
|
190 |
-
with open(os.path.join(guild_name, f"{msgchannel_name}.txt"), "r") as f:
|
191 |
-
o = f.read()
|
192 |
-
title = ""
|
193 |
-
embed = discord.Embed(title=title, description= "...", color=0x1e81b0)
|
194 |
-
embed.set_footer(text="""Information or code generated by Lyre may not always be correct. Lyre was made by Araeyn.""")
|
195 |
-
e = await message.reply(embed=embed)
|
196 |
-
o += "GPT4 Correct Assistant: "
|
197 |
-
print("right before query step.")
|
198 |
-
y = query({"inputs": o})
|
199 |
-
print("Got past query step.")
|
200 |
-
print(y)
|
201 |
-
r = y[0]["generated_text"][len(o):]
|
202 |
-
title = extract_content(r, first_delimiter="<title>", second_delimiter="</title>")[0]
|
203 |
-
l = 0
|
204 |
-
r = ""
|
205 |
-
while not ("<|end_of_turn|>" in y[0]["generated_text"][
|
206 |
-
len(o):] or "GPT4 Correct" in y[0]["generated_text"][len(o):]):
|
207 |
-
l += 1
|
208 |
-
r = y[0]["generated_text"][len(o):]
|
209 |
-
title = extract_content(r, "<title>", "</title>")[0]
|
210 |
-
print(title)
|
211 |
-
r = r.replace("<title>" + title + "</title>", "")
|
212 |
-
print(r)
|
213 |
-
embed.title = title
|
214 |
-
embed.description = r
|
215 |
-
await e.edit(embed=embed)
|
216 |
-
py = y[0]["generated_text"]
|
217 |
-
y = query({"inputs": y[0]["generated_text"]})
|
218 |
-
if y[0]["generated_text"] == py:
|
219 |
-
break
|
220 |
-
r = r.split("<|end_of_turn|>")[0]
|
221 |
-
r = r.split("GPT4 Correct")[0]
|
222 |
-
embed.description = r
|
223 |
-
await e.edit(embed=embed)
|
224 |
-
with open(os.path.join(guild_name, f"{msgchannel_name}.txt"), "a") as f:
|
225 |
-
f.write("GPT4 Correct Assistant: ")
|
226 |
-
f.write(r)
|
227 |
-
f.write("<|end_of_turn|>")
|
228 |
-
if "<image>" in r and "</image>" in r:
|
229 |
-
i = extract_content(r)[0]
|
230 |
-
r = r.replace("<image>" + i + "</image>", "")
|
231 |
-
load = random.choice(["https://cdn.dribbble.com/users/744913/screenshots/4094897/media/771a495231b798c0ccf7a59a19f31946.gif", "https://cdn.dribbble.com/users/563824/screenshots/3633228/media/b620ccb3ae8c14ea5447d159ebb1da58.gif", "https://cdn.dribbble.com/users/563824/screenshots/4155980/media/d3828cd14ed415eb6f90310991e06f27.gif", "https://cdn.dribbble.com/users/107759/screenshots/3498589/media/5bc45101de34a80ea71238a02f3a75b5.gif"])
|
232 |
-
if r.replace("\n", "") != "":
|
233 |
-
embed.description = r
|
234 |
-
await e.edit(embed=embed)
|
235 |
-
embed.set_image(url=load)
|
236 |
-
await e.edit(embed=embed)
|
237 |
-
else:
|
238 |
-
embed.clear_fields()
|
239 |
-
embed.set_image(url=load)
|
240 |
-
await e.edit(embed=embed)
|
241 |
-
image_bytes = generate(i)
|
242 |
-
try: image = Image.open(io.BytesIO(image_bytes))
|
243 |
-
except: print(image_bytes)
|
244 |
-
image.save(f"latest.png")
|
245 |
-
# prompt = ""
|
246 |
-
# url = "latest.png"
|
247 |
-
# init_image = load_image(url).convert("RGB")
|
248 |
-
# image = pipe(prompt, image=init_image).images[0]
|
249 |
-
# print(image)
|
250 |
-
image.save("latest.png")
|
251 |
-
embed.set_image(url="attachment://latest.png")
|
252 |
-
await e.edit(embed=embed, attachments=[discord.File(fp=f"latest.png")])
|
253 |
-
os.system("rm " + f"latest.png")
|
254 |
-
else:
|
255 |
-
embed.description = r
|
256 |
-
await e.edit(embed=embed)
|
257 |
-
except Exception as e:
|
258 |
-
embed = discord.Embed(title="Error", description=f"```{traceback.format_exc()}```")
|
259 |
-
await message.channel.send(embed=embed)
|
260 |
|
261 |
ka()
|
|
|
|
|
262 |
def start():
|
263 |
import gradio as gr
|
|
|
264 |
def greet(name, intensity):
|
265 |
return "1 " * intensity + name + "!"
|
266 |
-
|
267 |
demo = gr.Interface(
|
268 |
fn=greet,
|
269 |
inputs=["text", "slider"],
|
270 |
outputs=["text"],
|
271 |
)
|
272 |
-
|
273 |
demo.launch()
|
|
|
|
|
274 |
token = os.environ["TOKEN"]
|
275 |
if token == "":
|
276 |
-
|
277 |
print("started gradio app")
|
278 |
Process(target=start).start()
|
279 |
|
|
|
280 |
def t():
|
281 |
client.run(token)
|
282 |
|
283 |
-
|
|
|
|
40 |
|
41 |
import re
|
42 |
|
|
|
|
|
|
|
43 |
|
44 |
+
def extract_content(
|
45 |
+
input_string, first_delimiter="<image>", second_delimiter="</image>"
|
46 |
+
):
|
47 |
+
# Define the pattern to match text inside delimiters
|
48 |
+
pattern = re.escape(first_delimiter) + "(.*?)" + re.escape(second_delimiter)
|
49 |
|
50 |
+
# Use re.findall to find all matches of the pattern in the input string
|
51 |
+
matches = re.findall(pattern, input_string)
|
52 |
+
if len(matches) == 0:
|
53 |
+
matches.append("")
|
54 |
|
55 |
+
return matches
|
56 |
|
57 |
|
58 |
+
app = Flask("")
|
59 |
+
|
60 |
+
|
61 |
+
@app.route("/")
|
62 |
def main():
|
63 |
+
return "Your bot is alive!"
|
64 |
|
65 |
|
66 |
def run():
|
67 |
+
from waitress import serve
|
68 |
+
|
69 |
+
serve(app, host="0.0.0.0", port=8080)
|
70 |
|
71 |
|
72 |
def ka():
|
73 |
+
server = Thread(target=run)
|
74 |
+
server.start()
|
75 |
|
76 |
|
77 |
HF_TOKEN = os.environ["HF_TOKEN"]
|
|
|
88 |
# )
|
89 |
# pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
90 |
|
91 |
+
|
92 |
def generate_latex_image(latex_code):
|
93 |
+
api_url = "https://latex.codecogs.com/svg.latex?"
|
94 |
+
Response = requests.get(api_url + latex_code)
|
95 |
+
return Response.content
|
96 |
+
|
97 |
|
98 |
imgmodel = "stabilityai/stable-diffusion-2-1"
|
99 |
IMG_URL = "https://api-inference.huggingface.co/models/" + imgmodel
|
100 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
101 |
|
102 |
+
|
103 |
def generate(prompt):
|
104 |
+
payload = {"inputs": prompt, "options": {"wait_for_model": True}}
|
105 |
+
Response = requests.post(IMG_URL, headers=headers, json=payload)
|
106 |
+
return Response.content
|
107 |
+
|
108 |
|
109 |
def query(x):
|
110 |
+
x["parameters"] = {}
|
111 |
+
x["options"] = {"wait_for_model": True}
|
112 |
+
Response = requests.post(API_URL, headers=headers, json=x)
|
113 |
+
return Response.json()
|
114 |
+
|
115 |
|
116 |
def caption(filename):
|
117 |
with open(filename, "rb") as f:
|
|
|
130 |
|
131 |
@tasks.loop(minutes=1)
|
132 |
async def presence():
|
133 |
+
delta_uptime = datetime.datetime.utcnow() - launch_time
|
134 |
+
hours, remainder = divmod(int(delta_uptime.total_seconds()), 3600)
|
135 |
+
minutes, seconds = divmod(remainder, 60)
|
136 |
+
days, hours = divmod(hours, 24)
|
137 |
+
print(f"Online Time: {days:02d}d | {hours:02d}h | {minutes:02d}m | {seconds:02d}s")
|
138 |
+
await client.change_presence(
|
139 |
+
status=discord.Status.dnd,
|
140 |
+
activity=discord.Activity(
|
141 |
+
type=discord.ActivityType.playing,
|
142 |
+
large_image="https://i.imgur.com/Kk2BvJg.jpg",
|
143 |
+
large_text="This is Game Icon",
|
144 |
+
name="Escaping the IRS.",
|
145 |
+
details="",
|
146 |
+
state=f"Running for {days:02d}d | {hours:02d}h | {minutes:02d}m",
|
147 |
+
),
|
148 |
+
)
|
149 |
+
|
150 |
|
151 |
@client.event
|
152 |
async def on_ready():
|
153 |
+
print(f"We have logged in as {client.user}")
|
154 |
+
presence.start()
|
155 |
|
156 |
|
157 |
@client.event
|
158 |
+
async def on_message(message: discord.Message):
|
159 |
+
for user in message.mentions:
|
160 |
+
message.content = message.content.replace(
|
161 |
+
f"<@{user.id}>", f"@{user.display_name}"
|
162 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
try:
|
164 |
+
msgchannel = message.channel
|
165 |
+
try:
|
166 |
+
msgchannel_name = msgchannel.name
|
167 |
+
guild = message.guild
|
168 |
+
guild_name = guild.name
|
169 |
+
except:
|
170 |
+
guild_name = "Direct"
|
171 |
+
msgchannel_name = message.author.display_name
|
172 |
+
# await msgchannel.typing()
|
173 |
+
s = f"{Fore.LIGHTGREEN_EX}{message.author.display_name}#{message.author.discriminator}: {Fore.WHITE}{message.content} {Fore.LIGHTBLUE_EX} {msgchannel_name} {Fore.LIGHTMAGENTA_EX} {guild_name}"
|
174 |
+
print(Style.RESET_ALL)
|
175 |
+
print(s)
|
176 |
+
if message.author == client.user:
|
177 |
+
return
|
178 |
+
ats = []
|
179 |
+
if len(message.attachments) > 0:
|
180 |
+
for i in message.attachments:
|
181 |
+
if i.content_type == "image":
|
182 |
+
Image.open(io.BytesIO(i.read())).save("image.png")
|
183 |
+
ats.append(f"ImageInput:{caption('image.png')}")
|
184 |
+
s = f"GPT4 Correct {message.author}: {message.content} Attachments: {' '.join(ats)}<|end_of_turn|>"
|
185 |
+
try:
|
186 |
+
with open(os.path.join(guild_name, f"{msgchannel_name}.txt"), "a") as f:
|
187 |
+
f.write(s)
|
188 |
+
except:
|
189 |
+
os.mkdir(guild_name)
|
190 |
+
with open(os.path.join(guild_name, f"{msgchannel_name}.txt"), "a") as f:
|
191 |
+
f.write(
|
192 |
+
"""GPT4 Correct system: You are lr, a discord bot. You were made by Araeyn.
|
193 |
You must use the markdown format for your responses.
|
194 |
Do not excessively use bullet points.
|
195 |
Use emojis at the start of your responses.
|
|
|
200 |
Always include a title.
|
201 |
Do not generate images unless the user speciifes that they want an image.
|
202 |
<|end_of_turn|>"""
|
203 |
+
)
|
204 |
+
f.write(s)
|
205 |
+
if client.user in message.mentions or guild_name == "Direct":
|
206 |
+
async with msgchannel.typing():
|
207 |
+
with open(os.path.join(guild_name, f"{msgchannel_name}.txt"), "r") as f:
|
208 |
+
o = f.read()
|
209 |
+
title = ""
|
210 |
+
embed = discord.Embed(title=title, description="...", color=0x1E81B0)
|
211 |
+
embed.set_footer(
|
212 |
+
text="""Information or code generated by Lyre may not always be correct. Lyre was made by Araeyn."""
|
213 |
+
)
|
214 |
+
e = await message.reply(embed=embed)
|
215 |
+
o += "GPT4 Correct Assistant: "
|
216 |
+
print("right before query step.")
|
217 |
+
y = query({"inputs": o})
|
218 |
+
print("Got past query step.")
|
219 |
+
print(y)
|
220 |
+
r = y[0]["generated_text"][len(o) :]
|
221 |
+
title = extract_content(
|
222 |
+
r, first_delimiter="<title>", second_delimiter="</title>"
|
223 |
+
)[0]
|
224 |
+
l = 0
|
225 |
+
r = ""
|
226 |
+
while not (
|
227 |
+
"<|end_of_turn|>" in y[0]["generated_text"][len(o) :]
|
228 |
+
or "GPT4 Correct" in y[0]["generated_text"][len(o) :]
|
229 |
+
):
|
230 |
+
l += 1
|
231 |
+
r = y[0]["generated_text"][len(o) :]
|
232 |
+
title = extract_content(r, "<title>", "</title>")[0]
|
233 |
+
print(title)
|
234 |
+
r = r.replace("<title>" + title + "</title>", "")
|
235 |
+
print(r)
|
236 |
+
embed.title = title
|
237 |
+
embed.description = r
|
238 |
+
await e.edit(embed=embed)
|
239 |
+
py = y[0]["generated_text"]
|
240 |
+
y = query({"inputs": y[0]["generated_text"]})
|
241 |
+
if y[0]["generated_text"] == py:
|
242 |
+
break
|
243 |
+
r = r.split("<|end_of_turn|>")[0]
|
244 |
+
r = r.split("GPT4 Correct")[0]
|
245 |
+
embed.description = r
|
246 |
+
await e.edit(embed=embed)
|
247 |
+
with open(os.path.join(guild_name, f"{msgchannel_name}.txt"), "a") as f:
|
248 |
+
f.write("GPT4 Correct Assistant: ")
|
249 |
+
f.write(r)
|
250 |
+
f.write("<|end_of_turn|>")
|
251 |
+
if "<image>" in r and "</image>" in r:
|
252 |
+
i = extract_content(r)[0]
|
253 |
+
r = r.replace("<image>" + i + "</image>", "")
|
254 |
+
load = random.choice(
|
255 |
+
[
|
256 |
+
"https://cdn.dribbble.com/users/744913/screenshots/4094897/media/771a495231b798c0ccf7a59a19f31946.gif",
|
257 |
+
"https://cdn.dribbble.com/users/563824/screenshots/3633228/media/b620ccb3ae8c14ea5447d159ebb1da58.gif",
|
258 |
+
"https://cdn.dribbble.com/users/563824/screenshots/4155980/media/d3828cd14ed415eb6f90310991e06f27.gif",
|
259 |
+
"https://cdn.dribbble.com/users/107759/screenshots/3498589/media/5bc45101de34a80ea71238a02f3a75b5.gif",
|
260 |
+
]
|
261 |
+
)
|
262 |
+
if r.replace("\n", "") != "":
|
263 |
+
embed.description = r
|
264 |
+
await e.edit(embed=embed)
|
265 |
+
embed.set_image(url=load)
|
266 |
+
await e.edit(embed=embed)
|
267 |
+
else:
|
268 |
+
embed.clear_fields()
|
269 |
+
embed.set_image(url=load)
|
270 |
+
await e.edit(embed=embed)
|
271 |
+
image_bytes = generate(i)
|
272 |
+
try:
|
273 |
+
image = Image.open(io.BytesIO(image_bytes))
|
274 |
+
except:
|
275 |
+
print(image_bytes)
|
276 |
+
image.save(f"latest.png")
|
277 |
+
# prompt = ""
|
278 |
+
# url = "latest.png"
|
279 |
+
# init_image = load_image(url).convert("RGB")
|
280 |
+
# image = pipe(prompt, image=init_image).images[0]
|
281 |
+
# print(image)
|
282 |
+
image.save("latest.png")
|
283 |
+
embed.set_image(url="attachment://latest.png")
|
284 |
+
await e.edit(embed=embed, attachments=[discord.File(fp=f"latest.png")])
|
285 |
+
os.system("rm " + f"latest.png")
|
286 |
+
else:
|
287 |
+
embed.description = r
|
288 |
+
await e.edit(embed=embed)
|
289 |
+
except Exception as e:
|
290 |
+
embed = discord.Embed(
|
291 |
+
title="Error", description=f"```{traceback.format_exc()}```"
|
292 |
)
|
293 |
+
await message.channel.send(embed=embed)
|
294 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
295 |
|
296 |
ka()
|
297 |
+
|
298 |
+
|
299 |
def start():
|
300 |
import gradio as gr
|
301 |
+
|
302 |
def greet(name, intensity):
|
303 |
return "1 " * intensity + name + "!"
|
304 |
+
|
305 |
demo = gr.Interface(
|
306 |
fn=greet,
|
307 |
inputs=["text", "slider"],
|
308 |
outputs=["text"],
|
309 |
)
|
310 |
+
|
311 |
demo.launch()
|
312 |
+
|
313 |
+
|
314 |
token = os.environ["TOKEN"]
|
315 |
if token == "":
|
316 |
+
raise Exception("Please add your token to the Secrets pane.")
|
317 |
print("started gradio app")
|
318 |
Process(target=start).start()
|
319 |
|
320 |
+
|
321 |
def t():
|
322 |
client.run(token)
|
323 |
|
324 |
+
|
325 |
+
Process(target=t).start()
|