Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -91,11 +91,11 @@ class HuggingFaceSpaceSearcherTool(Tool):
|
|
| 91 |
f" Likes: {space_data.likes if hasattr(space_data, 'likes') else 'N/A'}\n"
|
| 92 |
f" Last Modified: {space_data.lastModified if hasattr(space_data, 'lastModified') else 'N/A'}\n\n"
|
| 93 |
)
|
| 94 |
-
results += ("\nTo use one of these, you
|
| 95 |
"`Tool.from_space(repo_id='SPACE_ID_HERE', name='custom_tool_name')`. "
|
| 96 |
"Then call that new tool: `result = custom_tool_name(argument_name=value)`. "
|
| 97 |
"The arguments depend on the specific Space. If `Tool.from_space` fails, "
|
| 98 |
-
"the Space might not have a compatible public API.")
|
| 99 |
return results
|
| 100 |
except Exception as e:
|
| 101 |
print(f"Error searching Spaces: {str(e)}")
|
|
@@ -121,10 +121,10 @@ Follow these steps:
|
|
| 121 |
1. **Understand the Request:** Carefully analyze the user's prompt. Identify the core task and any specific requirements or inputs.
|
| 122 |
2. **Check Predefined Tools:** Review your list of available tools. If a predefined tool can directly address the request, use it.
|
| 123 |
* For the 'huggingface_space_searcher' tool, call it with direct keyword arguments like: `huggingface_space_searcher(query="your search term", top_k=3)`. The `query` is mandatory. `top_k` is optional and defaults to 3 if not provided.
|
| 124 |
-
3. **Search for Spaces (If Needed):** If no predefined tool is suitable, use the `huggingface_space_searcher` tool as described above.
|
| 125 |
-
4. **Select and Instantiate a Space Tool (PRIORITY):** From the search results, choose the most promising Space. **
|
| 126 |
-
* If `Tool.from_space()` succeeds, you now have a new tool. Call this new tool with the appropriate arguments for that Space.
|
| 127 |
-
* If `Tool.from_space()` fails (e.g., raises an exception),
|
| 128 |
5. **Execute the Tool:** Call the tool (predefined, or dynamically created via `Tool.from_space()`) with the necessary arguments.
|
| 129 |
* **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.
|
| 130 |
* **Imports in Generated Code:** If your code block for execution uses modules like `os` or `uuid`, **you must include the import statements (e.g., `import os`, `import uuid`) within that specific code block.**
|
|
@@ -133,7 +133,7 @@ Follow these steps:
|
|
| 133 |
* If a tool returns text, return that text.
|
| 134 |
7. **Clarity and Error Handling:** If you encounter issues (e.g., a Space tool fails, required inputs are missing), clearly explain the problem in your response. If a Space doesn't work, try to explain why or suggest an alternative if possible.
|
| 135 |
|
| 136 |
-
Example of the PREFERRED way to use a discovered Space:
|
| 137 |
```python
|
| 138 |
# User prompt: "Find a space that can make an image of a cat and use it."
|
| 139 |
#
|
|
@@ -141,7 +141,7 @@ Example of the PREFERRED way to use a discovered Space:
|
|
| 141 |
# search_results = huggingface_space_searcher(query="text to image cat", top_k=1)
|
| 142 |
# print(search_results) # Assume 'someuser/cat-image-generator' is found.
|
| 143 |
#
|
| 144 |
-
# Step 2: Try to create a tool from the discovered space
|
| 145 |
# try:
|
| 146 |
# cat_tool = Tool.from_space(repo_id="someuser/cat-image-generator", name="cat_image_generator_tool")
|
| 147 |
# # Now use the newly created tool. Arguments depend on the Space's API.
|
|
@@ -149,9 +149,10 @@ Example of the PREFERRED way to use a discovered Space:
|
|
| 149 |
# image_filepath = cat_tool(prompt="A fluffy siamese cat, cyberpunk style")
|
| 150 |
# return image_filepath # Return the filepath directly
|
| 151 |
# except Exception as e:
|
| 152 |
-
# print(f"Failed to create or use tool from Space 'someuser/cat-image-generator': {e}")
|
| 153 |
-
# #
|
| 154 |
-
# #
|
|
|
|
| 155 |
```
|
| 156 |
|
| 157 |
Example of using a predefined tool that returns a filepath:
|
|
@@ -171,15 +172,20 @@ def gradio_interface(user_prompt, input_image_path, input_audio_path, input_vide
|
|
| 171 |
try:
|
| 172 |
progress(0, desc="Initializing Agent...")
|
| 173 |
full_prompt_with_instructions = f"{AGENT_INSTRUCTIONS}\n\nUSER PROMPT: {user_prompt}"
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
progress(0.2, desc="Agent processing request...")
|
| 182 |
-
|
|
|
|
| 183 |
|
| 184 |
progress(0.8, desc="Processing result...")
|
| 185 |
outputs = {
|
|
@@ -244,7 +250,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 244 |
|
| 245 |
submit_button.click(
|
| 246 |
fn=gradio_interface,
|
| 247 |
-
inputs=[prompt_input, input_image, input_audio, input_video,
|
| 248 |
outputs=[image_output, file_output, path_output, audio_output, model3d_output, text_output]
|
| 249 |
)
|
| 250 |
|
|
|
|
| 91 |
f" Likes: {space_data.likes if hasattr(space_data, 'likes') else 'N/A'}\n"
|
| 92 |
f" Last Modified: {space_data.lastModified if hasattr(space_data, 'lastModified') else 'N/A'}\n\n"
|
| 93 |
)
|
| 94 |
+
results += ("\nTo use one of these, you **MUST** first try creating a tool using " # Emphasized MUST
|
| 95 |
"`Tool.from_space(repo_id='SPACE_ID_HERE', name='custom_tool_name')`. "
|
| 96 |
"Then call that new tool: `result = custom_tool_name(argument_name=value)`. "
|
| 97 |
"The arguments depend on the specific Space. If `Tool.from_space` fails, "
|
| 98 |
+
"the Space might not have a compatible public API for this method.")
|
| 99 |
return results
|
| 100 |
except Exception as e:
|
| 101 |
print(f"Error searching Spaces: {str(e)}")
|
|
|
|
| 121 |
1. **Understand the Request:** Carefully analyze the user's prompt. Identify the core task and any specific requirements or inputs.
|
| 122 |
2. **Check Predefined Tools:** Review your list of available tools. If a predefined tool can directly address the request, use it.
|
| 123 |
* For the 'huggingface_space_searcher' tool, call it with direct keyword arguments like: `huggingface_space_searcher(query="your search term", top_k=3)`. The `query` is mandatory. `top_k` is optional and defaults to 3 if not provided.
|
| 124 |
+
3. **Search for Spaces (If Needed):** If no predefined tool is suitable, use the `huggingface_space_searcher` tool as described above. The search results will explicitly tell you to use `Tool.from_space()`.
|
| 125 |
+
4. **Select and Instantiate a Space Tool (CRITICAL PRIORITY):** From the search results, choose the most promising Space. **You MUST attempt to use this Space by creating a tool from it using `Tool.from_space(repo_id='SELECTED_SPACE_ID', name='a_unique_and_descriptive_tool_name')`. DO NOT use `gradio_client.Client()` directly unless `Tool.from_space()` explicitly fails for that Space.**
|
| 126 |
+
* If `Tool.from_space()` succeeds, you now have a new tool. Call this new tool with the appropriate arguments for that Space (e.g., `newly_created_tool(prompt="some prompt")`).
|
| 127 |
+
* If `Tool.from_space()` fails (e.g., raises an exception), print a message saying it failed and then you may consider trying the next Space from your search results using `Tool.from_space()` again, or falling back to a predefined tool if appropriate. Only consider `gradio_client.Client()` as an absolute last resort if all other methods fail and you have a very specific understanding of the Space's raw API.
|
| 128 |
5. **Execute the Tool:** Call the tool (predefined, or dynamically created via `Tool.from_space()`) with the necessary arguments.
|
| 129 |
* **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.
|
| 130 |
* **Imports in Generated Code:** If your code block for execution uses modules like `os` or `uuid`, **you must include the import statements (e.g., `import os`, `import uuid`) within that specific code block.**
|
|
|
|
| 133 |
* If a tool returns text, return that text.
|
| 134 |
7. **Clarity and Error Handling:** If you encounter issues (e.g., a Space tool fails, required inputs are missing), clearly explain the problem in your response. If a Space doesn't work, try to explain why or suggest an alternative if possible.
|
| 135 |
|
| 136 |
+
Example of the **CORRECT AND PREFERRED** way to use a discovered Space:
|
| 137 |
```python
|
| 138 |
# User prompt: "Find a space that can make an image of a cat and use it."
|
| 139 |
#
|
|
|
|
| 141 |
# search_results = huggingface_space_searcher(query="text to image cat", top_k=1)
|
| 142 |
# print(search_results) # Assume 'someuser/cat-image-generator' is found.
|
| 143 |
#
|
| 144 |
+
# Step 2: Try to create a tool from the discovered space using Tool.from_space()
|
| 145 |
# try:
|
| 146 |
# cat_tool = Tool.from_space(repo_id="someuser/cat-image-generator", name="cat_image_generator_tool")
|
| 147 |
# # Now use the newly created tool. Arguments depend on the Space's API.
|
|
|
|
| 149 |
# image_filepath = cat_tool(prompt="A fluffy siamese cat, cyberpunk style")
|
| 150 |
# return image_filepath # Return the filepath directly
|
| 151 |
# except Exception as e:
|
| 152 |
+
# print(f"Failed to create or use tool from Space 'someuser/cat-image-generator' using Tool.from_space(): {e}")
|
| 153 |
+
# # If Tool.from_space() fails, DO NOT immediately try gradio_client.Client().
|
| 154 |
+
# # Instead, consider another space or a predefined tool.
|
| 155 |
+
# # return "Could not use the discovered space via Tool.from_space(). Trying a fallback..." (then try another step)
|
| 156 |
```
|
| 157 |
|
| 158 |
Example of using a predefined tool that returns a filepath:
|
|
|
|
| 172 |
try:
|
| 173 |
progress(0, desc="Initializing Agent...")
|
| 174 |
full_prompt_with_instructions = f"{AGENT_INSTRUCTIONS}\n\nUSER PROMPT: {user_prompt}"
|
| 175 |
+
|
| 176 |
+
# Prepare a dictionary of global variables for the agent's execution scope
|
| 177 |
+
# These will be available as global variables in the agent's Python execution environment
|
| 178 |
+
# when agent.run is called with the code_globals parameter.
|
| 179 |
+
agent_code_globals = {}
|
| 180 |
+
if input_image_path: agent_code_globals["input_image_path"] = str(input_image_path)
|
| 181 |
+
if input_audio_path: agent_code_globals["input_audio_path"] = str(input_audio_path)
|
| 182 |
+
if input_video_path: agent_code_globals["input_video_path"] = str(input_video_path)
|
| 183 |
+
if input_3d_model_path: agent_code_globals["input_3d_model_path"] = str(input_3d_model_path)
|
| 184 |
+
if input_file_path: agent_code_globals["input_file_path"] = str(input_file_path)
|
| 185 |
|
| 186 |
progress(0.2, desc="Agent processing request...")
|
| 187 |
+
# Pass the global variables using the code_globals parameter
|
| 188 |
+
result = agent.run(full_prompt_with_instructions, code_globals=agent_code_globals)
|
| 189 |
|
| 190 |
progress(0.8, desc="Processing result...")
|
| 191 |
outputs = {
|
|
|
|
| 250 |
|
| 251 |
submit_button.click(
|
| 252 |
fn=gradio_interface,
|
| 253 |
+
inputs=[prompt_input, input_image, input_audio, input_video, input_3d_model_path, input_file],
|
| 254 |
outputs=[image_output, file_output, path_output, audio_output, model3d_output, text_output]
|
| 255 |
)
|
| 256 |
|