基本上我试图让 gpt2 响应变量 {text} 中的提示,我遇到了这个错误:
ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()
到目前为止,这是我的代码:
import gradio as gr
from transformers import pipeline, GPT2Tokenizer, GPT2LMHeadModel
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')#gpt2-xl #for very powerful model
model = GPT2LMHeadModel.from_pretrained('gpt2', pad_token_id=tokenizer.eos_token_id)
text = "what is natural language processing?"
encoded_input = tokenizer.encode(text, return_tensors='pt')
#print(tokenizer.decode((encoded_input[0][0]))) # works well to here
def generate_text(inp):
input_ids = tokenizer.encode(inp, return_tensors='tf')
beam_output = model.generate(input_ids, max_length=100, num_beams=5, no_repeat_ngram_size=2, early_stopping=True)
output = tokenizer.decode(beam_output[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
return ".".join(output.split(".")[:-1]) + "."
output_text = gr.outputs.Textbox() # works well to here
text1 = generate_text(text) # BREAKS HERE
谁能帮我弄清楚我做错了什么?谢谢。