Araeynn commited on
Commit
85efe48
1 Parent(s): f8005f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -61
app.py CHANGED
@@ -2,15 +2,21 @@ print("Started.")
2
  import json
3
  import os
4
  import re
 
 
 
5
  from threading import Thread
6
  import discord
7
- from colorama import Fore, Style
8
- import random
9
- from huggingface_hub import InferenceClient, login
10
  from PIL import Image
11
- import math
12
  import streamlit as st
13
- import asyncio
 
 
 
 
 
14
 
15
  HF_TOKEN = os.environ["HF_TOKEN"]
16
 
@@ -21,9 +27,9 @@ LLM = InferenceClient(model="openchat/openchat-3.5-0106")
21
 
22
 
23
  def ec(x, fd="<image>", sd="</image>"):
24
- matches = re.findall(re.escape(fd) + "(.*?)" + re.escape(sd), x)
25
- matches = matches if matches else [""]
26
- return matches
27
 
28
 
29
  intents = discord.Intents.default()
@@ -31,67 +37,111 @@ intents.message_content = True
31
 
32
  client = discord.Client(intents=intents)
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  @client.event
36
  async def on_ready():
37
- print(f"Logged in as {client.user}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
 
40
  @client.event
41
  async def on_message(message):
42
- for user in message.mentions:
43
- message.content = message.content.replace(f"<@{user.id}>",
44
- f"@{str(user)}")
45
- try:
46
- msgchannel = message.channel
47
- try:
48
- msgchannel_name = msgchannel.name
49
- guild = message.guild
50
- guild_name = guild.name
51
- except:
52
- guild_name = "Direct"
53
- msgchannel_name = message.author.display_name
54
- s = f":green[{message.author}]: :white[{message.content}] :blue[{msgchannel_name}] :purple[{guild_name}]"
55
- st.markdown(s)
56
- if message.author == client.user:
57
- return
58
- sysp = """You are Lyre, a helpful discord chatbot. Respond in markdown. Your username is lyre#9828."""
59
  try:
60
- os.mkdir(guild_name)
61
- with open(f"{guild_name}/{msgchannel_name}", "w") as f:
62
- f.write(
63
- f"GPT4 Correct system: {sysp}<|end_of_turn|>\nGPT4 Correct {message.author}: {message.content}<|end_of_turn|>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  )
65
- except:
66
- with open(f"{guild_name}/{msgchannel_name}", "a") as f:
67
- n = "\n"
68
- f.write(
69
- f"""GPT4 Correct {message.author.name}: {message.content.strip(n)}<|end_of_turn|>"""
70
- )
71
- finally:
72
- with open(f"{guild_name}/{msgchannel_name}", "r") as f:
73
- context = f.read()
74
- context += f"GPT4 Correct Assistant: {message.content}<|end_of_turn|>\n"
75
- title = ec(context, "<title>", "</title>")[0]
76
- output = LLM.text_generation(context,
77
- stop_sequences=["<|end_of_turn|>"],
78
- max_new_tokens=4096)
79
- with open(f"{guild_name}/{msgchannel_name}", "a") as f:
80
- f.write(f"GPT4 Correct Assistant: {output}<|end_of_turn|>")
81
- words = output.split()
82
- embed = discord.Embed(title=title,
83
- description=" ".join(words[:i * 10]).replace(
84
- f"<title>{title}</title>", ""),
85
- color=0x1E81B0)
86
- embed.set_footer(
87
- text=
88
- """Information or code generated by Lyre may not always be correct. Lyre was made by Araeyn."""
89
- )
90
- e = await message.reply(embed=embed)
91
- except Exception as e:
92
- print(Fore.RED)
93
- print(e)
94
- print(Style.RESET_ALL)
95
 
96
  token = os.environ["TOKEN"]
97
- client.run(token)
 
2
  import json
3
  import os
4
  import re
5
+ import asyncio
6
+ import random
7
+ import datetime
8
  from threading import Thread
9
  import discord
10
+ from discord.ext import tasks
 
 
11
  from PIL import Image
12
+ from colorama import Fore, Style
13
  import streamlit as st
14
+ from huggingface_hub import InferenceClient, login
15
+
16
+ try:
17
+ os.mkdir("data")
18
+ finally:
19
+ pass
20
 
21
  HF_TOKEN = os.environ["HF_TOKEN"]
22
 
 
27
 
28
 
29
  def ec(x, fd="<image>", sd="</image>"):
30
+ matches = re.findall(re.escape(fd) + "(.*?)" + re.escape(sd), x)
31
+ matches = matches if matches else [""]
32
+ return matches
33
 
34
 
35
  intents = discord.Intents.default()
 
37
 
38
  client = discord.Client(intents=intents)
39
 
40
+ launch_time = datetime.datetime.utcnow()
41
+
42
+ ph = st.empty()
43
+
44
+ async def syncMessages():
45
+ with ph.container():
46
+ dirs = st.tabs(os.listdir("data"))
47
+ i = -1
48
+ for dir in os.listdir("data"):
49
+ i += 1
50
+ with dirs[i]:
51
+ files = st.tabs(os.listdir("data/" + dir))
52
+ k = -1
53
+ for file in os.listdir("data/" + dir):
54
+ k += 1
55
+ with files[k]:
56
+ with open(f"data/{dir}/{file}", "r") as f:
57
+ o = f.read().split("<|end_of_turn|>")
58
+ for item in o:
59
+ o = o.split(": ", 1)
60
+ st.markdown(f":blue[{o[0]}]: {o[1]}")
61
+
62
 
63
  @client.event
64
  async def on_ready():
65
+ print(f"Logged in as {client.user}")
66
+
67
+
68
+ @tasks.loop(seconds=0.1)
69
+ async def presence():
70
+ delta_uptime = datetime.datetime.utcnow() - launch_time
71
+ hours, remainder = divmod(int(delta_uptime.total_seconds()), 3600)
72
+ minutes, seconds = divmod(remainder, 60)
73
+ days, hours = divmod(hours, 24)
74
+ print(f"Online Time: {days:02d}d | {hours:02d}h | {minutes:02d}m | {seconds:02d}s")
75
+ await client.change_presence(
76
+ status=discord.Status.dnd,
77
+ activity=discord.Activity(
78
+ type=discord.ActivityType.playing,
79
+ large_image="https://i.imgur.com/Kk2BvJg.jpg",
80
+ large_text="This is Game Icon",
81
+ name="Escaping the IRS.",
82
+ details="",
83
+ state=f"Running for {days:02d}d | {hours:02d}h | {minutes:02d}m",
84
+ ),
85
+ )
86
+ await syncMessages()
87
 
88
 
89
  @client.event
90
  async def on_message(message):
91
+ for user in message.mentions:
92
+ message.content = message.content.replace(f"<@{user.id}>",
93
+ f"@{str(user)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  try:
95
+ msgchannel = message.channel
96
+ try:
97
+ msgchannel_name = msgchannel.name
98
+ guild = message.guild
99
+ guild_name = guild.name
100
+ except:
101
+ guild_name = "Direct"
102
+ msgchannel_name = message.author.display_name
103
+ s = f":green[{message.author}]: :violet[{message.content}] :blue[{msgchannel_name}] :orange[{guild_name}]"
104
+ if message.author == client.user:
105
+ return
106
+ sysp = """You are Lyre, a helpful discord chatbot. Respond in markdown. Your username is lyre#9828."""
107
+ try:
108
+ os.mkdir("data/" + guild_name)
109
+ with open(f"{guild_name}/{msgchannel_name}", "w") as f:
110
+ f.write(
111
+ f"GPT4 Correct system: {sysp}\nGPT4 Correct {message.author}: {message.content}"
112
+ )
113
+ except:
114
+ f = open(f"data/{guild_name}/{msgchannel_name}", "w")
115
+ f.close()
116
+ with open(f"data/{guild_name}/{msgchannel_name}", "a") as f:
117
+ n = "\n"
118
+ f.write(
119
+ f"""GPT4 Correct {message.author.name}: {message.content.strip(n)}"""
120
+ )
121
+ finally:
122
+ with open(f"data/{guild_name}/{msgchannel_name}", "r") as f:
123
+ context = f.read()
124
+ context += f"GPT4 Correct Assistant: {message.content}\n"
125
+ title = ec(context, "<title>", "</title>")[0]
126
+ output = LLM.text_generation(context,
127
+ stop_sequences=[""],
128
+ max_new_tokens=4096)
129
+ with open(f"{guild_name}/{msgchannel_name}", "a") as f:
130
+ f.write(f"GPT4 Correct Assistant: {output}")
131
+ words = output.split()
132
+ embed = discord.Embed(title=title,
133
+ description=" ".join(words[:i * 10]).replace(
134
+ f"<title>{title}</title>", ""),
135
+ color=0x1E81B0)
136
+ embed.set_footer(
137
+ text=
138
+ """Information or code generated by Lyre may not always be correct. Lyre was made by Araeyn."""
139
  )
140
+ e = await message.reply(embed=embed)
141
+ except Exception as e:
142
+ print(Fore.RED)
143
+ print(e)
144
+ print(Style.RESET_ALL)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
  token = os.environ["TOKEN"]
147
+ client.run(token)