|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Demo script showcasing LangChain AI/ML integration capabilities. |
| 4 | +
|
| 5 | +This script demonstrates the AI-enhanced web scraping features including: |
| 6 | +- Content analysis and summarization |
| 7 | +- Sentiment analysis |
| 8 | +- Topic classification |
| 9 | +- Entity extraction |
| 10 | +- Quality assessment |
| 11 | +
|
| 12 | +Run with: python demo_ai_features.py |
| 13 | +""" |
| 14 | + |
| 15 | +import asyncio |
| 16 | +import json |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +from src.ai.content_analyzer import create_content_analyzer |
| 20 | +from src.schemas.news import NewsArticle |
| 21 | +from src.parsers.ai_enhanced_news import AIEnhancedNewsParser, SmartParserFactory |
| 22 | + |
| 23 | + |
| 24 | +async def demo_content_analysis(): |
| 25 | + """Demonstrate AI-powered content analysis capabilities.""" |
| 26 | + print("🤖 AI-Enhanced Web Scraper Demo") |
| 27 | + print("=" * 50) |
| 28 | + |
| 29 | + # Create a sample article for demonstration |
| 30 | + # Only title and url are required - all other fields are Optional |
| 31 | + sample_article = NewsArticle( # type: ignore |
| 32 | + title="Revolutionary AI Breakthrough Achieves Human-Level Performance", |
| 33 | + content=""" |
| 34 | + In a groundbreaking development, researchers at leading technology institutes |
| 35 | + have successfully created an artificial intelligence system that demonstrates |
| 36 | + human-level performance across multiple cognitive tasks. This breakthrough |
| 37 | + represents years of dedicated research in machine learning and neural networks. |
| 38 | + |
| 39 | + The AI system, trained on diverse datasets, shows remarkable capabilities in |
| 40 | + natural language understanding, reasoning, and problem-solving. Early tests |
| 41 | + indicate that the technology could have profound implications for industries |
| 42 | + ranging from healthcare to autonomous vehicles. |
| 43 | + |
| 44 | + Scientists involved in the project express cautious optimism about the future |
| 45 | + applications of this technology, while emphasizing the importance of responsible |
| 46 | + development and deployment of advanced AI systems. |
| 47 | + """, |
| 48 | + url="https://example.com/ai-breakthrough-2024" |
| 49 | + ) |
| 50 | + |
| 51 | + # Create content analyzer |
| 52 | + analyzer = create_content_analyzer(use_mock_llm=True) |
| 53 | + |
| 54 | + print("\n📊 Analyzing the sample article...\n") |
| 55 | + |
| 56 | + print(f"Article: {sample_article.title}") |
| 57 | + print("-" * 60) |
| 58 | + |
| 59 | + # Run AI analysis |
| 60 | + analysis = await analyzer.analyze_article(sample_article) |
| 61 | + |
| 62 | + # Display results |
| 63 | + print(f"📝 Summary: {analysis.summary}") |
| 64 | + print(f"😊 Sentiment: {analysis.sentiment} (confidence: {analysis.confidence:.2f})") |
| 65 | + print(f"⭐ Quality Score: {analysis.quality_score:.1f}/10") |
| 66 | + print(f"📖 Readability: {analysis.readability}") |
| 67 | + print(f"🏷️ Topics: {', '.join(analysis.topics)}") |
| 68 | + print(f"👥 Entities: {', '.join(analysis.entities[:5])}...") |
| 69 | + print(f"🔤 Language: {analysis.language}") |
| 70 | + print(f"📊 Word Count: {analysis.word_count}") |
| 71 | + print(f"✅ Completeness: {analysis.completeness:.1%}") |
| 72 | + print() |
| 73 | + |
| 74 | + |
| 75 | +async def demo_ai_enhanced_parser(): |
| 76 | + """Demonstrate the AI-enhanced parser factory.""" |
| 77 | + print("🏭 AI-Enhanced Parser Factory Demo") |
| 78 | + print("=" * 40) |
| 79 | + |
| 80 | + # Create different parser configurations |
| 81 | + configurations = [ |
| 82 | + ("Standard Parser", SmartParserFactory.create_standard_parser()), |
| 83 | + ("AI-Enhanced Parser", SmartParserFactory.create_enhanced_parser(ai_enabled=True)), |
| 84 | + ] |
| 85 | + |
| 86 | + for name, parser in configurations: |
| 87 | + print(f"\n{name}:") |
| 88 | + print(f" • Parser ID: {parser.id}") |
| 89 | + print(f" • AI Analysis: {'✅ Enabled' if parser.enable_ai_analysis else '❌ Disabled'}") |
| 90 | + print(f" • Content Analyzer: {'✅ Available' if parser.content_analyzer else '❌ None'}") |
| 91 | + |
| 92 | + |
| 93 | +async def demo_langchain_integration(): |
| 94 | + """Demonstrate LangChain integration details.""" |
| 95 | + print("🔗 LangChain Integration Details") |
| 96 | + print("=" * 35) |
| 97 | + |
| 98 | + analyzer = create_content_analyzer() |
| 99 | + |
| 100 | + print("📦 LangChain Components:") |
| 101 | + print(f" • Core Framework: {type(analyzer).__name__}") |
| 102 | + print(f" • Mock LLM: {'✅ Active' if analyzer.use_mock_llm else '❌ Disabled'}") |
| 103 | + print(f" • Prompt Templates: ✅ Configured") |
| 104 | + print(f" • Output Parsers: ✅ Available") |
| 105 | + |
| 106 | + print("\n🧠 AI Capabilities:") |
| 107 | + capabilities = [ |
| 108 | + "Content Summarization", |
| 109 | + "Sentiment Analysis", |
| 110 | + "Topic Classification", |
| 111 | + "Entity Extraction", |
| 112 | + "Quality Assessment", |
| 113 | + "Language Detection", |
| 114 | + "Readability Analysis" |
| 115 | + ] |
| 116 | + |
| 117 | + for capability in capabilities: |
| 118 | + print(f" • {capability}: ✅ Implemented") |
| 119 | + |
| 120 | + print("\n🏗️ Architecture Benefits:") |
| 121 | + benefits = [ |
| 122 | + "Pluggable AI components", |
| 123 | + "Fallback analysis without LLM", |
| 124 | + "Structured output with Pydantic", |
| 125 | + "Async processing support", |
| 126 | + "Mock LLM for development/testing", |
| 127 | + "Extensible prompt templates" |
| 128 | + ] |
| 129 | + |
| 130 | + for benefit in benefits: |
| 131 | + print(f" • {benefit}") |
| 132 | + |
| 133 | + |
| 134 | +async def main(): |
| 135 | + """Run the complete AI features demonstration.""" |
| 136 | + try: |
| 137 | + await demo_content_analysis() |
| 138 | + await demo_ai_enhanced_parser() |
| 139 | + await demo_langchain_integration() |
| 140 | + |
| 141 | + print("\n🎯 Demo Complete!") |
| 142 | + print("\nThis demonstrates:") |
| 143 | + print("• LangChain integration for AI-powered content analysis") |
| 144 | + print("• Sophisticated prompt engineering and output parsing") |
| 145 | + print("• Production-ready error handling and fallbacks") |
| 146 | + print("• Type-safe AI results with Pydantic schemas") |
| 147 | + print("• Scalable architecture for ML/AI enhancements") |
| 148 | + |
| 149 | + except Exception as e: |
| 150 | + print(f"❌ Demo failed: {e}") |
| 151 | + import traceback |
| 152 | + traceback.print_exc() |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + print("🚀 Starting AI-Enhanced Web Scraper Demo...") |
| 157 | + asyncio.run(main()) |
0 commit comments