File size: 3,648 Bytes
a9fb7e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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()