Quazim0t0 commited on
Commit
9a39531
·
verified ·
1 Parent(s): 1f05384

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -55
app.py CHANGED
@@ -8,17 +8,8 @@ import os
8
  import gradio as gr
9
  import threading
10
  import time
11
-
12
- # Set essential OAuth environment variables
13
- os.environ["OAUTH_CLIENT_ID"] = "hf"
14
- os.environ["OAUTH_CLIENT_SECRET"] = "hf"
15
- os.environ["OAUTH_AUTHORIZATION_URL"] = "https://huggingface.co/oauth/authorize"
16
- os.environ["OAUTH_TOKEN_URL"] = "https://huggingface.co/oauth/token"
17
- os.environ["OAUTH_SCOPES"] = "inference"
18
- os.environ["OPENID_PROVIDER_URL"] = "https://huggingface.co"
19
-
20
  from database_schema import DynamicHighscoresDB
21
- from auth import HuggingFaceAuth, create_login_ui, setup_auth_handlers
22
  from benchmark_selection import BenchmarkSelector, create_benchmark_selection_ui
23
  from evaluation_queue import EvaluationQueue, create_model_submission_ui
24
  from leaderboard import Leaderboard, create_leaderboard_ui
@@ -139,19 +130,42 @@ input[type="text"], input[type="password"], textarea {
139
  }
140
  """
141
 
142
- # Check if the server is running in a HuggingFace Space
143
- def is_running_in_hf_space():
144
- return 'SPACE_ID' in os.environ
145
-
146
- # Function to trigger HuggingFace OAuth login
147
- def trigger_hf_oauth():
148
- return """
 
 
 
 
 
 
 
149
  <script>
150
- // Redirect to HuggingFace Space login
151
- window.location.href = window.location.origin + "?__space_auth_callback=true";
152
  </script>
153
  """
154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  # Start evaluation queue worker
156
  def start_queue_worker():
157
  # Wait a moment to ensure app is initialized
@@ -162,16 +176,21 @@ def start_queue_worker():
162
  except Exception as e:
163
  print(f"Error starting queue worker: {e}")
164
 
165
- # Create Gradio app - without theme to avoid OAuth issues
166
  with gr.Blocks(css=css, title="Dynamic Highscores") as app:
167
- login_info = gr.LoginButton(visible=True)
168
- auth_status = gr.State(None) # Store user auth state
169
 
170
- # Add a prominent login section at the top
171
  with gr.Row(visible=True, elem_classes=["login-section"]) as login_section:
172
  with gr.Column():
173
- login_status = gr.Markdown("### 🔒 Not logged in", elem_id="login-status")
174
- login_button = gr.Button("Login with HuggingFace", size="lg", variant="primary", elem_classes=["login-button"])
 
 
 
 
 
175
 
176
  gr.Markdown("# 🏆 Dynamic Highscores", elem_classes=["header"])
177
  gr.Markdown("""
@@ -204,39 +223,14 @@ with gr.Blocks(css=css, title="Dynamic Highscores") as app:
204
  Created by Quazim0t0
205
  """, elem_classes=["footer"])
206
 
207
- # Auth status check on page load
208
- def check_auth_on_load(request: gr.Request):
209
- # Check if running in HF Space with OAuth
210
- if is_running_in_hf_space():
211
- username = request.headers.get("HF-User")
212
- if username:
213
- print(f"Detected logged-in user via Space OAuth: {username}")
214
- # Get or create user
215
- user = db.get_user_by_username(username)
216
- if not user:
217
- print(f"Creating new user: {username}")
218
- is_admin = (username == "Quazim0t0") # Replace with your admin username
219
- db.add_user(username, username, is_admin)
220
- user = db.get_user_by_username(username)
221
-
222
- # Update UI for logged in state
223
- return user, f"### ✅ Logged in as {username}", gr.update(visible=False)
224
-
225
- # Not logged in - make sure login section is visible
226
- return None, "### 🔒 Not logged in", gr.update(visible=True)
227
-
228
- # Connect event handlers
229
- login_button.click(
230
- fn=trigger_hf_oauth,
231
- inputs=[],
232
- outputs=[gr.HTML()]
233
- )
234
 
235
- # Check auth on load
236
  app.load(
237
- fn=check_auth_on_load,
238
  inputs=[],
239
- outputs=[auth_status, login_status, login_section]
240
  )
241
 
242
  # Launch the app
 
8
  import gradio as gr
9
  import threading
10
  import time
 
 
 
 
 
 
 
 
 
11
  from database_schema import DynamicHighscoresDB
12
+ from auth import HuggingFaceAuth
13
  from benchmark_selection import BenchmarkSelector, create_benchmark_selection_ui
14
  from evaluation_queue import EvaluationQueue, create_model_submission_ui
15
  from leaderboard import Leaderboard, create_leaderboard_ui
 
130
  }
131
  """
132
 
133
+ # Function to generate a login URL
134
+ def get_login_url():
135
+ # Get the space host from environment variable
136
+ space_host = os.environ.get("SPACE_HOST", "localhost:7860")
137
+ print(f"Space host: {space_host}")
138
+
139
+ # Construct the full redirect URL
140
+ redirect_uri = f"https://{space_host}/auth/callback"
141
+
142
+ # Create the complete authorization URL
143
+ auth_url = f"https://huggingface.co/oauth/authorize?client_id=hf&redirect_uri={redirect_uri}&scope=openid%20profile&response_type=code"
144
+
145
+ # Return JavaScript to open the login page
146
+ return f"""
147
  <script>
148
+ window.open("{auth_url}", "_blank");
 
149
  </script>
150
  """
151
 
152
+ # Simple manual authentication check
153
+ def check_user(request: gr.Request):
154
+ if request:
155
+ username = request.headers.get("HF-User")
156
+ if username:
157
+ # User is logged in via HF Spaces
158
+ print(f"User logged in: {username}")
159
+ user = db.get_user_by_username(username)
160
+ if not user:
161
+ # Create user if they don't exist
162
+ print(f"Creating new user: {username}")
163
+ is_admin = (username == "Quazim0t0")
164
+ db.add_user(username, username, is_admin)
165
+ user = db.get_user_by_username(username)
166
+ return f"Logged in as: {username}", username, gr.update(visible=False), gr.update(visible=True)
167
+ return "Not logged in", None, gr.update(visible=True), gr.update(visible=False)
168
+
169
  # Start evaluation queue worker
170
  def start_queue_worker():
171
  # Wait a moment to ensure app is initialized
 
176
  except Exception as e:
177
  print(f"Error starting queue worker: {e}")
178
 
179
+ # Create Gradio app
180
  with gr.Blocks(css=css, title="Dynamic Highscores") as app:
181
+ # State to track user
182
+ user_state = gr.State(None)
183
 
184
+ # Login section
185
  with gr.Row(visible=True, elem_classes=["login-section"]) as login_section:
186
  with gr.Column():
187
+ status_display = gr.Markdown("Checking login status...")
188
+ login_button = gr.Button("Login with HuggingFace", variant="primary", visible=True, elem_classes=["login-button"])
189
+
190
+ # User info section
191
+ with gr.Row(visible=False, elem_classes=["login-section"]) as user_section:
192
+ with gr.Column():
193
+ user_info = gr.Markdown("User information")
194
 
195
  gr.Markdown("# 🏆 Dynamic Highscores", elem_classes=["header"])
196
  gr.Markdown("""
 
223
  Created by Quazim0t0
224
  """, elem_classes=["footer"])
225
 
226
+ # Connect login handler
227
+ login_button.click(fn=get_login_url, inputs=[], outputs=[gr.HTML()])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
 
229
+ # Check login on page load
230
  app.load(
231
+ fn=check_user,
232
  inputs=[],
233
+ outputs=[status_display, user_state, login_section, user_section]
234
  )
235
 
236
  # Launch the app