Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
async, try/excepts added
Browse files
app.py
CHANGED
@@ -55,32 +55,37 @@ data = pd.read_csv("https://docs.google.com/spreadsheets/d/1hQSsIg1Y9WdBF_CdNM1L
|
|
55 |
|
56 |
@bot.event
|
57 |
async def on_ready():
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
70 |
|
71 |
-
def update_google_sheet():
|
72 |
"""save data from HF Space -> google sheets (makes 1 API call)"""
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
84 |
|
85 |
"""
|
86 |
scheduler = BackgroundScheduler()
|
@@ -90,11 +95,11 @@ scheduler.start()
|
|
90 |
"""
|
91 |
|
92 |
|
93 |
-
def calculate_level(xp):
|
94 |
return int(xp ** (1.0 / 3.0))
|
95 |
|
96 |
|
97 |
-
def calculate_xp(level):
|
98 |
return (int(level ** 3))
|
99 |
|
100 |
|
@@ -198,7 +203,7 @@ async def add_exp(member_id):
|
|
198 |
|
199 |
|
200 |
# level up
|
201 |
-
current_level = calculate_level(new_xp)
|
202 |
print(f"Current_level for {member}: {current_level}")
|
203 |
if current_level >= 2 and current_level <=30:
|
204 |
current_role = lvls[current_level]
|
@@ -219,7 +224,7 @@ async def add_exp(member_id):
|
|
219 |
xp = 10 # define somewhere else?
|
220 |
member_id = str(member_id)
|
221 |
member_name = str(member.name)
|
222 |
-
current_level = calculate_level(xp)
|
223 |
row_data = [member_id, member_name, xp, current_level]
|
224 |
global_df.loc[len(global_df.index)] = row_data
|
225 |
|
@@ -235,7 +240,7 @@ async def add_exp(member_id):
|
|
235 |
print(f"----------------------------------------------------")
|
236 |
|
237 |
if member_id == 811235357663297546:
|
238 |
-
update_google_sheet()
|
239 |
except Exception as e:
|
240 |
print(f"add_exp Error: {e}")
|
241 |
|
@@ -266,64 +271,68 @@ async def on_reaction_add(reaction, user):
|
|
266 |
|
267 |
@bot.command(name='update_leaderboard')
|
268 |
async def update_leaderboard(ctx, num_results: int = 10): # needs rewrite
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
names_list = worksheet.col_values(2)[1:]
|
274 |
-
levels_list = worksheet.col_values(4)[1:]
|
275 |
-
exp_list = worksheet.col_values(3)[1:]
|
276 |
-
channel = bot.get_channel(1197143964994773023)
|
277 |
-
message = await channel.fetch_message(1197148293164187678)
|
278 |
-
|
279 |
-
# for 3 lists
|
280 |
-
combined_list = [list(sublist) for sublist in zip(names_list, levels_list, exp_list)]
|
281 |
-
combined_list = [[name, int(level), int(exp)] for name, level, exp in combined_list]
|
282 |
-
combined_list = sorted(combined_list, key=lambda x: x[1], reverse=True)
|
283 |
-
|
284 |
-
print(combined_list)
|
285 |
-
top_results = combined_list[:num_results]
|
286 |
-
#print(top_results)
|
287 |
-
|
288 |
-
"""
|
289 |
-
# get position, then find that value in updated_names_list
|
290 |
-
levels_list = list(map(int, levels_list))
|
291 |
-
data_pairs = list(zip(names_list, levels_list))
|
292 |
-
sorted_data_pairs = sorted(data_pairs, key=lambda x: x[1], reverse=True)
|
293 |
-
top_data_pairs = sorted_data_pairs[:num_results]
|
294 |
-
"""
|
295 |
-
|
296 |
-
# remove huggingfolks
|
297 |
-
guild = ctx.guild
|
298 |
-
role = discord.utils.get(guild.roles, id=897376942817419265)
|
299 |
-
if role is None:
|
300 |
-
await ctx.send("Role not found.")
|
301 |
-
return
|
302 |
-
members_with_role = [member.name for member in guild.members if role in member.roles]
|
303 |
-
top_results = [r for r in top_results if r[0] not in members_with_role]
|
304 |
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
321 |
|
322 |
@bot.command(name='xp_help')
|
323 |
async def xp_help(ctx):
|
324 |
-
|
325 |
-
|
326 |
-
|
|
|
|
|
327 |
|
328 |
|
329 |
|
@@ -360,10 +369,13 @@ csv_url = URL.replace('/edit#gid=', '/export?format=csv&gid=')
|
|
360 |
|
361 |
|
362 |
def get_data():
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
|
|
|
|
|
|
367 |
# csv
|
368 |
# read into pandas dataframe1
|
369 |
# read levels column and create pandas dataframe2 with first column containing levels from 2-max found in dataframe1
|
@@ -371,40 +383,42 @@ def get_data():
|
|
371 |
|
372 |
demo = gr.Blocks()
|
373 |
with demo:
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
with gr.
|
389 |
-
|
390 |
-
|
391 |
-
with gr.
|
392 |
-
gr.
|
393 |
-
|
394 |
-
|
395 |
-
gr.
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
|
|
|
|
408 |
demo.queue().launch()
|
409 |
|
410 |
|
|
|
55 |
|
56 |
@bot.event
|
57 |
async def on_ready():
|
58 |
+
try:
|
59 |
+
global global_df
|
60 |
+
print(f'Logged in as {bot.user.name}')
|
61 |
+
print(f"XP_PER_MESSAGE: {XP_PER_MESSAGE}")
|
62 |
+
# testing sheet -> read -> paste sheet
|
63 |
+
|
64 |
+
"""import data from google sheets -> HF Space .csv (doesn't make API call this way, as it's read-only)"""
|
65 |
+
|
66 |
+
data['discord_user_id'] = data['discord_user_id'].astype(str)
|
67 |
+
global_df = data
|
68 |
+
print(f"csv successfully retrieved: \n {global_df}")
|
69 |
+
#data.to_csv(csv_file, index=False)
|
70 |
+
except Exception as e:
|
71 |
+
print(f"on_message Error: {e}")
|
72 |
|
73 |
+
async def update_google_sheet():
|
74 |
"""save data from HF Space -> google sheets (makes 1 API call)"""
|
75 |
+
try:
|
76 |
+
print("test")
|
77 |
+
name = "levelbot"
|
78 |
+
worksheet = gspread_bot.open(name).sheet1
|
79 |
+
global_df['discord_user_id'] = global_df['discord_user_id'].astype(str)
|
80 |
+
set_with_dataframe(worksheet, global_df)
|
81 |
+
worksheet.format(f"A1:A{len(global_df.index)}", {
|
82 |
+
"numberFormat": {
|
83 |
+
"type": 'TEXT',
|
84 |
+
},
|
85 |
+
})
|
86 |
+
print({f"Google sheet {name} successfully updated! \n {global_df}"})
|
87 |
+
except Exception as e:
|
88 |
+
print(f"on_message Error: {e}")
|
89 |
|
90 |
"""
|
91 |
scheduler = BackgroundScheduler()
|
|
|
95 |
"""
|
96 |
|
97 |
|
98 |
+
async def calculate_level(xp):
|
99 |
return int(xp ** (1.0 / 3.0))
|
100 |
|
101 |
|
102 |
+
async def calculate_xp(level):
|
103 |
return (int(level ** 3))
|
104 |
|
105 |
|
|
|
203 |
|
204 |
|
205 |
# level up
|
206 |
+
current_level = await calculate_level(new_xp)
|
207 |
print(f"Current_level for {member}: {current_level}")
|
208 |
if current_level >= 2 and current_level <=30:
|
209 |
current_role = lvls[current_level]
|
|
|
224 |
xp = 10 # define somewhere else?
|
225 |
member_id = str(member_id)
|
226 |
member_name = str(member.name)
|
227 |
+
current_level = await calculate_level(xp)
|
228 |
row_data = [member_id, member_name, xp, current_level]
|
229 |
global_df.loc[len(global_df.index)] = row_data
|
230 |
|
|
|
240 |
print(f"----------------------------------------------------")
|
241 |
|
242 |
if member_id == 811235357663297546:
|
243 |
+
await update_google_sheet()
|
244 |
except Exception as e:
|
245 |
print(f"add_exp Error: {e}")
|
246 |
|
|
|
271 |
|
272 |
@bot.command(name='update_leaderboard')
|
273 |
async def update_leaderboard(ctx, num_results: int = 10): # needs rewrite
|
274 |
+
try:
|
275 |
+
if ctx.author.id == 811235357663297546:
|
276 |
+
await asyncio.sleep(1)
|
277 |
+
worksheet = gspread_bot.open("levelbot").sheet1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
|
279 |
+
names_list = worksheet.col_values(2)[1:]
|
280 |
+
levels_list = worksheet.col_values(4)[1:]
|
281 |
+
exp_list = worksheet.col_values(3)[1:]
|
282 |
+
channel = bot.get_channel(1197143964994773023)
|
283 |
+
message = await channel.fetch_message(1197148293164187678)
|
284 |
+
|
285 |
+
# for 3 lists
|
286 |
+
combined_list = [list(sublist) for sublist in zip(names_list, levels_list, exp_list)]
|
287 |
+
combined_list = [[name, int(level), int(exp)] for name, level, exp in combined_list]
|
288 |
+
combined_list = sorted(combined_list, key=lambda x: x[1], reverse=True)
|
289 |
+
|
290 |
+
print(combined_list)
|
291 |
+
top_results = combined_list[:num_results]
|
292 |
+
#print(top_results)
|
293 |
+
|
294 |
+
"""
|
295 |
+
# get position, then find that value in updated_names_list
|
296 |
+
levels_list = list(map(int, levels_list))
|
297 |
+
data_pairs = list(zip(names_list, levels_list))
|
298 |
+
sorted_data_pairs = sorted(data_pairs, key=lambda x: x[1], reverse=True)
|
299 |
+
top_data_pairs = sorted_data_pairs[:num_results]
|
300 |
+
"""
|
301 |
+
|
302 |
+
# remove huggingfolks
|
303 |
+
guild = ctx.guild
|
304 |
+
role = discord.utils.get(guild.roles, id=897376942817419265)
|
305 |
+
if role is None:
|
306 |
+
await ctx.send("Role not found.")
|
307 |
+
return
|
308 |
+
members_with_role = [member.name for member in guild.members if role in member.roles]
|
309 |
+
top_results = [r for r in top_results if r[0] not in members_with_role]
|
310 |
+
|
311 |
+
for name, level, xp in top_results:
|
312 |
+
print(f"Name: {name}, Level: {level}, Exp: {xp}")
|
313 |
+
|
314 |
+
|
315 |
+
def xp_required_to_next_level(current_level, current_xp):
|
316 |
+
level_floor_xp = await calculate_xp(current_level)
|
317 |
+
level_ceiling_xp = await calculate_xp(current_level+1)
|
318 |
+
xp_to_level_up = level_ceiling_xp - current_xp
|
319 |
+
return (xp_to_level_up)
|
320 |
+
|
321 |
+
|
322 |
+
# put into message / leaderboard
|
323 |
+
new_leaderboard_data = [(name, level, str(xp_required_to_next_level(int(level), int(xp)))) for name, level, xp in top_results]
|
324 |
+
new_table = tabulate(new_leaderboard_data, headers=["Name", "Level", "XP to level up"], tablefmt="plain")
|
325 |
+
await message.edit(content=f"Updated Leaderboard:\n```\n{new_table}\n```")
|
326 |
+
except Exception as e:
|
327 |
+
print(f"on_message Error: {e}")
|
328 |
|
329 |
@bot.command(name='xp_help')
|
330 |
async def xp_help(ctx):
|
331 |
+
try:
|
332 |
+
help_message = "How to earn Discord / Hub exp: Post messages, react, Like, discuss, create repos and papers"
|
333 |
+
await ctx.author.send(help_message)
|
334 |
+
except Exception as e:
|
335 |
+
print(f"on_message Error: {e}")
|
336 |
|
337 |
|
338 |
|
|
|
369 |
|
370 |
|
371 |
def get_data():
|
372 |
+
try:
|
373 |
+
data = pd.read_csv(csv_url)
|
374 |
+
first_3_columns = data.iloc[:, 1:4]
|
375 |
+
first_3_columns.to_csv('first_3_columns.csv', index=False)
|
376 |
+
return first_3_columns
|
377 |
+
except Exception as e:
|
378 |
+
print(f"on_message Error: {e}")
|
379 |
# csv
|
380 |
# read into pandas dataframe1
|
381 |
# read levels column and create pandas dataframe2 with first column containing levels from 2-max found in dataframe1
|
|
|
383 |
|
384 |
demo = gr.Blocks()
|
385 |
with demo:
|
386 |
+
try:
|
387 |
+
dataframe1 = pd.read_csv(csv_url)
|
388 |
+
column_values_unique = sorted(dataframe1.iloc[:, 3].unique())
|
389 |
+
dataframe2 = pd.DataFrame({'Levels': column_values_unique})
|
390 |
+
counts = {}
|
391 |
+
for value in data.iloc[:, 3]:
|
392 |
+
counts[value] = counts.get(value, 0) + 1
|
393 |
+
dataframe2['Members'] = dataframe2['Levels'].map(counts)
|
394 |
+
|
395 |
+
print("\nDataframe 2:")
|
396 |
+
print(dataframe2)
|
397 |
+
|
398 |
+
TITLE = """<h1 align="center" id="space-title">π€ Hugging Face Level Leaderboard</h1>"""
|
399 |
+
gr.HTML(TITLE)
|
400 |
+
with gr.Tabs(elem_classes="tab-buttons") as tabs:
|
401 |
+
with gr.TabItem("π
Level leaderboard", elem_id="level-table", id=0):
|
402 |
+
#gr.Markdown("# π Experience Leaderboard")
|
403 |
+
with gr.Row():
|
404 |
+
with gr.Column():
|
405 |
+
gr.DataFrame(get_data, every=5, height=500, interactive=False, col_count=(3, "fixed"), column_widths=["100px","100px","100px"])
|
406 |
+
|
407 |
+
with gr.Column():
|
408 |
+
gr.BarPlot(
|
409 |
+
value=dataframe2,
|
410 |
+
x="Levels",
|
411 |
+
y="Members",
|
412 |
+
title="Level Distribution",
|
413 |
+
height=450,
|
414 |
+
width=450,
|
415 |
+
interactive=False
|
416 |
+
)
|
417 |
+
#with gr.TabItem("π Members of the Week", elem_id="week-table", id=1):
|
418 |
+
|
419 |
+
#with gr.TabItem("π Hub-only leaderboard", elem_id="hub-table", id=2):
|
420 |
+
except Exception as e:
|
421 |
+
print(f"on_message Error: {e}")
|
422 |
demo.queue().launch()
|
423 |
|
424 |
|