Spaces:
Sleeping
Sleeping
Fetching metadata from the HF Docker repository...
-
1.52 kB
initial commit
-
248 Bytes
Update README.md
-
1.03 kB
app.py, import gradio as gr import random from smolagents import GradioUI, CodeAgent, HfApiModel # Import our custom tools from their modules from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool from retriever import load_guest_dataset # Initialize the Hugging Face model model = HfApiModel() # Initialize the web search tool search_tool = DuckDuckGoSearchTool() # Initialize the weather tool weather_info_tool = WeatherInfoTool() # Initialize the Hub stats tool hub_stats_tool = HubStatsTool() # Load the guest dataset and initialize the guest info tool guest_info_tool = load_guest_dataset() # Create Alfred with all the tools alfred = CodeAgent( tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool], model=model, add_base_tools=True, # Add any additional base tools planning_interval=3 # Enable planning every 3 steps ) if __name__ == "__main__": GradioUI(alfred).launch() retriever.py, from smolagents import Tool from langchain_community.retrievers import BM25Retriever from langchain.docstore.document import Document import datasets class GuestInfoRetrieverTool(Tool): name = "guest_info_retriever" description = "Retrieves detailed information about gala guests based on their name or relation." inputs = { "query": { "type": "string", "description": "The name or relation of the guest you want information about." } } output_type = "string" def __init__(self, docs): self.is_initialized = False self.retriever = BM25Retriever.from_documents(docs) def forward(self, query: str): results = self.retriever.get_relevant_documents(query) if results: return "\n\n".join([doc.page_content for doc in results[:3]]) else: return "No matching guest information found." def load_guest_dataset(): # Load the dataset guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train") # Convert dataset entries into Document objects docs = [ Document( page_content="\n".join([ f"Name: {guest['name']}", f"Relation: {guest['relation']}", f"Description: {guest['description']}", f"Email: {guest['email']}" ]), metadata={"name": guest["name"]} ) for guest in guest_dataset ] # Return the tool return GuestInfoRetrieverTool(docs) tools.py, from smolagents import DuckDuckGoSearchTool from smolagents import Tool import random from huggingface_hub import list_models # Initialize the DuckDuckGo search tool #search_tool = DuckDuckGoSearchTool() class WeatherInfoTool(Tool): name = "weather_info" description = "Fetches dummy weather information for a given location." inputs = { "location": { "type": "string", "description": "The location to get weather information for." } } output_type = "string" def forward(self, location: str): # Dummy weather data weather_conditions = [ {"condition": "Rainy", "temp_c": 15}, {"condition": "Clear", "temp_c": 25}, {"condition": "Windy", "temp_c": 20} ] # Randomly select a weather condition data = random.choice(weather_conditions) return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C" class HubStatsTool(Tool): name = "hub_stats" description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub." inputs = { "author": { "type": "string", "description": "The username of the model author/organization to find models from." } } output_type = "string" def forward(self, author: str): try: # List models from the specified author, sorted by downloads models = list(list_models(author=author, sort="downloads", direction=-1, limit=1)) if models: model = models[0] return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads." else: return f"No models found for author {author}." except Exception as e: return f"Error fetching models for {author}: {str(e)}" runtime error Exit code: 1. Reason: 4, in _inner_fn return fn(*args, **kwargs) File "/usr/local/lib/python3.10/site-packages/huggingface_hub/hf_api.py", line 5514, in hf_hub_download return hf_hub_download( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn return fn(*args, **kwargs) File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1008, in hf_hub_download return _hf_hub_download_to_cache_dir( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1115, in _hf_hub_download_to_cache_dir _raise_on_head_call_error(head_call_error, force_download, local_files_only) File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1645, in _raise_on_head_call_error raise head_call_error File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1533, in _get_metadata_or_catch_error metadata = get_hf_file_metadata( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn return fn(*args, **kwargs) File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1450, in get_hf_file_metadata r = _request_wrapper( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 286, in _request_wrapper response = _request_wrapper( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 310, in _request_wrapper hf_raise_for_status(response) File "/usr/local/lib/python3.10/site-packages/huggingface_hub/utils/_http.py", line 482, in hf_raise_for_status raise _format(HfHubHTTPError, str(e), response) from e huggingface_hub.errors.HfHubHTTPError: 401 Client Error: Unauthorized for url: https://huggingface.co/datasets/agents-course/unit3-invitees/resolve/main/README.md (Request ID: Root=1-6876c53c-42ed5d3c0c0ddf0930de2f8b;322d77ad-822e-4f6c-8560-6544cff53afd) Invalid credentials in Authorization header Container logs: ===== Application Startup at 2025-07-15 21:16:13 ===== /usr/local/lib/python3.10/site-packages/smolagents/models.py:1424: FutureWarning: HfApiModel was renamed to InferenceClientModel in version 1.14.0 and will be removed in 1.17.0. warnings.warn( Traceback (most recent call last): File "/home/user/app/app.py", line 22, in <module> guest_info_tool = load_guest_dataset() File "/home/user/app/retriever.py", line 33, in load_guest_dataset guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train") File "/usr/local/lib/python3.10/site-packages/datasets/load.py", line 2062, in load_dataset builder_instance = load_dataset_builder( File "/usr/local/lib/python3.10/site-packages/datasets/load.py", line 1782, in load_dataset_builder dataset_module = dataset_module_factory( File "/usr/local/lib/python3.10/site-packages/datasets/load.py", line 1664, in dataset_module_factory raise e1 from None File "/usr/local/lib/python3.10/site-packages/datasets/load.py", line 1534, in dataset_module_factory dataset_readme_path = api.hf_hub_download( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn return fn(*args, **kwargs) File "/usr/local/lib/python3.10/site-packages/huggingface_hub/hf_api.py", line 5514, in hf_hub_download return hf_hub_download( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn return fn(*args, **kwargs) File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1008, in hf_hub_download return _hf_hub_download_to_cache_dir( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1115, in _hf_hub_download_to_cache_dir _raise_on_head_call_error(head_call_error, force_download, local_files_only) File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1645, in _raise_on_head_call_error raise head_call_error File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1533, in _get_metadata_or_catch_error metadata = get_hf_file_metadata( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn return fn(*args, **kwargs) File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1450, in get_hf_file_metadata r = _request_wrapper( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 286, in _request_wrapper response = _request_wrapper( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 310, in _request_wrapper hf_raise_for_status(response) File "/usr/local/lib/python3.10/site-packages/huggingface_hub/utils/_http.py", line 482, in hf_raise_for_status raise _format(HfHubHTTPError, str(e), response) from e huggingface_hub.errors.HfHubHTTPError: 401 Client Error: Unauthorized for url: https://huggingface.co/datasets/agents-course/unit3-invitees/resolve/main/README.md (Request ID: Root=1-6876c53c-42ed5d3c0c0ddf0930de2f8b;322d77ad-822e-4f6c-8560-6544cff53afd) Invalid credentials in Authorization header /usr/local/lib/python3.10/site-packages/smolagents/models.py:1424: FutureWarning: HfApiModel was renamed to InferenceClientModel in version 1.14.0 and will be removed in 1.17.0. warnings.warn( Traceback (most recent call last): File "/home/user/app/app.py", line 22, in <module> guest_info_tool = load_guest_dataset() File "/home/user/app/retriever.py", line 33, in load_guest_dataset guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train") File "/usr/local/lib/python3.10/site-packages/datasets/load.py", line 2062, in load_dataset builder_instance = load_dataset_builder( File "/usr/local/lib/python3.10/site-packages/datasets/load.py", line 1782, in load_dataset_builder dataset_module = dataset_module_factory( File "/usr/local/lib/python3.10/site-packages/datasets/load.py", line 1664, in dataset_module_factory raise e1 from None File "/usr/local/lib/python3.10/site-packages/datasets/load.py", line 1534, in dataset_module_factory dataset_readme_path = api.hf_hub_download( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn return fn(*args, **kwargs) File "/usr/local/lib/python3.10/site-packages/huggingface_hub/hf_api.py", line 5514, in hf_hub_download return hf_hub_download( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn return fn(*args, **kwargs) File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1008, in hf_hub_download return _hf_hub_download_to_cache_dir( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1115, in _hf_hub_download_to_cache_dir _raise_on_head_call_error(head_call_error, force_download, local_files_only) File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1645, in _raise_on_head_call_error raise head_call_error File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1533, in _get_metadata_or_catch_error metadata = get_hf_file_metadata( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn return fn(*args, **kwargs) File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1450, in get_hf_file_metadata r = _request_wrapper( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 286, in _request_wrapper response = _request_wrapper( File "/usr/local/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 310, in _request_wrapper hf_raise_for_status(response) File "/usr/local/lib/python3.10/site-packages/huggingface_hub/utils/_http.py", line 482, in hf_raise_for_status raise _format(HfHubHTTPError, str(e), response) from e huggingface_hub.errors.HfHubHTTPError: 401 Client Error: Unauthorized for url: https://huggingface.co/datasets/agents-course/unit3-invitees/resolve/main/README.md (Request ID: Root=1-6876c543-30459abf68e203117ddd51e2;91b3b223-01f5-456a-8073-21c21fec63b1) Invalid credentials in Authorization header The error is a 401 Client Error: Unauthorized from the Hugging Face Hub when trying to access the dataset "agents-course/unit3-invitees". This indicates that the application lacks valid credentials to access the dataset, likely because it is private or restricted, or because no valid authentication token is provided. Cause The load_guest_dataset function in retriever.py attempts to load the dataset using datasets.load_dataset("agents-course/unit3-invitees", split="train"). The error occurs during the attempt to download the dataset's metadata (specifically the README.md file) from the Hugging Face Hub, as the request lacks proper authorization. Possible reasons: Missing or Invalid Hugging Face Token: The datasets library requires a valid Hugging Face API token to access private or gated datasets. Private Dataset: The dataset "agents-course/unit3-invitees" may be private or restricted, requiring explicit access permissions. Environment Misconfiguration: The Hugging Face token might not be set in the environment or configuration. Solution To resolve this issue, you need to authenticate with the Hugging Face Hub. Here’s how to fix it: 1. Set Up Hugging Face Authentication Log in to the Hugging Face CLI: Run the following command in your terminal to authenticate: bash Collapse Wrap Run Copy huggingface-cli login Follow the prompts to enter your Hugging Face API token. You can generate or find your token in your Hugging Face account settings under Settings > Access Tokens. Set the Token as an Environment Variable: Alternatively, set the HF_TOKEN environment variable in your environment: bash Collapse Wrap Run Copy export HF_TOKEN="your_huggingface_token" Replace "your_huggingface_token" with your actual token. If you're running this in a container, ensure the environment variable is set in the container's environment (e.g., via a .env file or Docker configuration). Pass the Token Programmatically: Modify retriever.py to pass the token explicitly when loading the dataset: python Collapse Wrap Run Copy def load_guest_dataset(): # Load the dataset with authentication guest_dataset = datasets.load_dataset( "agents-course/unit3-invitees", split="train", use_auth_token="your_huggingface_token" # Replace with your token ) # Convert dataset entries into Document objects docs = [ Document( page_content="\n".join([ f"Name: {guest['name']}", f"Relation: {guest['relation']}", f"Description: {guest['description']}", f"Email: {guest['email']}" ]), metadata={"name": guest["name"]} ) for guest in guest_dataset ] # Return the tool return GuestInfoRetrieverTool(docs) 2. Verify Dataset Access Ensure you have access to the "agents-course/unit3-invitees" dataset. If it’s private, you need to be granted access by the dataset owner. Visit the dataset page on Hugging Face and request access if necessary. 3. Check Dataset Availability Confirm that the dataset exists and is correctly named. If the dataset name is incorrect or the dataset is not available, you’ll need to update the dataset path in retriever.py or use a different dataset. 4. Update Container Configuration If running in a containerized environment (as suggested by the container logs), ensure the Hugging Face token is available in the container: Add the HF_TOKEN to your container’s environment variables in the Dockerfile or docker-compose.yml: yaml Collapse Wrap Copy environment: - HF_TOKEN=your_huggingface_token Alternatively, pass the token as a configuration file or secret, depending on your setup. 5. Handle the FutureWarning. The warning about HfApiModel being renamed to InferenceClientModel in smolagents/models.py indicates that the library is using a deprecated class.
-
85 Bytes
Update requirements.txt
-
1.55 kB
Update retriever.py
-
1.9 kB
Started Alfred