NatalieCheong commited on
Commit
cb9ee3a
·
verified ·
1 Parent(s): 260b911

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -20
app.py CHANGED
@@ -16,26 +16,38 @@ def my_custom_tool(search_query: str, generate_image: bool = False) -> str:
16
  search_query: The search query to look up
17
  generate_image: Whether to also generate an image based on the search results
18
  """
19
- # First, search using DuckDuckGo
20
- search_tool = DuckDuckGoSearchTool()
21
- results = search_tool(search_query)
22
-
23
- if not results:
24
- return "No results found for the query."
25
-
26
- # Get the first result
27
- first_result = results[0]
28
- summary = f"Found: {first_result['title']}\nSummary: {first_result['snippet']}"
29
-
30
- # If image generation is requested, also generate an image
31
- if generate_image:
32
- try:
33
- image_result = image_generation_tool(search_query)
34
- summary += f"\n\nGenerated visualization based on the query."
35
- except Exception as e:
36
- summary += f"\n\nImage generation failed: {str(e)}"
37
-
38
- return summary
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  @tool
41
  def get_current_time_in_timezone(timezone: str) -> str:
 
16
  search_query: The search query to look up
17
  generate_image: Whether to also generate an image based on the search results
18
  """
19
+ try:
20
+ # Initialize the search tool
21
+ search_tool = DuckDuckGoSearchTool()
22
+
23
+ # Perform the search and add debug output
24
+ print(f"Searching for: {search_query}")
25
+ results = search_tool(search_query)
26
+ print(f"Got {len(results) if results else 0} results")
27
+
28
+ if not results:
29
+ return "No results found for the query."
30
+
31
+ # Get the first result and format it
32
+ first_result = results[0]
33
+ summary = f"Latest AI News:\nTitle: {first_result['title']}\nSummary: {first_result['snippet']}"
34
+
35
+ # Add image generation if requested
36
+ if generate_image:
37
+ try:
38
+ print("Attempting to generate image...")
39
+ image_result = image_generation_tool(search_query)
40
+ summary += f"\n\nGenerated visualization based on the query."
41
+ except Exception as e:
42
+ print(f"Image generation failed: {str(e)}")
43
+ summary += f"\n\nNote: Image generation was requested but failed."
44
+
45
+ return summary
46
+
47
+ except Exception as e:
48
+ error_msg = f"Error in my_custom_tool: {str(e)}"
49
+ print(error_msg) # For debugging
50
+ return error_msg
51
 
52
  @tool
53
  def get_current_time_in_timezone(timezone: str) -> str: