takala/financial_phrasebank
Updated • 9.32k • 257
How to use Sharpaxis/Finance_DistilBERT_sentiment with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-classification", model="Sharpaxis/Finance_DistilBERT_sentiment") # Load model directly
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained("Sharpaxis/Finance_DistilBERT_sentiment")
model = AutoModelForSequenceClassification.from_pretrained("Sharpaxis/Finance_DistilBERT_sentiment")This model is a fine-tuned version of distilbert-base-uncased on the financial_phrasebank dataset. It achieves the following results on the evaluation set:
More information needed
More information needed
More information needed
The following hyperparameters were used during training:
| Training Loss | Epoch | Step | Validation Loss | F1 | Acc |
|---|---|---|---|---|---|
| 0.0975 | 1.0 | 87 | 0.2763 | 0.9101 | 0.9088 |
import matplotlib.pyplot as plt
import plotly.graph_objects as go
from IPython.display import display, HTML
import numpy as np
from transformers import pipeline
%matplotlib inline
# Pipelines
classifier = pipeline("text-classification", model="Sharpaxis/Finance_DistilBERT_sentiment", top_k=None)
pipe = pipeline("text-classification", model="Sharpaxis/News_classification_distilbert")
def finance_text_predictor(text):
text = str(text)
out = classifier(text)[0]
type_news = pipe(text)[0]
# Display news type and text in HTML
if type_news['label'] == 'LABEL_1':
display(HTML(f"""
<div style="border: 2px solid red; padding: 10px; margin: 10px; background-color: #ffe6e6; color: black; font-weight: bold;">
IMPORTANT TECH/FIN News<br>
<div style="margin-top: 10px; font-weight: normal; font-size: 14px; color: darkred;">{text}</div>
</div>
"""))
elif type_news['label'] == 'LABEL_0':
display(HTML(f"""
<div style="border: 2px solid green; padding: 10px; margin: 10px; background-color: #e6ffe6; color: black; font-weight: bold;">
NON IMPORTANT NEWS<br>
<div style="margin-top: 10px; font-weight: normal; font-size: 14px; color: darkgreen;">{text}</div>
</div>
"""))
# Sentiment analysis scores
scores = [sample['score'] for sample in out]
labels = [sample['label'] for sample in out]
label_map = {'LABEL_0': "Negative", 'LABEL_1': "Neutral", 'LABEL_2': "Positive"}
sentiments = [label_map[label] for label in labels]
print("SCORES")
for i in range(len(scores)):
print(f"{sentiments[i]} : {scores[i]:.4f}")
print(f"Sentiment of text is {sentiments[np.argmax(scores)]}")
# Bar chart for sentiment scores
fig = go.Figure(
data=[go.Bar(x=sentiments, y=scores, marker=dict(color=["red", "blue", "green"]), width=0.3)]
)
fig.update_layout(
title="Sentiment Analysis Scores",
xaxis_title="Sentiments",
yaxis_title="Scores",
template="plotly_dark"
)
fig.show()
Base model
distilbert/distilbert-base-uncased