Nattapong Tapachoom commited on
Commit
7908154
·
1 Parent(s): af5619b

Implement Hugging Face OAuth login requirement; update README for usage instructions

Browse files
Files changed (2) hide show
  1. README.md +2 -0
  2. app.py +38 -24
README.md CHANGED
@@ -7,6 +7,7 @@ sdk: gradio
7
  sdk_version: 5.44.1
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
@@ -27,3 +28,4 @@ This app uses LangChain with the Hugging Face Inference API to generate QA datas
27
 
28
  - Uses HF Inference API via LangChain; no local `transformers` needed.
29
  - Output files are saved to `outputs/` as JSON and JSONL.
 
 
7
  sdk_version: 5.44.1
8
  app_file: app.py
9
  pinned: false
10
+ hf_oauth: true
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
28
 
29
  - Uses HF Inference API via LangChain; no local `transformers` needed.
30
  - Output files are saved to `outputs/` as JSON and JSONL.
31
+ - Login: the app requires Hugging Face login by default on Spaces. Set env `REQUIRE_LOGIN=0` to disable (useful for local runs). A Login button is shown at the top; Generate is enabled after login.
app.py CHANGED
@@ -7,8 +7,12 @@ from typing import List, Dict, Any, Tuple
7
 
8
  import gradio as gr
9
 
10
- # Require login to use the app (default true). Set REQUIRE_LOGIN=0 to disable.
11
- REQUIRE_LOGIN = os.getenv("REQUIRE_LOGIN", "1").strip().lower() in ("1", "true", "yes", "y")
 
 
 
 
12
 
13
  try:
14
  from pypdf import PdfReader
@@ -238,15 +242,23 @@ with gr.Blocks(title="AutoGDataset - PDF to QA Dataset (LangChain)") as demo:
238
  Choose one of the preset models or provide a custom repo id. Provide a valid `HF_TOKEN` if required by the model.
239
  """)
240
 
241
- # Login requirement (Hugging Face OAuth via Gradio LoginButton)
242
  user_state = gr.State(value=None)
 
243
  with gr.Row():
244
  login_info = gr.Markdown(
245
- value=("Please login with your Hugging Face account to use the app." if REQUIRE_LOGIN else "Login is optional; proceed or login for personalization."),
 
 
 
 
 
 
246
  elem_id="login-info",
247
  )
248
- with gr.Row():
249
- login_btn = gr.LoginButton(value="Login with Hugging Face")
 
250
 
251
  with gr.Row():
252
  pdf_files = gr.File(label="Upload PDF(s)", file_count="multiple", file_types=[".pdf"])
@@ -271,7 +283,7 @@ with gr.Blocks(title="AutoGDataset - PDF to QA Dataset (LangChain)") as demo:
271
  max_pairs = gr.Slider(1, 12, value=6, step=1, label="Max Pairs/Chunk")
272
  custom_instruction = gr.Textbox(label="Custom Instruction (optional)", lines=3, placeholder="Override default instruction. Must ask for a pure JSON array of {question, answer}.")
273
 
274
- generate_btn = gr.Button("Generate Dataset", variant="primary", interactive=(not REQUIRE_LOGIN))
275
 
276
  with gr.Row():
277
  status = gr.Markdown()
@@ -301,23 +313,25 @@ with gr.Blocks(title="AutoGDataset - PDF to QA Dataset (LangChain)") as demo:
301
  api_name="generate",
302
  )
303
 
304
- def _on_login(user):
305
- try:
306
- username = None
307
- if isinstance(user, dict):
308
- username = user.get("username") or user.get("name")
309
- if not username and hasattr(user, "username"):
310
- username = getattr(user, "username")
311
- msg = f"Logged in as @{username}" if username else "Logged in"
312
- except Exception:
313
- msg = "Logged in"
314
- return user, gr.update(value=msg), gr.update(interactive=True)
315
-
316
- # Enable Generate button after login and store user profile
317
- if hasattr(login_btn, "login"):
318
- login_btn.login(_on_login, inputs=None, outputs=[user_state, login_info, generate_btn])
319
- else:
320
- login_btn.click(_on_login, inputs=None, outputs=[user_state, login_info, generate_btn])
 
 
321
 
322
  if __name__ == "__main__":
323
  # For local runs
 
7
 
8
  import gradio as gr
9
 
10
+ # Detect if OAuth is available (enabled on Spaces when hf_oauth: true)
11
+ OAUTH_AVAILABLE = bool(os.getenv("OAUTH_CLIENT_ID"))
12
+
13
+ # Require login to use the app. Defaults to on only when OAuth is available.
14
+ _default_require = "1" if OAUTH_AVAILABLE else "0"
15
+ REQUIRE_LOGIN = os.getenv("REQUIRE_LOGIN", _default_require).strip().lower() in ("1", "true", "yes", "y")
16
 
17
  try:
18
  from pypdf import PdfReader
 
242
  Choose one of the preset models or provide a custom repo id. Provide a valid `HF_TOKEN` if required by the model.
243
  """)
244
 
245
+ # Login requirement (Hugging Face OAuth via Gradio LoginButton when available)
246
  user_state = gr.State(value=None)
247
+ effective_require_login = bool(REQUIRE_LOGIN and OAUTH_AVAILABLE)
248
  with gr.Row():
249
  login_info = gr.Markdown(
250
+ value=(
251
+ "Please login with your Hugging Face account to use the app."
252
+ if effective_require_login
253
+ else (
254
+ "Login is optional." if OAUTH_AVAILABLE else "OAuth login not configured on this deployment."
255
+ )
256
+ ),
257
  elem_id="login-info",
258
  )
259
+ if OAUTH_AVAILABLE:
260
+ with gr.Row():
261
+ login_btn = gr.LoginButton(value="Login with Hugging Face")
262
 
263
  with gr.Row():
264
  pdf_files = gr.File(label="Upload PDF(s)", file_count="multiple", file_types=[".pdf"])
 
283
  max_pairs = gr.Slider(1, 12, value=6, step=1, label="Max Pairs/Chunk")
284
  custom_instruction = gr.Textbox(label="Custom Instruction (optional)", lines=3, placeholder="Override default instruction. Must ask for a pure JSON array of {question, answer}.")
285
 
286
+ generate_btn = gr.Button("Generate Dataset", variant="primary", interactive=(not effective_require_login))
287
 
288
  with gr.Row():
289
  status = gr.Markdown()
 
313
  api_name="generate",
314
  )
315
 
316
+ if OAUTH_AVAILABLE:
317
+ def _on_login(user):
318
+ try:
319
+ username = None
320
+ if isinstance(user, dict):
321
+ username = user.get("username") or user.get("name")
322
+ if not username and hasattr(user, "username"):
323
+ username = getattr(user, "username")
324
+ msg = f"Logged in as @{username}" if username else "Logged in"
325
+ except Exception:
326
+ msg = "Logged in"
327
+ return user, gr.update(value=msg), gr.update(interactive=True)
328
+
329
+ # Enable Generate button after login and store user profile
330
+ if hasattr(login_btn, "login"):
331
+ login_btn.login(_on_login, inputs=None, outputs=[user_state, login_info, generate_btn])
332
+ else:
333
+ # In local/dev without OAuth routing, clicking will mock-login
334
+ login_btn.click(lambda: ("local_user", gr.update(value="Logged in (local)"), gr.update(interactive=True)), inputs=None, outputs=[user_state, login_info, generate_btn])
335
 
336
  if __name__ == "__main__":
337
  # For local runs