Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
trying using code from testbot
Browse files
app.py
CHANGED
|
@@ -1,77 +1,87 @@
|
|
| 1 |
-
import discord
|
| 2 |
import os
|
| 3 |
import threading
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
import time
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
from io import BytesIO
|
| 9 |
import gradio_client
|
| 10 |
import gradio as gr
|
| 11 |
from gradio_client import Client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
| 15 |
-
intents = discord.Intents.all()
|
| 16 |
-
bot = commands.Bot(command_prefix='!', intents=intents)
|
| 17 |
|
| 18 |
-
XP_PER_MESSAGE = 10
|
| 19 |
|
| 20 |
-
|
|
|
|
| 21 |
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
def
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
return json.load(f)
|
| 27 |
-
except FileNotFoundError:
|
| 28 |
-
return {}
|
| 29 |
|
| 30 |
|
| 31 |
-
|
| 32 |
-
with open('xp_data.json', 'w') as f:
|
| 33 |
-
json.dump(xp_data, f)
|
| 34 |
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
async def on_ready():
|
| 38 |
-
print(f
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
-
@
|
| 42 |
async def on_message(message):
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
print(f"{message.author.mention} xp: {xp_data[message.author.id]}")
|
| 57 |
-
print(f"{message.author.mention} level: {level}")
|
| 58 |
-
save_xp_data(xp_data)
|
| 59 |
-
|
| 60 |
|
|
|
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
def calculate_level(xp):
|
| 64 |
return int(xp ** (1.0 / 3.0)) # 100k messages = lvl 100, good for super long term plan
|
| 65 |
|
| 66 |
|
| 67 |
-
@
|
| 68 |
async def level(ctx):
|
| 69 |
global xp_data
|
| 70 |
print(ctx.author.id)
|
| 71 |
print(ctx.author.mention)
|
| 72 |
-
if ctx.author.id == 811235357663297546:
|
| 73 |
if ctx.author.id in xp_data:
|
| 74 |
-
|
| 75 |
xp = xp_data[ctx.author.id]
|
| 76 |
level = calculate_level(xp)
|
| 77 |
|
|
@@ -81,60 +91,18 @@ async def level(ctx):
|
|
| 81 |
await ctx.send('You have not earned any XP yet.')
|
| 82 |
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
if ctx.author.id == 811235357663297546:
|
| 87 |
-
author_id = str(ctx.author.id)
|
| 88 |
-
if author_id in xp_data:
|
| 89 |
-
timestamps, xp_values = zip(*xp_data[author_id])
|
| 90 |
-
plt.plot(timestamps, xp_values)
|
| 91 |
-
plt.xlabel('Timestamp')
|
| 92 |
-
plt.ylabel('XP')
|
| 93 |
-
plt.title('XP Over Time')
|
| 94 |
-
|
| 95 |
-
image_stream = BytesIO()
|
| 96 |
-
plt.savefig(image_stream, format='png')
|
| 97 |
-
plt.close()
|
| 98 |
-
|
| 99 |
-
image_stream.seek(0)
|
| 100 |
-
await ctx.send(file=discord.File(fp=image_stream, filename='xp_graph.png'))
|
| 101 |
-
else:
|
| 102 |
-
await ctx.send('You have not earned any XP yet.')
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
@bot.command()
|
| 106 |
-
async def show_xp_data(ctx):
|
| 107 |
-
if ctx.author.id == 811235357663297546:
|
| 108 |
-
author_id = str(ctx.author.id)
|
| 109 |
-
if author_id in xp_data:
|
| 110 |
-
xp = xp_data[author_id]
|
| 111 |
-
await ctx.send(f'Your XP data: {xp}')
|
| 112 |
-
else:
|
| 113 |
-
await ctx.send('You have not earned any XP yet.')
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
@bot.command()
|
| 117 |
-
async def load_xp(ctx):
|
| 118 |
-
if ctx.author.id == 811235357663297546:
|
| 119 |
-
try:
|
| 120 |
-
xp_data.clear() # Clear current XP data
|
| 121 |
-
with open('xp_data.json', 'r') as f:
|
| 122 |
-
loaded_data = json.load(f)
|
| 123 |
-
xp_data.update(loaded_data) # Update with loaded data
|
| 124 |
-
await ctx.send('XP data has been loaded from the file.')
|
| 125 |
-
except FileNotFoundError:
|
| 126 |
-
await ctx.send('No XP data file found.')
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
|
| 131 |
|
| 132 |
-
|
| 133 |
-
""""""
|
| 134 |
-
def run_bot():
|
| 135 |
-
bot.run(DISCORD_TOKEN)
|
| 136 |
threading.Thread(target=run_bot).start()
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import threading
|
| 3 |
+
|
| 4 |
+
import discord
|
|
|
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
from io import BytesIO
|
| 7 |
import gradio_client
|
| 8 |
import gradio as gr
|
| 9 |
from gradio_client import Client
|
| 10 |
+
import json
|
| 11 |
+
import time
|
| 12 |
+
from discord import app_commands
|
| 13 |
+
from discord.ext import commands
|
| 14 |
+
|
| 15 |
|
| 16 |
|
| 17 |
+
# HF GUILD SETTINGS
|
| 18 |
+
MY_GUILD_ID = 879548962464493619
|
| 19 |
+
MY_GUILD = discord.Object(id=MY_GUILD_ID)
|
| 20 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
|
|
|
|
|
|
| 21 |
|
|
|
|
| 22 |
|
| 23 |
+
class Bot(commands.Bot):
|
| 24 |
+
"""This structure allows slash commands to work instantly."""
|
| 25 |
|
| 26 |
+
def __init__(self):
|
| 27 |
+
super().__init__(command_prefix="/", intents=discord.Intents.all())
|
| 28 |
|
| 29 |
+
async def setup_hook(self):
|
| 30 |
+
await self.tree.sync(guild=discord.Object(MY_GUILD_ID))
|
| 31 |
+
print(f"Synced slash commands for {self.user}.")
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
+
client = Bot()
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
+
|
| 38 |
+
XP_PER_MESSAGE = 10
|
| 39 |
+
xp_data = {}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
@client.event
|
| 43 |
async def on_ready():
|
| 44 |
+
print(f"Logged in as {client.user} (ID: {client.user.id})")
|
| 45 |
+
print("------")
|
| 46 |
|
| 47 |
|
| 48 |
+
@client.event
|
| 49 |
async def on_message(message):
|
| 50 |
+
try:
|
| 51 |
+
global xp_data
|
| 52 |
+
|
| 53 |
+
if message.author.bot:
|
| 54 |
+
return
|
| 55 |
|
| 56 |
+
if message.author.id not in xp_data:
|
| 57 |
+
xp_data[message.author.id] = 0
|
| 58 |
+
|
| 59 |
+
old = xp_data[message.author.id]
|
| 60 |
+
new = old + XP_PER_MESSAGE
|
| 61 |
+
xp_data[message.author.id] = new
|
| 62 |
+
level = calculate_level(new)
|
| 63 |
|
| 64 |
+
print(f"{message.author} xp: {xp_data[message.author.id]}")
|
| 65 |
+
print(f"{message.author} level: {level}")
|
| 66 |
+
print(f"xp_data: {xp_data}")
|
| 67 |
+
save_xp_data(xp_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"Error: {e}")
|
| 71 |
|
| 72 |
|
| 73 |
def calculate_level(xp):
|
| 74 |
return int(xp ** (1.0 / 3.0)) # 100k messages = lvl 100, good for super long term plan
|
| 75 |
|
| 76 |
|
| 77 |
+
@client.command()
|
| 78 |
async def level(ctx):
|
| 79 |
global xp_data
|
| 80 |
print(ctx.author.id)
|
| 81 |
print(ctx.author.mention)
|
| 82 |
+
if ctx.author.id == 811235357663297546:
|
| 83 |
if ctx.author.id in xp_data:
|
| 84 |
+
print(f"{ctx.author} is in xp_data")
|
| 85 |
xp = xp_data[ctx.author.id]
|
| 86 |
level = calculate_level(xp)
|
| 87 |
|
|
|
|
| 91 |
await ctx.send('You have not earned any XP yet.')
|
| 92 |
|
| 93 |
|
| 94 |
+
def run_bot():
|
| 95 |
+
client.run(DISCORD_TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
threading.Thread(target=run_bot).start()
|
| 99 |
+
"""This allows us to run the Discord bot in a Python thread"""
|
| 100 |
+
with gr.Blocks() as demo:
|
| 101 |
+
gr.Markdown("""
|
| 102 |
+
# Huggingbots Server
|
| 103 |
+
This space hosts the huggingbots discord bot.
|
| 104 |
+
Currently supported models are Falcon and DeepfloydIF
|
| 105 |
+
""")
|
| 106 |
+
demo.queue(concurrency_count=100)
|
| 107 |
+
demo.queue(max_size=100)
|
| 108 |
+
demo.launch()
|