IS361Group4 commited on
Commit
a8cd4fa
·
verified ·
1 Parent(s): 3a80b46

Update modules/financial_analyst.py

Browse files
Files changed (1) hide show
  1. modules/financial_analyst.py +97 -0
modules/financial_analyst.py CHANGED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.system("pip install gradio==3.0.18")
3
+ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification
4
+ import gradio as gr
5
+ import spacy
6
+ nlp = spacy.load('en_core_web_sm')
7
+ nlp.add_pipe('sentencizer')
8
+
9
+ def split_in_sentences(text):
10
+ doc = nlp(text)
11
+ return [str(sent).strip() for sent in doc.sents]
12
+
13
+ def make_spans(text,results):
14
+ results_list = []
15
+ for i in range(len(results)):
16
+ results_list.append(results[i]['label'])
17
+ facts_spans = []
18
+ facts_spans = list(zip(split_in_sentences(text),results_list))
19
+ return facts_spans
20
+
21
+ auth_token = os.environ.get("HF_Token")
22
+
23
+ ##Speech Recognition
24
+ asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
25
+ def transcribe(audio):
26
+ text = asr(audio)["text"]
27
+ return text
28
+ def speech_to_text(speech):
29
+ text = asr(speech)["text"]
30
+ return text
31
+
32
+ ##Summarization
33
+ summarizer = pipeline("summarization", model="knkarthick/MEETING_SUMMARY")
34
+ def summarize_text(text):
35
+ resp = summarizer(text)
36
+ stext = resp[0]['summary_text']
37
+ return stext
38
+
39
+ ##Fiscal Tone Analysis
40
+ fin_model= pipeline("sentiment-analysis", model='yiyanghkust/finbert-tone', tokenizer='yiyanghkust/finbert-tone')
41
+ def text_to_sentiment(text):
42
+ sentiment = fin_model(text)[0]["label"]
43
+ return sentiment
44
+
45
+ ##Company Extraction
46
+ def fin_ner(text):
47
+ api = gr.Interface.load("dslim/bert-base-NER", src='models', use_auth_token=auth_token)
48
+ replaced_spans = api(text)
49
+ return replaced_spans
50
+
51
+ ##Fiscal Sentiment by Sentence
52
+ def fin_ext(text):
53
+ results = fin_model(split_in_sentences(text))
54
+ return make_spans(text,results)
55
+
56
+ ##Forward Looking Statement
57
+ def fls(text):
58
+ # fls_model = pipeline("text-classification", model="yiyanghkust/finbert-fls", tokenizer="yiyanghkust/finbert-fls")
59
+ fls_model = pipeline("text-classification", model="demo-org/finbert_fls", tokenizer="demo-org/finbert_fls", use_auth_token=auth_token)
60
+ results = fls_model(split_in_sentences(text))
61
+ return make_spans(text,results)
62
+
63
+ demo = gr.Blocks()
64
+
65
+ with demo:
66
+ gr.Markdown("## Financial Analyst AI")
67
+ gr.Markdown("This project applies AI trained by our financial analysts to analyze earning calls and other financial documents.")
68
+ with gr.Row():
69
+ with gr.Column():
70
+ audio_file = gr.inputs.Audio(source="microphone", type="filepath")
71
+ with gr.Row():
72
+ b1 = gr.Button("Recognize Speech")
73
+ with gr.Row():
74
+ 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.")
75
+ b1.click(speech_to_text, inputs=audio_file, outputs=text)
76
+ with gr.Row():
77
+ b2 = gr.Button("Summarize Text")
78
+ stext = gr.Textbox()
79
+ b2.click(summarize_text, inputs=text, outputs=stext)
80
+ with gr.Row():
81
+ b3 = gr.Button("Classify Financial Tone")
82
+ label = gr.Label()
83
+ b3.click(text_to_sentiment, inputs=stext, outputs=label)
84
+ with gr.Column():
85
+ b5 = gr.Button("Financial Tone and Forward Looking Statement Analysis")
86
+ with gr.Row():
87
+ fin_spans = gr.HighlightedText()
88
+ b5.click(fin_ext, inputs=text, outputs=fin_spans)
89
+ with gr.Row():
90
+ fls_spans = gr.HighlightedText()
91
+ b5.click(fls, inputs=text, outputs=fls_spans)
92
+ with gr.Row():
93
+ b4 = gr.Button("Identify Companies & Locations")
94
+ replaced_spans = gr.HighlightedText()
95
+ b4.click(fin_ner, inputs=text, outputs=replaced_spans)
96
+
97
+ demo.launch()