Spaces:
Running on CPU Upgrade

lunarflu HF Staff commited on
Commit
39a3bfb
·
verified ·
1 Parent(s): 8e267f5

start migrating to HF datasets and away from google sheets

Browse files
Files changed (1) hide show
  1. app.py +22 -0
app.py CHANGED
@@ -11,6 +11,7 @@ import os.path
11
  import secrets
12
  import gspread
13
  import datetime
 
14
  import requests
15
  import threading
16
  import gradio_client
@@ -28,6 +29,8 @@ from discord.ui import Button, View
28
  from discord.ext import commands, tasks
29
  from datetime import datetime, timedelta
30
  from urllib.parse import urlparse, parse_qs
 
 
31
  from apscheduler.executors.pool import ThreadPoolExecutor
32
  from apscheduler.executors.asyncio import AsyncIOExecutor
33
  from apscheduler.schedulers.asyncio import AsyncIOScheduler
@@ -67,6 +70,8 @@ community_global_df_with_id = pd.DataFrame()
67
  community_global_df_gradio = pd.DataFrame()
68
  test_merge = pd.read_csv("https://docs.google.com/spreadsheets/d/1C8aLqgCqLYcMiIFf-P_Aosaa03C_WLIB_UyqvjSdWg8/export?format=csv&gid=0")
69
 
 
 
70
 
71
  class DMButton(Button):
72
  def __init__(self, label, style):
@@ -135,6 +140,7 @@ def update_google_sheet():
135
  print("------------------------------------------------------------------------")
136
  name = "test_merge_worksheet"
137
  set_with_dataframe(test_merge_worksheet, global_df)
 
138
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
139
  print("------------------------------------------------------------------------")
140
  print(f"Google sheet {name} {test_merge_worksheet} successfully updated at {timestamp}! \n{global_df}")
@@ -143,6 +149,22 @@ def update_google_sheet():
143
  print(f"update_google_sheet Error: {e}")
144
 
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  #@tasks.loop(minutes=1) tasks.loop leads to heartbeat blocked issues (merging calculations too much with normal discord bot functions)
147
  def update_hub_stats():
148
  try:
 
11
  import secrets
12
  import gspread
13
  import datetime
14
+ import tempfile
15
  import requests
16
  import threading
17
  import gradio_client
 
29
  from discord.ext import commands, tasks
30
  from datetime import datetime, timedelta
31
  from urllib.parse import urlparse, parse_qs
32
+ # starting to migrate to HF datasets and away from google sheets
33
+ from huggingface_hub import HfApi, HfFolder, Repository
34
  from apscheduler.executors.pool import ThreadPoolExecutor
35
  from apscheduler.executors.asyncio import AsyncIOExecutor
36
  from apscheduler.schedulers.asyncio import AsyncIOScheduler
 
70
  community_global_df_gradio = pd.DataFrame()
71
  test_merge = pd.read_csv("https://docs.google.com/spreadsheets/d/1C8aLqgCqLYcMiIFf-P_Aosaa03C_WLIB_UyqvjSdWg8/export?format=csv&gid=0")
72
 
73
+ hf_token = os.environ.get("HF_TOKEN")
74
+
75
 
76
  class DMButton(Button):
77
  def __init__(self, label, style):
 
140
  print("------------------------------------------------------------------------")
141
  name = "test_merge_worksheet"
142
  set_with_dataframe(test_merge_worksheet, global_df)
143
+ update_hf_dataset(global_df) # starting to migrate away from google sheets and towards HF datasets
144
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
145
  print("------------------------------------------------------------------------")
146
  print(f"Google sheet {name} {test_merge_worksheet} successfully updated at {timestamp}! \n{global_df}")
 
149
  print(f"update_google_sheet Error: {e}")
150
 
151
 
152
+ # starting to migrate away from google sheets and towards HF datasets
153
+ def update_hf_dataset(df, repo_id="discord-community/levelbot-data", filename="levelbot-data.csv"):
154
+ try:
155
+ with tempfile.TemporaryDirectory() as tempdir:
156
+ file_path = os.path.join(tempdir, filename)
157
+ df.to_csv(file_path, index=False)
158
+
159
+ repo = Repository(local_dir=tempdir, clone_from=repo_id, use_auth_token=hf_token)
160
+ repo.git_add()
161
+ repo.git_commit("Update leaderboard CSV")
162
+ repo.git_push()
163
+ print(f"Hugging Face dataset {repo_id}/{filename} updated successfully.")
164
+ except Exception as e:
165
+ print(f"update_hf_dataset Error: {e}")
166
+
167
+
168
  #@tasks.loop(minutes=1) tasks.loop leads to heartbeat blocked issues (merging calculations too much with normal discord bot functions)
169
  def update_hub_stats():
170
  try: