File size: 2,400 Bytes
37d1515
 
 
 
dd103f7
 
 
37d1515
 
dd103f7
 
37d1515
 
 
 
 
dd103f7
37d1515
 
 
 
 
dd103f7
37d1515
 
dd103f7
37d1515
 
 
dd103f7
37d1515
 
 
 
dd103f7
37d1515
dd103f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37d1515
 
dd103f7
 
 
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
from smolagents import Tool
from typing import Any, Optional

class SimpleTool(Tool):
    name = "web_content_analyzer"
    description = "Analyzes web content using AI models."
    inputs = {"url":{"type":"string","description":"The webpage URL to analyze."}}
    output_type = "string"

    def forward(self, url: str) -> str:
        """Analyzes web content using AI models.

        Args:
            url: The webpage URL to analyze.

        Returns:
            str: Analysis results in JSON format.
        """
        import requests
        from bs4 import BeautifulSoup
        import re
        from transformers import pipeline
        import json

        try:
            # Fetch content
            headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'}
            response = requests.get(url, headers=headers, timeout=10)

            # Parse HTML
            soup = BeautifulSoup(response.text, 'html.parser')
            for tag in soup(['script', 'style', 'meta']):
                tag.decompose()

            # Extract basic info
            title = soup.title.string if soup.title else "No title found"
            text = re.sub(r'\s+', ' ', soup.get_text()).strip()

            if len(text) < 100:
                return json.dumps({
                    "error": "Not enough content to analyze"
                })

            # Get summary
            summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
            summary = summarizer(text[:1024], max_length=100, min_length=30)[0]['summary_text']

            # Get sentiment
            classifier = pipeline("text-classification", 
                               model="nlptown/bert-base-multilingual-uncased-sentiment")
            sentiment = classifier(text[:512])[0]
            score = int(sentiment['label'][0])
            mood = ["Very Negative", "Negative", "Neutral", "Positive", "Very Positive"][score-1]

            # Format results
            result = {
                "title": title,
                "summary": summary,
                "sentiment": f"{mood} ({score}/5)",
                "stats": {
                    "words": len(text.split()),
                    "chars": len(text)
                }
            }

            return json.dumps(result)

        except Exception as e:
            return json.dumps({
                "error": str(e)
            })