Views
No views yet
| Training Loss | Epoch | Step | Validation Loss | F1 | Acc |
|---|---|---|---|---|---|
| 0.0975 | 1.0 | 87 | 0.2763 | 0.9101 | 0.9088 |
1import matplotlib.pyplot as plt
2import plotly.graph_objects as go
3from IPython.display import display, HTML
4import numpy as np
5from transformers import pipeline
6%matplotlib inline
7
8# Pipelines
9classifier = pipeline("text-classification", model="Sharpaxis/Finance_DistilBERT_sentiment", top_k=None)
10pipe = pipeline("text-classification", model="Sharpaxis/News_classification_distilbert")
11
12def finance_text_predictor(text):
13 text = str(text)
14 out = classifier(text)[0]
15 type_news = pipe(text)[0]
16
17 # Display news type and text in HTML
18 if type_news['label'] == 'LABEL_1':
19 display(HTML(f"""
20 <div style="border: 2px solid red; padding: 10px; margin: 10px; background-color: #ffe6e6; color: black; font-weight: bold;">
21 IMPORTANT TECH/FIN News<br>
22 <div style="margin-top: 10px; font-weight: normal; font-size: 14px; color: darkred;">{text}</div>
23 </div>
24 """))
25 elif type_news['label'] == 'LABEL_0':
26 display(HTML(f"""
27 <div style="border: 2px solid green; padding: 10px; margin: 10px; background-color: #e6ffe6; color: black; font-weight: bold;">
28 NON IMPORTANT NEWS<br>
29 <div style="margin-top: 10px; font-weight: normal; font-size: 14px; color: darkgreen;">{text}</div>
30 </div>
31 """))
32
33 # Sentiment analysis scores
34 scores = [sample['score'] for sample in out]
35 labels = [sample['label'] for sample in out]
36 label_map = {'LABEL_0': "Negative", 'LABEL_1': "Neutral", 'LABEL_2': "Positive"}
37 sentiments = [label_map[label] for label in labels]
38
39 print("SCORES")
40 for i in range(len(scores)):
41 print(f"{sentiments[i]} : {scores[i]:.4f}")
42
43 print(f"Sentiment of text is {sentiments[np.argmax(scores)]}")
44
45 # Bar chart for sentiment scores
46 fig = go.Figure(
47 data=[go.Bar(x=sentiments, y=scores, marker=dict(color=["red", "blue", "green"]), width=0.3)]
48 )
49 fig.update_layout(
50 title="Sentiment Analysis Scores",
51 xaxis_title="Sentiments",
52 yaxis_title="Scores",
53 template="plotly_dark"
54 )
55 fig.show()