Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,15 +5,14 @@ import requests
|
|
5 |
import yaml
|
6 |
import os
|
7 |
from typing import Dict, List, Optional
|
8 |
-
|
9 |
@tool
|
10 |
def fetch_news(topic: str, num_results: int = 5) -> List[Dict]:
|
11 |
"""Fetches recent news articles about any topic using Serper.dev.
|
12 |
-
|
13 |
Args:
|
14 |
topic: The topic to search for news about
|
15 |
num_results: Number of news articles to retrieve (default: 5)
|
16 |
-
|
17 |
Returns:
|
18 |
List of dictionaries containing article information
|
19 |
"""
|
@@ -21,7 +20,7 @@ def fetch_news(topic: str, num_results: int = 5) -> List[Dict]:
|
|
21 |
api_key = os.environ.get("SERPER_API_KEY")
|
22 |
if not api_key:
|
23 |
return "Error: SERPER_API_KEY not found in environment variables"
|
24 |
-
|
25 |
url = f"https://google.serper.dev/news"
|
26 |
headers = {
|
27 |
"X-API-KEY": api_key
|
@@ -31,15 +30,15 @@ def fetch_news(topic: str, num_results: int = 5) -> List[Dict]:
|
|
31 |
"gl": "us",
|
32 |
"hl": "en"
|
33 |
}
|
34 |
-
|
35 |
response = requests.get(url, headers=headers, params=params)
|
36 |
response.raise_for_status()
|
37 |
-
|
38 |
results = response.json()
|
39 |
-
|
40 |
if "news" not in results:
|
41 |
return []
|
42 |
-
|
43 |
articles = []
|
44 |
for article in results["news"][:num_results]:
|
45 |
articles.append({
|
@@ -49,33 +48,32 @@ def fetch_news(topic: str, num_results: int = 5) -> List[Dict]:
|
|
49 |
'link': article.get('link', 'No link'),
|
50 |
'snippet': article.get('snippet', 'No preview available')
|
51 |
})
|
52 |
-
|
53 |
return articles
|
54 |
-
|
55 |
except Exception as e:
|
56 |
return f"Error: {str(e)}"
|
57 |
-
|
58 |
@tool
|
59 |
def summarize_news(articles: List[Dict]) -> str:
|
60 |
"""Creates a summary of the news articles followed by a list of sources.
|
61 |
-
|
62 |
Args:
|
63 |
articles: List of article dictionaries containing title, source, date, link, and snippet
|
64 |
-
|
65 |
Returns:
|
66 |
A string containing a summary followed by article references
|
67 |
"""
|
68 |
if not articles or not isinstance(articles, list):
|
69 |
return "No articles to summarize"
|
70 |
-
|
71 |
# Collect all snippets for the overall summary
|
72 |
all_snippets = [article['snippet'] for article in articles if article.get('snippet')]
|
73 |
-
|
74 |
# Create a high-level summary from snippets
|
75 |
summary = "📰 Summary:\n"
|
76 |
summary += "Latest news covers " + ", ".join(set(article['source'] for article in articles)) + ". "
|
77 |
summary += "Key points: " + ". ".join(all_snippets[:2]) + "\n\n"
|
78 |
-
|
79 |
# List individual articles
|
80 |
summary += "🔍 Articles:\n"
|
81 |
for idx, article in enumerate(articles, 1):
|
@@ -83,17 +81,15 @@ def summarize_news(articles: List[Dict]) -> str:
|
|
83 |
link = article['link']
|
84 |
date = article['date']
|
85 |
snippet = article['snippet'][:100] + "..." if len(article['snippet']) > 100 else article['snippet']
|
86 |
-
|
87 |
-
summary += f"{idx}.
|
88 |
summary += f" {snippet}\n"
|
89 |
summary += f" [Read more]({link}) ({date})\n\n"
|
90 |
-
|
91 |
-
return summary
|
92 |
|
|
|
93 |
# Load prompt templates
|
94 |
with open("prompts.yaml", 'r') as stream:
|
95 |
prompt_templates = yaml.safe_load(stream)
|
96 |
-
|
97 |
# Initialize the model
|
98 |
model = HfApiModel(
|
99 |
max_tokens=2096,
|
@@ -101,9 +97,7 @@ model = HfApiModel(
|
|
101 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
102 |
custom_role_conversions=None,
|
103 |
)
|
104 |
-
|
105 |
final_answer = FinalAnswerTool()
|
106 |
-
|
107 |
# Create the agent with all tools
|
108 |
agent = CodeAgent(
|
109 |
model=model,
|
@@ -116,13 +110,6 @@ agent = CodeAgent(
|
|
116 |
description="An agent that fetches and summarizes news about any topic",
|
117 |
prompt_templates=prompt_templates
|
118 |
)
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
# Override the interact_with_agent method to always reset
|
123 |
-
return super().interact_with_agent(prompt, reset_agent_memory=True)
|
124 |
-
|
125 |
-
# Launch the modified Gradio interface
|
126 |
-
if __name__ == "__main__":
|
127 |
-
ui = ResetGradioUI(agent)
|
128 |
-
ui.launch(debug=True, share=True) # Remove reset_agent_memory from launch parameters
|
|
|
5 |
import yaml
|
6 |
import os
|
7 |
from typing import Dict, List, Optional
|
|
|
8 |
@tool
|
9 |
def fetch_news(topic: str, num_results: int = 5) -> List[Dict]:
|
10 |
"""Fetches recent news articles about any topic using Serper.dev.
|
11 |
+
|
12 |
Args:
|
13 |
topic: The topic to search for news about
|
14 |
num_results: Number of news articles to retrieve (default: 5)
|
15 |
+
|
16 |
Returns:
|
17 |
List of dictionaries containing article information
|
18 |
"""
|
|
|
20 |
api_key = os.environ.get("SERPER_API_KEY")
|
21 |
if not api_key:
|
22 |
return "Error: SERPER_API_KEY not found in environment variables"
|
23 |
+
|
24 |
url = f"https://google.serper.dev/news"
|
25 |
headers = {
|
26 |
"X-API-KEY": api_key
|
|
|
30 |
"gl": "us",
|
31 |
"hl": "en"
|
32 |
}
|
33 |
+
|
34 |
response = requests.get(url, headers=headers, params=params)
|
35 |
response.raise_for_status()
|
36 |
+
|
37 |
results = response.json()
|
38 |
+
|
39 |
if "news" not in results:
|
40 |
return []
|
41 |
+
|
42 |
articles = []
|
43 |
for article in results["news"][:num_results]:
|
44 |
articles.append({
|
|
|
48 |
'link': article.get('link', 'No link'),
|
49 |
'snippet': article.get('snippet', 'No preview available')
|
50 |
})
|
51 |
+
|
52 |
return articles
|
53 |
+
|
54 |
except Exception as e:
|
55 |
return f"Error: {str(e)}"
|
|
|
56 |
@tool
|
57 |
def summarize_news(articles: List[Dict]) -> str:
|
58 |
"""Creates a summary of the news articles followed by a list of sources.
|
59 |
+
|
60 |
Args:
|
61 |
articles: List of article dictionaries containing title, source, date, link, and snippet
|
62 |
+
|
63 |
Returns:
|
64 |
A string containing a summary followed by article references
|
65 |
"""
|
66 |
if not articles or not isinstance(articles, list):
|
67 |
return "No articles to summarize"
|
68 |
+
|
69 |
# Collect all snippets for the overall summary
|
70 |
all_snippets = [article['snippet'] for article in articles if article.get('snippet')]
|
71 |
+
|
72 |
# Create a high-level summary from snippets
|
73 |
summary = "📰 Summary:\n"
|
74 |
summary += "Latest news covers " + ", ".join(set(article['source'] for article in articles)) + ". "
|
75 |
summary += "Key points: " + ". ".join(all_snippets[:2]) + "\n\n"
|
76 |
+
|
77 |
# List individual articles
|
78 |
summary += "🔍 Articles:\n"
|
79 |
for idx, article in enumerate(articles, 1):
|
|
|
81 |
link = article['link']
|
82 |
date = article['date']
|
83 |
snippet = article['snippet'][:100] + "..." if len(article['snippet']) > 100 else article['snippet']
|
84 |
+
|
85 |
+
summary += f"{idx}. {title}\n"
|
86 |
summary += f" {snippet}\n"
|
87 |
summary += f" [Read more]({link}) ({date})\n\n"
|
|
|
|
|
88 |
|
89 |
+
return summary
|
90 |
# Load prompt templates
|
91 |
with open("prompts.yaml", 'r') as stream:
|
92 |
prompt_templates = yaml.safe_load(stream)
|
|
|
93 |
# Initialize the model
|
94 |
model = HfApiModel(
|
95 |
max_tokens=2096,
|
|
|
97 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
98 |
custom_role_conversions=None,
|
99 |
)
|
|
|
100 |
final_answer = FinalAnswerTool()
|
|
|
101 |
# Create the agent with all tools
|
102 |
agent = CodeAgent(
|
103 |
model=model,
|
|
|
110 |
description="An agent that fetches and summarizes news about any topic",
|
111 |
prompt_templates=prompt_templates
|
112 |
)
|
113 |
+
# Launch the Gradio interface
|
114 |
+
if name == "main":
|
115 |
+
GradioUI(agent).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|