File size: 1,781 Bytes
cd5b134
 
 
 
 
 
 
 
 
a413745
cd5b134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#importing the spacy and bert model
from transformers import BertTokenizer, BertForSequenceClassification
from transformers import pipeline
import gradio as gr
from gradio.mix import Parallel
import spacy
import pandas as pd

#Intializing the spacy model for NER and the finbert model for sentiment analysis
nlp = spacy.load('spacy/en_core_web_sm')
finbert = BertForSequenceClassification.from_pretrained('yiyanghkust/finbert-tone',num_labels=3)
tokenizer = BertTokenizer.from_pretrained('yiyanghkust/finbert-tone')
sentiment = pipeline("sentiment-analysis", model=finbert, tokenizer=tokenizer)


#defining a function to give us the sentiment of the article
def return_sentiment(text):
  results = sentiment(text[:512])
  return (f"{results[0]['label']} ---> {results[0]['score']}")
  
#defining a function to return the names of the organization present in the article
def show_org(text):
  org = []
  doc = nlp(text)
  if doc.ents:
    for ent in doc.ents:
      if ent.label_ == 'ORG':
        org.append(ent.text)
  None
  
  df = pd.DataFrame()
  org = list(set(org))
  df['Organization'] = org
  return df
  
  
sentiment_analysis = gr.Interface(
    fn=return_sentiment,
    inputs = gr.inputs.Textbox(label="Input your news article here", optional=False),
    outputs=gr.outputs.Textbox(label="Sentiment Analysis"),
)

named_organization = gr.Interface(
    fn=show_org,
    inputs = gr.inputs.Textbox(label="Input your news article here", optional=False),
    outputs=gr.outputs.Dataframe(label="Named organizations"),
)

Parallel(
    sentiment_analysis,
    named_organization,
    title="Sentiment Analysis of stock news",
    inputs=gr.inputs.Textbox(
        label="Input your article here",
    ),
    theme="darkhuggingface",
).launch(enable_queue=True, debug=True)