Skip to content

Commit b6899a9

Browse files
committed
Refactor Hugging Face authentication in push command
- Removed dependency on `HF_TOKEN` environment variable for Hugging Face authentication. - Updated `_ensure_hf_authenticated` function to handle login without requiring a token. - Modified README.md to reflect changes in authentication process. - Adjusted tests to verify initialization of `HfApi` without token parameter.
1 parent a37982e commit b6899a9

File tree

3 files changed

+29
-38
lines changed

3 files changed

+29
-38
lines changed

src/openenv_cli/commands/push.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from __future__ import annotations
1010

11-
import os
1211
import shutil
1312
import tempfile
1413
from pathlib import Path
@@ -64,12 +63,9 @@ def _ensure_hf_authenticated() -> str:
6463
Returns:
6564
Username of authenticated user
6665
"""
67-
# Check for HF_TOKEN environment variable
68-
token = os.getenv("HF_TOKEN")
69-
7066
try:
7167
# Try to get current user
72-
user_info = whoami(token=token)
68+
user_info = whoami()
7369
# Handle both dict and object return types
7470
if isinstance(user_info, dict):
7571
username = user_info.get("name") or user_info.get("fullname") or user_info.get("username")
@@ -84,14 +80,10 @@ def _ensure_hf_authenticated() -> str:
8480
return username
8581
except Exception:
8682
# Not authenticated, prompt for login
87-
if token:
88-
# Token might be invalid
89-
console.print("[bold yellow]HF_TOKEN is set but invalid. Please login...[/bold yellow]")
90-
else:
91-
console.print("[bold yellow]Not authenticated with Hugging Face. Please login...[/bold yellow]")
83+
console.print("[bold yellow]Not authenticated with Hugging Face. Please login...[/bold yellow]")
9284

9385
try:
94-
login(token=token)
86+
login()
9587
# Verify login worked
9688
user_info = whoami()
9789
# Handle both dict and object return types
@@ -108,7 +100,7 @@ def _ensure_hf_authenticated() -> str:
108100
except Exception as e:
109101
raise typer.BadParameter(
110102
f"Hugging Face authentication failed: {e}. "
111-
"Please set HF_TOKEN environment variable or run login manually."
103+
"Please run login manually."
112104
) from e
113105

114106

@@ -353,8 +345,7 @@ def push(
353345
)
354346

355347
# Initialize Hugging Face API
356-
token = os.getenv("HF_TOKEN")
357-
api = HfApi(token=token)
348+
api = HfApi()
358349

359350
# Prepare staging directory
360351
console.print("[bold cyan]Preparing files for Hugging Face deployment...[/bold cyan]")

src/openenv_cli/templates/openenv_env/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ The `openenv push` command will:
7979

8080
### Prerequisites
8181

82-
- Authenticate with Hugging Face: Set `HF_TOKEN` environment variable or the command will prompt for login
82+
- Authenticate with Hugging Face: The command will prompt for login if not already authenticated
8383

8484
### Options
8585

tests/test_cli/test_push.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -371,31 +371,31 @@ def test_push_handles_missing_readme(tmp_path: Path) -> None:
371371
assert mock_api.upload_folder.called
372372

373373

374-
def test_push_uses_hf_token_from_env(tmp_path: Path) -> None:
375-
"""Test that push uses HF_TOKEN from environment if available."""
374+
def test_push_initializes_hf_api_without_token(tmp_path: Path) -> None:
375+
"""Test that push initializes HfApi without token parameter."""
376376
_create_test_openenv_env(tmp_path)
377377

378-
with patch.dict(os.environ, {"HF_TOKEN": "test-token-123"}):
379-
with patch("openenv_cli.commands.push.whoami") as mock_whoami, \
380-
patch("openenv_cli.commands.push.login") as mock_login, \
381-
patch("openenv_cli.commands.push.HfApi") as mock_hf_api_class:
382-
383-
mock_whoami.return_value = {"name": "testuser"}
384-
mock_login.return_value = None # Prevent actual login prompt
385-
mock_api = MagicMock()
386-
mock_hf_api_class.return_value = mock_api
387-
388-
old_cwd = os.getcwd()
389-
try:
390-
os.chdir(str(tmp_path))
391-
result = runner.invoke(app, ["push"])
392-
finally:
393-
os.chdir(old_cwd)
394-
395-
# Verify HfApi was initialized with token
396-
mock_hf_api_class.assert_called_once()
397-
call_args = mock_hf_api_class.call_args
398-
assert call_args.kwargs.get("token") == "test-token-123"
378+
with patch("openenv_cli.commands.push.whoami") as mock_whoami, \
379+
patch("openenv_cli.commands.push.login") as mock_login, \
380+
patch("openenv_cli.commands.push.HfApi") as mock_hf_api_class:
381+
382+
mock_whoami.return_value = {"name": "testuser"}
383+
mock_login.return_value = None # Prevent actual login prompt
384+
mock_api = MagicMock()
385+
mock_hf_api_class.return_value = mock_api
386+
387+
old_cwd = os.getcwd()
388+
try:
389+
os.chdir(str(tmp_path))
390+
result = runner.invoke(app, ["push"])
391+
finally:
392+
os.chdir(old_cwd)
393+
394+
# Verify HfApi was initialized without token parameter
395+
mock_hf_api_class.assert_called_once()
396+
call_args = mock_hf_api_class.call_args
397+
# Should not have token in kwargs
398+
assert "token" not in (call_args.kwargs or {})
399399

400400

401401
def test_push_validates_repo_id_format(tmp_path: Path) -> None:

0 commit comments

Comments
 (0)