legacy107 commited on
Commit
53e3e9d
1 Parent(s): 9265710

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -10
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from gradio.components import Textbox
3
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, T5ForConditionalGeneration
4
  from peft import PeftModel, PeftConfig
5
  import torch
@@ -9,8 +9,8 @@ import datasets
9
  model_name = "google/flan-t5-large"
10
  peft_name = "legacy107/flan-t5-large-ia3-cpgQA"
11
  tokenizer = AutoTokenizer.from_pretrained(model_name)
12
- model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-large")
13
- model = PeftModel.from_pretrained(model, peft_name)
14
 
15
  peft_name = "legacy107/flan-t5-large-ia3-bioasq-paraphrase"
16
  peft_config = PeftConfig.from_pretrained(peft_name)
@@ -23,7 +23,7 @@ max_target_length = 200
23
  # Load your dataset
24
  dataset = datasets.load_dataset("minh21/cpgQA-v1.0-unique-context-test-10-percent-validation-10-percent", split="test")
25
  dataset = dataset.shuffle()
26
- dataset = dataset.select(range(5))
27
 
28
 
29
  def paraphrase_answer(question, answer):
@@ -50,7 +50,7 @@ def paraphrase_answer(question, answer):
50
 
51
 
52
  # Define your function to generate answers
53
- def generate_answer(question, context, ground_truth):
54
  # Combine question and context
55
  input_text = f"question: {question} context: {context}"
56
 
@@ -71,9 +71,18 @@ def generate_answer(question, context, ground_truth):
71
  generated_answer = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
72
 
73
  # Paraphrase answer
74
- paraphrased_answer = paraphrase_answer(question, generated_answer)
 
 
75
 
76
- return generated_answer, paraphrased_answer
 
 
 
 
 
 
 
77
 
78
 
79
  # Define a function to list examples from the dataset
@@ -83,7 +92,7 @@ def list_examples():
83
  context = example["context"]
84
  question = example["question"]
85
  answer = example["answer_text"]
86
- examples.append([question, context, answer])
87
  return examples
88
 
89
 
@@ -93,11 +102,14 @@ iface = gr.Interface(
93
  inputs=[
94
  Textbox(label="Question"),
95
  Textbox(label="Context"),
96
- Textbox(label="Ground truth")
 
 
97
  ],
98
  outputs=[
99
  Textbox(label="Generated Answer"),
100
- Textbox(label="Natural Answer")
 
101
  ],
102
  examples=list_examples()
103
  )
 
1
  import gradio as gr
2
+ from gradio.components import Textbox, Checkbox
3
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, T5ForConditionalGeneration
4
  from peft import PeftModel, PeftConfig
5
  import torch
 
9
  model_name = "google/flan-t5-large"
10
  peft_name = "legacy107/flan-t5-large-ia3-cpgQA"
11
  tokenizer = AutoTokenizer.from_pretrained(model_name)
12
+ pretrained_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-large")
13
+ model = PeftModel.from_pretrained(pretrained_model, peft_name)
14
 
15
  peft_name = "legacy107/flan-t5-large-ia3-bioasq-paraphrase"
16
  peft_config = PeftConfig.from_pretrained(peft_name)
 
23
  # Load your dataset
24
  dataset = datasets.load_dataset("minh21/cpgQA-v1.0-unique-context-test-10-percent-validation-10-percent", split="test")
25
  dataset = dataset.shuffle()
26
+ dataset = dataset.select(range(10))
27
 
28
 
29
  def paraphrase_answer(question, answer):
 
50
 
51
 
52
  # Define your function to generate answers
53
+ def generate_answer(question, context, ground_truth, do_pretrained, do_natural):
54
  # Combine question and context
55
  input_text = f"question: {question} context: {context}"
56
 
 
71
  generated_answer = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
72
 
73
  # Paraphrase answer
74
+ paraphrase_answer = ""
75
+ if do_natural:
76
+ paraphrased_answer = paraphrase_answer(question, generated_answer)
77
 
78
+ # Get pretrained model's answer
79
+ pretrained_answer = ""
80
+ if do_pretrained:
81
+ with torch.no_grad():
82
+ generated_ids = pretrained_model.generate(input_ids=input_ids, max_new_tokens=max_target_length)
83
+ pretrained_answer = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
84
+
85
+ return generated_answer, paraphrased_answer, pretrained_answer
86
 
87
 
88
  # Define a function to list examples from the dataset
 
92
  context = example["context"]
93
  question = example["question"]
94
  answer = example["answer_text"]
95
+ examples.append([question, context, answer, True, True])
96
  return examples
97
 
98
 
 
102
  inputs=[
103
  Textbox(label="Question"),
104
  Textbox(label="Context"),
105
+ Textbox(label="Ground truth"),
106
+ Checkbox(label="Include pretrained model's result"),
107
+ Checkbox(label="Include natural answer")
108
  ],
109
  outputs=[
110
  Textbox(label="Generated Answer"),
111
+ Textbox(label="Natural Answer"),
112
+ Textbox(label="Pretrained Model's Answer"),
113
  ],
114
  examples=list_examples()
115
  )