girishwangikar
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,250 +1,162 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
-
from smolagents import CodeAgent,
|
4 |
-
from typing import Union, List, Dict
|
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
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
completion = self.client.chat.completions.create(
|
32 |
model=self.model_name,
|
33 |
-
messages=[{
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
"""
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
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
|
75 |
-
"""
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
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 |
-
|
106 |
-
|
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 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
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 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
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 |
-
#
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
|
249 |
-
|
250 |
-
|
|
|
|
|
|
|
|
|
|
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 |
+
)
|