analyze_sentiment
Analyze text sentiment using FinBERT to determine polarity, confidence, and classification for news headlines or articles in quantitative finance contexts.
Instructions
Analyzes the sentiment of a given text using FinBERT on Modal (via Public Endpoint).
Args:
text: Text to analyze (e.g., news headline, article).
Returns:
Dictionary with polarity, confidence, and classification.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes |
Implementation Reference
- tools/news_intelligence.py:146-189 (handler)The core handler function that analyzes sentiment of input text using FinBERT model via Modal endpoint. Posts text to MODAL_ENDPOINT_URL, processes response to polarity and classification.def analyze_sentiment(text: str) -> Dict[str, Any]: """ Analyzes the sentiment of a given text using FinBERT on Modal (via Public Endpoint). Args: text: Text to analyze (e.g., news headline, article). Returns: Dictionary with polarity, confidence, and classification. """ # Try Modal first try: # Check if URL is configured if "replace-me" in MODAL_ENDPOINT_URL: raise ValueError("Modal URL not configured") response = requests.post(MODAL_ENDPOINT_URL, json={"text": text}, timeout=15) response.raise_for_status() result = response.json() # FinBERT returns {'label': 'positive'/'negative'/'neutral', 'score': float} label = result['label'].upper() score = result['score'] # Map to polarity-like score for compatibility (-1 to 1) if label == "POSITIVE": polarity = score elif label == "NEGATIVE": polarity = -score else: polarity = 0.0 return { "text": text[:100] + "..." if len(text) > 100 else text, "polarity": round(polarity, 3), "subjectivity": 0.0, # FinBERT doesn't give subjectivity "classification": label, "model": "FinBERT (Modal Public)" } except Exception as e: logger.error(f"Modal FinBERT failed: {e}") return {"error": f"Error analyzing sentiment: {str(e)}"}
- server.py:405-408 (registration)Registers the analyze_sentiment tool (along with related news tools) with the MCP server using the register_tools helper function.register_tools( [get_news, analyze_sentiment, get_symbol_sentiment], "News & Sentiment" )
- app.py:293-293 (registration)Includes analyze_sentiment in the tools_map dictionary under 'News & Sentiment' category, used for Gradio UI toolbox and potentially MCP exposure via Gradio's mcp_server=True."News & Sentiment": [get_news, analyze_sentiment, get_symbol_sentiment, get_news_resource],