Taranosaurus commited on
Commit
54d4ac0
1 Parent(s): 1619049

Added hypothesis_template and removed superfluous tokenizer

Browse files

The hypothesis_template can be used to make the labelling more malleable give better classifications from the input text

Files changed (1) hide show
  1. app.py +8 -7
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from transformers import pipeline, AutoTokenizer
2
  import gradio as gr
3
  import torch
4
 
@@ -9,17 +9,17 @@ else:
9
 
10
  summary_checkpoint = "facebook/bart-large-cnn" #"google/pegasus-large"
11
  oracle_checkpoint = "facebook/bart-large-mnli"
12
- tokenizer = AutoTokenizer.from_pretrained(summary_checkpoint, device=device)
13
- summary = pipeline(task="summarization", model=summary_checkpoint, tokenizer=tokenizer, device=device)
14
 
15
  oracle = pipeline(task="zero-shot-classification", model=oracle_checkpoint, device=device)
16
  labels = ["merge","revert","fix","feature","update","refactor","test","security","documentation","style"]
17
  selected_labels = ["feature","update","refactor","test","security","documentation","style"]
18
 
19
- def do_the_thing(input, labels):
20
- #print(labels)
 
21
  summarisation = summary(input, truncation=True)[0]['summary_text']
22
- zsc_results = oracle(sequences=[input, summarisation], candidate_labels=labels, multi_label=False, batch_size=2)
23
  classifications_input = {}
24
  for i in range(len(labels)):
25
  classifications_input.update({zsc_results[0]['labels'][i]: zsc_results[0]['scores'][i]})
@@ -37,6 +37,7 @@ with gr.Blocks() as frontend:
37
  with gr.Row():
38
  with gr.Column():
39
  input_labels = gr.Dropdown(label="Classification Labels", choices=labels, multiselect=True, value=selected_labels, interactive=True, allow_custom_value=True, info="Labels to classify the original text and summary")
 
40
  with gr.Column():
41
  output_summary_text = gr.TextArea(label="Summary of Notes")
42
  with gr.Row():
@@ -44,6 +45,6 @@ with gr.Blocks() as frontend:
44
  output_original_labels = gr.Label(label="Original Text Classification")
45
  with gr.Column():
46
  output_summary_labels = gr.Label(label="Summary Text Classification")
47
- btn_submit.click(fn=do_the_thing, inputs=[input_value, input_labels], outputs=[output_summary_text, output_original_labels, output_summary_labels])
48
 
49
  frontend.launch()
 
1
+ from transformers import pipeline
2
  import gradio as gr
3
  import torch
4
 
 
9
 
10
  summary_checkpoint = "facebook/bart-large-cnn" #"google/pegasus-large"
11
  oracle_checkpoint = "facebook/bart-large-mnli"
12
+ summary = pipeline(task="summarization", model=summary_checkpoint, device=device)
 
13
 
14
  oracle = pipeline(task="zero-shot-classification", model=oracle_checkpoint, device=device)
15
  labels = ["merge","revert","fix","feature","update","refactor","test","security","documentation","style"]
16
  selected_labels = ["feature","update","refactor","test","security","documentation","style"]
17
 
18
+ def do_the_thing(input, hypothesis, labels):
19
+ if hypothesis == None or hypothesis == "" or '{}' not in hypothesis:
20
+ hypothesis= "This example is {}."
21
  summarisation = summary(input, truncation=True)[0]['summary_text']
22
+ zsc_results = oracle(sequences=[input, summarisation], candidate_labels=labels, multi_label=False, batch_size=2, hypothesis_template=hypothesis)
23
  classifications_input = {}
24
  for i in range(len(labels)):
25
  classifications_input.update({zsc_results[0]['labels'][i]: zsc_results[0]['scores'][i]})
 
37
  with gr.Row():
38
  with gr.Column():
39
  input_labels = gr.Dropdown(label="Classification Labels", choices=labels, multiselect=True, value=selected_labels, interactive=True, allow_custom_value=True, info="Labels to classify the original text and summary")
40
+ input_hypothesis = gr.Textbox(label="Hypothesis Template", info="This must include the {} format syntax. Blank and invalid inputs get defaulted to the palceholder text.", value="This git commit relates to {} changes.", placeholder="This example is {}.")
41
  with gr.Column():
42
  output_summary_text = gr.TextArea(label="Summary of Notes")
43
  with gr.Row():
 
45
  output_original_labels = gr.Label(label="Original Text Classification")
46
  with gr.Column():
47
  output_summary_labels = gr.Label(label="Summary Text Classification")
48
+ btn_submit.click(fn=do_the_thing, inputs=[input_value, input_hypothesis, input_labels], outputs=[output_summary_text, output_original_labels, output_summary_labels])
49
 
50
  frontend.launch()