Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
reorganize gradio demo / thread running discord bot (RuntimeError: cannot schedule new futures after shutdown)
Browse files
app.py
CHANGED
@@ -66,7 +66,7 @@ async def on_ready():
|
|
66 |
#data.to_csv(csv_file, index=False)
|
67 |
|
68 |
|
69 |
-
|
70 |
"""save data from HF Space -> google sheets (makes 1 API call)"""
|
71 |
print("test")
|
72 |
name = "levelbot"
|
@@ -74,6 +74,13 @@ async def update_google_sheet():
|
|
74 |
set_with_dataframe(worksheet, global_df)
|
75 |
print({f"Google sheet {name} successfully updated!"})
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
def calculate_level(xp):
|
79 |
return int(xp ** (1.0 / 3.0))
|
@@ -82,6 +89,43 @@ def calculate_level(xp):
|
|
82 |
def calculate_xp(level):
|
83 |
return (int(level ** 3))
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
async def add_exp(member_id):
|
87 |
try:
|
@@ -174,7 +218,7 @@ async def add_exp(member_id):
|
|
174 |
print(f"Gave {member} {lvl1}") # can log this better
|
175 |
|
176 |
if member_id == 811235357663297546:
|
177 |
-
|
178 |
except Exception as e:
|
179 |
print(f"add_exp Error: {e}")
|
180 |
|
@@ -191,16 +235,147 @@ async def on_message(message):
|
|
191 |
except Exception as e:
|
192 |
print(f"on_message Error: {e}")
|
193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
|
195 |
""""""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
demo = gr.Blocks()
|
197 |
with demo:
|
198 |
-
|
199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
|
201 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
202 |
def run_bot():
|
203 |
bot.run(DISCORD_TOKEN)
|
204 |
threading.Thread(target=run_bot).start()
|
205 |
|
206 |
-
|
|
|
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"
|
|
|
74 |
set_with_dataframe(worksheet, global_df)
|
75 |
print({f"Google sheet {name} successfully updated!"})
|
76 |
|
77 |
+
"""
|
78 |
+
scheduler = BackgroundScheduler()
|
79 |
+
scheduler.add_job(update_google_sheet, "interval", seconds=60)
|
80 |
+
scheduler.start()
|
81 |
+
|
82 |
+
"""
|
83 |
+
|
84 |
|
85 |
def calculate_level(xp):
|
86 |
return int(xp ** (1.0 / 3.0))
|
|
|
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 |
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 |
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 |
+
def get_data():
|
327 |
+
first_3_columns = global_df.iloc[:, 1:4]
|
328 |
+
first_3_columns.to_csv('first_3_columns.csv', index=False)
|
329 |
+
return first_3_columns
|
330 |
+
|
331 |
+
# csv
|
332 |
+
# read into pandas dataframe1
|
333 |
+
# read levels column and create pandas dataframe2 with first column containing levels from 2-max found in dataframe1
|
334 |
+
# create second column in dataframe2 for number of each level found in dataframe1 levels column
|
335 |
+
|
336 |
demo = gr.Blocks()
|
337 |
with demo:
|
338 |
+
column_values_unique = sorted(global_df.iloc[:, 3].unique())
|
339 |
+
dataframe2 = pd.DataFrame({'Levels': column_values_unique})
|
340 |
+
counts = {}
|
341 |
+
for value in global_df.iloc[:, 3]:
|
342 |
+
counts[value] = counts.get(value, 0) + 1
|
343 |
+
dataframe2['Members'] = dataframe2['Levels'].map(counts)
|
344 |
+
|
345 |
+
print("Dataframe 1:")
|
346 |
+
print(dataframe1)
|
347 |
+
print("\nDataframe 2:")
|
348 |
+
print(dataframe2)
|
349 |
+
|
350 |
+
TITLE = """<h1 align="center" id="space-title">π€ Hugging Face Level Leaderboard</h1>"""
|
351 |
+
gr.HTML(TITLE)
|
352 |
+
with gr.Tabs(elem_classes="tab-buttons") as tabs:
|
353 |
+
with gr.TabItem("π
Level leaderboard", elem_id="level-table", id=0):
|
354 |
+
#gr.Markdown("# π Experience Leaderboard")
|
355 |
+
with gr.Row():
|
356 |
+
with gr.Column():
|
357 |
+
gr.DataFrame(get_data, every=5, height=500, interactive=False, col_count=(3, "fixed"), column_widths=["100px","100px","100px"])
|
358 |
+
|
359 |
+
with gr.Column():
|
360 |
+
gr.BarPlot(
|
361 |
+
value=dataframe2,
|
362 |
+
x="Levels",
|
363 |
+
y="Members",
|
364 |
+
title="Level Distribution",
|
365 |
+
height=450,
|
366 |
+
width=450,
|
367 |
+
interactive=False
|
368 |
+
)
|
369 |
+
#with gr.TabItem("π Members of the Week", elem_id="week-table", id=1):
|
370 |
+
|
371 |
+
#with gr.TabItem("π Hub-only leaderboard", elem_id="hub-table", id=2):
|
372 |
+
|
373 |
+
demo.queue().launch()
|
374 |
+
|
375 |
|
376 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
377 |
def run_bot():
|
378 |
bot.run(DISCORD_TOKEN)
|
379 |
threading.Thread(target=run_bot).start()
|
380 |
|
381 |
+
|