import gradio as gr import random class Human: def __init__(self): self.count = 0 print("init") def reset(self): self.count = 0 print("reset") def echo(self, message, history): self.count += 1 return "this round:"+str(self.count) #h = Human() #hs = gr.State(h) def output(message, history, s): r = s.echo(message, history) return r ''' demo = gr.ChatInterface( fn=output, additional_inputs=[hs], retry_btn=None, undo_btn=None, submit_btn="发送", #clear_btn="再来一局", clear_btn=gr.ClearButton(hs), title="Echo Bot") ''' def respond(message, history, state): bot_message = state.echo(message, history) history.append((message, bot_message)) return "", history, state def cl(message, history, state): message = "" history.clear() state.reset() return "", history, state with gr.Blocks() as demo: label = gr.Label("hello world") html = gr.HTML("

Greeting App

\

This app greets people.

") chatbot = gr.Chatbot(label="heoo world", value=[('ff','bb'),('cc','dd')]) msg = gr.Textbox() submit = gr.Button("发送" ) clear = gr.ClearButton() h = Human() state = gr.State(h) clear.click(cl, [msg, chatbot, state], [msg, chatbot, state]) submit.click(respond, [msg, chatbot, state], [msg, chatbot, state]) demo.launch()