<jupyter_start><jupyter_text>https://colab.research.google.com/drive/1C6_ahu0Eps_wLKcsfspEO0HIEouND-oI?usp=sharingscrollTo=l3KUgPcFIuxB<jupyter_code>!pip install transformers
!pip install chart_studio
from pathlib import Path
import shutil
import os
import logging
import sys
sys.path.append('..')

from textblob import TextBlob
from pprint import pprint
from sklearn.metrics import classification_report

from transformers import AutoModelForSequenceClassification
import torch

from plotly.offline import iplot
import chart_studio.plotly as py
import plotly.graph_objs as go

%load_ext autoreload
%autoreload 2
import numpy as np
import pandas as pd
project_dir = Path.cwd().parent
pd.set_option('max_colwidth', -1)
from transformers import AutoTokenizer, AutoModelForSequenceClassification
  
tokenizer = AutoTokenizer.from_pretrained("ProsusAI/finbert")

model = AutoModelForSequenceClassification.from_pretrained("ProsusAI/finbert")
def report(df, cols=['label','prediction','logits']):
    #print('Validation loss:{0:.2f}'.format(metrics['best_validation_loss']))
    cs = CrossEntropyLoss(weight=finbert.class_weights)
    loss = cs(torch.tensor(list(df[cols[2]])),torch.tensor(list(df[cols[0]])))
    print("Loss:{0:.2f}".format(loss))
    print("Accuracy:{0:.2f}".format((df[cols[0]] == df[cols[1]]).sum() / df.shape[0]) )
    print("\nClassification Report:")
    print(classification_report(df[cols[0]], df[cols[1]]))
from google.colab import drive

drive.mount('/content/gdrive')
df = pd.read_csv('./gdrive/MyDrive/data/sample_tweets.csv', index_col=0, parse_dates=[1])

df.head()
text = df.clean_tweet[1]
df.shape[0]
df.sentiment[0]
#Postprocessing with softmax
for i in range(df.shape[0]):
  text = df.clean_tweet[i]
  inputs = tokenizer(text, padding = True, truncation = True, return_tensors='pt')
  outputs = model(**inputs)
  predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
  model.config.id2label
  if torch.argmax(predictions)==0:
    df.sentiment[i] = 'Positive'
  elif torch.argmax(predictions)==1:
    df.sentiment[i] = 'Negative'
  else:
    df.sentiment[i] = 'Neutral'

# #Formatting the results as a pandas data frame


# #Headline #Positive #Negative #Neutral
# positive = predictions[:, 0].tolist()
# negative = predictions[:, 1].tolist()
# neutral = predictions[:, 2].tolist()

# table = {'Headline':text,
#          "Positive":positive,
#          "Negative":negative, 
#          "Neutral":neutral}
      
# df = pd.DataFrame(table, columns = ["Headline", "Positive", "Negative", "Neutral"])

# df.head(5)

df['sentiment'].value_counts()
df['sentiment'].value_counts().plot(kind='bar', xlabel='Sentiment',
                                    ylabel='Count', title='Overall Sentiment Distribution')
# Filter tweets by sentiment and user followers
df.clean_tweet[(df.sentiment=='Negative') & (df.user_followers>np.mean(df.user_followers))]

from datetime import datetime, timedelta, timezone
# select last hour tweets 
df.sentiment[(df.created_at > (datetime.now() - timedelta(hours = 5)).replace(tzinfo=timezone.utc))].value_counts().plot(kind='bar')
from wordcloud import WordCloud, STOPWORDS

text = df.clean_tweet.values 
stopwords = set(STOPWORDS)
wordcloud = WordCloud(background_color ='white', stopwords = stopwords).generate(str(text))
plt.figure(figsize=(14,10))
colormap="hsv"
plt.imshow(wordcloud)

plt.axis("off")
plt.show()
text = df.tweet.values 

wordcloud = WordCloud(background_color ='white', stopwords = stopwords).generate(str(text))
plt.figure(figsize=(14,10))
colormap="hsv"
plt.imshow(wordcloud)

plt.axis("off")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14845474613686535, 'fraction_numerical': 0.011865342163355408, 'mean_word_length': 5.788389513108614, 'pattern_counts': {'":': 3, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '190567', 'license': 'Apache-2.0', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1235, 'n_words': 233, 'path': 'Twiter-Sentiment-Analysis-with-FinBERT .ipynb', 'repo_name': 'giorgosfatouros/sentiment-analysis-for-financial-news', 'size': '3624'}