adityasrathore commited on
Commit
49c39ca
·
1 Parent(s): 952a6aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -88
app.py CHANGED
@@ -1,97 +1,42 @@
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ demo = gr.Interface()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  with demo:
4
  gr.Markdown("## Financial Analyst AI")
5
  gr.Markdown("This project applies AI trained by our financial analysts to analyze earning calls and other financial documents.")
6
+
7
+ # Row 1: Speech Recognition and Text Summarization
8
  with gr.Row():
9
  with gr.Column():
10
  audio_file = gr.inputs.Audio(source="microphone", type="filepath")
11
+ b1 = gr.Button("Recognize Speech")
12
+ text = gr.Textbox(value="") # Textbox for speech-to-text output
13
+
 
 
 
 
 
 
 
 
 
 
14
  with gr.Column():
15
+ b2 = gr.Button("Summarize Text")
16
+ stext = gr.Textbox() # Textbox for summarized text output
 
 
 
 
 
 
 
 
 
17
 
18
+ # Row 2: Financial Tone Analysis
19
+ with gr.Row():
20
+ b3 = gr.Button("Classify Financial Tone")
21
+ label = gr.Label() # Label for sentiment analysis output
22
+
23
+ # Row 3: Financial Tone and Forward Looking Statement Analysis
24
+ with gr.Row():
25
+ b5 = gr.Button("Financial Tone and Forward Looking Statement Analysis")
26
+ fin_spans = gr.HighlightedText() # HighlightedText for financial sentiment spans
27
+ fls_spans = gr.HighlightedText() # HighlightedText for forward looking statement spans
28
+
29
+ # Row 4: Identify Companies & Locations
30
+ with gr.Row():
31
+ b4 = gr.Button("Identify Companies & Locations")
32
+ replaced_spans = gr.HighlightedText() # HighlightedText for named entity recognition spans
33
+
34
+ # Define the click handlers for the buttons
35
+ b1.click(speech_to_text, inputs=audio_file, outputs=text)
36
+ b2.click(summarize_text, inputs=text, outputs=stext)
37
+ b3.click(text_to_sentiment, inputs=stext, outputs=label)
38
+ b5.click(fin_ext, inputs=text, outputs=fin_spans)
39
+ b5.click(fls, inputs=text, outputs=fls_spans)
40
+ b4.click(fin_ner, inputs=text, outputs=replaced_spans)
41
+
42
+ demo.launch()