File size: 5,349 Bytes
3932ad8
7ea9b2b
3932ad8
 
 
 
 
 
 
7ea9b2b
c75d824
3932ad8
 
 
 
7ea9b2b
3932ad8
7ea9b2b
3932ad8
7ea9b2b
 
 
 
3932ad8
7ea9b2b
c75d824
3932ad8
 
 
7ea9b2b
 
 
3932ad8
c75d824
3932ad8
 
1ca1b3a
 
c75d824
1ca1b3a
 
 
7ea9b2b
c75d824
 
7ea9b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c75d824
3932ad8
 
 
7ea9b2b
 
 
 
53e63bc
3932ad8
7ea9b2b
c75d824
7ea9b2b
c75d824
 
 
 
3932ad8
7ea9b2b
c75d824
 
 
 
 
 
 
3932ad8
 
 
1ca1b3a
 
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
import gradio as gr
from bs4 import BeautifulSoup
from transformers import pipeline
from transformers_interpret import SequenceClassificationExplainer

# Setup model
classifier = pipeline("text-classification", model="simonschoe/TransformationTransformer")
explainer = SequenceClassificationExplainer(classifier.model, classifier.tokenizer)

LEGEND = """
<div style="text-align: left; display: block; margin-left: auto; margin-right: auto; border-top: 1px solid; margin-top: 5px; padding-top: 5px;"><b>Legend:&emsp;</b><span style="display: inline-block; width: 10px; height: 10px; border: 1px solid; background-color: hsl(0, 75%, 60%)"></span> No Transformation Talk&emsp;<span style="display: inline-block; width: 10px; height: 10px; border: 1px solid; background-color: hsl(120, 75%, 50%)"></span> Transformation Talk</div>
"""

def classify(_input):
    """
    wrapper function to compute label 1 probability and explanation for given input
    """
    # label probabilities
    result = classifier(_input)[0]
    labels = {
        "Transformation Talk": result['score'] if result['label'] == 'LABEL_1' else 1-result['score'],
        "No Transformation Talk": result['score'] if result['label'] == 'LABEL_0' else 1-result['score']       
    }

    # word importance scores
    attributions = explainer(_input, class_name='LABEL_1')
    html = explainer.visualize().__html__()
    soup = BeautifulSoup(html, 'html.parser')
    explanation = soup.find_all('td')[-1].__str__().replace('td', 'div')
    result_html = explanation + LEGEND

    return labels, result_html

app = gr.Blocks(theme=gr.themes.Default(), css='#component-0 {max-width: 730px; margin: auto; padding-top: 1.5rem}')

with app:
    gr.Markdown(
        """
        # Transformation Talk
        ## Detect Transformation-Related Sentences in Quarterly Earnings Calls
        """
    )

    with gr.Tabs() as tabs:
        with gr.TabItem("πŸ” Model", id=0):
            with gr.Row():
                text_in = gr.Textbox(lines=1, placeholder="Insert text here", label="Input Sentence", scale=5)
                compute_bt = gr.Button("Classify", scale=1)
            score_out = gr.Label(label="Scores", scale=1)
            html_out = gr.HTML(label="Explanation")
            #score_out = gr.Number(label="Score", value=float("NaN"), interactive=False, scale=1)
            gr.Examples(
                examples=[
                    ["If we look at the plans for 2018, it is to introduce 650 new products, which is an absolute all- time high."],
                    ["We have been doing kind of an integrated campaign, so it's TV, online, we do the Google Ad Words - all those different elements together."],
                    ["So that turned out to be beneficial for us, and I think, we'll just see how the market and interest rates move over the course of the year,"]
                ],
                label="Examples (click to start detection)",
                inputs=[text_in],
                outputs=[score_out, html_out],
                fn=classify,
                run_on_click=True,
                cache_examples=False
            )
        with gr.TabItem("πŸ“ Usage", id=1):
            gr.Markdown(
                """
                #### App usage
                The model is intented to be used for **sequence classification**: It encodes the input sentence (entered in the textbox "Input Sentence") in a dense vector space and runs it through a deep neural network classifier (*RoBERTa*).
                
                It returns a confidence score that indicates the probability of the sentence containing a discussion on transformation activities. A value of 1 (0) signals a high confidence of the sentence being transformation-related (generic). A score in the range of [0.25; 0.75] implies that the model is equivocal about the correct label.
                
                In addition, the app returns the tokenized version of the sentence, alongside word importances that are indicated by color codes. Those visuals illustrates the ability of the context-aware classifier to simultaneously pay attention to various parts in the input sentence to derive a final label.                
                """
            )
        with gr.TabItem("πŸ“– About", id=2):
            gr.Markdown(
                """
                #### Project Description
                Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
                """
            )

    with gr.Accordion("πŸ“™ Citation", open=False):
        citation_button = gr.Textbox(
            value='Placeholder',
            label='Copy to cite these results.',
            show_copy_button=True
        )

    compute_bt.click(classify, inputs=[text_in], outputs=[score_out, html_out])


if __name__ == "__main__":
    app.launch()