girishwangikar commited on
Commit
d8a9363
Β·
verified Β·
1 Parent(s): 45896a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -222
app.py CHANGED
@@ -1,250 +1,162 @@
1
  import streamlit as st
2
  import pandas as pd
3
- from smolagents import CodeAgent, tool
4
- from typing import Union, List, Dict, Optional
5
- from duckduckgo_search import DDGS
6
- import requests
7
- from bs4 import BeautifulSoup
8
- from datetime import datetime
9
  from groq import Groq
10
  import os
11
- import re
12
- from dataclasses import dataclass
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
20
 
21
  class GroqLLM:
22
  """Compatible LLM interface for smolagents CodeAgent"""
23
- def __init__(self, model_name: str = "llama-3.1-8B-Instant"):
24
  self.client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
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(
32
  model=self.model_name,
33
- messages=[{"role": "user", "content": prompt_str}],
 
 
 
34
  temperature=0.7,
35
  max_tokens=1024,
36
  stream=False
37
  )
38
  return completion.choices[0].message.content if completion.choices else "Error: No response generated"
39
  except Exception as e:
40
- return f"Error generating response: {str(e)}"
 
 
41
 
42
- class NewsAnalysisAgent(CodeAgent):
43
- """Extended CodeAgent with news search and analysis capabilities"""
44
- def __init__(self, *args, **kwargs):
45
- super().__init__(*args, **kwargs)
46
- self._articles = []
47
- self._search_results = []
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
64
- - Extract and analyze article content
65
- - Summarize key points
66
- - Identify trends and patterns
67
-
68
- Task: {prompt}
69
-
70
- Use the provided tools to search and analyze news content.
71
- """
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'
86
- }
87
- response = requests.get(url, headers=headers, timeout=10)
88
- response.raise_for_status()
89
-
90
- soup = BeautifulSoup(response.text, 'html.parser')
91
-
92
- # Remove unwanted elements
93
- for element in soup(['script', 'style', 'nav', 'header', 'footer']):
94
- element.decompose()
95
-
96
- # Extract text from paragraphs
97
- paragraphs = soup.find_all('p')
98
- text = ' '.join(p.get_text().strip() for p in paragraphs if p.get_text().strip())
99
-
100
- return re.sub(r'\s+', ' ', text)
101
-
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(
119
- query,
120
- max_results=max_results or 5,
121
- timeframe='d'
122
- ))
123
-
124
- # Store results in agent
125
- tool.agent._search_results = [
126
- SearchResult(title=r['title'], link=r['link'], date=r['date'])
127
- for r in search_results
128
- ]
129
-
130
- # Format results
131
- formatted_results = []
132
- for idx, result in enumerate(search_results, 1):
133
- formatted_results.append(
134
- f"{idx}. {result['title']}\n URL: {result['link']}\n Date: {result['date']}\n"
135
- )
136
-
137
- return "\n".join(formatted_results)
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
-
154
- analysis_prompt = f"""
155
- Please analyze this article content and provide:
156
- 1. A brief summary (2-3 sentences)
157
- 2. Key points (3-5 main takeaways)
158
- 3. Main topics/themes discussed
159
-
160
- Article content:
161
- {content[:3000]}
162
- """
163
-
164
- analysis = tool.agent.model(analysis_prompt)
165
-
166
- # Store article data
167
- article_data = {
168
- 'url': url,
169
- 'content': content[:1000],
170
- 'analysis': analysis,
171
- 'date': datetime.now().strftime('%Y-%m-%d')
172
- }
173
- tool.agent._articles.append(article_data)
174
-
175
- return analysis
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:
192
- return "No articles available for trend analysis"
193
-
194
- combined_analyses = "\n".join(article['analysis'] for article in articles)
195
-
196
- trend_prompt = f"""
197
- Based on the analyses of {len(articles)} articles, please identify:
198
- 1. Common themes or topics across articles
199
- 2. Any notable patterns or trends
200
- 3. Different perspectives or viewpoints presented
201
-
202
- Combined analyses:
203
- {combined_analyses}
204
- """
205
-
206
- return tool.agent.model(trend_prompt)
207
-
208
- def main():
209
- st.title("News Analysis Assistant")
210
- st.write("Search and analyze recent news articles with natural language interaction.")
211
-
212
- # Initialize session state
213
- if 'agent' not in st.session_state:
214
- st.session_state['agent'] = NewsAnalysisAgent(
215
- tools=[search_news, analyze_article, identify_trends],
216
- model=GroqLLM(),
217
- additional_authorized_imports=[
218
- "requests", "bs4", "duckduckgo_search", "pandas"
219
- ]
220
  )
221
 
222
- # News search interface
223
- search_query = st.text_input("Enter news search query:")
224
- if search_query:
225
- with st.spinner('Searching news...'):
226
- search_results = st.session_state['agent'].run(
227
- f"Use the search_news tool to find recent articles about: {search_query}"
228
- )
229
- st.write(search_results)
230
-
231
- # Article analysis interface
232
- st.subheader("Article Analysis")
233
- article_url = st.text_input("Enter article URL to analyze:")
234
- if article_url:
235
- with st.spinner('Analyzing article...'):
236
- analysis = st.session_state['agent'].run(
237
- f"Use the analyze_article tool to analyze this article: {article_url}"
238
- )
239
- st.write(analysis)
240
-
241
- # Trend analysis interface
242
- if st.button("Analyze Trends"):
243
- with st.spinner('Identifying trends...'):
244
- trends = st.session_state['agent'].run(
245
- "Use the identify_trends tool to analyze patterns across all articles"
246
- )
247
- st.write(trends)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
 
249
- if __name__ == "__main__":
250
- main()
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
+ from smolagents import CodeAgent, DuckDuckGoSearchTool
4
+ from typing import Union, List, Dict
 
 
 
 
5
  from groq import Groq
6
  import os
 
 
 
 
 
 
 
 
 
7
 
8
  class GroqLLM:
9
  """Compatible LLM interface for smolagents CodeAgent"""
10
+ def __init__(self, model_name="llama-3.1-8B-Instant"):
11
  self.client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
12
  self.model_name = model_name
13
 
14
  def __call__(self, prompt: Union[str, dict, List[Dict]]) -> str:
 
15
  try:
16
+ # Handle different prompt formats
17
+ if isinstance(prompt, (dict, list)):
18
+ prompt_str = str(prompt)
19
+ else:
20
+ prompt_str = str(prompt)
21
+
22
+ # Create a properly formatted message
23
  completion = self.client.chat.completions.create(
24
  model=self.model_name,
25
+ messages=[{
26
+ "role": "user",
27
+ "content": prompt_str
28
+ }],
29
  temperature=0.7,
30
  max_tokens=1024,
31
  stream=False
32
  )
33
  return completion.choices[0].message.content if completion.choices else "Error: No response generated"
34
  except Exception as e:
35
+ error_msg = f"Error generating response: {str(e)}"
36
+ print(error_msg)
37
+ return error_msg
38
 
39
+ def create_analysis_prompt(topic: str, search_results: str) -> str:
40
+ """Creates a structured prompt for news analysis"""
41
+ return f"""Analyze the following news information about {topic}.
42
+ Search Results: {search_results}
43
+
44
+ Please provide:
45
+ 1. Summary of key points
46
+ 2. Main stakeholders involved
47
+ 3. Potential implications
48
+ 4. Analysis of different perspectives
49
+ 5. Fact-check of major claims (if applicable)
50
+
51
+ Format the analysis in a clear, journalistic style."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
+ def log_agent_activity(prompt: str, result: str, agent_name: str):
54
+ """Logs agent activities in the Streamlit interface"""
55
+ with st.expander("View Agent Activity Log"):
56
+ st.write(f"### Agent Activity ({agent_name}):")
57
+ st.write("**Input Prompt:**")
58
+ st.code(prompt, language="text")
59
+ st.write("**Analysis Output:**")
60
+ st.code(result, language="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
+ # Initialize Streamlit app
63
+ st.set_page_config(page_title="News Analysis Tool", layout="wide")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
+ # Title and description
66
+ st.title("πŸ” AI News Analysis Tool")
67
+ st.write("""
68
+ This tool uses advanced AI to analyze news topics, providing comprehensive insights
69
+ and analysis using real-time data from the web. Powered by Groq's LLama 3.1 8B
70
+ Instant model and DuckDuckGo search.
71
+ """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
+ # Initialize the agents
74
+ try:
75
+ # Initialize LLM and tools
76
+ llm = GroqLLM()
77
+ search_tool = DuckDuckGoSearchTool()
78
+
79
+ # Create the analysis agent
80
+ news_agent = CodeAgent(
81
+ tools=[search_tool],
82
+ model=llm
83
+ )
84
+
85
+ # Input section
86
+ news_topic = st.text_input(
87
+ "Enter News Topic or Query:",
88
+ placeholder="E.g., Recent developments in renewable energy"
89
+ )
90
+
91
+ # Analysis options
92
+ col1, col2 = st.columns(2)
93
+ with col1:
94
+ search_depth = st.slider(
95
+ "Search Depth (number of results)",
96
+ min_value=3,
97
+ max_value=10,
98
+ value=5
99
+ )
100
+ with col2:
101
+ analysis_type = st.selectbox(
102
+ "Analysis Type",
103
+ ["Comprehensive", "Quick Summary", "Technical", "Simplified"]
 
 
 
 
 
 
 
 
 
 
104
  )
105
 
106
+ # Generate analysis button
107
+ if st.button("Analyze News"):
108
+ if news_topic:
109
+ with st.spinner("Gathering information and analyzing..."):
110
+ try:
111
+ # First, get search results
112
+ search_results = search_tool.run(
113
+ f"Latest news about {news_topic} last 7 days"
114
+ )
115
+
116
+ # Create analysis prompt
117
+ analysis_prompt = create_analysis_prompt(news_topic, search_results)
118
+
119
+ # Get analysis from the agent
120
+ analysis_result = news_agent.run(analysis_prompt)
121
+
122
+ # Display results
123
+ st.subheader("πŸ“Š Analysis Results")
124
+ st.markdown(analysis_result)
125
+
126
+ # Log the activity
127
+ log_agent_activity(
128
+ analysis_prompt,
129
+ analysis_result,
130
+ "News Analysis Agent"
131
+ )
132
+
133
+ except Exception as e:
134
+ st.error(f"An error occurred during analysis: {str(e)}")
135
+ else:
136
+ st.warning("Please enter a news topic to analyze.")
137
+
138
+ # Add helpful tips
139
+ with st.expander("πŸ’‘ Tips for Better Results"):
140
+ st.write("""
141
+ - Be specific with your topic for more focused analysis
142
+ - Use keywords related to recent events for timely information
143
+ - Consider including timeframes in your query
144
+ - Try different analysis types for various perspectives
145
+ """)
146
+
147
+ except Exception as e:
148
+ st.error(f"""
149
+ Failed to initialize the application: {str(e)}
150
+
151
+ Please ensure:
152
+ 1. Your GROQ_API_KEY is properly set in environment variables
153
+ 2. All required packages are installed
154
+ 3. You have internet connectivity for DuckDuckGo searches
155
+ """)
156
 
157
+ # Footer
158
+ st.markdown("---")
159
+ st.caption(
160
+ "Powered by Groq LLama 3.1 8B Instant, DuckDuckGo, and Streamlit | "
161
+ "Created for news analysis and research purposes"
162
+ )