Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/smolagents/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import yaml
from huggingface_hub import create_repo, metadata_update, snapshot_download, upload_folder
from huggingface_hub.constants import SPACES_SDK_TYPES
from jinja2 import StrictUndefined, Template
from rich.console import Group
from rich.live import Live
Expand Down Expand Up @@ -1164,6 +1165,7 @@ def push_to_hub(
private: bool | None = None,
token: bool | str | None = None,
create_pr: bool = False,
space_sdk: str = "gradio",
) -> str:
"""
Upload the agent to the Hub.
Expand All @@ -1181,14 +1183,20 @@ def push_to_hub(
when running `huggingface-cli login` (stored in `~/.huggingface`).
create_pr (`bool`, *optional*, defaults to `False`):
Whether to create a PR with the uploaded files or directly commit.
space_sdk (`str`, *optional*, defaults to `"gradio"`):
The Space SDK to use when creating the Space on the Hub. Must be one of `"gradio"`, `"streamlit"`, `"docker"`, or `"static"`.
"""
if space_sdk not in SPACES_SDK_TYPES:
raise ValueError(
f"Invalid space_sdk '{space_sdk}'. Supported values are: {', '.join(sorted(SPACES_SDK_TYPES))}"
)
repo_url = create_repo(
repo_id=repo_id,
token=token,
private=private,
exist_ok=True,
repo_type="space",
space_sdk="gradio",
space_sdk=space_sdk,
)
repo_id = repo_url.repo_id
metadata_update(
Expand Down
46 changes: 46 additions & 0 deletions tests/test_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -2626,3 +2626,49 @@ def _sample_tool(_: str) -> float:
agent = ToolCallingAgent(model=FakeToolCallModel(), tools=[_sample_tool])
with pytest.raises(AgentExecutionError):
agent.execute_tool_call(_sample_tool.name, "sample")


def test_push_to_hub_space_sdk_default():
agent = CodeAgent(tools=[], model=FakeToolCallModel())
with (
patch("smolagents.agents.create_repo") as mock_create_repo,
patch("smolagents.agents.metadata_update"),
patch("smolagents.agents.upload_folder"),
patch.object(agent, "save"),
):
mock_create_repo.return_value = MagicMock(repo_id="test/agent")
agent.push_to_hub("test/agent")
mock_create_repo.assert_called_once_with(
repo_id="test/agent",
token=None,
private=None,
exist_ok=True,
repo_type="space",
space_sdk="gradio",
)


def test_push_to_hub_space_sdk_custom():
agent = CodeAgent(tools=[], model=FakeToolCallModel())
with (
patch("smolagents.agents.create_repo") as mock_create_repo,
patch("smolagents.agents.metadata_update"),
patch("smolagents.agents.upload_folder"),
patch.object(agent, "save"),
):
mock_create_repo.return_value = MagicMock(repo_id="test/agent")
agent.push_to_hub("test/agent", space_sdk="static")
mock_create_repo.assert_called_once_with(
repo_id="test/agent",
token=None,
private=None,
exist_ok=True,
repo_type="space",
space_sdk="static",
)


def test_push_to_hub_space_sdk_invalid():
agent = CodeAgent(tools=[], model=FakeToolCallModel())
with pytest.raises(ValueError, match="Invalid space_sdk 'invalid_sdk'"):
agent.push_to_hub("test/agent", space_sdk="invalid_sdk")