File size: 3,885 Bytes
81adca3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49c39ca
952a6aa
 
 
 
49c39ca
 
952a6aa
 
 
49c39ca
 
 
952a6aa
49c39ca
 
952a6aa
49c39ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
os.system("pip install gradio==3.0.18")
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification
import gradio as gr
import spacy
nlp = spacy.load('en_core_web_sm')
nlp.add_pipe('sentencizer')

def split_in_sentences(text):
    doc = nlp(text)
    return [str(sent).strip() for sent in doc.sents]

def make_spans(text,results):
    results_list = []
    for i in range(len(results)):
        results_list.append(results[i]['label'])
    facts_spans = []
    facts_spans = list(zip(split_in_sentences(text),results_list))
    return facts_spans
    
auth_token = os.environ.get("HF_Token")

##Speech Recognition
asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
def transcribe(audio):
    text = asr(audio)["text"]
    return text
def speech_to_text(speech):
    text = asr(speech)["text"]
    return text

##Summarization 
summarizer = pipeline("summarization", model="knkarthick/MEETING_SUMMARY")
def summarize_text(text):
    resp = summarizer(text)
    stext = resp[0]['summary_text']
    return stext

##Fiscal Tone Analysis
fin_model= pipeline("sentiment-analysis", model='yiyanghkust/finbert-tone', tokenizer='yiyanghkust/finbert-tone')
def text_to_sentiment(text):
    sentiment = fin_model(text)[0]["label"]
    return sentiment 

##Company Extraction    
def fin_ner(text):
    api = gr.Interface.load("dslim/bert-base-NER", src='models', use_auth_token=auth_token)
    replaced_spans = api(text)
    return replaced_spans    

##Fiscal Sentiment by Sentence
def fin_ext(text):
    results = fin_model(split_in_sentences(text))
    return make_spans(text,results)
    
##Forward Looking Statement
def fls(text):
#    fls_model = pipeline("text-classification", model="yiyanghkust/finbert-fls", tokenizer="yiyanghkust/finbert-fls")
    fls_model = pipeline("text-classification", model="demo-org/finbert_fls", tokenizer="demo-org/finbert_fls", use_auth_token=auth_token)
    results = fls_model(split_in_sentences(text))
    return make_spans(text,results) 

demo = gr.Interface()

with demo:
    gr.Markdown("## Financial Analyst AI")
    gr.Markdown("This project applies AI trained by our financial analysts to analyze earning calls and other financial documents.")
    
    # Row 1: Speech Recognition and Text Summarization
    with gr.Row():
        with gr.Column():
            audio_file = gr.inputs.Audio(source="microphone", type="filepath")
            b1 = gr.Button("Recognize Speech")
            text = gr.Textbox(value="")  # Textbox for speech-to-text output
            
        with gr.Column():
            b2 = gr.Button("Summarize Text")
            stext = gr.Textbox()  # Textbox for summarized text output
    
    # Row 2: Financial Tone Analysis
    with gr.Row():
        b3 = gr.Button("Classify Financial Tone")
        label = gr.Label()  # Label for sentiment analysis output
        
    # Row 3: Financial Tone and Forward Looking Statement Analysis
    with gr.Row():
        b5 = gr.Button("Financial Tone and Forward Looking Statement Analysis")
        fin_spans = gr.HighlightedText()  # HighlightedText for financial sentiment spans
        fls_spans = gr.HighlightedText()  # HighlightedText for forward looking statement spans
        
    # Row 4: Identify Companies & Locations
    with gr.Row():
        b4 = gr.Button("Identify Companies & Locations")
        replaced_spans = gr.HighlightedText()  # HighlightedText for named entity recognition spans

# Define the click handlers for the buttons
b1.click(speech_to_text, inputs=audio_file, outputs=text)
b2.click(summarize_text, inputs=text, outputs=stext)
b3.click(text_to_sentiment, inputs=stext, outputs=label)
b5.click(fin_ext, inputs=text, outputs=fin_spans)
b5.click(fls, inputs=text, outputs=fls_spans)
b4.click(fin_ner, inputs=text, outputs=replaced_spans)

demo.launch()