File size: 4,366 Bytes
333d5f2
768161f
 
 
 
333d5f2
768161f
f35ea6d
333d5f2
768161f
 
 
 
 
 
 
 
 
333d5f2
 
768161f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Financial Sentiment Analysis Ensemble
emoji: πŸ“ˆ
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 4.44.0
app_file: app.py
pinned: false
license: apache-2.0
tags:
- financial-sentiment
- sentiment-analysis
- finance
- nlp
- ensemble
- transformers
- gradio
---

# πŸš€ Financial Sentiment Analysis Ensemble

An advanced AI-powered sentiment analysis system specifically designed for financial texts. This application uses an ensemble of three fine-tuned transformer models to provide highly accurate sentiment predictions for financial news, social media posts, and market commentary.

## 🎯 Features

- **Ensemble Prediction**: Combines predictions from 3 specialized models for higher accuracy
- **Real-time Analysis**: Instant sentiment analysis with confidence scores
- **Interactive Interface**: User-friendly Gradio interface with examples
- **Detailed Results**: Individual model predictions and probability distributions
- **Financial Focus**: Specifically trained on financial datasets

## 🧠 Model Architecture

The ensemble consists of three fine-tuned models:

1. **DistilBERT Model** (`codealchemist01/financial-sentiment-distilbert`)
   - Fast and efficient for real-time analysis
   - Based on DistilBERT-base-uncased
   - Optimized for speed without sacrificing accuracy

2. **BERT-Large Model** (`codealchemist01/financial-sentiment-bert-large`)
   - High accuracy with deep contextual understanding
   - Based on BERT-Large-uncased
   - Superior performance on complex financial texts

3. **Improved Model** (`codealchemist01/financial-sentiment-improved`)
   - Enhanced with advanced training techniques
   - Balanced dataset training
   - Custom loss functions and optimization

## πŸ“Š Performance

- **Ensemble Accuracy**: 79.7%
- **Individual Model Accuracies**: 79.7% (DistilBERT), 84.3% (BERT-Large), 82.1% (Improved)
- **Dataset**: Twitter Financial News Sentiment
- **Categories**: Bearish πŸ“‰, Neutral ➑️, Bullish πŸ“ˆ

## πŸš€ Usage

### Web Interface

Simply enter your financial text in the input box and click "Analyze Sentiment" to get:
- Ensemble prediction with confidence score
- Probability distribution across all sentiment categories
- Individual predictions from each model
- Visual probability chart

### API Usage

You can also use the individual models directly:

```python
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

# Load any of the models
model_name = "codealchemist01/financial-sentiment-distilbert"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

def predict_sentiment(text):
    inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
    
    with torch.no_grad():
        outputs = model(**inputs)
        predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
    
    labels = ["Bearish", "Neutral", "Bullish"]
    predicted_class = torch.argmax(predictions, dim=-1).item()
    confidence = predictions[0][predicted_class].item()
    
    return {
        "label": labels[predicted_class],
        "confidence": confidence
    }

# Example
result = predict_sentiment("The stock market is showing strong growth today")
print(result)
```

## πŸ“ˆ Example Predictions

- **"Tesla's innovative battery technology could revolutionize the automotive industry."**
  - Prediction: Bullish πŸ“ˆ (85.2% confidence)

- **"Company earnings fell short of expectations, leading to a significant drop in share price."**
  - Prediction: Bearish πŸ“‰ (91.7% confidence)

- **"The Federal Reserve maintained interest rates, keeping market conditions stable."**
  - Prediction: Neutral ➑️ (78.3% confidence)

## πŸ› οΈ Technical Details

- **Framework**: Transformers, PyTorch
- **Interface**: Gradio 4.0+
- **Training**: Fine-tuned on financial datasets with advanced techniques
- **Ensemble Method**: Average probability aggregation
- **Preprocessing**: Text normalization and tokenization

## πŸ“ License

This project is licensed under the Apache 2.0 License.

## 🀝 Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

## πŸ“§ Contact

For questions or collaborations, please reach out through the Hugging Face community.

---

*Built with ❀️ for the financial AI community*