Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,36 +10,46 @@ device = get_device()
|
|
| 10 |
model = BartForConditionalGeneration.from_pretrained(model_name).to(device)
|
| 11 |
model.eval()
|
| 12 |
|
| 13 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
print(question, censor)
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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=
|
|
|
|
| 10 |
model = BartForConditionalGeneration.from_pretrained(model_name).to(device)
|
| 11 |
model.eval()
|
| 12 |
|
| 13 |
+
def get_device():
|
| 14 |
+
# If there's a GPU available...
|
| 15 |
+
if torch.cuda.is_available():
|
| 16 |
+
device = torch.device("cuda")
|
| 17 |
+
n_gpus = torch.cuda.device_count()
|
| 18 |
+
first_gpu = torch.cuda.get_device_name(0)
|
| 19 |
+
|
| 20 |
+
print(f'There are {n_gpus} GPU(s) available.')
|
| 21 |
+
print(f'GPU gonna be used: {first_gpu}')
|
| 22 |
+
else:
|
| 23 |
+
print('No GPU available, using the CPU instead.')
|
| 24 |
+
device = torch.device("cpu")
|
| 25 |
+
return device
|
| 26 |
+
|
| 27 |
+
def run_bart(question, censor):
|
| 28 |
print(question, censor)
|
| 29 |
+
|
| 30 |
+
model_input = tokenizer(question_input, truncation=True, padding=True, return_tensors="pt")
|
| 31 |
+
generated_answers_encoded = model.generate(input_ids=model_input["input_ids"].to(device),
|
| 32 |
+
attention_mask=model_input["attention_mask"].to(device),
|
| 33 |
+
#bad_words_ids=bad_words_ids,
|
| 34 |
+
force_words_ids=None,
|
| 35 |
+
min_length=1,
|
| 36 |
+
max_length=100,
|
| 37 |
+
do_sample=True,
|
| 38 |
+
early_stopping=True,
|
| 39 |
+
num_beams=4,
|
| 40 |
+
temperature=1.0,
|
| 41 |
+
top_k=None,
|
| 42 |
+
top_p=None,
|
| 43 |
+
# eos_token_id=tokenizer.eos_token_id,
|
| 44 |
+
no_repeat_ngram_size=2,
|
| 45 |
+
num_return_sequences=1,
|
| 46 |
+
return_dict_in_generate=True,
|
| 47 |
+
output_scores=True)
|
| 48 |
+
response = tokenizer.batch_decode(generated_answers_encoded['sequences'], skip_special_tokens=True,clean_up_tokenization_spaces=True)[0]
|
| 49 |
+
return response
|
| 50 |
|
| 51 |
examples = [["What's the meaning of life?", True]]
|
|
|
|
| 52 |
checkbox = gr.Checkbox(value=True, label="should censor output")
|
| 53 |
+
question_input = gr.Textbox(lines=2, label='Question:')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
answer_output = gr.Textbox(lines=2, label='Answer:')
|
| 55 |
+
gr.Interface(fn=run_bart, inputs=[question_input, checkbox], outputs=[answer_output], allow_flagging="never", examples=examples).launch()
|