Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,8 @@ from smolagents import LiteLLMModel, CodeAgent, ToolCallingAgent, Tool
|
|
| 4 |
import wikipedia
|
| 5 |
import gradio as gr
|
| 6 |
import pandas as pd
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Optional: Hugging Face token (for private models)
|
| 9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
@@ -25,6 +27,59 @@ class WebSearchTool(Tool):
|
|
| 25 |
tool = DuckDuckGoSearchTool()
|
| 26 |
return tool.forward(query)
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
class LoadCsvTool(Tool):
|
| 29 |
name = "load_csv"
|
| 30 |
description = "Load and analyze CSV file. Returns summary statistics and first few rows. Input: file path."
|
|
@@ -123,7 +178,7 @@ You can send JSON payloads to external webhook URLs using the webhook_post tool.
|
|
| 123 |
|
| 124 |
# --- Initialize Tool-Calling Agent ---
|
| 125 |
agent = ToolCallingAgent(
|
| 126 |
-
tools=[WebSearchTool(), WikipediaTool(), WeatherTool(), LoadCsvTool(), WebhookPostTool()],
|
| 127 |
model=model,
|
| 128 |
max_steps=10,
|
| 129 |
)
|
|
|
|
| 4 |
import wikipedia
|
| 5 |
import gradio as gr
|
| 6 |
import pandas as pd
|
| 7 |
+
import json
|
| 8 |
+
from datetime import datetime
|
| 9 |
|
| 10 |
# Optional: Hugging Face token (for private models)
|
| 11 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 27 |
tool = DuckDuckGoSearchTool()
|
| 28 |
return tool.forward(query)
|
| 29 |
|
| 30 |
+
class MemoryTool(Tool):
|
| 31 |
+
name = "memory_store"
|
| 32 |
+
description = "Store and retrieve persistent agent memory."
|
| 33 |
+
inputs = {
|
| 34 |
+
"action": {
|
| 35 |
+
"type": "string",
|
| 36 |
+
"description": "Either 'write' or 'read'"
|
| 37 |
+
},
|
| 38 |
+
"key": {
|
| 39 |
+
"type": "string",
|
| 40 |
+
"description": "Memory key"
|
| 41 |
+
},
|
| 42 |
+
"value": {
|
| 43 |
+
"type": "string",
|
| 44 |
+
"description": "Memory value (required for write)"
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
output_type = "string"
|
| 48 |
+
|
| 49 |
+
def __init__(self, memory_path: str = "/app/memory.json"):
|
| 50 |
+
self.memory_path = memory_path
|
| 51 |
+
if not os.path.exists(self.memory_path):
|
| 52 |
+
with open(self.memory_path, "w") as f:
|
| 53 |
+
json.dump([], f)
|
| 54 |
+
|
| 55 |
+
def forward(self, action: str, key: str, value: str = "") -> str:
|
| 56 |
+
try:
|
| 57 |
+
with open(self.memory_path, "r") as f:
|
| 58 |
+
memory = json.load(f)
|
| 59 |
+
|
| 60 |
+
if action == "write":
|
| 61 |
+
memory.append({
|
| 62 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 63 |
+
"key": key,
|
| 64 |
+
"value": value
|
| 65 |
+
})
|
| 66 |
+
with open(self.memory_path, "w") as f:
|
| 67 |
+
json.dump(memory, f, indent=2)
|
| 68 |
+
return "Memory stored successfully."
|
| 69 |
+
|
| 70 |
+
elif action == "read":
|
| 71 |
+
results = [m for m in memory if m["key"] == key]
|
| 72 |
+
if not results:
|
| 73 |
+
return "No memory found for this key."
|
| 74 |
+
return json.dumps(results, indent=2)
|
| 75 |
+
|
| 76 |
+
else:
|
| 77 |
+
return "Invalid action. Use 'write' or 'read'."
|
| 78 |
+
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return f"Memory error: {str(e)}"
|
| 81 |
+
|
| 82 |
+
|
| 83 |
class LoadCsvTool(Tool):
|
| 84 |
name = "load_csv"
|
| 85 |
description = "Load and analyze CSV file. Returns summary statistics and first few rows. Input: file path."
|
|
|
|
| 178 |
|
| 179 |
# --- Initialize Tool-Calling Agent ---
|
| 180 |
agent = ToolCallingAgent(
|
| 181 |
+
tools=[WebSearchTool(), WikipediaTool(), WeatherTool(), LoadCsvTool(), WebhookPostTool(), MemoryTool()],
|
| 182 |
model=model,
|
| 183 |
max_steps=10,
|
| 184 |
)
|