openfree commited on
Commit
ad3eeca
·
verified ·
1 Parent(s): 85138d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -18
app.py CHANGED
@@ -432,25 +432,40 @@ if __name__ == "__main__":
432
  "summary": f"Smart template created for {repo_name}"
433
  }
434
 
435
- def clone(repo_git, repo_hf, sdk_type, skip_lfs, enable_smart_generation, profile: gr.OAuthProfile = None, oauth_token: gr.OAuthToken = None):
436
  folder = str(uuid.uuid4())
437
 
438
- # OAuth 인증 우선, 없으면 환경변수 사용
439
- if oauth_token:
 
 
 
 
 
 
 
 
440
  hf_token = oauth_token.token
441
  username = profile.username if profile else None
442
- yield f" Authenticated via OAuth as: @{username}"
443
- elif profile:
444
- # Profile은 있지만 token이 없는 경우 (권한 부족)
445
- yield "❌ Error: Insufficient permissions. Please logout and login again with write access."
446
- return
447
  else:
448
- # OAuth 없을 때 환경변수 체크
449
  hf_token = os.getenv("HF_TOKEN")
450
  if not hf_token:
451
- yield "❌ Error: Please login with HuggingFace using the 'Sign in with Hugging Face' button at the top."
 
 
 
 
 
 
 
 
 
 
 
452
  return
453
- username = None
454
 
455
  try:
456
  yield "🔄 Starting clone process..."
@@ -458,13 +473,30 @@ def clone(repo_git, repo_hf, sdk_type, skip_lfs, enable_smart_generation, profil
458
  # HuggingFace API 인증
459
  try:
460
  api = HfApi(token=hf_token)
461
- if not username: # OAuth로 로그인하지 않은 경우 username 가져오기
462
- user_info = api.whoami()
463
- username = user_info["name"]
464
- yield f"✅ Authenticated as HuggingFace user: @{username}"
 
 
 
465
  except Exception as e:
466
- yield f" HuggingFace authentication failed: {str(e)}"
467
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
468
 
469
  # URL 정규화 및 검증
470
  repo_git = repo_git.strip()
@@ -1222,6 +1254,21 @@ with gr.Blocks(css=css, theme=gr.themes.Soft(
1222
 
1223
  with gr.Tabs(elem_classes="tabs") as main_tabs:
1224
  with gr.Tab("🚀 GitHub → HF Space", elem_id="tab1"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1225
  with gr.Row():
1226
  with gr.Column(scale=5):
1227
  with gr.Group(elem_classes="input-section"):
@@ -1277,7 +1324,7 @@ with gr.Blocks(css=css, theme=gr.themes.Soft(
1277
 
1278
  btn_to_hf.click(
1279
  fn=clone,
1280
- inputs=[repo_git, repo_hf, sdk_choices, skip_lfs, enable_smart_generation],
1281
  outputs=output_to_hf
1282
  )
1283
 
 
432
  "summary": f"Smart template created for {repo_name}"
433
  }
434
 
435
+ def clone(repo_git, repo_hf, sdk_type, skip_lfs, enable_smart_generation, user_hf_token=None, profile: gr.OAuthProfile = None, oauth_token: gr.OAuthToken = None):
436
  folder = str(uuid.uuid4())
437
 
438
+ # 토큰 우선순위: 1. 사용자 입력 토큰, 2. OAuth 토큰, 3. 환경변수
439
+ hf_token = None
440
+ username = None
441
+
442
+ if user_hf_token and user_hf_token.strip():
443
+ # 사용자가 직접 입력한 토큰 사용
444
+ hf_token = user_hf_token.strip()
445
+ yield "🔑 Using provided HuggingFace token..."
446
+ elif oauth_token and oauth_token.token:
447
+ # OAuth 토큰 시도 (하지만 Space 생성 권한이 없을 수 있음)
448
  hf_token = oauth_token.token
449
  username = profile.username if profile else None
450
+ yield f"🔐 Attempting with OAuth token for @{username}..."
451
+ yield "⚠️ Note: OAuth may have limited permissions. If Space creation fails, please provide a personal access token."
 
 
 
452
  else:
453
+ # 환경변수 체크
454
  hf_token = os.getenv("HF_TOKEN")
455
  if not hf_token:
456
+ yield """Authentication Required!
457
+
458
+ Please choose one of these options:
459
+ 1. **Recommended**: Enter your HuggingFace token in the field below
460
+ 2. Login with the 'Sign in with Hugging Face' button (may have limited permissions)
461
+ 3. Set HF_TOKEN in environment variables
462
+
463
+ To get a token:
464
+ 1. Go to https://huggingface.co/settings/tokens
465
+ 2. Click 'New token'
466
+ 3. Select 'write' role
467
+ 4. Copy and paste the token"""
468
  return
 
469
 
470
  try:
471
  yield "🔄 Starting clone process..."
 
473
  # HuggingFace API 인증
474
  try:
475
  api = HfApi(token=hf_token)
476
+ user_info = api.whoami()
477
+ username = user_info["name"]
478
+ yield f"✅ Authenticated as HuggingFace user: @{username}"
479
+
480
+ # 권한 확인
481
+ if "write" not in user_info.get("auth", {}).get("accessToken", {}).get("role", ""):
482
+ yield "⚠️ Warning: Token may not have write permissions. If Space creation fails, please use a token with 'write' role."
483
  except Exception as e:
484
+ if "403" in str(e) or "401" in str(e):
485
+ yield f"""❌ Authentication failed!
486
+
487
+ The token doesn't have sufficient permissions.
488
+ Please create a new token with 'write' access:
489
+ 1. Go to https://huggingface.co/settings/tokens
490
+ 2. Click 'New token'
491
+ 3. Give it a name
492
+ 4. Select 'write' role (important!)
493
+ 5. Copy and paste the token
494
+
495
+ Error: {str(e)}"""
496
+ return
497
+ else:
498
+ yield f"❌ HuggingFace authentication failed: {str(e)}"
499
+ return
500
 
501
  # URL 정규화 및 검증
502
  repo_git = repo_git.strip()
 
1254
 
1255
  with gr.Tabs(elem_classes="tabs") as main_tabs:
1256
  with gr.Tab("🚀 GitHub → HF Space", elem_id="tab1"):
1257
+ # 인증 옵션 섹션 추가
1258
+ with gr.Group(elem_classes="input-section"):
1259
+ gr.Markdown("""
1260
+ ### 🔐 Authentication Options
1261
+ **Option 1**: Enter your HuggingFace token (Recommended)
1262
+ **Option 2**: Use OAuth login (may have limited permissions)
1263
+ """)
1264
+
1265
+ user_hf_token = gr.Textbox(
1266
+ label="HuggingFace Access Token (Optional but Recommended)",
1267
+ type="password",
1268
+ placeholder="hf_xxxxxxxxxxxxxxxxxxxxx",
1269
+ info="Get token from: https://huggingface.co/settings/tokens (needs 'write' role)"
1270
+ )
1271
+
1272
  with gr.Row():
1273
  with gr.Column(scale=5):
1274
  with gr.Group(elem_classes="input-section"):
 
1324
 
1325
  btn_to_hf.click(
1326
  fn=clone,
1327
+ inputs=[repo_git, repo_hf, sdk_choices, skip_lfs, enable_smart_generation, user_hf_token],
1328
  outputs=output_to_hf
1329
  )
1330