simonschoe commited on
Commit
7ea9b2b
β€’
1 Parent(s): 95888be

add pred labels

Browse files
Files changed (1) hide show
  1. app.py +39 -40
app.py CHANGED
@@ -1,36 +1,35 @@
1
  import gradio as gr
 
2
  from transformers import pipeline
3
  from transformers_interpret import SequenceClassificationExplainer
4
- from bs4 import BeautifulSoup
5
-
6
 
7
  # Setup model
8
  classifier = pipeline("text-classification", model="simonschoe/TransformationTransformer")
9
  explainer = SequenceClassificationExplainer(classifier.model, classifier.tokenizer)
10
 
11
- legend = """
12
  <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>
13
  """
14
 
15
  def classify(_input):
16
  """
17
- wrapper method to compute label 1 probability and explanation for given input
18
  """
 
19
  result = classifier(_input)[0]
20
- score = result['score']
21
- if result['label'] == 'LABEL_0':
22
- score = 1 - score
 
23
 
24
- # getting visualization
25
  attributions = explainer(_input, class_name='LABEL_1')
26
  html = explainer.visualize().__html__()
27
-
28
  soup = BeautifulSoup(html, 'html.parser')
29
  explanation = soup.find_all('td')[-1].__str__().replace('td', 'div')
30
-
31
- # adding legend to word importance explanation
32
- result_html = explanation + legend
33
- return round(score, 4), result_html
34
 
35
  app = gr.Blocks(theme=gr.themes.Default(), css='#component-0 {max-width: 730px; margin: auto; padding-top: 1.5rem}')
36
 
@@ -41,53 +40,53 @@ with app:
41
  ## Detect Transformation-Related Sentences in Quarterly Earnings Calls
42
  """
43
  )
44
-
45
  with gr.Tabs() as tabs:
46
  with gr.TabItem("πŸ” Model", id=0):
47
- #with gr.Row():
48
- with gr.Row():
49
- text_in = gr.Textbox(lines=1, placeholder="Insert text here", label="Input Sentence", scale=5)
50
- compute_bt = gr.Button("Classify", scale=1)
51
- html_out = gr.HTML(label="Explanation", scale=5)
52
- score_out = gr.Number(label="Score", value=float("NaN"), interactive=False, scale=1)
53
- gr.Examples(
54
- examples=[
55
- ["If we look at the plans for 2018, it is to introduce 650 new products, which is an absolute all- time high."],
56
- ["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."],
57
- ["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,"]
58
- ],
59
- label="Examples (click to start detection)",
60
- inputs=[text_in],
61
- outputs=[score_out, html_out],
62
- fn=classify,
63
- run_on_click=True,
64
- cache_examples=False
65
- )
66
  with gr.TabItem("πŸ“ Usage", id=1):
67
  gr.Markdown(
68
  """
69
  #### App usage
70
- The model is intented to be used for **sequence classification**: It encodes the input sentence (entered in the textbox on the left) in a dense vector space and runs it through a deep neural network classifier (*Distill-RoBERTa*).
71
- 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 rather undecided about the correct label.
 
 
72
  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.
73
  """
74
- )
75
  with gr.TabItem("πŸ“– About", id=2):
76
- gr.Markdown(
77
  """
78
  #### Project Description
79
  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.
80
  """
81
  )
82
-
83
-
84
  with gr.Accordion("πŸ“™ Citation", open=False):
85
  citation_button = gr.Textbox(
86
  value='Placeholder',
87
  label='Copy to cite these results.',
88
  show_copy_button=True
89
  )
90
-
91
 
92
  compute_bt.click(classify, inputs=[text_in], outputs=[score_out, html_out])
93
 
 
1
  import gradio as gr
2
+ from bs4 import BeautifulSoup
3
  from transformers import pipeline
4
  from transformers_interpret import SequenceClassificationExplainer
 
 
5
 
6
  # Setup model
7
  classifier = pipeline("text-classification", model="simonschoe/TransformationTransformer")
8
  explainer = SequenceClassificationExplainer(classifier.model, classifier.tokenizer)
9
 
10
+ LEGEND = """
11
  <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>
12
  """
13
 
14
  def classify(_input):
15
  """
16
+ wrapper function to compute label 1 probability and explanation for given input
17
  """
18
+ # label probabilities
19
  result = classifier(_input)[0]
20
+ labels = {
21
+ "Transformation Talk": result['score'] if result['label'] == 'LABEL_1' else 1-result['score'],
22
+ "No Transformation Talk": result['score'] if result['label'] == 'LABEL_0' else 1-result['score']
23
+ }
24
 
25
+ # word importance scores
26
  attributions = explainer(_input, class_name='LABEL_1')
27
  html = explainer.visualize().__html__()
 
28
  soup = BeautifulSoup(html, 'html.parser')
29
  explanation = soup.find_all('td')[-1].__str__().replace('td', 'div')
30
+ result_html = explanation + LEGEND
31
+
32
+ return labels, result_html
 
33
 
34
  app = gr.Blocks(theme=gr.themes.Default(), css='#component-0 {max-width: 730px; margin: auto; padding-top: 1.5rem}')
35
 
 
40
  ## Detect Transformation-Related Sentences in Quarterly Earnings Calls
41
  """
42
  )
43
+
44
  with gr.Tabs() as tabs:
45
  with gr.TabItem("πŸ” Model", id=0):
46
+ with gr.Row():
47
+ text_in = gr.Textbox(lines=1, placeholder="Insert text here", label="Input Sentence", scale=5)
48
+ compute_bt = gr.Button("Classify", scale=1)
49
+ score_out = gr.Label(label="Scores", scale=1)
50
+ html_out = gr.HTML(label="Explanation")
51
+ #score_out = gr.Number(label="Score", value=float("NaN"), interactive=False, scale=1)
52
+ gr.Examples(
53
+ examples=[
54
+ ["If we look at the plans for 2018, it is to introduce 650 new products, which is an absolute all- time high."],
55
+ ["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."],
56
+ ["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,"]
57
+ ],
58
+ label="Examples (click to start detection)",
59
+ inputs=[text_in],
60
+ outputs=[score_out, html_out],
61
+ fn=classify,
62
+ run_on_click=True,
63
+ cache_examples=False
64
+ )
65
  with gr.TabItem("πŸ“ Usage", id=1):
66
  gr.Markdown(
67
  """
68
  #### App usage
69
+ 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*).
70
+
71
+ 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.
72
+
73
  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.
74
  """
75
+ )
76
  with gr.TabItem("πŸ“– About", id=2):
77
+ gr.Markdown(
78
  """
79
  #### Project Description
80
  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.
81
  """
82
  )
83
+
 
84
  with gr.Accordion("πŸ“™ Citation", open=False):
85
  citation_button = gr.Textbox(
86
  value='Placeholder',
87
  label='Copy to cite these results.',
88
  show_copy_button=True
89
  )
 
90
 
91
  compute_bt.click(classify, inputs=[text_in], outputs=[score_out, html_out])
92