File size: 2,866 Bytes
434f798
30462e3
1f188d9
 
c87648d
1f188d9
b0918e1
1f188d9
8dd1108
1f188d9
 
f6602f2
1f188d9
 
 
 
d522acf
1f188d9
 
 
 
 
 
 
 
 
 
 
 
 
ad9d692
1f188d9
 
 
 
def7a78
c6565e2
773ec73
d522acf
773ec73
78dea1a
b7eca85
d522acf
09d6ac2
06157f0
 
 
 
d522acf
 
 
 
b80bf81
78dea1a
 
d522acf
61513be
d522acf
2a906f6
a56363d
ae4f533
 
 
2a906f6
5e34c0f
 
 
 
 
 
 
 
 
 
 
 
30462e3
5e34c0f
 
0c6f218
5e34c0f
 
 
0c6f218
5e34c0f
d522acf
ae4f533
 
c87648d
d522acf
 
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
import os
import io
import re
import csv
import sys
import json
import time
import random
import asyncio
import discord
import logging
import aiohttp
import gspread
import datetime
import requests
import threading
import schedule
import gradio_client

import numpy as np
import pandas as pd
import gradio as gr
import plotly.graph_objects as go

from tabulate import tabulate
from requests import HTTPError
from gradio_client import Client
from discord import Color, Embed
from discord.ui import Button, View
from discord.ext import commands, tasks
from datetime import datetime, timedelta
from urllib.parse import urlparse, parse_qs
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from gspread_formatting.dataframe import format_with_dataframe
from gspread_dataframe import get_as_dataframe, set_with_dataframe
from huggingface_hub import HfApi, list_liked_repos, list_models

DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)

intents = discord.Intents.all() 
bot = commands.Bot(command_prefix='!', intents=intents)

scheduler = AsyncIOScheduler()

def restart_bot():
    print("Restarting bot...")
    os.execv(sys.executable, ['python'] + sys.argv)

@scheduler.scheduled_job('interval', minutes=20)
def periodic_restart():
    print("Scheduled restart triggered...")
    restart_bot()

@bot.event
async def on_ready():
    """Import data from Google Sheets -> HF Space df"""
    print(f"We have logged in as {bot.user}")
    scheduler.start()
    await give_verified_roles()

async def give_verified_roles():
    while True:
        try:
            global_df = pd.DataFrame()

            async with aiohttp.ClientSession() as session:
                try:
                    async with session.get(
                        "https://docs.google.com/spreadsheets/d/1C8aLqgCqLYcMiIFf-P_Aosaa03C_WLIB_UyqvjSdWg8/export?format=csv&gid=0",
                        timeout=10
                    ) as response:
                        if response.status != 200:
                            print(f"Failed to fetch CSV: HTTP {response.status}")
                            await asyncio.sleep(60)
                            continue
                        csv_data = await response.text()
                        global_df = pd.read_csv(io.StringIO(csv_data))
                except asyncio.TimeoutError:
                    print("CSV fetch timed out.")
                    await asyncio.sleep(60)
                    continue
                except Exception as e:
                    print(f"Error fetching CSV: {e}")
                    await asyncio.sleep(60)
                    continue

            guild = bot.get_guild(879548962464493619)
            role = guild.get_role(900063512829755413)

            # Define the invite message
            org_link = "https://huggingface.co/organizations/discord-community/share/wPKRAHYbAlaEaCxUxcq