Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,20 +18,51 @@ 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 |
import requests
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
data = response.json()
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
# Execute the function and prepare the final answer
|
| 32 |
-
result = get_weather("New York")
|
| 33 |
-
final_answer = f"The current weather in New York is: {result}"
|
| 34 |
-
print(final_answer)
|
| 35 |
|
| 36 |
@tool
|
| 37 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
| 21 |
+
@tool
|
| 22 |
+
def duckduckgo_search(query: str) -> str:
|
| 23 |
+
"""A tool that performs a DuckDuckGo search using the Instant Answer API.
|
| 24 |
+
|
| 25 |
+
Args:
|
| 26 |
+
query: The search query string.
|
| 27 |
+
|
| 28 |
+
Returns:
|
| 29 |
+
str: A summary of the search results or an error message.
|
| 30 |
+
"""
|
| 31 |
import requests
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
url = "https://api.duckduckgo.com/"
|
| 35 |
+
params = {
|
| 36 |
+
"q": query,
|
| 37 |
+
"format": "json",
|
| 38 |
+
"no_html": 1,
|
| 39 |
+
"skip_disambig": 1,
|
| 40 |
+
}
|
| 41 |
+
response = requests.get(url, params=params)
|
| 42 |
+
if response.status_code != 200:
|
| 43 |
+
return f"Error: Received status code {response.status_code} from DuckDuckGo."
|
| 44 |
+
|
| 45 |
data = response.json()
|
| 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 available related topic's text.
|
| 51 |
+
related_topics = data.get("RelatedTopics", [])
|
| 52 |
+
for topic in related_topics:
|
| 53 |
+
if isinstance(topic, dict):
|
| 54 |
+
if "Text" in topic:
|
| 55 |
+
return f"Result: {topic['Text']}"
|
| 56 |
+
if "Topics" in topic:
|
| 57 |
+
for subtopic in topic["Topics"]:
|
| 58 |
+
if "Text" in subtopic:
|
| 59 |
+
return f"Result: {subtopic['Text']}"
|
| 60 |
+
|
| 61 |
+
return "No results found."
|
| 62 |
+
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return f"Error performing search: {str(e)}"
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
@tool
|
| 68 |
def get_current_time_in_timezone(timezone: str) -> str:
|