Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 8,853 Bytes
cf1e692 7187a61 cf1e692 5cb8bba 11fb06f 5086fc0 5a56b6c 5086fc0 d692f1f d8bda33 d692f1f 6d949e0 7187a61 f8e106c a5603d3 7187a61 cf1e692 a5603d3 6c5f294 cf1e692 80a1e02 cf1e692 5086fc0 7aa3ae0 5086fc0 458a357 ac98ac7 f8e106c 0aa83ad 6b5ae25 88c0b0c 458a357 ac98ac7 ce27741 f8e106c 66e82df f8e106c 60a6d40 f8e106c 8f2e17f 4783d56 8f2e17f 172ae87 f8e106c 66e82df 735e3a6 69f6ffd 66e82df fafa626 69f6ffd 66e82df 69f6ffd e27b336 69f6ffd fafa626 66e82df 0c69f93 46e8d58 66e82df 0c69f93 66e82df 735e3a6 66e82df f8e106c 38302f8 4d17bea 458a357 f8e106c 4d17bea 458a357 91f6a94 458a357 91f6a94 458a357 4d17bea 458a357 4d17bea 458a357 0040dad d870674 458a357 91f6a94 458a357 d3bb60f 458a357 a5cef39 0122efc d3bb60f a5cef39 458a357 0040dad 458a357 4d17bea f8e106c 20630d7 458a357 0040dad 458a357 f8e106c 458a357 680ad08 cf1e692 6d949e0 cf1e692 7187a61 cf1e692 40b2a54 |
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 |
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 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)
""""""
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()
""""""
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
print(f"XP_PER_MESSAGE: {XP_PER_MESSAGE}")
def calculate_level(xp):
return int(xp ** (1.0 / 3.0))
def calculate_xp(level):
return (int(level ** 3))
# use a command
# check xp record presence (done in add_exp)
# check discord_user_id is verified
# do add_exp for hub?
"""
@bot.command(name='add_exp_hub')
async def add_exp_hub(ctx):
try:
column_values_7 = worksheet2.col_values(7)
column_values_3 = worksheet2.col_values(3)
for i, value in enumerate(column_values_7):
if not value:
print(f"cell empty, updating with likes")
hf_user_name = column_values_3[i]
print(f"hf_user_name = {hf_user_name}")
try:
likes = list_liked_repos(f"{hf_user_name}")
hf_likes_new = likes.total
print(f"hf_likes_new = {hf_likes_new}")
worksheet2.update(f'G{i+1}', f'{hf_likes_new}')
except Exception as e:
print(f"list_liked_repos Error: {e}")
except Exception as e:
print(f"add_exp_hub Error: {e}")
"""
"""
@bot.command(name='api_test')
async def api_test(ctx):
while True:
await periodic_api_test()
await asyncio.sleep(60) # Sleep for 60 seconds
"""
processed_users = set()
async def periodic_api_test():
try:
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}")
"""
@bot.command(name='get_cell_value')
async def get_cell_value(ctx, row, col):
cell = worksheet2.cell(row, col)
print(cell.value)
"""
"""
@bot.command(name='check')
async def check(ctx):
try:
column_values_7 = worksheet2.col_values(7)
column_values_3 = worksheet2.col_values(3)
for i, value in enumerate(column_values_7):
print(i)
print(value)
except Exception as e:
print(f"add_exp_hub Error: {e}")
"""
async def add_exp(member_id):
try:
print(f"1")
guild = bot.get_guild(879548962464493619)
member = guild.get_member(member_id)
print(f" {member}")
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)
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,
}
print(f"2")
#if member.id == 811235357663297546:
# does a record already exist?
cell = worksheet.find(str(member.id))
length = len(worksheet.col_values(1))
print(f"3")
if cell is None:
print(f"creating new record for {member}")
# if not, create new record
string_member_id = str(member.id)
xp = 10
current_level = calculate_level(xp)
member_name = member.name
worksheet.update(values=[[string_member_id, member_name, xp, current_level]], range_name=f'A{length+1}:D{length+1}')
# initial role assignment
if current_level == 1:
if lvl1 not in member.roles:
await member.add_roles(lvl1)
print(f"Gave {member} {lvl1}")
else:
print(f"4")
if cell:
# if so, update that row...
xp = worksheet.cell(cell.row, cell.col+2).value
xp = int(xp) + XP_PER_MESSAGE
current_level = calculate_level(xp)
print(f"updating record for {member}: {xp} xp")
# write with added xp
worksheet.update(values=[[xp, current_level]], range_name=f'C{cell.row}:D{cell.row}')
# level up
if current_level >= 2 and current_level <=20:
current_role = lvls[current_level]
if current_role not in member.roles:
await member.add_roles(current_role)
print(f"Giving {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:
await member.send(f"Level up! {current_level-1} -> {current_level}!")
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:
print(f"adding exp from message {message.author}")
await add_exp(message.author.id)
#await periodic_api_test() # run this ~1 per day otherwise will lead to issues
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:
await add_exp(user.id)
except Exception as e:
print(f"on_reaction_add Error: {e}")
""""""
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
def run_bot():
bot.run(DISCORD_TOKEN)
threading.Thread(target=run_bot).start()
def greet(name):
return "Hello " + name + "!"
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch()
|