Jupyter Notebook - 第二次无法获取用户输入 - EOFError: EOF when reading a line

问题描述

我正在尝试创建一个类似 UI 的聊天机器人,我希望在其中递归地要求用户通过 input() 函数输入问题并将其传递给外部函数并获取相关答案。

---> 36   ques[0] = input("How can i help you? ")
     37   chat(ques[0])
     38 
EOFError: EOF when reading a line

下面的代码第一次工作 - 获取输入甚至返回相关输出。但是,当我单击“再试一次”按钮(第二次)时出现上述错误。

from ipywidgets import interact,widgets
from IPython.display import display,clear_output

ques = [""] 
def chat(q):
  a = faq(q) #FAQ is a function that returns answers to questions
  question = widgets.Text(
      value= ques[0],disabled=True
  )
  display(question)

  answer = widgets.Textarea(
      value= a[0][0],disabled=True
  )
  display(answer)

  def callback(wdgt):
      display(wdgt.value)

  question.on_submit(callback)

def btn_eventhandler(obj):
  ques[0] = input("How can i help you? ")
  chat(ques[0])    

ques[0] = input("How can i help you? ")
chat(ques[0])

btn = widgets.Button(description='Try again ?')
display(btn)
btn.on_click(btn_eventhandler)

我还希望使用 clear_output() 函数,以便为下一个用户输入获得清晰的屏幕。

我真的迷路了。请帮帮我!!

解决方法

input() 是为 terminal/console/cmd.exe 创建的,也许这就是它在 jupyter 中无法正常工作的原因。

我宁愿使用 widgets.Text 来创建 input_widget


最少的工作代码

from ipywidgets import interact,widgets
from IPython.display import display,clear_output

#ques = [""] 

def faq(q):
    return [["I don't know !!!"]]

def chat(q):
    a = faq(q) #FAQ is a function that returns answers to questions
    
    question = widgets.Text(
        value = q,disabled=True
    )
    display(question)

    answer = widgets.Textarea(
        value= a[0][0],disabled=True
    )
    display(answer)

def input_widget(text,callback):
    label = widgets.Label(text)
    
    text = widgets.Text()
    text.on_submit(callback)

    box = widgets.HBox([label,text])
    display(box)

def result(event):
    chat(event.value)    
    
    btn = widgets.Button(description='Try again ?')
    btn.on_click(ask)
    display(btn)

def ask(event=None):
    input_widget("How can i help you? ",result)
    
ask()   

enter image description here


编辑:

在新问题之前使用 clear_output() 删除小部件的版本。

最终您可以使用 widget.close() 仅删除一些小部件 - 但它们必须是 global 才能在其他功能中访问它们。

from ipywidgets import interact,clear_output

def faq(q):
    return [["I don't know !!!"]]

def chat(q):
    #global question,answer

    a = faq(q) #FAQ is a function that returns answers to questions
    
    question = widgets.Text(
        value = q,callback):
    #global input_label,input_text,input_box
    
    input_label = widgets.Label(text)
    
    input_text = widgets.Text()
    input_text.on_submit(callback)

    input_box = widgets.HBox([input_label,input_text])
    display(input_box)

def result(event):
    #global btn

    chat(event.value)    
    
    btn = widgets.Button(description='Try again ?')
    btn.on_click(try_again)
    display(btn)

def try_again(event):
    #input_box.close()
    #question.close()
    #answer.close()
    #btn.close()
    clear_output()
    
    ask()
    
def ask():
    input_widget("How can i help you? ",result)
    
ask()

编辑:

版本缩减为两个函数 ask_questionget_answer

from ipywidgets import interact,clear_output

def faq(question):
    return [["I don't know !!!"]]

def get_answer(event):
    question = event.value
    answer = faq(question) 
    answer = answer[0][0]

    chat_question = widgets.Text(
        value = question,disabled=True
    )
    display(chat_question)

    chat_answer = widgets.Textarea(
        value= answer,disabled=True
    )
    display(chat_answer)

    chat_button = widgets.Button(description='Try again ?')
    chat_button.on_click(ask_question)
    display(chat_button)
    
def ask_question(event=None):
    clear_output()

    input_label = widgets.Label("How can i help you? ")
    
    input_text = widgets.Text()
    input_text.on_submit(get_answer)

    input_box = widgets.HBox([input_label,input_text])
    display(input_box)

ask_question()

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...