Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import (
|
3 |
+
BartForConditionalGeneration,
|
4 |
+
BartTokenizer
|
5 |
+
)
|
6 |
+
|
7 |
+
model_name = 'unlisboa/bart_qa_assistant'
|
8 |
+
tokenizer = BartTokenizer.from_pretrained(model_name)
|
9 |
+
device = get_device()
|
10 |
+
model = BartForConditionalGeneration.from_pretrained(model_name).to(device)
|
11 |
+
model.eval()
|
12 |
+
|
13 |
+
def example(question, censor):
|
14 |
+
print(question, censor)
|
15 |
+
return question + str(censor)
|
16 |
+
|
17 |
+
examples = [["What's the meaning of life?", True]]
|
18 |
+
|
19 |
+
checkbox = gr.Checkbox(value=True, label="should censor output")
|
20 |
+
question_input = gr.Textbox(lines=2, label='Question:')
|
21 |
+
|
22 |
+
model_input = tokenizer(question_input, truncation=True, padding=True, return_tensors="pt")
|
23 |
+
generated_answers_encoded = model.generate(input_ids=model_input["input_ids"].to(device),
|
24 |
+
attention_mask=model_input["attention_mask"].to(device),
|
25 |
+
#bad_words_ids=bad_words_ids,
|
26 |
+
force_words_ids=None,
|
27 |
+
min_length=1,
|
28 |
+
max_length=100,
|
29 |
+
do_sample=True,
|
30 |
+
early_stopping=True,
|
31 |
+
num_beams=4,
|
32 |
+
temperature=1.0,
|
33 |
+
top_k=None,
|
34 |
+
top_p=None,
|
35 |
+
# eos_token_id=tokenizer.eos_token_id,
|
36 |
+
no_repeat_ngram_size=2,
|
37 |
+
num_return_sequences=1,
|
38 |
+
return_dict_in_generate=True,
|
39 |
+
output_scores=True)
|
40 |
+
|
41 |
+
response = tokenizer.batch_decode(generated_answers_encoded['sequences'], skip_special_tokens=True,
|
42 |
+
clean_up_tokenization_spaces=True)[0]
|
43 |
+
|
44 |
+
answer_output = gr.Textbox(lines=2, label='Answer:')
|
45 |
+
gr.Interface(fn=example, inputs=[question_input, checkbox], outputs=[answer_output], allow_flagging="never", examples=examples).launch()
|