| import gradio as gr |
|
|
| def add_text(history, text): |
| history = history + [(text, None)] |
| return history, "" |
|
|
| def add_file(history, file): |
| |
| if isinstance(file, str): |
| history = history + [((file,), None)] |
| else: |
| history = history + [((file.name,), None)] |
| return history |
|
|
| def bot(history, state): |
| response = "**That's cool!**" |
| history[-1][1] = response |
| state.append(123) |
| return history, state |
|
|
| |
|
|
| with gr.Blocks() as demo: |
| |
| state = gr.State(value=[]) |
|
|
| with gr.Row(): |
| |
| with gr.Column(scale=0.85): |
| chatbot = gr.Chatbot([], elem_id="chatbot").style(height=900) |
| with gr.Row(): |
| with gr.Column(scale=0.85): |
| txt = gr.Textbox( |
| show_label=False, |
| placeholder="Enter text and press enter, or upload an image", |
| ).style(container=False) |
| gr.Examples(examples=["输出图中所有K-V", "给出上图分类结果", "输出图中所有主体信息"], inputs=txt) |
| with gr.Column(scale=0.15, min_width=0): |
| btn = gr.UploadButton("📁", file_types=["image", "video", "audio"]) |
| |
| |
| |
| with gr.Column(scale=0.15): |
| with gr.Row(max_height=20): |
| example1 = gr.Image("images/test.png", interactive=False).style(width=100, height=100) |
| example1_path = gr.Text("images/test.png", interactive=False, visible=False) |
| example1ImageButton = gr.Button("Try it!") |
| example1ImageButton.click(add_file, [chatbot, example1_path], [chatbot, ]).then(bot, [chatbot, state], [chatbot, state]) |
| |
| with gr.Row(max_height=20): |
| example2 = gr.Image("images/test.png", interactive=False).style(width=100, height=100) |
| example2_path = gr.Text("images/test.png", interactive=False, visible=False) |
| example2ImageButton = gr.Button("Try it!") |
| example2ImageButton.click(add_file, [chatbot, example1_path], [chatbot, ]).then(bot, [chatbot, state], [chatbot, state]) |
| |
| with gr.Row(max_height=20): |
| example2 = gr.Image("images/test.png", interactive=False).style(width=100, height=100) |
| example2_path = gr.Text("images/test.png", interactive=False, visible=False) |
| example2ImageButton = gr.Button("Try it!") |
| example2ImageButton.click(add_file, [chatbot, example1_path], [chatbot, ]).then(bot, [chatbot, state], [chatbot, state]) |
| |
| with gr.Row(max_height=20): |
| example2 = gr.Image("images/test.png", interactive=False).style(width=100, height=100) |
| example2_path = gr.Text("images/test.png", interactive=False, visible=False) |
| example2ImageButton = gr.Button("Try it!") |
| example2ImageButton.click(add_file, [chatbot, example1_path], [chatbot, ]).then(bot, [chatbot, state], [chatbot, state]) |
| |
| with gr.Row(max_height=20): |
| example2 = gr.Image("images/test.png", interactive=False).style(width=100, height=100) |
| example2_path = gr.Text("images/test.png", interactive=False, visible=False) |
| example2ImageButton = gr.Button("Try it!") |
| example2ImageButton.click(add_file, [chatbot, example1_path], [chatbot, ]).then(bot, [chatbot, state], [chatbot, state]) |
| |
| |
| |
| |
|
|
| txt.submit(add_text, [chatbot, txt], [chatbot, txt]).then( |
| bot, [chatbot, state], [chatbot, state] |
| ) |
| btn.upload(add_file, [chatbot, btn], [chatbot]).then( |
| bot, [chatbot, state], [chatbot, state] |
| ) |
|
|
| demo.launch() |