Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,64 +18,57 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
Supports:
|
| 26 |
-
- Basic operations: +, -, *, /, and parentheses.
|
| 27 |
-
- Exponentiation: using either '^' or '**'.
|
| 28 |
-
- Math functions: sqrt, sin, cos, tan, log, exp.
|
| 29 |
-
|
| 30 |
Args:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
Returns:
|
| 34 |
-
str:
|
| 35 |
"""
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
"
|
| 43 |
-
"sin": math.sin,
|
| 44 |
-
"cos": math.cos,
|
| 45 |
-
"tan": math.tan,
|
| 46 |
-
"log": math.log, # natural logarithm by default
|
| 47 |
-
"exp": math.exp,
|
| 48 |
-
"pi": math.pi,
|
| 49 |
-
"e": math.e,
|
| 50 |
-
"abs": abs,
|
| 51 |
-
"pow": pow,
|
| 52 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
result = eval(operation, {"__builtins__": None}, safe_env)
|
| 61 |
-
return f"Result: {result}"
|
| 62 |
|
| 63 |
except Exception as e:
|
| 64 |
-
return f"Error
|
| 65 |
|
| 66 |
# Example usage:
|
| 67 |
if __name__ == "__main__":
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
"3 * (4 + 5)",
|
| 71 |
-
"2^3", # using '^' for exponentiation
|
| 72 |
-
"sqrt(16)",
|
| 73 |
-
"sin(pi/2)",
|
| 74 |
-
"log(e)"
|
| 75 |
-
]
|
| 76 |
|
| 77 |
-
for expr in expressions:
|
| 78 |
-
print(f"{expr} = {extended_calculator(expr)}")
|
| 79 |
@tool
|
| 80 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 81 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
| 21 |
+
def duckduckgo_search(query: str) -> str:
|
| 22 |
+
"""
|
| 23 |
+
A simple search tool that queries DuckDuckGo's Instant Answer API and returns a result snippet.
|
| 24 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
Args:
|
| 26 |
+
query (str): The search query string.
|
| 27 |
+
|
| 28 |
Returns:
|
| 29 |
+
str: A summary from DuckDuckGo or an error message.
|
| 30 |
"""
|
| 31 |
try:
|
| 32 |
+
url = "https://api.duckduckgo.com/"
|
| 33 |
+
params = {
|
| 34 |
+
"q": query,
|
| 35 |
+
"format": "json",
|
| 36 |
+
"no_html": 1,
|
| 37 |
+
"skip_disambig": 1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
}
|
| 39 |
+
response = requests.get(url, params=params)
|
| 40 |
+
if response.status_code != 200:
|
| 41 |
+
return f"Error: Received status code {response.status_code} from DuckDuckGo."
|
| 42 |
+
|
| 43 |
+
data = response.json()
|
| 44 |
+
|
| 45 |
+
# Use the AbstractText if available as a summary.
|
| 46 |
+
abstract_text = data.get("AbstractText", "")
|
| 47 |
+
if abstract_text:
|
| 48 |
+
return f"Result: {abstract_text}"
|
| 49 |
|
| 50 |
+
# If no abstract is provided, try returning the first related topic's text.
|
| 51 |
+
related_topics = data.get("RelatedTopics", [])
|
| 52 |
+
for topic in related_topics:
|
| 53 |
+
# Some topics might be nested; check both levels.
|
| 54 |
+
if isinstance(topic, dict):
|
| 55 |
+
if "Text" in topic:
|
| 56 |
+
return f"Result: {topic['Text']}"
|
| 57 |
+
if "Topics" in topic:
|
| 58 |
+
for subtopic in topic["Topics"]:
|
| 59 |
+
if "Text" in subtopic:
|
| 60 |
+
return f"Result: {subtopic['Text']}"
|
| 61 |
|
| 62 |
+
return "No results found."
|
|
|
|
|
|
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
+
return f"Error performing search: {str(e)}"
|
| 66 |
|
| 67 |
# Example usage:
|
| 68 |
if __name__ == "__main__":
|
| 69 |
+
search_query = "Python programming"
|
| 70 |
+
print(duckduckgo_search(search_query))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
|
|
|
|
|
|
| 72 |
@tool
|
| 73 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 74 |
"""A tool that fetches the current local time in a specified timezone.
|