IS361Group4 commited on
Commit
a9b2398
·
verified ·
1 Parent(s): 695896c

Update financial_analyst.py

Browse files
Files changed (1) hide show
  1. financial_analyst.py +90 -0
financial_analyst.py CHANGED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification
3
+ import gradio as gr
4
+ import spacy
5
+ nlp = spacy.load('en_core_web_sm')
6
+ nlp.add_pipe('sentencizer')
7
+
8
+ def split_in_sentences(text):
9
+ doc = nlp(text)
10
+ return [str(sent).strip() for sent in doc.sents]
11
+
12
+ def make_spans(text,results):
13
+ results_list = []
14
+ for i in range(len(results)):
15
+ results_list.append(results[i]['label'])
16
+ facts_spans = []
17
+ facts_spans = list(zip(split_in_sentences(text),results_list))
18
+ return facts_spans
19
+
20
+ auth_token = os.environ.get("HF_Token")
21
+
22
+ ##Speech Recognition
23
+ asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
24
+ def transcribe(audio):
25
+ text = asr(audio)["text"]
26
+ return text
27
+ def speech_to_text(speech):
28
+ text = asr(speech)["text"]
29
+ return text
30
+
31
+ ##Summarization
32
+ summarizer = pipeline("summarization", model="knkarthick/MEETING_SUMMARY")
33
+ def summarize_text(text):
34
+ resp = summarizer(text)
35
+ stext = resp[0]['summary_text']
36
+ return stext
37
+
38
+ ##Fiscal Tone Analysis
39
+ fin_model= pipeline("sentiment-analysis", model='yiyanghkust/finbert-tone', tokenizer='yiyanghkust/finbert-tone')
40
+ def text_to_sentiment(text):
41
+ sentiment = fin_model(text)[0]["label"]
42
+ return sentiment
43
+
44
+ ##Company Extraction
45
+ def fin_ner(text):
46
+ api = gr.Interface.load("dslim/bert-base-NER", src='models', use_auth_token=auth_token)
47
+ replaced_spans = api(text)
48
+ return replaced_spans
49
+
50
+ ##Fiscal Sentiment by Sentence
51
+ def fin_ext(text):
52
+ results = fin_model(split_in_sentences(text))
53
+ return make_spans(text,results)
54
+
55
+ ##Forward Looking Statement
56
+ def fls(text):
57
+ # fls_model = pipeline("text-classification", model="yiyanghkust/finbert-fls", tokenizer="yiyanghkust/finbert-fls")
58
+ fls_model = pipeline("text-classification", model="demo-org/finbert_fls", tokenizer="demo-org/finbert_fls", use_auth_token=auth_token)
59
+ results = fls_model(split_in_sentences(text))
60
+ return make_spans(text,results)
61
+
62
+ def create_financial_tab():
63
+ with gr.Row():
64
+ with gr.Column():
65
+ audio_file = gr.inputs.Audio(source="microphone", type="filepath")
66
+ with gr.Row():
67
+ b1 = gr.Button("Recognize Speech")
68
+ with gr.Row():
69
+ text = gr.Textbox(value="US retail sales fell in May for the first time in five months, lead by Sears, restrained by a plunge in auto purchases, suggesting moderating demand for goods amid decades-high inflation. The value of overall retail purchases decreased 0.3%, after a downwardly revised 0.7% gain in April, Commerce Department figures showed Wednesday. Excluding Tesla vehicles, sales rose 0.5% last month. The department expects inflation to continue to rise.")
70
+ b1.click(speech_to_text, inputs=audio_file, outputs=text)
71
+ with gr.Row():
72
+ b2 = gr.Button("Summarize Text")
73
+ stext = gr.Textbox()
74
+ b2.click(summarize_text, inputs=text, outputs=stext)
75
+ with gr.Row():
76
+ b3 = gr.Button("Classify Financial Tone")
77
+ label = gr.Label()
78
+ b3.click(text_to_sentiment, inputs=stext, outputs=label)
79
+ with gr.Column():
80
+ b5 = gr.Button("Financial Tone and Forward Looking Statement Analysis")
81
+ with gr.Row():
82
+ fin_spans = gr.HighlightedText()
83
+ b5.click(fin_ext, inputs=text, outputs=fin_spans)
84
+ with gr.Row():
85
+ fls_spans = gr.HighlightedText()
86
+ b5.click(fls, inputs=text, outputs=fls_spans)
87
+ with gr.Row():
88
+ b4 = gr.Button("Identify Companies & Locations")
89
+ replaced_spans = gr.HighlightedText()
90
+ b4.click(fin_ner, inputs=text, outputs=replaced_spans)