import gradio as gr # Import UI creation and handler functions from tab modules from tab_chat import create_chat_tab, handle_chat from tab_code import create_code_tab from tab_search import create_search_tab, handle_web_search from tab_workflow import create_workflow_tab, handle_workflow_generation, handle_workflow_chat # --- Main Gradio UI Definition --- with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo: # Global state for the workflow tab workflow_state = gr.State() # --- Header --- with gr.Row(): gr.Markdown(""" # Ling & Ring Playground ### 体验下一代聊天、编码、检索与工作流自动化 """) with gr.Row(): gr.Markdown(""" [Ling Model Card](https://huggingface.co) | [Ring Model Card](https://huggingface.co) | [Read the Paper](https://huggingface.co) | [Join our Discord](https://huggingface.co) """) # --- Main UI Tabs --- with gr.Tabs() as main_ui: # Create tabs by calling functions from modules with gr.Tab("聊天 (Chat)"): chat_components = create_chat_tab() with gr.Tab("代码生成 (Code Generation)"): create_code_tab() # The code tab now handles its own events with gr.Tab("网页检索 (Web Search)"): search_components = create_search_tab() with gr.Tab("工作流 (Workflow)"): workflow_components = create_workflow_tab() # --- Event Handling Logic --- # Chat Tab Events chat_submit_event = chat_components["chat_input"].submit( fn=handle_chat, inputs=[ chat_components["chat_input"], chat_components["chatbot"], chat_components["system_prompt"], chat_components["temperature_slider"], chat_components["model_selector"] ], outputs=[ chat_components["chatbot"], chat_components["chat_input"] ] ) chat_components["send_button"].click( fn=handle_chat, inputs=[ chat_components["chat_input"], chat_components["chatbot"], chat_components["system_prompt"], chat_components["temperature_slider"], chat_components["model_selector"] ], outputs=[ chat_components["chatbot"], chat_components["chat_input"] ] ) # Web Search Tab Events search_components["search_button"].click( fn=handle_web_search, inputs=[search_components["search_input"]], outputs=[search_components["search_results_output"]] ) # Workflow Tab Events workflow_components["generate_workflow_button"].click( fn=handle_workflow_generation, inputs=[workflow_components["workflow_description_input"]], outputs=[ workflow_components["workflow_visualization_output"], workflow_components["workflow_status_output"], workflow_components["workflow_chatbot"], workflow_state, workflow_components["workflow_chat_input"] # 新增:直接作为输出 ] ) workflow_components["workflow_chat_input"].submit( fn=handle_workflow_chat, inputs=[ workflow_components["workflow_chat_input"], workflow_components["workflow_chatbot"], workflow_state ], outputs=[ workflow_components["workflow_chatbot"], workflow_state, workflow_components["workflow_status_output"], workflow_components["workflow_chat_input"] ] ) demo.launch()