Spaces:
Running
Running
import gradio as gr | |
import os | |
import threading | |
from urllib.parse import urlparse, parse_qs | |
import discord | |
from discord.ext import commands | |
# discord bot ----------------------------------------------------------------------------------------------- | |
intents = discord.Intents.all() | |
bot = commands.Bot(command_prefix="!", intents=intents) | |
GRADIO_APP_URL = "https://lunarflu-gradio-oauth2" | |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None) | |
async def on_ready(): | |
print(f'Logged in as {bot.user}') | |
async def sendlink(ctx, user: discord.User): | |
if ctx.author.id == 811235357663297546: | |
unique_link = f"{GRADIO_APP_URL}?user_id={user.id}" | |
await user.send(f"Click the link to sign in with Hugging Face: {unique_link}") | |
def run_bot(): | |
bot.run(DISCORD_TOKEN) | |
threading.Thread(target=run_bot).start() | |
#gradio------------------------------------------------------------------------------------------------------------ | |
def hello(profile: gr.OAuthProfile | None, request: gr.Request) -> str: | |
url_str = str(request.url) | |
query_params = parse_qs(urlparse(url_str).query) | |
user_id = query_params.get('user_id', [None])[0] | |
if profile is None: | |
return f"❌ Not logged in. User ID: {user_id}" | |
return f"✅ Successfully logged in as {profile.username}. User ID: {user_id}" | |
with gr.Blocks() as demo: | |
gr.Markdown( | |
"# Gradio OAuth Space" | |
) | |
gr.LoginButton() | |
# ^ add a login button to the Space | |
m1 = gr.Markdown() | |
demo.load(hello, inputs=None, outputs=m1) | |
demo.launch() | |