import gradio as gr from visualization_gary import* # 定义模式选择操作,加载不同的界面 def choose_mode(selected_mode): if selected_mode == "get interpretable Result ": return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), "in Mode 1" elif selected_mode == "To give Feedbacks": return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), "in Mode 2" else: return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), "please choose a mode" # clear if reupload def reset_state_mod1(): return None, None, "State has been reset." def reset_state_mod2(): return None, None,None,gr.update(interactive=True),gr.update(interactive=True),gr.update(interactive=True),gr.update(interactive=True),gr.update(interactive=True) # 主界面,包含模式选择和每种模式的界面 with gr.Blocks() as demo: gr.Markdown("# please choose a mode") # 模式选择器 mode_selector = gr.Radio(["get interpretable Result ", "To give Feedbacks"], label="Mode Selection") # 模式1界面 with gr.Column(visible=False) as mode1_ui: gr.Markdown("### interpretebale Result") mode1_button = gr.Button("get interpretable Classification") mode1_input_img=gr.Image() mode1_output_img= gr.Image() mode1_output_txt=gr.Markdown() #clear if reupload mode1_state = gr.State() mode1_input_img.upload(fn=reset_state_mod1, outputs=[mode1_output_img, mode1_state, mode1_output_txt]) #clear if reupload mode1_button.click(fn=direct_inference,inputs=mode1_input_img, outputs=[mode1_output_img,mode1_output_txt]) # 模式2界面 with gr.Column(visible=False) as mode2_ui: gr.Markdown("### These are some class explanations from our model for different classes,which of these classes has our model predicted?") image_input=gr.Image() gallery_output = gr.Gallery(label="Initial Label") text_output=gr.Markdown() but_generate=gr.Button("Get some interpretable Features") but_feedback_A=gr.Button("A") but_feedback_B=gr.Button("B") but_feedback_C=gr.Button("C") but_feedback_D=gr.Button("D") key_op = gr.State() A= gr.State("A") B= gr.State("B") C= gr.State("C") D= gr.State("D") #clear if reupload image_input.upload( fn=reset_state_mod2, outputs=[gallery_output, key_op, text_output,but_generate,but_feedback_A,but_feedback_B,but_feedback_C,but_feedback_D] ) #clear if reupload but_generate.click(fn=get_features_on_interface, inputs=image_input, outputs=[gallery_output,key_op,text_output,but_generate]) but_feedback_A.click(fn=post_next_image, inputs=[A,key_op], outputs=[text_output,but_feedback_A,but_feedback_B,but_feedback_C,but_feedback_D]) but_feedback_B.click(fn=post_next_image, inputs=[B,key_op], outputs=[text_output,but_feedback_A,but_feedback_B,but_feedback_C,but_feedback_D]) but_feedback_C.click(fn=post_next_image, inputs=[C,key_op], outputs=[text_output,but_feedback_A,but_feedback_B,but_feedback_C,but_feedback_D]) but_feedback_D.click(fn=post_next_image, inputs=[D,key_op], outputs=[text_output,but_feedback_A,but_feedback_B,but_feedback_C,but_feedback_D]) # but_feedback_B.click(fn=post_next_image, inputs=image_list, outputs=[image_list,image_output,text_output]) # 状态输出 status_output = gr.Textbox(label="Status") # 选择器点击事件绑定 mode_selector.change(choose_mode, inputs=mode_selector, outputs=[mode1_ui, mode2_ui, status_output]) demo.launch()