Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
test2
Browse files
app.py
CHANGED
|
@@ -66,7 +66,7 @@ async def on_ready():
|
|
| 66 |
#data.to_csv(csv_file, index=False)
|
| 67 |
|
| 68 |
|
| 69 |
-
def update_google_sheet():
|
| 70 |
"""save data from HF Space -> google sheets (makes 1 API call)"""
|
| 71 |
print("test")
|
| 72 |
name = "levelbot"
|
|
@@ -89,43 +89,6 @@ def calculate_level(xp):
|
|
| 89 |
def calculate_xp(level):
|
| 90 |
return (int(level ** 3))
|
| 91 |
|
| 92 |
-
|
| 93 |
-
processed_users = set()
|
| 94 |
-
|
| 95 |
-
async def periodic_api_test(): # needs rewrite, can do same thing and interact with csv -> push to google sheets
|
| 96 |
-
try:
|
| 97 |
-
await asyncio.sleep(1)
|
| 98 |
-
column_values_3 = worksheet2.col_values(3)
|
| 99 |
-
column_values_8 = worksheet2.col_values(8)
|
| 100 |
-
|
| 101 |
-
for i, user in enumerate(column_values_3):
|
| 102 |
-
if user in processed_users:
|
| 103 |
-
continue
|
| 104 |
-
|
| 105 |
-
url = f"https://huggingface.co/api/users/{user}/overview"
|
| 106 |
-
response = requests.get(url)
|
| 107 |
-
|
| 108 |
-
if response.status_code == 200:
|
| 109 |
-
data = response.json()
|
| 110 |
-
|
| 111 |
-
likes = data["numLikes"]
|
| 112 |
-
models = data["numModels"]
|
| 113 |
-
datasets = data["numDatasets"]
|
| 114 |
-
spaces = data["numSpaces"]
|
| 115 |
-
discussions = data["numDiscussions"]
|
| 116 |
-
papers = data["numPapers"]
|
| 117 |
-
upvotes = data["numUpvotes"]
|
| 118 |
-
|
| 119 |
-
worksheet2.update(values=[[likes, models, datasets, spaces, discussions, papers, upvotes]],
|
| 120 |
-
range_name=f'G{i+1}:M{i+1}')
|
| 121 |
-
|
| 122 |
-
processed_users.add(user)
|
| 123 |
-
else:
|
| 124 |
-
print(f"Failed to retrieve data for user {user}. Status code: {response.status_code}")
|
| 125 |
-
|
| 126 |
-
except Exception as e:
|
| 127 |
-
print(f"periodic_api_test Error: {e}")
|
| 128 |
-
|
| 129 |
|
| 130 |
async def add_exp(member_id):
|
| 131 |
try:
|
|
@@ -218,7 +181,7 @@ async def add_exp(member_id):
|
|
| 218 |
print(f"Gave {member} {lvl1}") # can log this better
|
| 219 |
|
| 220 |
if member_id == 811235357663297546:
|
| 221 |
-
update_google_sheet()
|
| 222 |
except Exception as e:
|
| 223 |
print(f"add_exp Error: {e}")
|
| 224 |
|
|
@@ -235,92 +198,8 @@ async def on_message(message):
|
|
| 235 |
except Exception as e:
|
| 236 |
print(f"on_message Error: {e}")
|
| 237 |
|
| 238 |
-
|
| 239 |
-
@bot.event
|
| 240 |
-
async def on_reaction_add(reaction, user):
|
| 241 |
-
try:
|
| 242 |
-
if user.id not in bot_ids:
|
| 243 |
-
print(f"adding exp from react {user.id}")
|
| 244 |
-
await asyncio.sleep(1)
|
| 245 |
-
await add_exp(user.id)
|
| 246 |
-
except Exception as e:
|
| 247 |
-
print(f"on_reaction_add Error: {e}")
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
@bot.command(name='update_leaderboard')
|
| 251 |
-
async def update_leaderboard(ctx, num_results: int = 10): # needs rewrite
|
| 252 |
-
if ctx.author.id == 811235357663297546:
|
| 253 |
-
await asyncio.sleep(1)
|
| 254 |
-
worksheet = gspread_bot.open("levelbot").sheet1
|
| 255 |
-
|
| 256 |
-
names_list = worksheet.col_values(2)[1:]
|
| 257 |
-
levels_list = worksheet.col_values(4)[1:]
|
| 258 |
-
exp_list = worksheet.col_values(3)[1:]
|
| 259 |
-
channel = bot.get_channel(1197143964994773023)
|
| 260 |
-
message = await channel.fetch_message(1197148293164187678)
|
| 261 |
-
|
| 262 |
-
# for 3 lists
|
| 263 |
-
combined_list = [list(sublist) for sublist in zip(names_list, levels_list, exp_list)]
|
| 264 |
-
combined_list = [[name, int(level), int(exp)] for name, level, exp in combined_list]
|
| 265 |
-
combined_list = sorted(combined_list, key=lambda x: x[1], reverse=True)
|
| 266 |
-
|
| 267 |
-
print(combined_list)
|
| 268 |
-
top_results = combined_list[:num_results]
|
| 269 |
-
#print(top_results)
|
| 270 |
-
|
| 271 |
-
"""
|
| 272 |
-
# get position, then find that value in updated_names_list
|
| 273 |
-
levels_list = list(map(int, levels_list))
|
| 274 |
-
data_pairs = list(zip(names_list, levels_list))
|
| 275 |
-
sorted_data_pairs = sorted(data_pairs, key=lambda x: x[1], reverse=True)
|
| 276 |
-
top_data_pairs = sorted_data_pairs[:num_results]
|
| 277 |
-
"""
|
| 278 |
-
|
| 279 |
-
# remove huggingfolks
|
| 280 |
-
guild = ctx.guild
|
| 281 |
-
role = discord.utils.get(guild.roles, id=897376942817419265)
|
| 282 |
-
if role is None:
|
| 283 |
-
await ctx.send("Role not found.")
|
| 284 |
-
return
|
| 285 |
-
members_with_role = [member.name for member in guild.members if role in member.roles]
|
| 286 |
-
top_results = [r for r in top_results if r[0] not in members_with_role]
|
| 287 |
-
|
| 288 |
-
for name, level, xp in top_results:
|
| 289 |
-
print(f"Name: {name}, Level: {level}, Exp: {xp}")
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
def xp_required_to_next_level(current_level, current_xp):
|
| 293 |
-
level_floor_xp = calculate_xp(current_level)
|
| 294 |
-
level_ceiling_xp = calculate_xp(current_level+1)
|
| 295 |
-
xp_to_level_up = level_ceiling_xp - current_xp
|
| 296 |
-
return (xp_to_level_up)
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
# put into message / leaderboard
|
| 300 |
-
new_leaderboard_data = [(name, level, str(xp_required_to_next_level(int(level), int(xp)))) for name, level, xp in top_results]
|
| 301 |
-
new_table = tabulate(new_leaderboard_data, headers=["Name", "Level", "XP to level up"], tablefmt="plain")
|
| 302 |
-
await message.edit(content=f"Updated Leaderboard:\n```\n{new_table}\n```")
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
@bot.command(name='xp_help')
|
| 306 |
-
async def xp_help(ctx):
|
| 307 |
-
help_message = "How to earn Discord / Hub exp: Post messages, react, Like, discuss, create repos and papers"
|
| 308 |
-
await ctx.author.send(help_message)
|
| 309 |
-
|
| 310 |
-
# embeds with user pfps?
|
| 311 |
-
# name, pfp, time in server....
|
| 312 |
-
|
| 313 |
-
# discord_level column
|
| 314 |
-
# pick 10 highest
|
| 315 |
-
# update
|
| 316 |
|
| 317 |
-
# weekly do different count
|
| 318 |
-
# count number of messages per user for every channel (total messages)
|
| 319 |
-
# fix sheet if necessary
|
| 320 |
|
| 321 |
-
# might need cell location data to pull both level and username at same time
|
| 322 |
-
# add emojis for some color
|
| 323 |
-
# check if members are still in the server
|
| 324 |
|
| 325 |
""""""
|
| 326 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
|
@@ -329,10 +208,6 @@ def run_bot():
|
|
| 329 |
threading.Thread(target=run_bot).start()
|
| 330 |
|
| 331 |
|
| 332 |
-
# csv
|
| 333 |
-
# read into pandas dataframe1
|
| 334 |
-
# read levels column and create pandas dataframe2 with first column containing levels from 2-max found in dataframe1
|
| 335 |
-
# create second column in dataframe2 for number of each level found in dataframe1 levels column
|
| 336 |
|
| 337 |
demo = gr.Blocks()
|
| 338 |
with demo:
|
|
|
|
| 66 |
#data.to_csv(csv_file, index=False)
|
| 67 |
|
| 68 |
|
| 69 |
+
async def update_google_sheet():
|
| 70 |
"""save data from HF Space -> google sheets (makes 1 API call)"""
|
| 71 |
print("test")
|
| 72 |
name = "levelbot"
|
|
|
|
| 89 |
def calculate_xp(level):
|
| 90 |
return (int(level ** 3))
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
async def add_exp(member_id):
|
| 94 |
try:
|
|
|
|
| 181 |
print(f"Gave {member} {lvl1}") # can log this better
|
| 182 |
|
| 183 |
if member_id == 811235357663297546:
|
| 184 |
+
await update_google_sheet()
|
| 185 |
except Exception as e:
|
| 186 |
print(f"add_exp Error: {e}")
|
| 187 |
|
|
|
|
| 198 |
except Exception as e:
|
| 199 |
print(f"on_message Error: {e}")
|
| 200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
|
|
|
|
|
|
|
|
|
| 202 |
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
""""""
|
| 205 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
|
|
|
| 208 |
threading.Thread(target=run_bot).start()
|
| 209 |
|
| 210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
demo = gr.Blocks()
|
| 213 |
with demo:
|