File size: 3,408 Bytes
171f2ef
9602bb7
de239b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171f2ef
9602bb7
21916d9
fdbc2bf
9602bb7
de239b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171f2ef
9602bb7
 
171f2ef
9602bb7
 
 
de239b9
0d2c9df
fdbc2bf
 
 
 
de239b9
fdbc2bf
 
 
 
0d2c9df
171f2ef
de239b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9602bb7
171f2ef
 
de239b9
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
import gradio as gr
from comp import generate_response
import re

# --- Constants ---
WORKFLOW_SYSTEM_PROMPT = """You are an expert in analyzing conversations and extracting user workflows.
Based on the provided chat history, identify the user's main goal or intent.
Then, break down the conversation into a series of actionable steps that represent the workflow to achieve that goal.
The output should be in two parts, clearly separated:
**Intent**: [A concise description of the user's goal]
**Steps**:
[A numbered list of steps]
"""

# --- Helper Functions ---
def parse_workflow_response(response):
    intent_match = re.search(r"\*\*Intent\*\*:\s*(.*)", response, re.IGNORECASE)
    steps_match = re.search(r"\*\*Steps\*\*:\s*(.*)", response, re.DOTALL | re.IGNORECASE)

    intent = intent_match.group(1).strip() if intent_match else "Could not determine intent."
    steps = steps_match.group(1).strip() if steps_match else "Could not determine steps."
    
    return intent, steps

# --- Gradio UI ---

with gr.Blocks() as demo:
    gr.Markdown("# Ling Playground")

    with gr.Row():
        with gr.Column(scale=2):
            gr.Markdown("## Chat")
            chat_chatbot = gr.Chatbot(label="Chat", bubble_full_width=False)
            chat_msg = gr.Textbox(label="Your Message")
            
        with gr.Column(scale=1):
            gr.Markdown("## Workflow Extraction")
            intent_textbox = gr.Textbox(label="Task Intent", interactive=False)
            steps_textbox = gr.Textbox(
                label="Extracted Steps", interactive=False, lines=15
            )
    
    chat_clear = gr.ClearButton([chat_msg, chat_chatbot, intent_textbox, steps_textbox])

    def user(user_message, history):
        return "", history + [[user_message, None]]

    def bot(history):
        user_message = history[-1][0]
        history[-1][1] = ""
        # Main chat model call (uses default system prompt)
        for response in generate_response(user_message, history[:-1]):
            if "</think>" in response:
                parts = response.split("</think>", 1)
                thinking_text = parts[0].replace("<think>", "")
                body_text = parts[1]

                md_output = f"**Thinking...**\n```\n{thinking_text}\n```\n\n{body_text}"
                history[-1][1] = md_output
            else:
                history[-1][1] = response
            yield history

    def update_workflow(history):
        if not history or not history[-1][0]:
            return "", ""

        # The last user message is the main prompt for the workflow agent
        user_message = history[-1][0]
        # The rest of the conversation is the history
        chat_history_for_workflow = history[:-1]

        # Call the model with the workflow system prompt
        full_response = ""
        for response in generate_response(
            user_message,
            chat_history_for_workflow,
            system_prompt=WORKFLOW_SYSTEM_PROMPT
        ):
            full_response = response
        
        intent, steps = parse_workflow_response(full_response)
        return intent, steps

    (   chat_msg.submit(user, [chat_msg, chat_chatbot], [chat_msg, chat_chatbot], queue=False)
        .then(bot, chat_chatbot, chat_chatbot)
        .then(update_workflow, chat_chatbot, [intent_textbox, steps_textbox])
    )

if __name__ == "__main__":
    demo.launch(share=True)