Download app.py from anu151105/last: direct link, hf CLI and curl.
- Browser
- Download file 37.4 kB
-
https://huggingface.co/spaces/anu151105/last/resolve/main/app.py
- Command line
-
hf download hf://spaces/anu151105/last/app.py
-
curl -L -o app.py https://huggingface.co/spaces/anu151105/last/resolve/main/app.py
37.4 kB
| import os | |
| import json | |
| import asyncio | |
| import logging | |
| from datetime import datetime, timedelta | |
| from typing import List, Dict, Any, Optional, Union | |
| from dataclasses import dataclass | |
| from enum import Enum | |
| import aiohttp | |
| import requests | |
| from bs4 import BeautifulSoup | |
| from langdetect import detect | |
| import gradio as gr | |
| from pydantic import BaseModel, Field | |
| from langchain_google_genai import ChatGoogleGenerativeAI | |
| from langchain_core.messages import HumanMessage | |
| from langchain_community.tools.tavily_search import TavilySearchResults | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| from wordcloud import WordCloud | |
| from uuid import uuid4 | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| # Agent Roles | |
| class AgentRole(Enum): | |
| RESEARCH_COORDINATOR = "research_coordinator" | |
| NEWS_COLLECTOR = "news_collector" | |
| CONTENT_ANALYZER = "content_analyzer" | |
| FACT_CHECKER = "fact_checker" | |
| EDITOR = "editor" | |
| QUALITY_CONTROLLER = "quality_controller" | |
| VISUALIZATION_SPECIALIST = "visualization_specialist" | |
| # Data Models | |
| class NewsArticle(BaseModel): | |
| title: str = Field(description="Article title") | |
| url: str = Field(description="Article URL") | |
| source: str = Field(description="News source") | |
| author: Optional[str] = Field(default=None, description="Article author") | |
| publish_date: Optional[str] = Field(default=None, description="Publication date") | |
| content: str = Field(description="Full article content") | |
| summary: str = Field(description="Article summary") | |
| category: str = Field(description="News category") | |
| sentiment: float = Field(description="Sentiment score (-1 to 1)") | |
| credibility_score: float = Field(description="Source credibility (0 to 1)") | |
| keywords: List[str] = Field(default=[], description="Key terms") | |
| fact_check_status: str = Field(default="pending", description="Fact-check status") | |
| class NewsSection(BaseModel): | |
| name: str = Field(description="Section name") | |
| description: str = Field(description="Section description") | |
| articles: List[NewsArticle] = Field(description="Articles in section") | |
| priority: int = Field(description="Section priority (1-10)") | |
| word_count: int = Field(description="Total word count") | |
| class NewspaperReport(BaseModel): | |
| title: str = Field(description="Newspaper title") | |
| date: str = Field(description="Publication date") | |
| summary: str = Field(description="Executive summary") | |
| sections: List[NewsSection] = Field(description="News sections") | |
| total_articles: int = Field(description="Total number of articles") | |
| generation_time: float = Field(description="Time taken to generate") | |
| quality_score: float = Field(description="Overall quality score") | |
| metadata: Dict[str, Any] = Field(default={}, description="Additional metadata") | |
| # Memory Fragment for RAG | |
| class MemoryFragment: | |
| task_id: str | |
| query: str | |
| context: Dict[str, Any] | |
| action: str | |
| outcome: str | |
| relevance_score: float | |
| timestamp: datetime = field(default_factory=datetime.now) | |
| # Agent Definition | |
| class Agent: | |
| role: AgentRole | |
| agent_id: str = field(default_factory=lambda: str(uuid4())) | |
| capabilities: List[str] = field(default_factory=list) | |
| capability_vector: np.ndarray = field(default_factory=lambda: np.zeros(10)) # Simplified capability vector | |
| memory_router: List[MemoryFragment] = field(default_factory=list) | |
| memory_executor: List[MemoryFragment] = field(default_factory=list) | |
| max_memory_size: int = 50 | |
| llm: Any = None | |
| tools: Dict[str, Any] = field(default_factory=dict) | |
| connections: Dict[str, float] = field(default_factory=dict) # Agent ID -> weight | |
| async def process_task(self, task: Dict[str, Any], agents: Dict[str, 'Agent']) -> Dict[str, Any]: | |
| """Process a task using router and executor, updating memory and connections.""" | |
| task_id = task.get('task_id', str(uuid4())) | |
| query = task.get('query', '') | |
| context = task.get('context', {}) | |
| # Router: Decide whether to forward, split, or execute | |
| routing_decision = await self._route_task(task, agents) | |
| result = {} | |
| if routing_decision['action'] == 'execute': | |
| # Executor: Perform the task | |
| result = await self._execute_task(task) | |
| # Update memory | |
| self._update_memory('executor', task_id, query, context, 'execute', result.get('outcome', 'success')) | |
| # Update capability vector | |
| self._update_capability_vector(task, success=True) | |
| elif routing_decision['action'] == 'forward': | |
| next_agent_id = routing_decision['next_agent_id'] | |
| if next_agent_id in agents: | |
| # Update memory | |
| self._update_memory('router', task_id, query, context, f'forward_to_{next_agent_id}', 'success') | |
| # Forward task | |
| result = await agents[next_agent_id].process_task(task, agents) | |
| # Update connection weight | |
| self._update_connection(next_agent_id, success=True) | |
| elif routing_decision['action'] == 'split': | |
| subtasks = routing_decision['subtasks'] | |
| results = [] | |
| for subtask in subtasks: | |
| next_agent_id = subtask['next_agent_id'] | |
| if next_agent_id in agents: | |
| # Update memory | |
| self._update_memory('router', task_id, query, context, f'split_to_{next_agent_id}', 'success') | |
| # Process subtask | |
| sub_result = await agents[next_agent_id].process_task(subtask, agents) | |
| results.append(sub_result) | |
| # Update connection weight | |
| self._update_connection(next_agent_id, success=True) | |
| result = {'outcome': 'success', 'sub_results': results} | |
| return result | |
| async def _route_task(self, task: Dict[str, Any], agents: Dict[str, 'Agent']) -> Dict[str, Any]: | |
| """Router logic to decide task action based on RAG and capability matching.""" | |
| query = task.get('query', '') | |
| context = task.get('context', {}) | |
| # Retrieve relevant memory fragments | |
| fragments = self._retrieve_fragments('router', query, context, k=3) | |
| # Compute task capability requirements | |
| task_vector = self._compute_task_vector(task) | |
| # Compare with own capabilities | |
| similarity = np.dot(self.capability_vector, task_vector) / ( | |
| np.linalg.norm(self.capability_vector) * np.linalg.norm(task_vector) + 1e-10 | |
| ) | |
| if similarity > 0.8: # Threshold for execution | |
| return {'action': 'execute'} | |
| # Find best agent to forward to | |
| best_agent_id = None | |
| best_score = -1 | |
| for agent_id, weight in self.connections.items(): | |
| if agent_id in agents: | |
| agent_vector = agents[agent_id].capability_vector | |
| score = np.dot(agent_vector, task_vector) * weight | |
| if score > best_score: | |
| best_score = score | |
| best_agent_id = agent_id | |
| if best_agent_id: | |
| return {'action': 'forward', 'next_agent_id': best_agent_id} | |
| # Split task if complex | |
| if self._is_complex_task(task): | |
| subtasks = self._split_task(task, agents) | |
| return {'action': 'split', 'subtasks': subtasks} | |
| # Default: Forward to a random connected agent | |
| if self.connections: | |
| import random | |
| return {'action': 'forward', 'next_agent_id': random.choice(list(self.connections.keys()))} | |
| return {'action': 'execute'} # Fallback | |
| async def _execute_task(self, task: Dict[str, Any]) -> Dict[str, Any]: | |
| """Execute the task based on role and tools.""" | |
| query = task.get('query', '') | |
| context = task.get('context', {}) | |
| # Retrieve relevant memory fragments | |
| fragments = self._retrieve_fragments('executor', query, context, k=3) | |
| # Execute based on role | |
| if self.role == AgentRole.RESEARCH_COORDINATOR: | |
| return await self._coordinate_research(task) | |
| elif self.role == AgentRole.NEWS_COLLECTOR: | |
| return await self._collect_news(task) | |
| elif self.role == AgentRole.CONTENT_ANALYZER: | |
| return await self._analyze_content(task) | |
| elif self.role == AgentRole.FACT_CHECKER: | |
| return await self._fact_check(task) | |
| elif self.role == AgentRole.EDITOR: | |
| return await self._edit_content(task) | |
| elif self.role == AgentRole.QUALITY_CONTROLLER: | |
| return await self._control_quality(task) | |
| elif self.role == AgentRole.VISUALIZATION_SPECIALIST: | |
| return await self._create_visualizations(task) | |
| return {'outcome': 'error', 'message': 'Unknown role'} | |
| def _retrieve_fragments(self, module: str, query: str, context: Dict, k: int) -> List[MemoryFragment]: | |
| """Retrieve k most relevant memory fragments using a simple similarity metric.""" | |
| memory = self.memory_router if module == 'router' else self.memory_executor | |
| if not memory: | |
| return [] | |
| # Simplified embedding: Use query length and keyword overlap | |
| query_words = set(query.lower().split()) | |
| scored_fragments = [] | |
| for fragment in memory: | |
| fragment_words = set(fragment.query.lower().split()) | |
| overlap = len(query_words.intersection(fragment_words)) / len(query_words.union(fragment_words)) | |
| score = overlap * (1 - (datetime.now() - fragment.timestamp).days / 30) # Decay over time | |
| scored_fragments.append((fragment, score)) | |
| scored_fragments.sort(key=lambda x: x[1], reverse=True) | |
| return [frag for frag, _ in scored_fragments[:k]] | |
| def _update_memory(self, module: str, task_id: str, query: str, context: Dict, action: str, outcome: str): | |
| """Update memory with a new fragment, pruning if necessary.""" | |
| memory = self.memory_router if module == 'router' else self.memory_executor | |
| fragment = MemoryFragment( | |
| task_id=task_id, | |
| query=query, | |
| context=context, | |
| action=action, | |
| outcome=outcome, | |
| relevance_score=1.0 # Simplified | |
| ) | |
| memory.append(fragment) | |
| if len(memory) > self.max_memory_size: | |
| # Prune least relevant fragment | |
| memory.sort(key=lambda x: x.relevance_score * (1 - (datetime.now() - x.timestamp).days / 30)) | |
| memory.pop(0) | |
| def _update_capability_vector(self, task: Dict, success: bool): | |
| """Update capability vector based on task execution.""" | |
| task_vector = self._compute_task_vector(task) | |
| alpha = 0.1 # Learning rate | |
| if success: | |
| self.capability_vector = (1 - alpha) * self.capability_vector + alpha * task_vector | |
| def _update_connection(self, agent_id: str, success: bool): | |
| """Update connection weight based on interaction outcome.""" | |
| alpha = 0.1 # Decay factor | |
| current_weight = self.connections.get(agent_id, 0.5) | |
| new_weight = alpha * current_weight + (1 - alpha) * (1.0 if success else 0.0) | |
| self.connections[agent_id] = new_weight | |
| def _compute_task_vector(self, task: Dict) -> np.ndarray: | |
| """Compute a simplified capability vector for a task.""" | |
| query = task.get('query', '').lower() | |
| vector = np.zeros(10) # 10-dimensional capability space | |
| if 'research' in query or 'plan' in query: | |
| vector[0] = 1.0 | |
| if 'news' in query or 'article' in query: | |
| vector[1] = 1.0 | |
| if 'analyze' in query or 'sentiment' in query: | |
| vector[2] = 1.0 | |
| if 'fact' in query or 'verify' in query: | |
| vector[3] = 1.0 | |
| if 'edit' in query or 'format' in query: | |
| vector[4] = 1.0 | |
| if 'quality' in query or 'review' in query: | |
| vector[5] = 1.0 | |
| if 'visual' in query or 'chart' in query: | |
| vector[6] = 1.0 | |
| return vector | |
| def _is_complex_task(self, task: Dict) -> bool: | |
| """Determine if a task is complex enough to split.""" | |
| query = task.get('query', '') | |
| return len(query.split()) > 20 or 'multiple' in query.lower() | |
| def _split_task(self, task: Dict, agents: Dict[str, 'Agent']) -> List[Dict]: | |
| """Split a complex task into subtasks.""" | |
| query = task.get('query', '') | |
| context = task.get('context', {}) | |
| # Simplified splitting logic | |
| subtasks = [ | |
| { | |
| 'task_id': str(uuid4()), | |
| 'query': query, | |
| 'context': context, | |
| 'next_agent_id': agent_id | |
| } | |
| for agent_id in list(self.connections.keys())[:2] # Split to top 2 connected agents | |
| ] | |
| return subtasks | |
| async def _coordinate_research(self, task: Dict) -> Dict: | |
| """Coordinate research planning.""" | |
| query = task.get('query', '') | |
| plan = { | |
| 'search_strategies': [f"Search for {query} from reliable sources"], | |
| 'categories': ['Politics', 'Economy', 'Technology'], | |
| 'quality_criteria': 'Credibility > 0.7', | |
| 'timeline': '1 hour' | |
| } | |
| return {'outcome': 'success', 'result': plan} | |
| async def _collect_news(self, task: Dict) -> Dict: | |
| """Collect news articles using the search tool.""" | |
| query = task.get('query', '') | |
| search_tool = self.tools.get('advanced_web_search') | |
| if not search_tool: | |
| return {'outcome': 'error', 'message': 'No search tool available'} | |
| results = search_tool._run(query, days_back=1, language='en') | |
| articles = [ | |
| NewsArticle( | |
| title=r.get('title', ''), | |
| url=r.get('url', ''), | |
| source=r.get('source', 'Unknown'), | |
| content=r.get('content', ''), | |
| summary=r.get('content', '')[:200], | |
| category='Other', | |
| sentiment=0.0, | |
| credibility_score=r.get('credibility_score', 0.6), | |
| fact_check_status='pending' | |
| ) | |
| for r in results[:5] # Limit to 5 articles | |
| ] | |
| return {'outcome': 'success', 'result': articles} | |
| async def _analyze_content(self, task: Dict) -> Dict: | |
| """Analyze content using the analysis tool.""" | |
| articles = task.get('context', {}).get('articles', []) | |
| analysis_tool = self.tools.get('content_analysis') | |
| if not analysis_tool or not articles: | |
| return {'outcome': 'error', 'message': 'No analysis tool or articles'} | |
| analyzed_articles = [] | |
| for article in articles: | |
| analysis = analysis_tool._run(article.content, article.title) | |
| article.category = analysis.get('category', 'Other') | |
| article.sentiment = analysis.get('sentiment', 0.0) | |
| article.summary = analysis.get('summary', article.summary) | |
| article.keywords = analysis.get('keywords', []) | |
| analyzed_articles.append(article) | |
| return {'outcome': 'success', 'result': analyzed_articles} | |
| async def _fact_check(self, task: Dict) -> Dict: | |
| """Fact-check articles using the fact-check tool.""" | |
| articles = task.get('context', {}).get('articles', []) | |
| fact_check_tool = self.tools.get('fact_check') | |
| if not fact_check_tool or not articles: | |
| return {'outcome': 'error', 'message': 'No fact-check tool or articles'} | |
| checked_articles = [] | |
| for article in articles: | |
| fact_check = fact_check_tool._run(article.content, article.title) | |
| article.fact_check_status = fact_check.get('status', 'needs_review') | |
| article.credibility_score *= fact_check.get('confidence', 0.5) | |
| checked_articles.append(article) | |
| return {'outcome': 'success', 'result': checked_articles} | |
| async def _edit_content(self, task: Dict) -> Dict: | |
| """Edit and structure content into sections.""" | |
| articles = task.get('context', {}).get('articles', []) | |
| if not articles: | |
| return {'outcome': 'error', 'message': 'No articles to edit'} | |
| # Group by category | |
| sections = {} | |
| for article in articles: | |
| cat = article.category | |
| if cat not in sections: | |
| sections[cat] = NewsSection( | |
| name=cat, | |
| description=f"News section covering {cat.lower()}", | |
| articles=[], | |
| priority=len(sections) + 1, | |
| word_count=0 | |
| ) | |
| sections[cat].articles.append(article) | |
| sections[cat].word_count += len(article.content.split()) | |
| return {'outcome': 'success', 'result': list(sections.values())} | |
| async def _control_quality(self, task: Dict) -> Dict: | |
| """Perform quality control on sections.""" | |
| sections = task.get('context', {}).get('sections', []) | |
| if not sections: | |
| return {'outcome': 'error', 'message': 'No sections to review'} | |
| quality_score = 0.85 # Simplified | |
| for section in sections: | |
| for article in section.articles: | |
| if article.credibility_score < 0.5 or article.fact_check_status == 'suspicious': | |
| quality_score -= 0.1 | |
| quality_score = max(0.0, min(1.0, quality_score)) | |
| return {'outcome': 'success', 'result': sections, 'quality_score': quality_score} | |
| async def _create_visualizations(self, task: Dict) -> Dict: | |
| """Create visualizations using the visualization tool.""" | |
| articles = task.get('context', {}).get('articles', []) | |
| viz_tool = self.tools.get('create_visualizations') | |
| if not viz_tool or not articles: | |
| return {'outcome': 'error', 'message': 'No visualization tool or articles'} | |
| articles_data = [ | |
| {'title': a.title, 'category': a.category, 'sentiment': a.sentiment, 'credibility_score': a.credibility_score} | |
| for a in articles | |
| ] | |
| viz_path = viz_tool._run(articles_data, "news_viz.png") | |
| return {'outcome': 'success', 'result': viz_path} | |
| # Tools (Reused from Original Code) | |
| class AdvancedWebSearchTool: | |
| def __init__(self, tavily_api_key: str): | |
| self.search_tool = TavilySearchResults( | |
| max_results=20, | |
| search_depth="advanced", | |
| include_answer=True, | |
| include_raw_content=True | |
| ) | |
| os.environ["TAVILY_API_KEY"] = tavily_api_key | |
| def _run(self, query: str, days_back: int = 1, language: str = "en") -> List[Dict]: | |
| try: | |
| date_filter = (datetime.now() - timedelta(days=days_back)).strftime("%Y-%m-%d") | |
| enhanced_query = f"{query} after:{date_filter} language:{language} -site:youtube.com" | |
| results = self.search_tool.invoke({"query": enhanced_query}) | |
| filtered_results = [] | |
| for result in results: | |
| if self._is_quality_source(result): | |
| result['credibility_score'] = self._calculate_credibility(result) | |
| result['content_quality'] = self._assess_content_quality(result) | |
| filtered_results.append(result) | |
| return sorted(filtered_results, key=lambda x: x['credibility_score'], reverse=True) | |
| except Exception as e: | |
| logger.error(f"Search error: {e}") | |
| return [] | |
| def _is_quality_source(self, result: Dict) -> bool: | |
| url = result.get('url', '').lower() | |
| title = result.get('title', '').lower() | |
| content = result.get('content', '') | |
| blocked_domains = ['youtube.com', 'tiktok.com', 'reddit.com', 'twitter.com'] | |
| if any(domain in url for domain in blocked_domains): | |
| return False | |
| if len(content) < 200: | |
| return False | |
| try: | |
| if detect(content[:500]) != 'en': | |
| return False | |
| except: | |
| return False | |
| return True | |
| def _calculate_credibility(self, result: Dict) -> float: | |
| url = result.get('url', '').lower() | |
| high_credibility = ['reuters.com', 'bbc.com', 'ap.org', 'npr.org', 'pbs.org'] | |
| medium_credibility = ['cnn.com', 'nytimes.com', 'washingtonpost.com', 'theguardian.com'] | |
| if any(domain in url for domain in high_credibility): | |
| return 0.9 | |
| elif any(domain in url for domain in medium_credibility): | |
| return 0.8 | |
| else: | |
| return 0.6 | |
| def _assess_content_quality(self, result: Dict) -> float: | |
| content = result.get('content', '') | |
| title = result.get('title', '') | |
| quality_score = 0.5 | |
| if len(content) > 1000: | |
| quality_score += 0.2 | |
| if len(title.split()) >= 5: | |
| quality_score += 0.1 | |
| if '.' in content and len(content.split('.')) > 3: | |
| quality_score += 0.2 | |
| return min(quality_score, 1.0) | |
| class ContentAnalysisTool: | |
| def __init__(self, llm): | |
| self.llm = llm | |
| def _run(self, content: str, title: str = "") -> Dict[str, Any]: | |
| try: | |
| analysis_prompt = f""" | |
| Analyze the following news content and provide: | |
| 1. Sentiment score (-1 to 1) | |
| 2. Category (Politics, Economy, International, Technology, Health, Sports, Entertainment, Other) | |
| 3. Top 5 keywords | |
| 4. Brief summary (2-3 sentences) | |
| Title: {title} | |
| Content: {content[:2000]} | |
| Respond in JSON format: | |
| {{ | |
| "sentiment": 0.0, | |
| "category": "Category", | |
| "keywords": ["keyword1", "keyword2", ...], | |
| "summary": "Brief summary here" | |
| }} | |
| """ | |
| response = self.llm.invoke([HumanMessage(content=analysis_prompt)]) | |
| result = json.loads(response.content) | |
| return result | |
| except Exception as e: | |
| logger.error(f"Analysis error: {e}") | |
| return { | |
| "sentiment": 0.0, | |
| "category": "Other", | |
| "keywords": [], | |
| "summary": "Analysis failed" | |
| } | |
| class FactCheckTool: | |
| def __init__(self, llm): | |
| self.llm = llm | |
| def _run(self, content: str, title: str = "") -> Dict[str, Any]: | |
| try: | |
| fact_check_prompt = f""" | |
| Review the following news content for potential factual issues: | |
| 1. Check for obvious factual errors | |
| 2. Identify claims that need verification | |
| 3. Assess overall credibility | |
| 4. Flag any suspicious content | |
| Title: {title} | |
| Content: {content[:1500]} | |
| Respond in JSON format: | |
| {{ | |
| "status": "verified|needs_review|suspicious", | |
| "confidence": 0.0-1.0, | |
| "flags": ["flag1", "flag2", ...], | |
| "notes": "Any important notes" | |
| }} | |
| """ | |
| response = self.llm.invoke([HumanMessage(content=fact_check_prompt)]) | |
| result = json.loads(response.content) | |
| return result | |
| except Exception as e: | |
| logger.error(f"Fact-check error: {e}") | |
| return { | |
| "status": "needs_review", | |
| "confidence": 0.5, | |
| "flags": ["analysis_failed"], | |
| "notes": "Automatic fact-check failed" | |
| } | |
| class VisualizationTool: | |
| def _run(self, articles: List[Dict], output_path: str = "news_viz.png") -> str: | |
| try: | |
| categories = [article.get('category', 'Other') for article in articles] | |
| sentiments = [float(article.get('sentiment', 0)) for article in articles] | |
| fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(15, 12)) | |
| category_counts = {} | |
| for cat in categories: | |
| category_counts[cat] = category_counts.get(cat, 0) + 1 | |
| ax1.pie(category_counts.values(), labels=category_counts.keys(), autopct='%1.1f%%') | |
| ax1.set_title('News Categories Distribution') | |
| ax2.hist(sentiments, bins=20, alpha=0.7, color='skyblue') | |
| ax2.set_xlabel('Sentiment Score') | |
| ax2.set_ylabel('Frequency') | |
| ax2.set_title('Sentiment Distribution') | |
| ax2.axvline(x=0, color='red', linestyle='--', alpha=0.5, label='Neutral') | |
| ax2.legend() | |
| import pandas as pd | |
| df = pd.DataFrame({'category': categories, 'sentiment': sentiments}) | |
| sns.boxplot(data=df, x='category', y='sentiment', ax=ax3) | |
| ax3.set_title('Sentiment by Category') | |
| ax3.tick_params(axis='x', rotation=45) | |
| all_text = ' '.join([article.get('title', '') for article in articles]) | |
| if all_text.strip(): | |
| wordcloud = WordCloud(width=400, height=300, background_color='white').generate(all_text) | |
| ax4.imshow(wordcloud, interpolation='bilinear') | |
| ax4.axis('off') | |
| ax4.set_title('Common Terms in Headlines') | |
| plt.tight_layout() | |
| plt.savefig(output_path, dpi=300, bbox_inches='tight') | |
| plt.close() | |
| return output_path | |
| except Exception as e: | |
| logger.error(f"Visualization error: {e}") | |
| return "" | |
| # Multi-Agent System | |
| class AgentNetNewsSystem: | |
| def __init__(self, google_api_key: str, tavily_api_key: str): | |
| self.llm = ChatGoogleGenerativeAI( | |
| model="gemini-1.5-flash", | |
| temperature=0.7, | |
| google_api_key=google_api_key | |
| ) | |
| self.agents = self._create_agents(tavily_api_key) | |
| self.task_history = [] | |
| def _create_agents(self, tavily_api_key: str) -> Dict[str, Agent]: | |
| """Create agents with initial capabilities and tools.""" | |
| search_tool = AdvancedWebSearchTool(tavily_api_key) | |
| analysis_tool = ContentAnalysisTool(self.llm) | |
| fact_check_tool = FactCheckTool(self.llm) | |
| viz_tool = VisualizationTool() | |
| agents = {} | |
| roles = [ | |
| (AgentRole.RESEARCH_COORDINATOR, ['planning', 'coordination'], {'advanced_web_search': search_tool}), | |
| (AgentRole.NEWS_COLLECTOR, ['search', 'data_collection'], {'advanced_web_search': search_tool}), | |
| (AgentRole.CONTENT_ANALYZER, ['analysis', 'categorization'], {'content_analysis': analysis_tool}), | |
| (AgentRole.FACT_CHECKER, ['verification', 'credibility'], {'fact_check': fact_check_tool}), | |
| (AgentRole.EDITOR, ['editing', 'formatting'], {}), | |
| (AgentRole.QUALITY_CONTROLLER, ['review', 'quality_assurance'], {}), | |
| (AgentRole.VISUALIZATION_SPECIALIST, ['visualization', 'data_analysis'], {'create_visualizations': viz_tool}), | |
| ] | |
| # Initialize fully connected network | |
| agent_ids = [str(uuid4()) for _ in roles] | |
| initial_connections = {aid: 0.5 for aid in agent_ids} | |
| for i, (role, capabilities, tools) in enumerate(roles): | |
| agent = Agent( | |
| role=role, | |
| agent_id=agent_ids[i], | |
| capabilities=capabilities, | |
| llm=self.llm, | |
| tools=tools, | |
| connections=initial_connections.copy() | |
| ) | |
| # Initialize capability vector based on role | |
| agent.capability_vector[i % 10] = 1.0 # Simplified | |
| agents[agent_ids[i]] = agent | |
| return agents | |
| async def generate_newspaper(self, query: str = "current affairs", date: str = None) -> NewspaperReport: | |
| """Generate a newspaper using decentralized coordination.""" | |
| if not date: | |
| date = datetime.now().strftime("%Y-%m-%d") | |
| start_time = datetime.now() | |
| try: | |
| # Initial task | |
| task = { | |
| 'task_id': str(uuid4()), | |
| 'query': f"Generate a newspaper for {query} on {date}", | |
| 'context': {'date': date, 'articles': [], 'sections': []}, | |
| } | |
| # Select initial agent based on capability matching | |
| task_vector = np.zeros(10) | |
| task_vector[0] = 1.0 # Research coordination | |
| initial_agent_id = max( | |
| self.agents.keys(), | |
| key=lambda aid: np.dot(self.agents[aid].capability_vector, task_vector) | |
| ) | |
| # Process task through the network | |
| result = await self.agents[initial_agent_id].process_task(task, self.agents) | |
| # Extract final output | |
| sections = result.get('result', []) | |
| quality_score = result.get('quality_score', 0.85) | |
| viz_path = result.get('viz_path', '') | |
| generation_time = (datetime.now() - start_time).total_seconds() | |
| # Create report | |
| report = NewspaperReport( | |
| title=f"Daily Current Affairs - {date}", | |
| date=date, | |
| summary="Comprehensive current affairs report generated by decentralized multi-agent system", | |
| sections=sections, | |
| total_articles=sum(len(s.articles) for s in sections), | |
| generation_time=generation_time, | |
| quality_score=quality_score, | |
| metadata={ | |
| "generated_by": "AgentNet News System", | |
| "agents_used": list(self.agents.keys()), | |
| "processing_time": f"{generation_time:.2f}s", | |
| "visualization_path": viz_path | |
| } | |
| ) | |
| logger.info(f"Newspaper generated in {generation_time:.2f}s with {report.total_articles} articles") | |
| return report | |
| except Exception as e: | |
| logger.error(f"Error in newspaper generation: {e}") | |
| return self._create_error_report(str(e), date) | |
| def _create_error_report(self, error_msg: str, date: str) -> NewspaperReport: | |
| error_article = NewsArticle( | |
| title="Generation Error", | |
| url="", | |
| source="System", | |
| content=f"An error occurred: {error_msg}", | |
| summary="Error occurred during generation", | |
| category="System", | |
| sentiment=0.0, | |
| credibility_score=0.0, | |
| keywords=["error"], | |
| fact_check_status="system_error" | |
| ) | |
| error_section = NewsSection( | |
| name="System Error", | |
| description="Error report", | |
| articles=[error_article], | |
| priority=1, | |
| word_count=len(error_msg.split()) | |
| ) | |
| return NewspaperReport( | |
| title=f"Error Report - {date}", | |
| date=date, | |
| summary="Error occurred during newspaper generation", | |
| sections=[error_section], | |
| total_articles=0, | |
| generation_time=0.0, | |
| quality_score=0.0, | |
| metadata={"error": error_msg} | |
| ) | |
| # Gradio Interface | |
| def create_gradio_interface(): | |
| def generate_newspaper_ui(google_key: str, tavily_key: str, query: str, date: str, | |
| include_viz: bool) -> tuple: | |
| if not google_key or not tavily_key: | |
| return "β Please provide both API keys.", "", "" | |
| try: | |
| system = AgentNetNewsSystem(google_key, tavily_key) | |
| loop = asyncio.new_event_loop() | |
| asyncio.set_event_loop(loop) | |
| report = loop.run_until_complete( | |
| system.generate_newspaper(query or "current affairs", date) | |
| ) | |
| formatted_report = format_newspaper_report(report) | |
| metrics = create_quality_metrics(report) | |
| viz_path = report.metadata.get('visualization_path', '') | |
| return formatted_report, metrics, viz_path | |
| except Exception as e: | |
| error_msg = f"β Error generating newspaper: {str(e)}" | |
| return error_msg, "", "" | |
| def format_newspaper_report(report: NewspaperReport) -> str: | |
| output = f"""# {report.title} | |
| **Publication Date:** {report.date} | |
| **Total Articles:** {report.total_articles} | |
| **Generation Time:** {report.generation_time:.2f} seconds | |
| **Quality Score:** {report.quality_score:.2f}/1.0 | |
| --- | |
| ## Executive Summary | |
| {report.summary} | |
| --- | |
| ## Table of Contents | |
| """ | |
| for i, section in enumerate(report.sections, 1): | |
| output += f"{i}. [{section.name}](#{section.name.lower().replace(' ', '-')})\n" | |
| output += "\n---\n\n" | |
| for section in report.sections: | |
| output += f"## {section.name}\n\n" | |
| output += f"*{section.description}*\n\n" | |
| for article in section.articles: | |
| output += f"### {article.title}\n\n" | |
| if article.source != "System": | |
| output += f"**Source:** [{article.source}]({article.url})\n\n" | |
| output += f"{article.content}\n\n" | |
| if article.keywords: | |
| output += f"**Keywords:** {', '.join(article.keywords)}\n\n" | |
| output += "---\n\n" | |
| if report.metadata: | |
| output += "## Generation Metadata\n\n" | |
| for key, value in report.metadata.items(): | |
| output += f"- **{key.replace('_', ' ').title()}:** {value}\n" | |
| return output | |
| def create_quality_metrics(report: NewspaperReport) -> str: | |
| metrics = f"""## Quality Metrics | |
| π **Overall Quality Score:** {report.quality_score:.2f}/1.0 | |
| β±οΈ **Generation Time:** {report.generation_time:.2f} seconds | |
| π° **Total Articles:** {report.total_articles} | |
| π **Total Sections:** {len(report.sections)} | |
| ### Section Breakdown: | |
| """ | |
| for section in report.sections: | |
| metrics += f"- **{section.name}:** {len(section.articles)} articles, {section.word_count} words\n" | |
| return metrics | |
| with gr.Blocks( | |
| title="AgentNet Multi-Agent News System", | |
| theme=gr.themes.Soft(), | |
| css=".container { max-width: 1200px; margin: auto; }" | |
| ) as demo: | |
| with gr.Row(): | |
| gr.Markdown(""" | |
| # π€ AgentNet Multi-Agent News System | |
| A decentralized, self-evolving multi-agent system based on AgentNet principles: | |
| - **Decentralized Coordination**: No central controller | |
| - **Dynamic Topology**: Agents adapt connections in real-time | |
| - **RAG-based Memory**: Agents learn from past tasks | |
| - **Autonomous Specialization**: Agents refine expertise dynamically | |
| """) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown("## π§ Configuration") | |
| google_api_key = gr.Textbox(label="Google Gemini API Key", type="password") | |
| tavily_api_key = gr.Textbox(label="Tavily Search API Key", type="password") | |
| search_query = gr.Textbox(label="Search Query", value="current affairs news") | |
| target_date = gr.Textbox(label="Target Date", value=datetime.now().strftime("%Y-%m-%d")) | |
| include_visualizations = gr.Checkbox(label="Include Data Visualizations", value=True) | |
| with gr.Row(): | |
| generate_btn = gr.Button("π Generate Newspaper", variant="primary") | |
| status_display = gr.Textbox(label="Generation Status", value="Ready to generate...", interactive=False) | |
| with gr.Row(): | |
| with gr.Column(scale=2): | |
| gr.Markdown("## π° Generated Newspaper") | |
| newspaper_output = gr.Markdown() | |
| with gr.Column(scale=1): | |
| gr.Markdown("## π Quality Metrics") | |
| metrics_output = gr.Markdown() | |
| with gr.Row(): | |
| gr.Markdown("## π Data Visualizations") | |
| viz_output = gr.Image() | |
| generate_btn.click( | |
| fn=generate_newspaper_ui, | |
| inputs=[google_api_key, tavily_api_key, search_query, target_date, include_visualizations], | |
| outputs=[newspaper_output, metrics_output, viz_output] | |
| ) | |
| return demo | |
| def main(): | |
| try: | |
| demo = create_gradio_interface() | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| share=False, | |
| debug=True | |
| ) | |
| except Exception as e: | |
| logger.error(f"Failed to launch application: {e}") | |
| print(f"β Error launching application: {e}") | |
| if __name__ == "__main__": | |
| main() |