Rawiwan1912 commited on
Commit
8332776
·
verified ·
1 Parent(s): 5b77fac

Update modules/financial_analyst.py

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