File size: 2,099 Bytes
cd5b134
 
 
 
0a6e2ba
e6201b9
cd5b134
 
 
 
0e2cfd4
cd5b134
 
 
 
 
 
 
4758cc2
 
 
 
 
 
 
 
 
cd5b134
 
 
 
 
4758cc2
 
 
 
 
 
 
 
 
cd5b134
 
 
 
 
 
 
 
0a6e2ba
cd5b134
0a6e2ba
 
 
 
cd5b134
 
0a6e2ba
cd5b134
0a6e2ba
 
cd5b134
 
 
0a6e2ba
 
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
#importing the spacy and bert model
from transformers import BertTokenizer, BertForSequenceClassification
from transformers import pipeline
import gradio as gr
from collections import Counter
import re
import spacy
import pandas as pd

#Intializing the spacy model for NER and the finbert model for sentiment analysis
nlp = spacy.load('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):
  text = re.sub(r'Photo by.+', '', text)
  text = re.sub(r"\n", " ", text)
  text = re.sub(r"\n\n", " ", text)
  text = re.sub(r"\t", " ", text)
  text = text.strip(" ")
  text = re.sub(
      " +", " ", text
      ).strip()  # get rid of multiple spaces and replace with a single
      
  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):
  text = re.sub(r'Photo by.+', '', text)
  text = re.sub(r"\n", " ", text)
  text = re.sub(r"\n\n", " ", text)
  text = re.sub(r"\t", " ", text)
  text = text.strip(" ")
  text = re.sub(
          " +", " ", text
      ).strip()  # get rid of multiple spaces and replace with a single
      
  org = []
  doc = nlp(text)
  if doc.ents:
    for ent in doc.ents:
      if ent.label_ == 'ORG':
        org.append(ent.text)
  None
  
  final = (Counter(org).most_common(1)[0][0])
  
  return (f'Organization: {final}')
  
def final_output(text):
  return return_sentiment(text), show_org(text)
  
sentiment_analysis = gr.Interface(
    final_output,
    inputs = gr.inputs.Textbox(label="Input your news article here", optional=False),
    outputs=[gr.outputs.Textbox(label="Sentiment Analysis"),
    gr.outputs.Textbox(label="Named Organization")]
)


if __name__ == "__main__":
    sentiment_analysis.launch(debug=True)