Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -166,7 +166,85 @@ def arxiv_search(query: str, load_max_docs: int = 3) -> Dict[str, str]:
|
|
| 166 |
)
|
| 167 |
return {"arxiv_results": formatted_search_docs}
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
|
| 172 |
|
|
@@ -183,9 +261,11 @@ class BasicAgent:
|
|
| 183 |
"web_search": web_search,
|
| 184 |
"arxiv_search": arxiv_search,
|
| 185 |
"extract_keywords": extract_keywords,
|
| 186 |
-
"
|
| 187 |
-
"
|
| 188 |
-
"
|
|
|
|
|
|
|
| 189 |
}
|
| 190 |
self._cache = {}
|
| 191 |
self.max_iterations = max_iterations
|
|
|
|
| 166 |
)
|
| 167 |
return {"arxiv_results": formatted_search_docs}
|
| 168 |
|
| 169 |
+
def extract_keywords(text: str) -> list:
|
| 170 |
+
"""
|
| 171 |
+
Simple keyword extractor that splits text into unique keywords.
|
| 172 |
+
|
| 173 |
+
Args:
|
| 174 |
+
text (str): Input text.
|
| 175 |
|
| 176 |
+
Returns:
|
| 177 |
+
list: List of extracted keywords.
|
| 178 |
+
"""
|
| 179 |
+
words = text.lower().split()
|
| 180 |
+
keywords = list(set([w.strip(".,!?") for w in words if len(w) > 3]))
|
| 181 |
+
return keywords
|
| 182 |
+
|
| 183 |
+
import re
|
| 184 |
+
|
| 185 |
+
def calculate_expression(expression: str) -> str:
|
| 186 |
+
"""
|
| 187 |
+
Evaluates a simple mathematical expression and returns the result.
|
| 188 |
+
|
| 189 |
+
Args:
|
| 190 |
+
expression (str): A math expression (e.g., "12 * (3+5) / 4").
|
| 191 |
+
|
| 192 |
+
Returns:
|
| 193 |
+
str: The result of the calculation or an error message if invalid.
|
| 194 |
+
"""
|
| 195 |
+
try:
|
| 196 |
+
# Allow only numbers, operators, parentheses, decimal points, and spaces
|
| 197 |
+
if not re.match(r'^[\d\s\+\-\*\/\(\)\.]+$', expression):
|
| 198 |
+
return "Invalid characters detected in expression."
|
| 199 |
+
|
| 200 |
+
result = eval(expression)
|
| 201 |
+
return str(result)
|
| 202 |
+
except Exception as e:
|
| 203 |
+
return f"Error evaluating expression: {str(e)}"
|
| 204 |
+
|
| 205 |
+
def basic_calculator(a: float, b: float, operation: str) -> str:
|
| 206 |
+
"""
|
| 207 |
+
Perform basic arithmetic operations between two numbers.
|
| 208 |
+
|
| 209 |
+
Args:
|
| 210 |
+
a (float): First number.
|
| 211 |
+
b (float): Second number.
|
| 212 |
+
operation (str): The operation to perform. Options: "add", "subtract", "multiply", "divide".
|
| 213 |
+
|
| 214 |
+
Returns:
|
| 215 |
+
str: The result of the calculation or an error message.
|
| 216 |
+
"""
|
| 217 |
+
try:
|
| 218 |
+
if operation == "add":
|
| 219 |
+
return str(a + b)
|
| 220 |
+
elif operation == "subtract":
|
| 221 |
+
return str(a - b)
|
| 222 |
+
elif operation == "multiply":
|
| 223 |
+
return str(a * b)
|
| 224 |
+
elif operation == "divide":
|
| 225 |
+
if b == 0:
|
| 226 |
+
return "Error: Division by zero is not allowed."
|
| 227 |
+
return str(a / b)
|
| 228 |
+
else:
|
| 229 |
+
return "Invalid operation. Use add, subtract, multiply, or divide."
|
| 230 |
+
except Exception as e:
|
| 231 |
+
return f"Error: {str(e)}"
|
| 232 |
+
|
| 233 |
+
def (items: list, reverse: bool = False) -> list:
|
| 234 |
+
"""
|
| 235 |
+
Sort a list of numbers or strings in ascending or descending order.
|
| 236 |
+
|
| 237 |
+
Args:
|
| 238 |
+
items (list): List of elements (numbers or strings) to be sorted.
|
| 239 |
+
reverse (bool): If True, sort in descending order. Default is False.
|
| 240 |
+
|
| 241 |
+
Returns:
|
| 242 |
+
list: Sorted list.
|
| 243 |
+
"""
|
| 244 |
+
try:
|
| 245 |
+
return sorted(items, reverse=reverse)
|
| 246 |
+
except Exception as e:
|
| 247 |
+
return [f"Error: {str(e)}"]
|
| 248 |
|
| 249 |
|
| 250 |
|
|
|
|
| 261 |
"web_search": web_search,
|
| 262 |
"arxiv_search": arxiv_search,
|
| 263 |
"extract_keywords": extract_keywords,
|
| 264 |
+
"calculate_expression":calculate_expression,
|
| 265 |
+
"basic_calculator":basic_calculator,
|
| 266 |
+
"sort_list":sort_list
|
| 267 |
+
|
| 268 |
+
|
| 269 |
}
|
| 270 |
self._cache = {}
|
| 271 |
self.max_iterations = max_iterations
|