jkorstad commited on
Commit
b24bf0f
·
verified ·
1 Parent(s): 24fb7b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -13
app.py CHANGED
@@ -59,19 +59,34 @@ class HuggingFaceSpaceSearcherTool(Tool):
59
  # Define attributes as class variables
60
  name = "huggingface_space_searcher"
61
  description = "Searches for Hugging Face Spaces that can perform a specific task. Input is a search query string (e.g., 'text to image', 'speech recognition'). Returns a list of Space IDs, their descriptions, and instructions on how to try using them."
62
- # Define input schema if needed, for now, we'll rely on type hinting in forward
63
- # inputs = { "query": {"type": "string", "description": "The search query for Hugging Face Spaces."} }
64
- # output_type = "string" # Optional: define output type
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  # The core logic goes into the forward method
 
67
  def forward(self, query: str, top_k: int = 3) -> str:
68
  """
69
  Searches Hugging Face Spaces for a given query and returns the top_k results.
70
  Provides repo_id, description, likes, and last modified date for each space found.
71
  """
72
  try:
73
- print(f"Searching spaces with query: {query}, top_k: {top_k}")
74
- spaces_found = list(list_spaces(search=query, full=True, limit=top_k, sort="likes", direction=-1))
 
 
 
75
  if not spaces_found:
76
  return "No Spaces found for your query."
77
 
@@ -104,14 +119,15 @@ space_search_tool = HuggingFaceSpaceSearcherTool()
104
  # ---- Debug print for the refactored tool ----
105
  try:
106
  print(f"\nDEBUG: 'space_search_tool' (refactored class) immediately after creation.")
107
- print(f"DEBUG: Name: {space_search_tool.name}") # Should now correctly access the class attribute
 
108
  print(f"DEBUG: Type: {type(space_search_tool)}")
109
- print(f"DEBUG: All attributes: {dir(space_search_tool)}\n")
110
  except AttributeError as e:
111
  print(f"\nDEBUG: 'space_search_tool' (refactored class) immediately after creation.")
112
- print(f"DEBUG: Name attribute STILL MISSING. Error: {e}")
113
  print(f"DEBUG: Type: {type(space_search_tool)}")
114
- print(f"DEBUG: All attributes: {dir(space_search_tool)}\n")
115
  # ---- END Debug print ----
116
  tools.append(space_search_tool)
117
 
@@ -124,7 +140,7 @@ for i, t in enumerate(tools):
124
  continue
125
  try:
126
  tool_name = t.name
127
- print(f"Tool {i}: Name='{tool_name}', Type={type(t)}")
128
  except AttributeError:
129
  print(f"!!! CRITICAL: Tool at index {i} (Type={type(t)}) is missing 'name' attribute.")
130
  except Exception as e:
@@ -147,8 +163,9 @@ AGENT_INSTRUCTIONS = """You are a highly capable AI assistant. Your primary goal
147
 
148
  Follow these steps:
149
  1. **Understand the Request:** Carefully analyze the user's prompt (which will follow these instructions). Identify the core task and any specific requirements or inputs.
150
- 2. **Check Predefined Tools:** Review your list of available tools. If a predefined tool can directly address the request, use it. For the 'huggingface_space_searcher' tool, the input should be a dictionary like `{"query": "your search term"}` if you defined an inputs schema, or directly as arguments like `huggingface_space_searcher(query="your search term")` if using type hints in the forward method. The refactored `HuggingFaceSpaceSearcherTool` uses type hints in its `forward(self, query: str, top_k: int = 3)` method, so call it like `huggingface_space_searcher(query="your search term")`.
151
- 3. **Search for Spaces (If Needed):** If no predefined tool is suitable, use the `huggingface_space_searcher` tool. Provide a concise search query related to the task (e.g., "image classification", "voice cloning", "document question answering").
 
152
  4. **Select and Instantiate a Space Tool:** From the search results, choose the most promising Space. Attempt to create a tool from it using `Tool.from_space(repo_id='SELECTED_SPACE_ID', name='a_unique_tool_name')`. You might need to give it a unique name. If `Tool.from_space` fails, the Space might not be compatible, or you could try another one from the search results.
153
  5. **Execute the Tool:** Call the tool (either predefined or dynamically created) with the necessary arguments.
154
  * **File Inputs:** If the user uploads files, their paths will be available as global string variables: `input_image_path`, `input_audio_path`, `input_video_path`, `input_3d_model_path`, `input_file_path`. Before using these variables, check if they exist and are not None. Pass these file paths as arguments to tools that require them.
@@ -160,7 +177,7 @@ Follow these steps:
160
 
161
  Example of dynamically using a Space after searching:
162
  ```python
163
- # search_results = huggingface_space_searcher(query="text to image cat")
164
  # print(search_results)
165
  # try:
166
  # cat_image_tool = Tool.from_space(repo_id="user/cat-generator", name="cat_generator_tool")
 
59
  # Define attributes as class variables
60
  name = "huggingface_space_searcher"
61
  description = "Searches for Hugging Face Spaces that can perform a specific task. Input is a search query string (e.g., 'text to image', 'speech recognition'). Returns a list of Space IDs, their descriptions, and instructions on how to try using them."
62
+
63
+ # Define input schema as required by smolagents.Tool base class
64
+ inputs = {
65
+ "query": {
66
+ "type": "string",
67
+ "description": "The search query for Hugging Face Spaces."
68
+ },
69
+ "top_k": {
70
+ "type": "integer",
71
+ "description": "The number of top results to return (default is 3).",
72
+ "optional": True # Marking as optional; the forward method has a default.
73
+ }
74
+ }
75
+ output_type = "string" # Optional: define output type
76
 
77
  # The core logic goes into the forward method
78
+ # The Tool base class will likely call this with arguments unpacked from a dictionary matching the 'inputs' schema.
79
  def forward(self, query: str, top_k: int = 3) -> str:
80
  """
81
  Searches Hugging Face Spaces for a given query and returns the top_k results.
82
  Provides repo_id, description, likes, and last modified date for each space found.
83
  """
84
  try:
85
+ # top_k will be provided by the Tool base class mechanism, potentially from kwargs if optional
86
+ # or directly if the agent passes it. The default in the signature is a fallback.
87
+ actual_top_k = top_k if top_k is not None else 3 # Ensure top_k has a value
88
+ print(f"Searching spaces with query: {query}, top_k: {actual_top_k}")
89
+ spaces_found = list(list_spaces(search=query, full=True, limit=actual_top_k, sort="likes", direction=-1))
90
  if not spaces_found:
91
  return "No Spaces found for your query."
92
 
 
119
  # ---- Debug print for the refactored tool ----
120
  try:
121
  print(f"\nDEBUG: 'space_search_tool' (refactored class) immediately after creation.")
122
+ print(f"DEBUG: Name: {space_search_tool.name}")
123
+ print(f"DEBUG: Inputs: {space_search_tool.inputs}") # Check if inputs are set
124
  print(f"DEBUG: Type: {type(space_search_tool)}")
125
+ # print(f"DEBUG: All attributes: {dir(space_search_tool)}\n") # Can be verbose
126
  except AttributeError as e:
127
  print(f"\nDEBUG: 'space_search_tool' (refactored class) immediately after creation.")
128
+ print(f"DEBUG: Attribute MISSING. Error: {e}")
129
  print(f"DEBUG: Type: {type(space_search_tool)}")
130
+ # print(f"DEBUG: All attributes: {dir(space_search_tool)}\n")
131
  # ---- END Debug print ----
132
  tools.append(space_search_tool)
133
 
 
140
  continue
141
  try:
142
  tool_name = t.name
143
+ print(f"Tool {i}: Name='{tool_name}', Type={type(t)}, Inputs: {getattr(t, 'inputs', 'Not defined')}")
144
  except AttributeError:
145
  print(f"!!! CRITICAL: Tool at index {i} (Type={type(t)}) is missing 'name' attribute.")
146
  except Exception as e:
 
163
 
164
  Follow these steps:
165
  1. **Understand the Request:** Carefully analyze the user's prompt (which will follow these instructions). Identify the core task and any specific requirements or inputs.
166
+ 2. **Check Predefined Tools:** Review your list of available tools. If a predefined tool can directly address the request, use it.
167
+ * For the 'huggingface_space_searcher' tool, you MUST provide its arguments as a dictionary. For example: `huggingface_space_searcher(arguments={"query": "your search term", "top_k": 3})`. The `query` is mandatory. `top_k` is optional and defaults to 3 if not provided.
168
+ 3. **Search for Spaces (If Needed):** If no predefined tool is suitable, use the `huggingface_space_searcher` tool as described above.
169
  4. **Select and Instantiate a Space Tool:** From the search results, choose the most promising Space. Attempt to create a tool from it using `Tool.from_space(repo_id='SELECTED_SPACE_ID', name='a_unique_tool_name')`. You might need to give it a unique name. If `Tool.from_space` fails, the Space might not be compatible, or you could try another one from the search results.
170
  5. **Execute the Tool:** Call the tool (either predefined or dynamically created) with the necessary arguments.
171
  * **File Inputs:** If the user uploads files, their paths will be available as global string variables: `input_image_path`, `input_audio_path`, `input_video_path`, `input_3d_model_path`, `input_file_path`. Before using these variables, check if they exist and are not None. Pass these file paths as arguments to tools that require them.
 
177
 
178
  Example of dynamically using a Space after searching:
179
  ```python
180
+ # search_results = huggingface_space_searcher(arguments={"query": "text to image cat", "top_k": 1}) # Note the arguments dictionary
181
  # print(search_results)
182
  # try:
183
  # cat_image_tool = Tool.from_space(repo_id="user/cat-generator", name="cat_generator_tool")