|
| 1 | +from typing import Callable, List |
| 2 | +import gradio as gr |
| 3 | + |
| 4 | + |
| 5 | +def handle_user_message(message, history): |
| 6 | + """ |
| 7 | + callback function for updating user messages in interface on submit button click |
| 8 | +
|
| 9 | + Params: |
| 10 | + message: current message |
| 11 | + history: conversation history |
| 12 | + Returns: |
| 13 | + None |
| 14 | + """ |
| 15 | + # Append the user's message to the conversation history |
| 16 | + return "", history + [[message, ""]] |
| 17 | + |
| 18 | + |
| 19 | +def make_demo(run_fn: Callable, stop_fn: Callable, examples: List): |
| 20 | + with gr.Blocks( |
| 21 | + theme=gr.themes.Soft(), |
| 22 | + css=".disclaimer {font-variant-caps: all-small-caps;}", |
| 23 | + ) as demo: |
| 24 | + gr.Markdown(f"""<h1><center>AI Agent with OpenVINO</center></h1>""") |
| 25 | + chatbot = gr.Chatbot(height=800) |
| 26 | + with gr.Row(): |
| 27 | + with gr.Column(): |
| 28 | + msg = gr.Textbox( |
| 29 | + label="Chat Message Box", |
| 30 | + placeholder="Chat Message Box", |
| 31 | + show_label=False, |
| 32 | + container=False, |
| 33 | + ) |
| 34 | + with gr.Column(): |
| 35 | + with gr.Row(): |
| 36 | + submit = gr.Button("Submit") |
| 37 | + stop = gr.Button("Stop") |
| 38 | + clear = gr.Button("Clear") |
| 39 | + gr.Examples(examples, inputs=msg, label="Click on any example and press the 'Submit' button") |
| 40 | + |
| 41 | + submit_event = msg.submit( |
| 42 | + fn=handle_user_message, |
| 43 | + inputs=[msg, chatbot], |
| 44 | + outputs=[msg, chatbot], |
| 45 | + queue=False, |
| 46 | + ).then( |
| 47 | + fn=run_fn, |
| 48 | + inputs=[ |
| 49 | + chatbot, |
| 50 | + ], |
| 51 | + outputs=chatbot, |
| 52 | + queue=True, |
| 53 | + ) |
| 54 | + submit_click_event = submit.click( |
| 55 | + fn=handle_user_message, |
| 56 | + inputs=[msg, chatbot], |
| 57 | + outputs=[msg, chatbot], |
| 58 | + queue=False, |
| 59 | + ).then( |
| 60 | + fn=run_fn, |
| 61 | + inputs=[ |
| 62 | + chatbot, |
| 63 | + ], |
| 64 | + outputs=chatbot, |
| 65 | + queue=True, |
| 66 | + ) |
| 67 | + stop.click( |
| 68 | + fn=stop_fn, |
| 69 | + inputs=None, |
| 70 | + outputs=None, |
| 71 | + cancels=[submit_event, submit_click_event], |
| 72 | + queue=False, |
| 73 | + ) |
| 74 | + clear.click(lambda: None, None, chatbot, queue=False) |
| 75 | + return demo |
0 commit comments