Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,33 +5,27 @@ from urllib.parse import urlparse, parse_qs
|
|
5 |
import discord
|
6 |
from discord.ext import commands
|
7 |
|
8 |
-
|
9 |
-
|
10 |
# discord bot -----------------------------------------------------------------------------------------------
|
11 |
intents = discord.Intents.all()
|
12 |
bot = commands.Bot(command_prefix="!", intents=intents)
|
13 |
GRADIO_APP_URL = "https://lunarflu-gradio-oauth2"
|
14 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
15 |
|
16 |
-
|
17 |
@bot.event
|
18 |
async def on_ready():
|
19 |
print(f'Logged in as {bot.user}')
|
20 |
|
21 |
-
|
22 |
@bot.command()
|
23 |
async def sendlink(ctx, user: discord.User):
|
24 |
if ctx.author.id == 811235357663297546:
|
25 |
unique_link = f"{GRADIO_APP_URL}?user_id={user.id}"
|
26 |
await user.send(f"Click the link to sign in with Hugging Face: {unique_link}")
|
27 |
|
28 |
-
|
29 |
def run_bot():
|
30 |
bot.run(DISCORD_TOKEN)
|
31 |
threading.Thread(target=run_bot).start()
|
32 |
|
33 |
-
|
34 |
-
#gradio------------------------------------------------------------------------------------------------------------
|
35 |
def hello(profile: gr.OAuthProfile | None, request: gr.Request) -> str:
|
36 |
url_str = str(request.url)
|
37 |
query_params = parse_qs(urlparse(url_str).query)
|
@@ -40,18 +34,25 @@ def hello(profile: gr.OAuthProfile | None, request: gr.Request) -> str:
|
|
40 |
return f"❌ Not logged in. User ID: {user_id}"
|
41 |
return f"✅ Successfully logged in as {profile.username}. User ID: {user_id}"
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
with gr.Blocks() as demo:
|
47 |
-
gr.Markdown(
|
48 |
-
|
49 |
-
)
|
50 |
-
gr.LoginButton()
|
51 |
-
# ^ add a login button to the Space
|
52 |
m1 = gr.Markdown()
|
53 |
demo.load(hello, inputs=None, outputs=m1)
|
54 |
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
|
|
57 |
|
|
|
|
5 |
import discord
|
6 |
from discord.ext import commands
|
7 |
|
|
|
|
|
8 |
# discord bot -----------------------------------------------------------------------------------------------
|
9 |
intents = discord.Intents.all()
|
10 |
bot = commands.Bot(command_prefix="!", intents=intents)
|
11 |
GRADIO_APP_URL = "https://lunarflu-gradio-oauth2"
|
12 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
13 |
|
|
|
14 |
@bot.event
|
15 |
async def on_ready():
|
16 |
print(f'Logged in as {bot.user}')
|
17 |
|
|
|
18 |
@bot.command()
|
19 |
async def sendlink(ctx, user: discord.User):
|
20 |
if ctx.author.id == 811235357663297546:
|
21 |
unique_link = f"{GRADIO_APP_URL}?user_id={user.id}"
|
22 |
await user.send(f"Click the link to sign in with Hugging Face: {unique_link}")
|
23 |
|
|
|
24 |
def run_bot():
|
25 |
bot.run(DISCORD_TOKEN)
|
26 |
threading.Thread(target=run_bot).start()
|
27 |
|
28 |
+
# gradio------------------------------------------------------------------------------------------------------------
|
|
|
29 |
def hello(profile: gr.OAuthProfile | None, request: gr.Request) -> str:
|
30 |
url_str = str(request.url)
|
31 |
query_params = parse_qs(urlparse(url_str).query)
|
|
|
34 |
return f"❌ Not logged in. User ID: {user_id}"
|
35 |
return f"✅ Successfully logged in as {profile.username}. User ID: {user_id}"
|
36 |
|
|
|
|
|
|
|
37 |
with gr.Blocks() as demo:
|
38 |
+
gr.Markdown("# Gradio OAuth Space")
|
39 |
+
login_button = gr.LoginButton()
|
|
|
|
|
|
|
40 |
m1 = gr.Markdown()
|
41 |
demo.load(hello, inputs=None, outputs=m1)
|
42 |
|
43 |
+
def check_login_status():
|
44 |
+
try:
|
45 |
+
return login_button.get_session().get("oauth_info", None)
|
46 |
+
except AttributeError:
|
47 |
+
return None
|
48 |
+
|
49 |
+
def check_login_wrapper():
|
50 |
+
session = check_login_status()
|
51 |
+
if session is None:
|
52 |
+
return "Not logged in."
|
53 |
+
else:
|
54 |
+
return f"Logged in as {session.get('username', 'Unknown')}"
|
55 |
|
56 |
+
login_button.click(check_login_wrapper, inputs=None, outputs=m1)
|
57 |
|
58 |
+
demo.launch()
|