将 jupyter 小部件与 QA 管道连接以解决问题选择问题

Connecting jupyter widgets with QA pipeline for question selection problem

我正在使用 Huggingface 中的一种预训练语言模型进行问答,但我正在努力将 jupyter 小部件与 nlp 管道连接起来。

我有这个片段,它向用户展示了一组他们可以用于他们的 QA 的问题。

w = widgets.Dropdown(
  options=['What are the risks of defamation?', 'Are there risks?', \
'What are the risks of the trial?', \
'Is the trial risky?'],
  value='What are the risks of defamation?',
  description='Question:',
)


def on_change(change):
    if change['type'] == 'change' and change['name'] == 'value':
        trial_q = change['new']
        #print(trial_q) works fine
        

w.observe(on_change)
display(w)

选择非常好。但是,我无法将其与管道连接。

def QA(q, a):
    print(nlp({
    'question': q,
    'context': a
}))
    
def transcriptName():

  with open('data/uploaded_file.txt', 'r') as myfile: 
  data=myfile.read().replace('\n', '')

trial_q = QA('What are you randomised for?', data)

调用函数时,我得到ValueError: Input is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers.

所以我认为这将是上下文的问题。但是,如果我用字符串表示或 'What is the risk?' 更改函数内部的 trial_q,它就可以正常工作。可能是什么问题?

这是一个范围问题。范围在小部件内。

w = widgets.Dropdown(
  options=['What are the risks of defamation?', 'Are there risks?', \
'What are the risks of the trial?', \
'Is the trial risky?'],
  value='What are the risks of defamation?',
  description='Question:',
)


def on_change(change):
    if change['type'] == 'change' and change['name'] == 'value':
        trial_q = change['new']
        
        ...pipeline here...
        

w.observe(on_change)
display(w)