girishwangikar
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,7 @@ from dataclasses import dataclass
|
|
13 |
|
14 |
@dataclass
|
15 |
class SearchResult:
|
16 |
-
"""Data class to store search results"""
|
17 |
title: str
|
18 |
link: str
|
19 |
date: str
|
@@ -25,6 +25,7 @@ class GroqLLM:
|
|
25 |
self.model_name = model_name
|
26 |
|
27 |
def __call__(self, prompt: Union[str, dict, List[Dict]]) -> str:
|
|
|
28 |
try:
|
29 |
prompt_str = str(prompt) if isinstance(prompt, (dict, list)) else prompt
|
30 |
completion = self.client.chat.completions.create(
|
@@ -47,13 +48,16 @@ class NewsAnalysisAgent(CodeAgent):
|
|
47 |
|
48 |
@property
|
49 |
def articles(self) -> List[Dict]:
|
|
|
50 |
return self._articles
|
51 |
|
52 |
@property
|
53 |
def search_results(self) -> List[SearchResult]:
|
|
|
54 |
return self._search_results
|
55 |
|
56 |
def run(self, prompt: str) -> str:
|
|
|
57 |
enhanced_prompt = f"""
|
58 |
You are a news analysis assistant that can:
|
59 |
- Search for recent news articles
|
@@ -68,7 +72,14 @@ class NewsAnalysisAgent(CodeAgent):
|
|
68 |
return super().run(enhanced_prompt)
|
69 |
|
70 |
def extract_text_from_url(url: str) -> str:
|
71 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
try:
|
73 |
headers = {
|
74 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
@@ -91,24 +102,17 @@ def extract_text_from_url(url: str) -> str:
|
|
91 |
except Exception as e:
|
92 |
return f"Error extracting text: {str(e)}"
|
93 |
|
94 |
-
@tool
|
95 |
-
name="search_news",
|
96 |
-
description="Search for recent news articles using DuckDuckGo",
|
97 |
-
inputs={
|
98 |
-
"query": {
|
99 |
-
"type": "string",
|
100 |
-
"description": "Search query string"
|
101 |
-
},
|
102 |
-
"max_results": {
|
103 |
-
"type": "integer",
|
104 |
-
"description": "Maximum number of results to return",
|
105 |
-
"default": 5,
|
106 |
-
"nullable": True
|
107 |
-
}
|
108 |
-
}
|
109 |
-
)
|
110 |
def search_news(query: str, max_results: Optional[int] = 5) -> str:
|
111 |
-
"""Search for recent news articles using DuckDuckGo.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
try:
|
113 |
with DDGS() as ddgs:
|
114 |
search_results = list(ddgs.news(
|
@@ -134,18 +138,16 @@ def search_news(query: str, max_results: Optional[int] = 5) -> str:
|
|
134 |
except Exception as e:
|
135 |
return f"Error searching news: {str(e)}"
|
136 |
|
137 |
-
@tool
|
138 |
-
name="analyze_article",
|
139 |
-
description="Extract and analyze content from a news article URL",
|
140 |
-
inputs={
|
141 |
-
"url": {
|
142 |
-
"type": "string",
|
143 |
-
"description": "URL of the news article to analyze"
|
144 |
-
}
|
145 |
-
}
|
146 |
-
)
|
147 |
def analyze_article(url: str) -> str:
|
148 |
-
"""Extract and analyze content from a news article URL.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
try:
|
150 |
content = extract_text_from_url(url)
|
151 |
|
@@ -174,19 +176,16 @@ def analyze_article(url: str) -> str:
|
|
174 |
except Exception as e:
|
175 |
return f"Error analyzing article: {str(e)}"
|
176 |
|
177 |
-
@tool
|
178 |
-
name="identify_trends",
|
179 |
-
description="Identify common themes and trends across analyzed articles",
|
180 |
-
inputs={
|
181 |
-
"articles": {
|
182 |
-
"type": "array",
|
183 |
-
"description": "List of analyzed articles",
|
184 |
-
"nullable": True
|
185 |
-
}
|
186 |
-
}
|
187 |
-
)
|
188 |
def identify_trends(articles: Optional[List[Dict]] = None) -> str:
|
189 |
-
"""Identify common themes and trends across analyzed articles.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
articles = articles or tool.agent._articles
|
191 |
|
192 |
if not articles:
|
|
|
13 |
|
14 |
@dataclass
|
15 |
class SearchResult:
|
16 |
+
"""Data class to store search results from news searches"""
|
17 |
title: str
|
18 |
link: str
|
19 |
date: str
|
|
|
25 |
self.model_name = model_name
|
26 |
|
27 |
def __call__(self, prompt: Union[str, dict, List[Dict]]) -> str:
|
28 |
+
"""Process the prompt and return the LLM's response"""
|
29 |
try:
|
30 |
prompt_str = str(prompt) if isinstance(prompt, (dict, list)) else prompt
|
31 |
completion = self.client.chat.completions.create(
|
|
|
48 |
|
49 |
@property
|
50 |
def articles(self) -> List[Dict]:
|
51 |
+
"""Access stored article data"""
|
52 |
return self._articles
|
53 |
|
54 |
@property
|
55 |
def search_results(self) -> List[SearchResult]:
|
56 |
+
"""Access stored search results"""
|
57 |
return self._search_results
|
58 |
|
59 |
def run(self, prompt: str) -> str:
|
60 |
+
"""Execute the agent with the given prompt"""
|
61 |
enhanced_prompt = f"""
|
62 |
You are a news analysis assistant that can:
|
63 |
- Search for recent news articles
|
|
|
72 |
return super().run(enhanced_prompt)
|
73 |
|
74 |
def extract_text_from_url(url: str) -> str:
|
75 |
+
"""Extract main text content from a given URL using BeautifulSoup.
|
76 |
+
|
77 |
+
Args:
|
78 |
+
url: The URL of the webpage to extract text from
|
79 |
+
|
80 |
+
Returns:
|
81 |
+
str: Extracted and cleaned text content from the webpage
|
82 |
+
"""
|
83 |
try:
|
84 |
headers = {
|
85 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
|
|
102 |
except Exception as e:
|
103 |
return f"Error extracting text: {str(e)}"
|
104 |
|
105 |
+
@tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
def search_news(query: str, max_results: Optional[int] = 5) -> str:
|
107 |
+
"""Search for recent news articles using DuckDuckGo.
|
108 |
+
|
109 |
+
Args:
|
110 |
+
query: Search query string to find relevant news articles
|
111 |
+
max_results: Maximum number of results to return (default: 5)
|
112 |
+
|
113 |
+
Returns:
|
114 |
+
str: Formatted string containing search results with titles and URLs
|
115 |
+
"""
|
116 |
try:
|
117 |
with DDGS() as ddgs:
|
118 |
search_results = list(ddgs.news(
|
|
|
138 |
except Exception as e:
|
139 |
return f"Error searching news: {str(e)}"
|
140 |
|
141 |
+
@tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
def analyze_article(url: str) -> str:
|
143 |
+
"""Extract and analyze content from a news article URL.
|
144 |
+
|
145 |
+
Args:
|
146 |
+
url: URL of the news article to analyze
|
147 |
+
|
148 |
+
Returns:
|
149 |
+
str: Analysis of the article including summary, key points, and main themes
|
150 |
+
"""
|
151 |
try:
|
152 |
content = extract_text_from_url(url)
|
153 |
|
|
|
176 |
except Exception as e:
|
177 |
return f"Error analyzing article: {str(e)}"
|
178 |
|
179 |
+
@tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
def identify_trends(articles: Optional[List[Dict]] = None) -> str:
|
181 |
+
"""Identify common themes and trends across analyzed articles.
|
182 |
+
|
183 |
+
Args:
|
184 |
+
articles: Optional list of analyzed article data. If None, uses stored articles.
|
185 |
+
|
186 |
+
Returns:
|
187 |
+
str: Analysis of trends and patterns found across the articles
|
188 |
+
"""
|
189 |
articles = articles or tool.agent._articles
|
190 |
|
191 |
if not articles:
|