Spaces:
Sleeping
Sleeping
Update app.py
Browse filesAdding the get_news_updates tool
app.py
CHANGED
|
@@ -7,6 +7,10 @@ from tools.final_answer import FinalAnswerTool
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
|
@@ -33,6 +37,38 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
|
| 11 |
+
NEWSAPI_KEY = "be0caa3084bf4bdc9052203cb149d9e1"
|
| 12 |
+
newsapi = NewApiClient(api_key = NEWSAPI_KEY)
|
| 13 |
+
|
| 14 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 15 |
@tool
|
| 16 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 39 |
|
| 40 |
+
@tool
|
| 41 |
+
def get_news_updates(query: str) -> str:
|
| 42 |
+
"""
|
| 43 |
+
Fetches and summarizes recent news articles based on a query.
|
| 44 |
+
|
| 45 |
+
Args:
|
| 46 |
+
query: The topic or keyword to search for in news articles.
|
| 47 |
+
"""
|
| 48 |
+
try:
|
| 49 |
+
# Fetch news articles based on the query
|
| 50 |
+
response = newsapi.get_everything(q=query, language='en', sort_by='publishedAt')
|
| 51 |
+
|
| 52 |
+
if response['status'] == 'ok' and len(response['articles']) > 0:
|
| 53 |
+
articles = response['articles']
|
| 54 |
+
|
| 55 |
+
# Summarize the first few articles
|
| 56 |
+
summaries = []
|
| 57 |
+
for article in articles[:3]: # Limiting to 3 for brevity
|
| 58 |
+
title = article.get('title', '')
|
| 59 |
+
description = article.get('description', '')
|
| 60 |
+
source = article.get('source', {}).get('name', '')
|
| 61 |
+
url = article.get('url', '')
|
| 62 |
+
datetime = article.get('publishedAt', '')
|
| 63 |
+
|
| 64 |
+
summary = f"Title: {title}\nSource: {source}\nDescription: {description}\nLink: {url}\nPublished at: {datetime}"
|
| 65 |
+
summaries.append(summary)
|
| 66 |
+
|
| 67 |
+
return '\n\n'.join(summaries)
|
| 68 |
+
else:
|
| 69 |
+
return "No news articles found for the given query."
|
| 70 |
+
except Exception as e:
|
| 71 |
+
return f"Error fetching the latest news because of {str(e)}"
|
| 72 |
|
| 73 |
final_answer = FinalAnswerTool()
|
| 74 |
|