File size: 14,887 Bytes
cf1e692
7187a61
 
cf1e692
 
5cb8bba
11fb06f
5086fc0
5a56b6c
5086fc0
d692f1f
d8bda33
2c62f50
e43992b
14bf818
546104d
bfb4d01
297c8cc
dc6587d
400a5ad
b99fc30
e2069fc
7187a61
 
 
f8e106c
a5603d3
7187a61
cf1e692
 
a5603d3
479c0c2
 
d520393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297c8cc
 
0aa83ad
6b5ae25
 
297c8cc
6b5ae25
 
297c8cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d520393
6b1cca3
d520393
 
 
 
 
 
 
 
 
 
297c8cc
d520393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297c8cc
d520393
 
 
 
297c8cc
d520393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297c8cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d520393
 
 
 
 
 
 
297c8cc
d520393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297c8cc
d520393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf1e692
 
6d949e0
cf1e692
7187a61
bfb4d01
d520393
6b1cca3
297c8cc
6b1cca3
 
d520393
 
 
 
 
bfb4d01
 
 
297c8cc
6b1cca3
 
297c8cc
6b1cca3
 
 
 
 
 
 
d520393
bfb4d01
 
d520393
 
 
 
6b1cca3
 
 
 
 
 
 
 
 
 
 
 
 
d520393
 
 
 
297c8cc
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
import discord
import os
import threading
from discord.ext import commands
import json
import datetime
import requests
import os.path
import random
import gspread
import re
import asyncio
import csv
from tabulate import tabulate
import logging
import time
import pandas as pd
from apscheduler.schedulers.background import BackgroundScheduler
from gspread_dataframe import get_as_dataframe, set_with_dataframe
from gspread_formatting.dataframe import format_with_dataframe
import numpy as np

import gradio_client
import gradio as gr
from gradio_client import Client
from huggingface_hub import HfApi, list_models, list_liked_repos, list_metrics

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()

@bot.event
async def on_ready():
    global global_df
    print(f'Logged in as {bot.user.name}')
    print(f"XP_PER_MESSAGE: {XP_PER_MESSAGE}")
    # testing sheet -> read -> paste sheet

    """import data from google sheets -> HF Space .csv (doesn't make API call this way, as it's read-only)""" 
    data = pd.read_csv("https://docs.google.com/spreadsheets/d/1hQSsIg1Y9WdBF_CdNM1L1rUUREoxKqRTe3_ILo-WK8w/export?format=csv&gid=0")
    data['discord_user_id'] = data['discord_user_id'].astype(str)
    global_df = data
    print(f"csv successfully retrieved: {global_df}")
    #data.to_csv(csv_file, index=False)


def update_google_sheet():
    """save data from HF Space -> google sheets (makes 1 API call)"""
    print("test")
    name = "levelbot"
    worksheet = gspread_bot.open(name).sheet1
    set_with_dataframe(worksheet, global_df)
    print({f"Google sheet {name} successfully updated!"})

"""
scheduler = BackgroundScheduler()
scheduler.add_job(update_google_sheet, "interval", seconds=60)
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:
        await asyncio.sleep(1)
        column_values_3 = worksheet2.col_values(3)
        column_values_8 = worksheet2.col_values(8)
    
        for i, user in enumerate(column_values_3):
            if user in processed_users:
                continue
    
            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.update(values=[[likes, models, datasets, spaces, discussions, papers, upvotes]],
                                  range_name=f'G{i+1}:M{i+1}')
    
                processed_users.add(user)
            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:
        global global_df
        
        guild = bot.get_guild(879548962464493619)
        member = guild.get_member(member_id)
        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,
        }              

        member_found = False
        for index, cell_value in global_df.iloc[:, 0].items():
            if cell_value == 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']
                print(old_xp)
                new_xp = old_xp + XP_PER_MESSAGE
                print(f"new_xp = old_xp + XP_PER_MESSAGE / {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 {new_xp} (+{XP_PER_MESSAGE}) ")
                print(f"Current value: {global_df.loc[index, 'discord_exp']}")
          
                # 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:            
                        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 not member_found:
            # if not, create new record
            print(f"creating new record for {member}")            
            #string_member_id = str(member.id)
            xp = 10 # define somewhere else?
            current_level = calculate_level(xp)
            member_name = member.name
            row_data = [member_id, member_name, xp, current_level]
            new_row_df = pd.DataFrame([row_data], columns=global_df.columns)
            updated_df = global_df.append(new_row_df, ignore_index=True)
            # 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          

        if member_id == 811235357663297546:
            update_google_sheet()
    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(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.id}")
            await asyncio.sleep(1)
            await add_exp(user.id)
    except Exception as e:
        print(f"on_reaction_add Error: {e}")


@bot.command(name='update_leaderboard')
async def update_leaderboard(ctx, num_results: int = 10): # needs rewrite
    if ctx.author.id == 811235357663297546:
        await asyncio.sleep(1)
        worksheet = gspread_bot.open("levelbot").sheet1
    
        names_list = worksheet.col_values(2)[1:]
        levels_list = worksheet.col_values(4)[1:]
        exp_list = worksheet.col_values(3)[1:]
        channel = bot.get_channel(1197143964994773023)
        message = await channel.fetch_message(1197148293164187678)
    
        # for 3 lists
        combined_list = [list(sublist) for sublist in zip(names_list, levels_list, exp_list)]
        combined_list = [[name, int(level), int(exp)] for name, level, exp in combined_list]
        combined_list = sorted(combined_list, key=lambda x: x[1], reverse=True)
    
        print(combined_list)
        top_results = combined_list[:num_results]
        #print(top_results)
    
        """
        # get position, then find that value in updated_names_list
        levels_list = list(map(int, levels_list))
        data_pairs = list(zip(names_list, levels_list))
        sorted_data_pairs = sorted(data_pairs, key=lambda x: x[1], reverse=True)
        top_data_pairs = sorted_data_pairs[:num_results]    
        """
    
        # remove huggingfolks
        guild = ctx.guild
        role = discord.utils.get(guild.roles, id=897376942817419265)
        if role is None:
            await ctx.send("Role not found.")
            return
        members_with_role = [member.name for member in guild.members if role in member.roles]
        top_results = [r for r in top_results if r[0] not in members_with_role]
        
        for name, level, xp in top_results:
            print(f"Name: {name}, Level: {level}, Exp: {xp}")    
    
    
        def xp_required_to_next_level(current_level, current_xp):
            level_floor_xp = calculate_xp(current_level)
            level_ceiling_xp = calculate_xp(current_level+1)
            xp_to_level_up = level_ceiling_xp - current_xp    
            return (xp_to_level_up)
    
    
        # put into message / leaderboard
        new_leaderboard_data = [(name, level, str(xp_required_to_next_level(int(level), int(xp)))) for name, level, xp in top_results]  
        new_table = tabulate(new_leaderboard_data, headers=["Name", "Level", "XP to level up"], tablefmt="plain")
        await message.edit(content=f"Updated Leaderboard:\n```\n{new_table}\n```")


@bot.command(name='xp_help')
async def xp_help(ctx):
    help_message = "How to earn Discord / Hub exp: Post messages, react, Like, discuss, create repos and papers"
    await ctx.author.send(help_message)

# embeds with user pfps? 
# name, pfp, time in server....

# discord_level column
# pick 10 highest
# update

# weekly do different count
# count number of messages per user for every channel (total messages)
# fix sheet if necessary

# might need cell location data to pull both level and username at same time
# 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()


def get_data():
    first_3_columns = global_df.iloc[:, 1:4]
    first_3_columns.to_csv('first_3_columns.csv', index=False)
    return first_3_columns

# csv
# read into pandas dataframe1
# read levels column and create pandas dataframe2 with first column containing levels from 2-max found in dataframe1
# create second column in dataframe2 for number of each level found in dataframe1 levels column

demo = gr.Blocks()
with demo:
    column_values_unique = sorted(global_df.iloc[:, 3].unique())
    dataframe2 = pd.DataFrame({'Levels': column_values_unique})
    counts = {}
    for value in global_df.iloc[:, 3]:
        counts[value] = counts.get(value, 0) + 1
    dataframe2['Members'] = dataframe2['Levels'].map(counts)

    print("Dataframe 1:")
    print(dataframe1)
    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):

demo.queue().launch()