andito HF Staff commited on
Commit
470a5e4
ยท
verified ยท
1 Parent(s): 3d4ea52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -31
app.py CHANGED
@@ -4,7 +4,7 @@ import csv
4
  import time
5
  import threading
6
  from datetime import datetime, timezone
7
- from typing import Dict, Optional, Tuple
8
 
9
  import gradio as gr
10
  from huggingface_hub import HfApi
@@ -122,23 +122,6 @@ def load_state(force: bool = False):
122
  # -------------------------
123
  # Core API
124
  # -------------------------
125
- def _get_user_info(request: Optional[gr.Request]) -> Optional[Dict]:
126
- """
127
- Extract user info from OAuth token in the request.
128
- Returns None if user is not authenticated.
129
- """
130
- if request is None:
131
- return None
132
-
133
- # In Gradio with OAuth enabled, user info is available via request.username
134
- # and other OAuth data via request.kwargs
135
- if hasattr(request, 'username') and request.username:
136
- return {
137
- "username": request.username,
138
- "headers": dict(request.headers) if hasattr(request, 'headers') else {}
139
- }
140
- return None
141
-
142
  def claim_key(
143
  request: Optional[gr.Request] = None,
144
  ) -> Tuple[str, str]:
@@ -215,7 +198,7 @@ def claim_b_key(
215
  return b_key["key"], f"Key provided."
216
 
217
  def claim_key_authenticated(
218
- request: Optional[gr.Request] = None,
219
  ) -> Tuple[str, str]:
220
  """
221
  OAuth-protected endpoint that returns the next available key.
@@ -223,12 +206,10 @@ def claim_key_authenticated(
223
  Returns (key, status_message)
224
  """
225
  # Check if user is authenticated
226
- user_info = _get_user_info(request)
227
-
228
- if user_info is None:
229
  return "", "Authentication required. Please sign in with your Hugging Face account."
230
 
231
- username = user_info["username"]
232
 
233
  with _lock:
234
  keys, _ = load_state(force=True)
@@ -261,7 +242,7 @@ def claim_key_authenticated(
261
  return key_to_send["key"], f"Key sent successfully to {username}."
262
 
263
  def claim_b_key_authenticated(
264
- request: Optional[gr.Request] = None,
265
  ) -> Tuple[str, str]:
266
  """
267
  OAuth-protected endpoint that returns the B key and increments the usage count.
@@ -269,12 +250,10 @@ def claim_b_key_authenticated(
269
  Returns (b_key, status_message)
270
  """
271
  # Check if user is authenticated
272
- user_info = _get_user_info(request)
273
-
274
- if user_info is None:
275
  return "", "Authentication required. Please sign in with your Hugging Face account."
276
 
277
- username = user_info["username"]
278
 
279
  with _lock:
280
  keys, b_keys = load_state(force=True)
@@ -308,7 +287,10 @@ def claim_b_key_authenticated(
308
  # -------------------------
309
  with gr.Blocks(title="API") as demo:
310
  gr.Markdown("## API Service")
311
- gr.Markdown("*Note: Authenticated endpoints require signing in with your Hugging Face account.*")
 
 
 
312
 
313
  with gr.Tab("Service A"):
314
  btn_a = gr.Button("Request", variant="secondary", size="sm")
@@ -336,7 +318,7 @@ with gr.Blocks(title="API") as demo:
336
 
337
  with gr.Tab("Service A (Authenticated)"):
338
  gr.Markdown("### ๐Ÿ” Authentication Required")
339
- gr.Markdown("This endpoint requires you to sign in with your Hugging Face account.")
340
  btn_a_auth = gr.Button("Request Key (OAuth)", variant="primary", size="sm")
341
  out_a_auth = gr.Textbox(label="Response", interactive=False, show_label=False)
342
  status_a_auth = gr.Textbox(label="", interactive=False, show_label=False)
@@ -350,7 +332,7 @@ with gr.Blocks(title="API") as demo:
350
 
351
  with gr.Tab("Service B (Authenticated)"):
352
  gr.Markdown("### ๐Ÿ” Authentication Required")
353
- gr.Markdown("This endpoint requires you to sign in with your Hugging Face account.")
354
  btn_b_auth = gr.Button("Request Key (OAuth)", variant="primary", size="sm")
355
  out_b_auth = gr.Textbox(label="Response", interactive=False, show_label=False)
356
  status_b_auth = gr.Textbox(label="", interactive=False, show_label=False)
@@ -364,3 +346,4 @@ with gr.Blocks(title="API") as demo:
364
 
365
  demo.queue()
366
  demo.launch()
 
 
4
  import time
5
  import threading
6
  from datetime import datetime, timezone
7
+ from typing import Optional, Tuple
8
 
9
  import gradio as gr
10
  from huggingface_hub import HfApi
 
122
  # -------------------------
123
  # Core API
124
  # -------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  def claim_key(
126
  request: Optional[gr.Request] = None,
127
  ) -> Tuple[str, str]:
 
198
  return b_key["key"], f"Key provided."
199
 
200
  def claim_key_authenticated(
201
+ profile: Optional[gr.OAuthProfile] = None,
202
  ) -> Tuple[str, str]:
203
  """
204
  OAuth-protected endpoint that returns the next available key.
 
206
  Returns (key, status_message)
207
  """
208
  # Check if user is authenticated
209
+ if profile is None:
 
 
210
  return "", "Authentication required. Please sign in with your Hugging Face account."
211
 
212
+ username = profile.username
213
 
214
  with _lock:
215
  keys, _ = load_state(force=True)
 
242
  return key_to_send["key"], f"Key sent successfully to {username}."
243
 
244
  def claim_b_key_authenticated(
245
+ profile: Optional[gr.OAuthProfile] = None,
246
  ) -> Tuple[str, str]:
247
  """
248
  OAuth-protected endpoint that returns the B key and increments the usage count.
 
250
  Returns (b_key, status_message)
251
  """
252
  # Check if user is authenticated
253
+ if profile is None:
 
 
254
  return "", "Authentication required. Please sign in with your Hugging Face account."
255
 
256
+ username = profile.username
257
 
258
  with _lock:
259
  keys, b_keys = load_state(force=True)
 
287
  # -------------------------
288
  with gr.Blocks(title="API") as demo:
289
  gr.Markdown("## API Service")
290
+
291
+ with gr.Row():
292
+ gr.Markdown("*Note: Authenticated endpoints require signing in with your Hugging Face account.*")
293
+ gr.LoginButton()
294
 
295
  with gr.Tab("Service A"):
296
  btn_a = gr.Button("Request", variant="secondary", size="sm")
 
318
 
319
  with gr.Tab("Service A (Authenticated)"):
320
  gr.Markdown("### ๐Ÿ” Authentication Required")
321
+ gr.Markdown("Click the 'Sign in with Hugging Face' button at the top to authenticate.")
322
  btn_a_auth = gr.Button("Request Key (OAuth)", variant="primary", size="sm")
323
  out_a_auth = gr.Textbox(label="Response", interactive=False, show_label=False)
324
  status_a_auth = gr.Textbox(label="", interactive=False, show_label=False)
 
332
 
333
  with gr.Tab("Service B (Authenticated)"):
334
  gr.Markdown("### ๐Ÿ” Authentication Required")
335
+ gr.Markdown("Click the 'Sign in with Hugging Face' button at the top to authenticate.")
336
  btn_b_auth = gr.Button("Request Key (OAuth)", variant="primary", size="sm")
337
  out_b_auth = gr.Textbox(label="Response", interactive=False, show_label=False)
338
  status_b_auth = gr.Textbox(label="", interactive=False, show_label=False)
 
346
 
347
  demo.queue()
348
  demo.launch()
349
+