Spaces:
Running
Running
| # sentiment_tools.py - CrewAI Native Version | |
| from crewai.tools import BaseTool | |
| from typing import Type | |
| from pydantic import BaseModel, Field | |
| class SentimentInput(BaseModel): | |
| """Input schema for SentimentTool.""" | |
| text: str = Field(..., description="Text to analyze for sentiment") | |
| class SentimentTool(BaseTool): | |
| name: str = "Analyze Sentiment" | |
| description: str = "Analyzes the sentiment of a given text using keyword analysis" | |
| args_schema: Type[BaseModel] = SentimentInput | |
| def _run(self, text: str) -> str: | |
| try: | |
| # Simple sentiment analysis without heavy models for faster execution | |
| text_lower = text.lower() | |
| # Positive indicators | |
| positive_words = [ | |
| 'bull', 'bullish', 'up', 'rise', 'rising', 'gain', 'gains', | |
| 'positive', 'strong', 'growth', 'increase', 'rally', 'surge', | |
| 'optimistic', 'good', 'great', 'excellent', 'buy', 'moon' | |
| ] | |
| # Negative indicators | |
| negative_words = [ | |
| 'bear', 'bearish', 'down', 'fall', 'falling', 'loss', 'losses', | |
| 'negative', 'weak', 'decline', 'decrease', 'crash', 'dump', | |
| 'pessimistic', 'bad', 'poor', 'terrible', 'sell', 'fear' | |
| ] | |
| positive_count = sum(1 for word in positive_words if word in text_lower) | |
| negative_count = sum(1 for word in negative_words if word in text_lower) | |
| if positive_count > negative_count: | |
| confidence = min(0.9, 0.6 + (positive_count - negative_count) * 0.1) | |
| return f"Positive (confidence: {confidence:.1f})" | |
| elif negative_count > positive_count: | |
| confidence = min(0.9, 0.6 + (negative_count - positive_count) * 0.1) | |
| return f"Negative (confidence: {confidence:.1f})" | |
| else: | |
| return "Neutral (confidence: 0.5)" | |
| except Exception as e: | |
| return f"Sentiment analysis error: {str(e)}" |