Update app.py
Browse files
app.py
CHANGED
|
@@ -34,29 +34,50 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 37 |
-
@tool
|
| 38 |
-
def news_to_image(topic: str) -> str:
|
| 39 |
"""
|
| 40 |
A tool that searches for recent news on a given topic and creates an image from the top headline.
|
| 41 |
|
| 42 |
Args:
|
| 43 |
topic: The subject to search for.
|
| 44 |
"""
|
| 45 |
-
|
| 46 |
# Use DuckDuckGo to search for news
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
# Extract the first headline (title or snippet)
|
| 51 |
-
first_result = results[0]["snippet"] if results else "Breaking news"
|
| 52 |
|
| 53 |
# Generate the image using your existing image_generation_tool
|
| 54 |
-
image_url = image_generation_tool.run(prompt=first_result)
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
-
return f"Failed to generate image
|
| 60 |
|
| 61 |
final_answer = FinalAnswerTool()
|
| 62 |
|
|
|
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 37 |
+
#@tool
|
| 38 |
+
#def news_to_image(topic: str) -> str:
|
| 39 |
"""
|
| 40 |
A tool that searches for recent news on a given topic and creates an image from the top headline.
|
| 41 |
|
| 42 |
Args:
|
| 43 |
topic: The subject to search for.
|
| 44 |
"""
|
| 45 |
+
# try:
|
| 46 |
# Use DuckDuckGo to search for news
|
| 47 |
+
# search_tool = DuckDuckGoSearchTool()
|
| 48 |
+
# results = search_tool.run(f"{topic} news") # Returns a list of links and snippets
|
| 49 |
|
| 50 |
# Extract the first headline (title or snippet)
|
| 51 |
+
#first_result = results[0]["snippet"] if results else "Breaking news"
|
| 52 |
|
| 53 |
# Generate the image using your existing image_generation_tool
|
| 54 |
+
#image_url = image_generation_tool.run(prompt=first_result)
|
| 55 |
+
|
| 56 |
+
#return f"Created image for headline: '{first_result}' → {image_url}"
|
| 57 |
|
| 58 |
+
# except Exception as e:
|
| 59 |
+
# return f"Failed to generate image from news: {str(e)}"
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
@tool
|
| 63 |
+
def news_to_image(topic: str) -> str:
|
| 64 |
+
"""
|
| 65 |
+
Fetches recent news on a topic and generates an image from the top headline.
|
| 66 |
+
Args:
|
| 67 |
+
topic: The news topic to search for.
|
| 68 |
+
"""
|
| 69 |
+
# Instead of calling DuckDuckGoSearchTool directly...
|
| 70 |
+
search_tool = agent.tool_dict["DuckDuckGoSearchTool"]
|
| 71 |
+
image_tool = agent.tool_dict["TextToImageTool"] # Assuming that's the name of your imported tool
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
search_results = search_tool(topic + " news")
|
| 75 |
+
first_result = search_results[0]["snippet"] if search_results else "Breaking news"
|
| 76 |
+
image = image_tool(first_result)
|
| 77 |
+
return f"Image created for: '{first_result}' → {image}"
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
+
return f"Failed to generate image: {str(e)}"
|
| 81 |
|
| 82 |
final_answer = FinalAnswerTool()
|
| 83 |
|