File size: 25,952 Bytes
e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c 065f30a e86d87c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 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 211 212 213 214 215 216 217 218 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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
from langchain_groq import ChatGroq
from langgraph.graph import StateGraph, START, END
from IPython.display import Image, display, Markdown
from typing_extensions import TypedDict
from langgraph.constants import Send
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_community.tools.tavily_search import TavilySearchResults
import os
import getpass
from typing import Annotated, List, Dict, Any
import operator
from pydantic import BaseModel, Field
from datetime import datetime
import requests
from bs4 import BeautifulSoup
import re
import json
import gradio as gr
from langdetect import detect
# Define models for structured output
class NewsItem(BaseModel):
title: str = Field(description="Title of the AI news article")
url: str = Field(description="URL of the news article")
source: str = Field(description="Source website of the news")
description: str = Field(description="Brief description of the news article")
class NewsResults(BaseModel):
news_items: List[NewsItem] = Field(description="List of AI news articles found")
class Subsection(BaseModel):
title: str = Field(description="Title of the subsection (based on news item title)")
source: str = Field(description="Source of the news item")
url: str = Field(description="URL of the news item")
content: str = Field(description="Content for this subsection")
class Section(BaseModel):
name: str = Field(description="Name for this section of the blog")
description: str = Field(description="Description for this section of the blog")
information: str = Field(description="Information which should be included in this section of the blog")
subsections: List[Subsection] = Field(description="Subsections for each news item in this category", default=[])
class Sections(BaseModel):
sections: List[Section] = Field(description="List of sections for this blog")
# State definitions
class NewsState(TypedDict):
query: str
date: str
search_results: List[Dict[str, Any]]
news_items: List[Dict[str, Any]]
class BlogState(TypedDict):
content: str
sections: List[Section]
completed_sections: Annotated[List, operator.add]
final_report: str
class WorkerState(TypedDict):
section: Section
completed_sections: Annotated[List, operator.add]
class ArticleScraperState(TypedDict):
url: str
article_content: str
# Helper function to detect English language
def is_english(text):
# Ensure we have enough text to analyze
if not text or len(text.strip()) < 50:
return False
try:
# Try primary language detection
return detect(text) == 'en'
except:
# If detection fails, use a more robust approach
common_english_words = ['the', 'and', 'in', 'to', 'of', 'is', 'for', 'with', 'on', 'that',
'this', 'are', 'was', 'be', 'have', 'it', 'not', 'they', 'by', 'from']
text_lower = text.lower()
# Count occurrences of common English words
english_word_count = sum(1 for word in common_english_words if f" {word} " in f" {text_lower} ")
# Calculate ratio of English words to text length
text_words = len(text_lower.split())
if text_words == 0: # Avoid division by zero
return False
english_ratio = english_word_count / min(20, text_words) # Cap at 20 to avoid skew
return english_word_count >= 5 or english_ratio > 0.25 # More stringent criteria
# News search functions
def search_ai_news(state: NewsState):
"""Search for the latest AI news using Tavily"""
search_tool = TavilySearchResults(max_results=10)
# Format today's date
today = state.get("date", datetime.now().strftime("%Y-%m-%d"))
# Create search query with date to get recent news
query = f"latest artificial intelligence news {today} english"
# Execute search
search_results = search_tool.invoke({"query": query})
# Filter out YouTube results and non-English content
filtered_results = []
for result in search_results:
if "youtube.com" not in result.get("url", "").lower():
# Check if content is in English
content = result.get("content", "") + " " + result.get("title", "")
if is_english(content):
filtered_results.append(result)
return {"search_results": filtered_results}
def parse_news_items(state: NewsState):
"""Parse search results into structured news items using a more robust approach"""
search_results = state["search_results"]
# Format results for the LLM
formatted_results = "\n\n".join([
f"Title: {result.get('title', 'No title')}\n"
f"URL: {result.get('url', 'No URL')}\n"
f"Content: {result.get('content', 'No content')}"
for result in search_results
])
# Use a direct prompt instead of structured output
system_prompt = """
Extract AI news articles from these search results. Filter out any that aren't about artificial intelligence.
For each relevant AI news article, provide:
- title: The title of the article
- url: The URL of the article
- source: The source website of the news
- description: A brief description of the article
Format your response as a JSON list of objects. Only include the relevant fields, nothing else.
Example format:
[
{
"title": "New AI Development",
"url": "https://example.com/news/ai-dev",
"source": "Example News",
"description": "Description of the AI development"
}
]
"""
# Get the response as a string
response = llm.invoke([
SystemMessage(content=system_prompt),
HumanMessage(content=f"Here are the search results:\n\n{formatted_results}")
])
# Extract the JSON part from the response
response_text = response.content
# Find JSON list in the response
json_match = re.search(r'\[\s*\{.*\}\s*\]', response_text, re.DOTALL)
news_items = []
if json_match:
try:
# Parse the JSON text
news_items = json.loads(json_match.group(0))
except json.JSONDecodeError:
# Fallback: create a simple item if JSON parsing fails
news_items = [{
"title": "AI News Roundup",
"url": "https://example.com/ai-news",
"source": "Various Sources",
"description": "Compilation of latest AI news from various sources."
}]
else:
# Create a default item if no JSON found
news_items = [{
"title": "AI News Roundup",
"url": "https://example.com/ai-news",
"source": "Various Sources",
"description": "Compilation of latest AI news from various sources."
}]
return {"news_items": news_items}
# Article scraping function
def scrape_article_content(state: ArticleScraperState):
"""Scrape the content from a news article URL"""
url = state["url"]
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
# Extract article content
article_text = ""
# Try to find the main article content
article = soup.find('article')
if article:
paragraphs = article.find_all('p')
else:
# Fallback to all paragraphs
paragraphs = soup.find_all('p')
# Extract text from paragraphs
article_text = "\n\n".join([p.get_text().strip() for p in paragraphs])
# Clean up the text
article_text = re.sub(r'\s+', ' ', article_text).strip()
# Trim to reasonable length for LLM processing
if len(article_text) > 10000:
article_text = article_text[:10000] + "..."
# Verify the content is in English
if not is_english(article_text[:500]): # Check first 500 chars to save processing time
return {"article_content": "Content not in English or insufficient text to analyze."}
return {"article_content": article_text}
except Exception as e:
return {"article_content": f"Error scraping article: {str(e)}"}
# Blog generation functions
def orchestrator(state: BlogState):
"""Orchestrator that generates a plan for the blog based on news items"""
try:
# Parse the content to extract news items
content_lines = state['content'].split('\n\n')
news_items = []
current_item = {}
for content_block in content_lines:
if content_block.startswith('TITLE:'):
# Start of a new item
if current_item and 'title' in current_item:
news_items.append(current_item)
current_item = {}
lines = content_block.split('\n')
for line in lines:
if line.startswith('TITLE:'):
current_item['title'] = line.replace('TITLE:', '').strip()
elif line.startswith('SOURCE:'):
current_item['source'] = line.replace('SOURCE:', '').strip()
elif line.startswith('URL:'):
current_item['url'] = line.replace('URL:', '').strip()
elif line.startswith('DESCRIPTION:'):
current_item['description'] = line.replace('DESCRIPTION:', '').strip()
elif line.startswith('CONTENT:'):
current_item['content'] = line.replace('CONTENT:', '').strip()
elif 'content' in current_item:
# Add to existing content
current_item['content'] += ' ' + content_block
# Add the last item
if current_item and 'title' in current_item:
news_items.append(current_item)
# Group news items by category
ai_tech_items = []
ai_business_items = []
ai_research_items = []
for item in news_items:
title = item.get('title', '').lower()
description = item.get('description', '').lower()
# Simple categorization based on keywords
if any(kw in title + description for kw in ['business', 'market', 'company', 'investment', 'startup']):
ai_business_items.append(item)
elif any(kw in title + description for kw in ['research', 'study', 'paper', 'university']):
ai_research_items.append(item)
else:
ai_tech_items.append(item)
# Create sections with subsections
sections = []
# AI Technology section
if ai_tech_items:
tech_subsections = [
Subsection(
title=item['title'],
source=item['source'],
url=item['url'],
content=f"{item.get('description', '')} {item.get('content', '')[:500]}..."
) for item in ai_tech_items
]
sections.append(Section(
name="AI Technology Developments",
description="Recent advancements in AI technology and applications",
information="Cover the latest developments in AI technology.",
subsections=tech_subsections
))
# AI Business section
if ai_business_items:
business_subsections = [
Subsection(
title=item['title'],
source=item['source'],
url=item['url'],
content=f"{item.get('description', '')} {item.get('content', '')[:500]}..."
) for item in ai_business_items
]
sections.append(Section(
name="AI in Business",
description="How AI is transforming industries and markets",
information="Focus on business applications and market trends in AI.",
subsections=business_subsections
))
# AI Research section
if ai_research_items:
research_subsections = [
Subsection(
title=item['title'],
source=item['source'],
url=item['url'],
content=f"{item.get('description', '')} {item.get('content', '')[:500]}..."
) for item in ai_research_items
]
sections.append(Section(
name="AI Research and Studies",
description="Latest research findings and academic work in AI",
information="Cover recent research papers and studies in AI.",
subsections=research_subsections
))
# If no items were categorized, create a general section
if not sections:
general_subsections = [
Subsection(
title=item['title'],
source=item['source'],
url=item['url'],
content=f"{item.get('description', '')} {item.get('content', '')[:500]}..."
) for item in news_items
]
sections.append(Section(
name="Latest AI News",
description="Roundup of the latest AI news from around the web",
information="Cover a range of AI news topics.",
subsections=general_subsections
))
return {"sections": sections}
except Exception as e:
print(f"Error in orchestrator: {str(e)}")
# Fallback plan if structured output fails
fallback_sections = [
Section(
name="Latest AI Developments",
description="Overview of recent AI advancements and research",
information="Summarize the latest AI developments from the provided content.",
subsections=[]
)
]
return {"sections": fallback_sections}
def llm_call(state: WorkerState):
"""Worker writes a section of the blog with subsections for each news item"""
section = state['section']
# Generate section header without ID for cleaner markdown
section_header = f"## {section.name}\n\n{section.description}\n"
# If there are subsections, process each one
subsections_content = ""
if section.subsections:
for idx, subsection in enumerate(section.subsections):
# Generate subsection using LLM
subsection_prompt = f"""
Write a detailed subsection about this AI news item:
Title: {subsection.title}
Source: {subsection.source}
URL: {subsection.url}
Content to summarize and expand on:
{subsection.content}
Keep your response focused on the news item and make it engaging. Use markdown formatting.
"""
subsection_content = llm.invoke([
SystemMessage(content="You are writing a subsection for an AI news blog. Write in a professional but engaging style. Include key details and insights. Use markdown formatting."),
HumanMessage(content=subsection_prompt)
])
# Format subsection with title and source (without ID tags)
formatted_subsection = f"### {subsection.title}\n\n"
formatted_subsection += f"*Source: [{subsection.source}]({subsection.url})*\n\n"
formatted_subsection += subsection_content.content
subsections_content += formatted_subsection + "\n\n"
else:
# If no subsections, generate the full section content
section_content = llm.invoke([
SystemMessage(content="Write a blog section following the provided name, description, and information. Include no preamble. Use markdown formatting."),
HumanMessage(content=f"Here is the section name: {section.name}\nDescription: {section.description}\nInformation: {section.information}")
])
subsections_content = section_content.content
# Combine section header and subsections
complete_section = section_header + subsections_content
# Return the completed section
return {"completed_sections": [complete_section]}
def synthesizer(state: BlogState):
"""Synthesize full blog from sections with proper formatting and hierarchical TOC"""
# List of completed sections
completed_sections = state["completed_sections"]
# Format completed sections into a full blog post
completed_report = "\n\n".join(completed_sections)
# Add title, date, and introduction
today = datetime.now().strftime("%Y-%m-%d")
blog_title = f"# AI News Roundup - {today}"
# Generate a brief introduction
intro = llm.invoke([
SystemMessage(content="Write a brief introduction for an AI news roundup blog post. Keep it under 100 words. Be engaging and professional."),
HumanMessage(content=f"Today's date is {today}. Write a brief introduction for an AI news roundup.")
])
# Create hierarchical table of contents
table_of_contents = "## Table of Contents\n\n"
# Find all section headings (## headings)
section_matches = re.findall(r'## ([^\n]+)', completed_report)
for i, section_name in enumerate(section_matches, 1):
# Add section to TOC with auto-generated link
# Create a clean anchor from the section name
section_anchor = section_name.lower().replace(' ', '-')
table_of_contents += f"{i}. [{section_name}](#{section_anchor})\n"
# Find all subsections within this section
section_start = completed_report.find(f"## {section_name}")
next_section_match = re.search(r'## ', completed_report[section_start+1:])
if next_section_match:
section_end = section_start + 1 + next_section_match.start()
section_text = completed_report[section_start:section_end]
else:
section_text = completed_report[section_start:]
# Extract subsection headings
subsection_matches = re.findall(r'### ([^\n]+)', section_text)
for j, subsection_name in enumerate(subsection_matches, 1):
# Create a clean anchor from the subsection name
subsection_anchor = subsection_name.lower().replace(' ', '-').replace(':', '').replace('?', '').replace('!', '').replace('.', '')
# Add subsection to TOC with proper indentation
table_of_contents += f" {i}.{j}. [{subsection_name}](#{subsection_anchor})\n"
final_report = f"{blog_title}\n\n{intro.content}\n\n{table_of_contents}\n\n---\n\n{completed_report}\n\n---\n\n*This AI News Roundup was automatically generated on {today}.*"
return {"final_report": final_report}
# Edge function to create workers for each section
def assign_workers(state: BlogState):
"""Assign a worker to each section in the plan"""
# Kick off section writing in parallel
return [Send("llm_call", {"section": s}) for s in state["sections"]]
# Main workflow functions
def create_news_search_workflow():
"""Create a workflow for searching and parsing AI news"""
workflow = StateGraph(NewsState)
# Add nodes
workflow.add_node("search_ai_news", search_ai_news)
workflow.add_node("parse_news_items", parse_news_items)
# Add edges
workflow.add_edge(START, "search_ai_news")
workflow.add_edge("search_ai_news", "parse_news_items")
workflow.add_edge("parse_news_items", END)
return workflow.compile()
def create_article_scraper_workflow():
"""Create a workflow for scraping article content"""
workflow = StateGraph(ArticleScraperState)
# Add node
workflow.add_node("scrape_article", scrape_article_content)
# Add edges
workflow.add_edge(START, "scrape_article")
workflow.add_edge("scrape_article", END)
return workflow.compile()
def create_blog_generator_workflow():
"""Create a workflow for generating the blog"""
workflow = StateGraph(BlogState)
# Add nodes
workflow.add_node("orchestrator", orchestrator)
workflow.add_node("llm_call", llm_call)
workflow.add_node("synthesizer", synthesizer)
# Add edges
workflow.add_edge(START, "orchestrator")
workflow.add_conditional_edges("orchestrator", assign_workers, ["llm_call"])
workflow.add_edge("llm_call", "synthesizer")
workflow.add_edge("synthesizer", END)
return workflow.compile()
def generate_ai_news_blog(groq_api_key=None, tavily_api_key=None, date=None):
"""Main function to generate AI news blog"""
# Set API keys if provided
if groq_api_key:
os.environ["GROQ_API_KEY"] = groq_api_key
if tavily_api_key:
os.environ["TAVILY_API_KEY"] = tavily_api_key
# Initialize LLM with the API key
global llm
llm = ChatGroq(model="qwen-2.5-32b")
# Get date
if not date:
today = datetime.now().strftime("%Y-%m-%d")
else:
today = date
# Step 1: Search for AI news
news_search = create_news_search_workflow()
news_results = news_search.invoke({"query": "latest artificial intelligence news", "date": today})
print(f"Found {len(news_results['news_items'])} AI news items")
# Step 2: Scrape content for each news item
article_scraper = create_article_scraper_workflow()
news_contents = []
for item in news_results["news_items"]:
print(f"Scraping: {item['title']} from {item['source']}")
result = article_scraper.invoke({"url": item['url']})
# Skip if not in English
if "not in English" in result["article_content"]:
print(f"Skipping non-English content: {item['title']}")
continue
news_contents.append({
"title": item['title'],
"url": item['url'],
"source": item['source'],
"description": item['description'],
"content": result["article_content"]
})
# Check if we have any news items
if not news_contents:
return "No English language AI news items found for the specified date. Please try a different date."
# Format news content for the blog generator
formatted_content = "\n\n".join([
f"TITLE: {item['title']}\nSOURCE: {item['source']}\nURL: {item['url']}\nDESCRIPTION: {item['description']}\nCONTENT: {item['content'][:2000]}..."
for item in news_contents
])
# Step 3: Generate the blog
blog_generator = create_blog_generator_workflow()
blog_result = blog_generator.invoke({
"content": formatted_content,
"completed_sections": []
})
return blog_result["final_report"]
# Gradio UI
def create_gradio_interface():
"""Create a Gradio interface for the AI News Blog Generator"""
def run_generation(groq_key, tavily_key, selected_date):
if not groq_key or not tavily_key:
return "Please provide both API keys."
try:
result = generate_ai_news_blog(groq_key, tavily_key, selected_date)
return result
except Exception as e:
return f"Error generating blog: {str(e)}"
# Create the interface
with gr.Blocks(title="AI News Blog Generator") as demo:
gr.Markdown("# AI News Blog Generator")
gr.Markdown("Generate a daily roundup of AI news articles, categorized by topic.")
with gr.Row():
with gr.Column():
groq_key = gr.Textbox(label="Groq API Key", placeholder="Enter your Groq API key", type="password")
tavily_key = gr.Textbox(label="Tavily API Key", placeholder="Enter your Tavily API key", type="password")
date_picker = gr.Textbox(label="Date (YYYY-MM-DD)", placeholder="Leave empty for today's date",
value=datetime.now().strftime("%Y-%m-%d"))
with gr.Row():
generate_button = gr.Button("Generate AI News Blog", variant="primary")
clear_button = gr.Button("Clear Output")
with gr.Column():
status_text = gr.Textbox(label="Status", placeholder="Ready to generate", interactive=False)
output_md = gr.Markdown("Your AI News Blog will appear here.")
# Add loading state and status updates
generate_button.click(
fn=lambda: "Generating AI News Blog... This may take several minutes.",
inputs=None,
outputs=status_text,
queue=False
).then(
fn=run_generation,
inputs=[groq_key, tavily_key, date_picker],
outputs=output_md
).then(
fn=lambda: "Blog generation complete!",
inputs=None,
outputs=status_text
)
# Clear output
clear_button.click(
fn=lambda: ("Ready to generate", ""),
inputs=None,
outputs=[status_text, output_md]
)
return demo
# Run the entire pipeline
if __name__ == "__main__":
try:
# Create and launch the Gradio interface
demo = create_gradio_interface()
demo.launch()
except Exception as e:
print(f"Error running the pipeline: {str(e)}") |