Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 20,118 Bytes
8b23687 5cb8bba 8b23687 5086fc0 5a56b6c d692f1f 8b23687 546104d 8b23687 bfb4d01 8b23687 297c8cc 8b23687 7586254 8b23687 dc6587d 400a5ad 8b23687 dd09b31 a5603d3 7187a61 cf1e692 a5603d3 479c0c2 298bd5f d520393 27b9d6d d520393 297c8cc ba302bd 26d0ad5 4f19e81 298bd5f 42a4c1b ba302bd 0aa83ad 6b5ae25 f5d5dc6 ba302bd 351bce1 f5d5dc6 ba302bd f5d5dc6 6b24b71 9b890aa 4f19e81 ba302bd 367e21c 04dbcbe 123b00d f5d5dc6 297c8cc 8279d6e 2753dea ba302bd f5d5dc6 ba302bd f5d5dc6 27b9d6d f5d5dc6 27b9d6d f5d5dc6 82e2581 ec5588a f5d5dc6 dd09b31 8b7cc1a 9b890aa 8b7cc1a 6b24b71 9b890aa 8b7cc1a dd09b31 f5d5dc6 297c8cc 021515a 6b24b71 64de934 6b1cca3 629ac9e d520393 629ac9e d520393 3b505e3 64de934 ba302bd 64de934 4339168 64de934 ba302bd 9a0cb1f ba302bd 64de934 9a0cb1f 64de934 3b505e3 d520393 c31f921 d520393 4f19e81 c31f921 297c8cc 298bd5f ac377ac 42cb0fd c31f921 d520393 4fe0a98 297c8cc 298bd5f 297c8cc ac906e6 297c8cc 629ac9e 297c8cc 6fe8226 297c8cc c53ef67 6fe8226 69f32be c3aee4c 69f32be 8279d6e 69f32be ed9981f 69f32be 5d8f291 4f19e81 ba302bd 6fe8226 123b00d 297c8cc 298bd5f 297c8cc 4fe0a98 629ac9e 297c8cc 4fe0a98 297c8cc ac906e6 123b00d d520393 297c8cc d520393 4f19e81 c31f921 d520393 3b505e3 ba302bd 4f19e81 3b505e3 4f19e81 ae789c3 3b505e3 367e21c 4f19e81 ba302bd 5fc7c16 1dc1037 5fc7c16 1dc1037 5fc7c16 7586254 a7da8d1 5fc7c16 ba302bd 646db22 5fc7c16 646db22 1f9a87d 646db22 ba302bd 4f19e81 6fe8226 3b505e3 f5d5dc6 e6f070d 229f240 3b505e3 d520393 66cef69 cf1e692 66cef69 5b24125 229f240 1080060 fc1e452 41ceb8f f5d5dc6 e994942 f5d5dc6 ba302bd 3b505e3 22d3c95 f5d5dc6 e994942 f5d5dc6 3b505e3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
import asyncio
import csv
import datetime
import json
import logging
import os
import os.path
import random
import re
import threading
import time
import discord
import gradio as gr
import gradio_client
import gspread
import numpy as np
import pandas as pd
import requests
from apscheduler.executors.pool import ThreadPoolExecutor
from apscheduler.schedulers.background import BackgroundScheduler
from discord import Color, Embed
from discord.ext import commands, tasks
from gradio_client import Client
from gspread_dataframe import get_as_dataframe, set_with_dataframe
from gspread_formatting.dataframe import format_with_dataframe
from huggingface_hub import HfApi, list_liked_repos, list_metrics, list_models
from tabulate import tabulate
from datetime import datetime
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)
#logger = logging.getLogger(__name__)
#logging.basicConfig(level=logging.DEBUG)
""""""
XP_PER_MESSAGE = 10 # 100k messages = 1M exp = lvl 100
""""""
service_account = json.loads(os.environ.get('KEY'))
file_path = 'service_account.json'
with open(file_path, 'w') as json_file:
json.dump(service_account, json_file)
gspread_bot = gspread.service_account(filename='service_account.json')
worksheet = gspread_bot.open("levelbot").sheet1
worksheet2 = gspread_bot.open("hf_discord_verified_users_test").sheet1
""""""
bot_ids = [1136614989411655780, 1166392942387265536, 1158038249835610123, 1130774761031610388, 1155489509518098565, 1155169841276260546, 1152238037355474964, 1154395078735953930]
""""""
api = HfApi()
""""""
#csv_file = 'data.csv'
global_df = pd.DataFrame()
worksheet2_df = pd.DataFrame()
print(type(global_df))
community_global_df = pd.DataFrame()
data = pd.read_csv("https://docs.google.com/spreadsheets/d/1hQSsIg1Y9WdBF_CdNM1L1rUUREoxKqRTe3_ILo-WK8w/export?format=csv&gid=0")
data2 = pd.read_csv("https://docs.google.com/spreadsheets/d/1T_twNONDSZ8zw7aDcYhw92DlawqPktti1xFw9ssJ13Y/export?format=csv&gid=0")
@bot.event
async def on_ready():
try:
global global_df
global worksheet2_df
await asyncio.sleep(1.1)
print(f'Logged in as {bot.user.name}')
print(f"XP_PER_MESSAGE: {XP_PER_MESSAGE}")
"""import data from google sheets -> HF Space df (doesn't make API call this way, as it's read-only)"""
data['discord_user_id'] = data['discord_user_id'].astype(str)
global_df = data
print(f"csv successfully retrieved: \n {global_df}")
worksheet2_df = data2.copy()
worksheet2_df['discord_user_id'] = worksheet2_df['discord_user_id'].astype(str)
worksheet2_df['id_recover_test'] = worksheet2_df['id_recover_test'].astype(str)
print(worksheet2_df.dtypes)
# updates both leaderboards
remove_huggingfolks.start()
await periodic_api_test()
print(f"------------------------------------------------------------------------")
except Exception as e:
print(f"on_message Error: {e}")
def update_google_sheet():
"""save data from HF Space -> google sheets (makes 2 API calls)"""
try:
print("Updating google sheets...")
name = "levelbot"
#worksheet = gspread_bot.open(name).sheet1
global_df['discord_user_id'] = global_df['discord_user_id'].astype(str)
# could just do this / format
set_with_dataframe(worksheet, global_df)
# very important, otherwise gspread will force format IDs in scientific notation by default (making them unusable)
worksheet.format(f"A1:A{len(global_df.index)+1}", {
"numberFormat": {
"type": 'TEXT',
},
})
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"Google sheet {name} successfully updated at {timestamp}! \n{global_df}")
worksheet2.format(f"E1:E{len(worksheet2_df.index)+1}", {
"numberFormat": {
"type": 'TEXT',
},
})
worksheet2.format(f"A1:A{len(worksheet2_df.index)+1}", {
"numberFormat": {
"type": 'TEXT',
},
})
name2 = "hf_discord_verified_users_test"
# could just do this / format
worksheet2['discord_user_id'] = worksheet2['discord_user_id'].astype(str)
worksheet2['id_recover_test'] = worksheet2['id_recover_test'].astype(str)
print(worksheet2_df.dtypes)
set_with_dataframe(worksheet2, worksheet2_df)
# very important, otherwise gspread will force format IDs in scientific notation by default (making them unusable)
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"Google sheet {name2} successfully updated at {timestamp}!")
print(f"------------------------------------------------------------------------")
except Exception as e:
print(f"on_message Error: {e}")
executor = ThreadPoolExecutor(max_workers=1)
scheduler = BackgroundScheduler(executors={'default': executor})
scheduler.add_job(update_google_sheet, trigger='interval', minutes=1, max_instances=1)
scheduler.start()
def calculate_level(xp):
return int(xp ** (1.0 / 3.0))
def calculate_xp(level):
return (int(level ** 3))
processed_users = set()
async def periodic_api_test(): # needs rewrite, can do same thing and interact with csv -> push to google sheets
try:
# this should be gspread type function (do not mix with discord at all)
global worksheet2_df
# get
for index, user in enumerate(worksheet2_df['hf_user_name']):
if user in processed_users:
continue
await asyncio.sleep(0.1)
url = f"https://huggingface.co/api/users/{user}/overview"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
likes = data["numLikes"]
models = data["numModels"]
datasets = data["numDatasets"]
spaces = data["numSpaces"]
discussions = data["numDiscussions"]
papers = data["numPapers"]
upvotes = data["numUpvotes"]
worksheet2_df.loc[index:index, 'likes':'upvotes'] = [[likes, models, datasets, spaces, discussions, papers, upvotes]]
pd.set_option('display.max_rows', 30)
print(worksheet2_df)
processed_users.add(user)
print(processed_users)
else:
print(f"Failed to retrieve data for user {user}. Status code: {response.status_code}")
except Exception as e:
print(f"periodic_api_test Error: {e}")
async def add_exp(member_id):
try:
"""Uses member_id to create new record or update old one [member_id_column] ... [member_exp_column]"""
await asyncio.sleep(0.1)
global global_df
guild = bot.get_guild(879548962464493619)
member = guild.get_member(member_id) # bot.get_user == User, guild.get_member == Member (not the same thing!)
lvl1 = guild.get_role(1171861537699397733)
lvl2 = guild.get_role(1171861595115245699)
lvl3 = guild.get_role(1171861626715115591)
lvl4 = guild.get_role(1171861657975259206)
lvl5 = guild.get_role(1171861686580412497)
lvl6 = guild.get_role(1171861900301172736)
lvl7 = guild.get_role(1171861936258941018)
lvl8 = guild.get_role(1171861968597024868)
lvl9 = guild.get_role(1171862009982242836)
lvl10 = guild.get_role(1164188093713223721)
lvl11 = guild.get_role(1171524944354607104)
lvl12 = guild.get_role(1171524990257082458)
lvl13 = guild.get_role(1171525021928263791)
lvl14 = guild.get_role(1171525062201966724)
lvl15 = guild.get_role(1171525098465918996)
lvl16 = guild.get_role(1176826165546201099)
lvl17 = guild.get_role(1176826221301092392)
lvl18 = guild.get_role(1176826260643659776)
lvl19 = guild.get_role(1176826288816791693)
lvl20 = guild.get_role(1176826319447801896)
lvl21 = guild.get_role(1195030831174008902)
lvl22 = guild.get_role(1195030883351150592)
lvl23 = guild.get_role(1196055555006009445)
lvl24 = guild.get_role(1196055640917938216)
lvl25 = guild.get_role(1196055712506318869)
lvl26 = guild.get_role(1196055775924195378)
lvl27 = guild.get_role(1196055837018435664)
lvl28 = guild.get_role(1196055908267081849)
lvl29 = guild.get_role(1196055970804150352)
lvl30 = guild.get_role(1196056027720847380)
lvls = {
1: lvl1, 2: lvl2, 3: lvl3, 4: lvl4, 5: lvl5, 6: lvl6, 7: lvl7, 8: lvl8, 9: lvl9, 10: lvl10,
11: lvl11, 12: lvl12, 13: lvl13, 14: lvl14, 15: lvl15, 16: lvl16, 17: lvl17, 18: lvl18, 19: lvl19, 20: lvl20,
21: lvl21, 22: lvl22, 23: lvl23, 24: lvl24, 25: lvl25, 26: lvl26, 27: lvl27, 28: lvl28, 29: lvl29, 30: lvl30,
}
# xp = int64
# member_name = string
# discord_user_id = string
member_found = False
for index, cell_value in global_df.iloc[:, 0].items():
if cell_value == str(member_id):
# if found, update that row...
member_found = True
print(f"Record for {member} found at row {index + 1}, column 1")
# increment the old experience value (better not to replace outright)
old_xp = global_df.loc[index, 'discord_exp']
new_xp = old_xp + XP_PER_MESSAGE
global_df.loc[index, 'discord_exp'] = new_xp # do not change column name
print(f"Record for {member} updated from {old_xp} to {global_df.loc[index, 'discord_exp']} (+{XP_PER_MESSAGE}) ")
# level up
current_level = calculate_level(new_xp)
print(f"Current_level for {member}: {current_level}")
if current_level >= 2 and current_level <=30:
current_role = lvls[current_level]
if current_role not in member.roles: # if we leveled up
await member.add_roles(current_role)
print(f"Level Up! Gave {member} {current_role}")
await member.remove_roles(lvls[current_level-1])
print(f"Removed {lvls[current_level-1]} from {member}")
#print(f"{member} Level up! {current_level-1} -> {current_level}!")
if current_role in member.roles: # needs update; reference exp reward for verification
#await member.send(f"Level up! {current_level-1} -> {current_level}!")
#if member_id == 811235357663297546:
row = global_df[global_df['discord_user_id'] == str(member_id)] # does this need to be string?
if not row.empty:
target_exp = row['discord_exp'].values[0]
rank = (global_df['discord_exp'] > target_exp).sum() + 1
print(f"The rank for discord_id {member_id} based on discord_exp is: {rank}")
else:
print(f"Discord ID {member_id} not found in the DataFrame.")
# send embed
embed = Embed(color=Color.blue())
embed.set_author(name=f"{member}", icon_url=member.avatar.url if member.avatar else bot.user.avatar.url)
embed.title = f"Level Up! `{current_level-1}` -> `{current_level}`"
msg = f'π€ Congrats {member}! You just leveled up in the Hugging Face Discord server'
embed.description = f"{msg}."
embed.add_field(name="Leaderboard Ranking:", value=f"π **{rank}**\n\nhttps://discord.com/channels/879548962464493619/1197143964994773023", inline=True)
# can use ^ to track changes in leaderboard ranking and display in levelup message when climbing
# (green red arrow thingies)
# could also pull ranks above and below yourself (if they exist, i.e.)
# 1 nateraw
# 2 [you are here]
# 3 osanseviero
# also, ahead by how much exp, how many levels maybe
msg3 = "- Posting\n- Reacting / being reacted to\n- Being active on the Hugging Face Hub (verify to link your Hub + Discord accounts!)"
embed.add_field(name="How to Level Up:", value=msg3, inline=True)
verification_link = "https://discord.com/channels/879548962464493619/900125909984624713"
embed.add_field(name="Verify Here:", value=verification_link, inline=True)
#lunar = bot.get_user(811235357663297546)
await member.send(embed=embed)
print(f"Sent levelup embed to {member}")
#You can verify your account to earn 100 points! To verify, do A.
print(f"------------------------------------------------------------------------")
if not member_found:
# if not, create new record
print(f"creating new record for {member}")
xp = 10 # define somewhere else?
member_id = str(member_id)
member_name = str(member.name)
current_level = calculate_level(xp)
row_data = [member_id, member_name, xp, current_level]
global_df.loc[len(global_df.index)] = row_data
# initial role assignment
if current_level == 1:
if lvl1 not in member.roles:
await member.add_roles(lvl1)
print(f"Gave {member} {lvl1}") # can log this better
print(f"------------------------------------------------------------------------")
except Exception as e:
print(f"add_exp Error: {e}")
@bot.event
async def on_message(message):
try:
if message.author.id not in bot_ids: # could change to if author does not have bot role (roleid)
if "!help_xp" not in message.content:
print(f"adding exp from message {message.author}")
await asyncio.sleep(0.1)
await add_exp(message.author.id)
await bot.process_commands(message)
except Exception as e:
print(f"on_message Error: {e}")
@bot.event
async def on_reaction_add(reaction, user):
try:
if user.id not in bot_ids:
print(f"adding exp from react {user}")
await asyncio.sleep(0.1)
await add_exp(user.id)
await asyncio.sleep(0.1)
await add_exp(reaction.message.author.id)
except Exception as e:
print(f"on_reaction_add Error: {e}")
@tasks.loop(minutes=1)
async def remove_huggingfolks():
try:
# remove huggingfolks
global community_global_df
community_global_df = global_df.copy()
guild = bot.get_guild(879548962464493619)
role = discord.utils.get(guild.roles, id=897376942817419265)
members_with_role = [member.id for member in guild.members if role in member.roles]
for member_id in members_with_role:
community_global_df = community_global_df[community_global_df.iloc[:, 0] != str(member_id)]
#print(community_global_df)
community_global_df.drop(community_global_df.columns[0], axis=1, inplace=True)
community_global_df['discord_exp'] = pd.to_numeric(community_global_df['discord_exp'])
community_global_df = community_global_df.nlargest(len(community_global_df), 'discord_exp')
top_30_exp = community_global_df.nlargest(30, 'discord_exp')
top_30_exp['D'] = ['π₯','π₯','π₯','','','','','','','','','','','','','','','','','','','','','','','','','','','']
top_30_rows = top_30_exp.values.tolist()
#print(top_30_rows)
channel = bot.get_channel(1197143964994773023)
message = await channel.fetch_message(1197148293164187678)
# put into message / leaderboard
new_table = tabulate(top_30_rows, headers=["Name", "Experience", "Level", "Rank"], tablefmt="plain")
await message.edit(content=f"Updated Leaderboard:\n```\n{new_table}\n```")
print(f"Updated discord leaderboard!")
except Exception as e:
print(f"remove_huggingfolks Error: {e}")
@bot.command(name='xp_help')
async def xp_help(ctx):
try:
help_message = "How to earn Discord / Hub exp: Post messages, react, Like, discuss, create repos and papers"
await ctx.author.send(help_message)
except Exception as e:
print(f"on_message Error: {e}")
@bot.command()
async def count_users_with_role(ctx, role_id):
role = discord.utils.get(ctx.guild.roles, id=int(role_id))
count = sum(1 for member in ctx.guild.members if role in member.roles)
await ctx.send(f"Number of users with the role '{role.name}': {count}")
# embeds with user pfps?
# name, pfp, time in server....
# weekly do different count
# count number of messages per user for every channel (total messages)
# fix sheet if necessary
# add emojis for some color
# check if members are still in the server
""""""
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
def run_bot():
bot.run(DISCORD_TOKEN)
threading.Thread(target=run_bot).start()
URL = "https://docs.google.com/spreadsheets/d/1hQSsIg1Y9WdBF_CdNM1L1rUUREoxKqRTe3_ILo-WK8w/edit#gid=0"
csv_url = URL.replace('/edit#gid=', '/export?format=csv&gid=')
def get_data():
try:
return community_global_df
except Exception as e:
print(f"on_message Error: {e}")
demo = gr.Blocks()
with demo:
try:
dataframe1 = pd.read_csv(csv_url)
column_values_unique = sorted(dataframe1.iloc[:, 3].unique())
dataframe2 = pd.DataFrame({'Levels': column_values_unique})
counts = {}
for value in data.iloc[:, 3]:
counts[value] = counts.get(value, 0) + 1
dataframe2['Members'] = dataframe2['Levels'].map(counts)
print("\nDataframe 2:")
print(dataframe2)
TITLE = """<h1 align="center" id="space-title">π€ Hugging Face Level Leaderboard</h1>"""
gr.HTML(TITLE)
with gr.Tabs(elem_classes="tab-buttons") as tabs:
with gr.TabItem("π
Level leaderboard", elem_id="level-table", id=0):
#gr.Markdown("# π Experience Leaderboard")
with gr.Row():
with gr.Column():
gr.DataFrame(get_data, every=5, height=500, interactive=False, col_count=(3, "fixed"), column_widths=["100px","100px","100px"])
with gr.Column():
gr.BarPlot(
value=dataframe2,
x="Levels",
y="Members",
title="Level Distribution",
height=450,
width=450,
interactive=False
)
#with gr.TabItem("π Members of the Week", elem_id="week-table", id=1):
#with gr.TabItem("π Hub-only leaderboard", elem_id="hub-table", id=2):
except Exception as e:
print(f"on_message Error: {e}")
demo.queue().launch()
|