Spaces:
Running
Running
same changes more robust approach
Browse files
app.py
CHANGED
@@ -10,42 +10,48 @@ from Gradio_UI import GradioUI
|
|
10 |
duckduckgo_tool=DuckDuckGoSearchTool()
|
11 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
12 |
@tool
|
|
|
|
|
13 |
def animewatcher(anime_name: str) -> str:
|
14 |
"""A tool that searches for anime details using DuckDuckGo.
|
15 |
Args:
|
16 |
anime_name: The name of the anime to search for.
|
|
|
|
|
17 |
"""
|
18 |
try:
|
19 |
search_query = f"{anime_name} anime details"
|
20 |
results = duckduckgo_tool(search_query) # Fetch results from DuckDuckGo
|
21 |
|
22 |
-
# Debugging: Print
|
23 |
-
print(f"Debug: DuckDuckGo results -> {results} (Type: {type(results)})")
|
24 |
-
|
|
|
25 |
if isinstance(results, str):
|
26 |
-
# Try to parse JSON if possible (if the API returns a JSON string)
|
27 |
try:
|
28 |
-
|
29 |
-
results = json.loads(results)
|
30 |
except json.JSONDecodeError:
|
31 |
-
return f"Error: Unexpected response format.
|
32 |
-
|
|
|
33 |
if not isinstance(results, list) or not results:
|
34 |
return f"No results found for '{anime_name}'. Maybe it's underrated!"
|
35 |
|
36 |
-
# Limit results to
|
37 |
results = results[:3]
|
38 |
|
39 |
-
|
|
|
40 |
for i, result in enumerate(results, 1):
|
41 |
title = result.get('title', 'No title available')
|
42 |
href = result.get('href', 'No link available')
|
43 |
-
response += f"{i}. **{title}**\n{href}\n\n"
|
44 |
|
45 |
return response.strip()
|
46 |
|
47 |
except Exception as e:
|
48 |
-
return f"Error searching for '{anime_name}': {str(e)}"
|
|
|
49 |
|
50 |
@tool
|
51 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
10 |
duckduckgo_tool=DuckDuckGoSearchTool()
|
11 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
12 |
@tool
|
13 |
+
import json
|
14 |
+
|
15 |
def animewatcher(anime_name: str) -> str:
|
16 |
"""A tool that searches for anime details using DuckDuckGo.
|
17 |
Args:
|
18 |
anime_name: The name of the anime to search for.
|
19 |
+
Returns:
|
20 |
+
A formatted string with anime search results or an error message.
|
21 |
"""
|
22 |
try:
|
23 |
search_query = f"{anime_name} anime details"
|
24 |
results = duckduckgo_tool(search_query) # Fetch results from DuckDuckGo
|
25 |
|
26 |
+
# Debugging: Print what we receive
|
27 |
+
print(f"Debug: Raw DuckDuckGo results -> {results} (Type: {type(results)})")
|
28 |
+
|
29 |
+
# If results is a string, try parsing it as JSON (if possible)
|
30 |
if isinstance(results, str):
|
|
|
31 |
try:
|
32 |
+
results = json.loads(results) # Attempt JSON parsing
|
|
|
33 |
except json.JSONDecodeError:
|
34 |
+
return f"Error: Unexpected response format. Raw output: {results}"
|
35 |
+
|
36 |
+
# Ensure results is a list
|
37 |
if not isinstance(results, list) or not results:
|
38 |
return f"No results found for '{anime_name}'. Maybe it's underrated!"
|
39 |
|
40 |
+
# Limit results to the first 3 entries
|
41 |
results = results[:3]
|
42 |
|
43 |
+
# Format response
|
44 |
+
response = f"🔍 **Anime Search Results for '{anime_name}':**\n\n"
|
45 |
for i, result in enumerate(results, 1):
|
46 |
title = result.get('title', 'No title available')
|
47 |
href = result.get('href', 'No link available')
|
48 |
+
response += f"{i}. **{title}**\n🔗 {href}\n\n"
|
49 |
|
50 |
return response.strip()
|
51 |
|
52 |
except Exception as e:
|
53 |
+
return f"⚠️ Error searching for '{anime_name}': {str(e)}"
|
54 |
+
|
55 |
|
56 |
@tool
|
57 |
def get_current_time_in_timezone(timezone: str) -> str:
|