lunarflu HF staff commited on
Commit
7daad99
·
verified ·
1 Parent(s): 3abdf8c
Files changed (1) hide show
  1. app.py +37 -46
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  import os
3
- import asyncio
4
  from urllib.parse import urlparse, parse_qs
5
  import discord
6
  from discord.ext import commands
@@ -30,65 +30,56 @@ async def sendlink(ctx, user: discord.User):
30
  unique_link = f"{GRADIO_APP_URL}?user_id={user.id}&token={unique_string}"
31
  await user.send(f"Click the link to sign in with Hugging Face: {unique_link}")
32
 
33
- async def run_bot():
34
- await bot.start(DISCORD_TOKEN)
 
 
35
 
36
  # Gradio ------------------------------------------------------------------------------------------------------------
37
- async def hello(profile: gr.OAuthProfile | None, request: gr.Request) -> str:
38
  url_str = str(request.url)
39
  query_params = parse_qs(urlparse(url_str).query)
40
  user_id = query_params.get('user_id', [None])[0]
41
  token = query_params.get('token', [None])[0]
42
- user = await bot.fetch_user(user_id)
43
-
44
- print(f"token:{token}\n user_id:{user_id}\n profile:{profile}\n user_tokens:{user_tokens}")
45
 
46
- if token is None:
47
- return "# ❌ Invalid link, unique code is missing. Generate a new link [here](https://discord.com/channels/879548962464493619/900125909984624713) !."
48
 
49
- if user_id is None:
50
- return "# ❌ Invalid link, discord user_id is missing. Generate a new link [here](https://discord.com/channels/879548962464493619/900125909984624713) !."
51
-
52
- if profile is None:
53
- return f"# ❌ Log in with Hugging Face to verify!"
54
-
55
  if int(user_id) not in user_tokens or user_tokens[int(user_id)] != token:
56
- return "# ❌ Invalid or expired link. Generate a new link [here](https://discord.com/channels/879548962464493619/900125909984624713) ! "
57
-
58
- return f"# ✅ Successfully logged in as {profile.username}. Discord username: {user.name}"
59
 
60
- def launch_gradio():
61
- with gr.Blocks() as demo:
62
- with gr.Row():
63
- gr.Markdown("# Discord Verification Space")
64
- with gr.Row():
65
- login_button = gr.LoginButton()
66
 
67
- m1 = gr.Markdown()
68
- demo.load(hello, inputs=None, outputs=m1)
69
 
70
- def check_login_status():
71
- try:
72
- return login_button.get_session().get("oauth_info", None)
73
- except AttributeError:
74
- return None
75
 
76
- def check_login_wrapper():
77
- session = check_login_status()
78
- if session is None:
79
- return "Not logged in."
80
- else:
81
- return f"Logged in as {session.get('username', 'Unknown')}"
82
 
83
- login_button.click(check_login_wrapper, inputs=None, outputs=m1)
84
 
85
- demo.launch()
86
 
87
- async def main():
88
- await asyncio.gather(
89
- run_bot(),
90
- asyncio.to_thread(launch_gradio)
91
- )
92
 
93
- if __name__ == "__main__":
94
- asyncio.run(main())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import os
3
+ import threading
4
  from urllib.parse import urlparse, parse_qs
5
  import discord
6
  from discord.ext import commands
 
30
  unique_link = f"{GRADIO_APP_URL}?user_id={user.id}&token={unique_string}"
31
  await user.send(f"Click the link to sign in with Hugging Face: {unique_link}")
32
 
33
+ def run_bot():
34
+ bot.run(DISCORD_TOKEN)
35
+
36
+ threading.Thread(target=run_bot).start()
37
 
38
  # Gradio ------------------------------------------------------------------------------------------------------------
39
+ def hello(profile: gr.OAuthProfile | None, request: gr.Request) -> str:
40
  url_str = str(request.url)
41
  query_params = parse_qs(urlparse(url_str).query)
42
  user_id = query_params.get('user_id', [None])[0]
43
  token = query_params.get('token', [None])[0]
 
 
 
44
 
45
+ if user_id is None or token is None:
46
+ return "# ❌ Invalid link. Generate a new one [here](https://discord.com/channels/879548962464493619/900125909984624713) !."
47
 
 
 
 
 
 
 
48
  if int(user_id) not in user_tokens or user_tokens[int(user_id)] != token:
49
+ return "# ❌ Invalid or expired link. Generate a new one [here](https://discord.com/channels/879548962464493619/900125909984624713) ! "
 
 
50
 
51
+ if profile is None:
52
+ return f"# ❌ Not logged in with Hugging Face yet."
 
 
 
 
53
 
54
+ return f"# ✅ Successfully logged in as {profile.username}. User ID: {user_id}"
 
55
 
56
+ with gr.Blocks() as demo:
57
+ with gr.Row():
58
+ gr.Markdown("# Discord Verification Space")
59
+ with gr.Row():
60
+ login_button = gr.LoginButton()
61
 
62
+
63
+ m1 = gr.Markdown()
64
+ demo.load(hello, inputs=None, outputs=m1)
 
 
 
65
 
 
66
 
 
67
 
 
 
 
 
 
68
 
69
+
70
+ def check_login_status():
71
+ try:
72
+ return login_button.get_session().get("oauth_info", None)
73
+ except AttributeError:
74
+ return None
75
+
76
+ def check_login_wrapper():
77
+ session = check_login_status()
78
+ if session is None:
79
+ return "Not logged in."
80
+ else:
81
+ return f"Logged in as {session.get('username', 'Unknown')}"
82
+
83
+ login_button.click(check_login_wrapper, inputs=None, outputs=m1)
84
+
85
+ demo.launch()