Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- main.py +49 -1
- requirements.txt +1 -1
main.py
CHANGED
@@ -1,7 +1,10 @@
|
|
1 |
from smolagents import CodeAgent, InferenceClientModel, GradioUI, tool
|
2 |
from huggingface_hub import HfApi
|
|
|
3 |
import requests
|
4 |
from typing import List, Dict
|
|
|
|
|
5 |
|
6 |
@tool
|
7 |
def leaderboard_search(query: str) -> str:
|
@@ -101,6 +104,51 @@ def get_space_content(space_id: str) -> str:
|
|
101 |
except Exception as e:
|
102 |
return f"Error accessing space {space_id}: {str(e)}"
|
103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
@tool
|
106 |
def get_file_from_space(space_id: str, file_path: str) -> str:
|
@@ -129,7 +177,7 @@ def get_file_from_space(space_id: str, file_path: str) -> str:
|
|
129 |
# Initialize the agent with the leaderboard search and space content tools
|
130 |
model = InferenceClientModel()
|
131 |
agent = CodeAgent(
|
132 |
-
tools=[leaderboard_search, get_space_content, get_file_from_space],
|
133 |
additional_authorized_imports=["json", "requests", "pandas"],
|
134 |
model=model,
|
135 |
add_base_tools=False,
|
|
|
1 |
from smolagents import CodeAgent, InferenceClientModel, GradioUI, tool
|
2 |
from huggingface_hub import HfApi
|
3 |
+
from huggingface_hub import snapshot_download
|
4 |
import requests
|
5 |
from typing import List, Dict
|
6 |
+
from typing import Optional, Union
|
7 |
+
from pathlib import Path
|
8 |
|
9 |
@tool
|
10 |
def leaderboard_search(query: str) -> str:
|
|
|
104 |
except Exception as e:
|
105 |
return f"Error accessing space {space_id}: {str(e)}"
|
106 |
|
107 |
+
@tool
|
108 |
+
def download_space_files(
|
109 |
+
space_id: str,
|
110 |
+
revision: Optional[str] = None,
|
111 |
+
local_dir: Optional[Union[str, Path]] = None,
|
112 |
+
allow_patterns: Optional[Union[List[str], str]] = None,
|
113 |
+
ignore_patterns: Optional[Union[List[str], str]] = None,
|
114 |
+
force_download: bool = False,
|
115 |
+
max_workers: int = 8
|
116 |
+
) -> str:
|
117 |
+
"""
|
118 |
+
Download all files from a Hugging Face Space (snapshot).
|
119 |
+
|
120 |
+
Args:
|
121 |
+
space_id: e.g. "owner/my-awesome-space"
|
122 |
+
revision: branch, tag or commit hash (defaults to default branch)
|
123 |
+
local_dir: where to put the files (defaults to HF cache)
|
124 |
+
allow_patterns: only download files matching these patterns
|
125 |
+
ignore_patterns: skip files matching these patterns
|
126 |
+
force_download: re‑download even if cached
|
127 |
+
max_workers: parallel downloads
|
128 |
+
|
129 |
+
Returns:
|
130 |
+
The local folder path where the space’s files now live.
|
131 |
+
"""
|
132 |
+
try:
|
133 |
+
folder = snapshot_download(
|
134 |
+
repo_id=space_id,
|
135 |
+
repo_type="space",
|
136 |
+
revision=revision,
|
137 |
+
local_dir=local_dir,
|
138 |
+
allow_patterns=allow_patterns,
|
139 |
+
ignore_patterns=ignore_patterns,
|
140 |
+
force_download=force_download,
|
141 |
+
max_workers=max_workers
|
142 |
+
)
|
143 |
+
# List how many files were downloaded
|
144 |
+
files = list(Path(folder).rglob("*"))
|
145 |
+
return (
|
146 |
+
f"✔️ Downloaded {len(files)} files from space `{space_id}`\n"
|
147 |
+
f"📂 Local path: {folder}"
|
148 |
+
)
|
149 |
+
except Exception as e:
|
150 |
+
return f"❌ Failed to download space `{space_id}`: {e}"
|
151 |
+
|
152 |
|
153 |
@tool
|
154 |
def get_file_from_space(space_id: str, file_path: str) -> str:
|
|
|
177 |
# Initialize the agent with the leaderboard search and space content tools
|
178 |
model = InferenceClientModel()
|
179 |
agent = CodeAgent(
|
180 |
+
tools=[leaderboard_search, get_space_content, get_file_from_space, download_space_files],
|
181 |
additional_authorized_imports=["json", "requests", "pandas"],
|
182 |
model=model,
|
183 |
add_base_tools=False,
|
requirements.txt
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
smolagents
|
2 |
huggingface_hub
|
3 |
-
gradio
|
|
|
1 |
smolagents
|
2 |
huggingface_hub
|
3 |
+
gradio<=3.12.0
|