File size: 3,283 Bytes
ea99abb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from tasks.intent_detection import intent_detection

DEFAULT_MODEL = "gemini-2.0-flash"
INTENT_MODELS = [DEFAULT_MODEL]
DEFAULT_INTENTS = ["book_flight", "order_food", "check_weather", "greeting", "goodbye"]

def intent_ui():
    with gr.Row():
        # Left column: input/config
        with gr.Column(scale=1):
            input_text = gr.Textbox(
                label="Input Text",
                lines=6,
                placeholder="Enter text to detect intent...",
                elem_id="intent-input-text"
            )
            gr.Examples(
                examples=[
                    ["I want to book a flight to Paris next week."],
                    ["Can you tell me what the weather is like in Hanoi?"]
                ],
                inputs=[input_text],
                label="Examples"
            )
            use_custom_intents = gr.Checkbox(
                label="Use custom intents",
                value=True,
                elem_id="intent-use-custom-intents"
            )
            intents_area = gr.TextArea(
                label="Candidate Intents (one per line)",
                value='\n'.join(DEFAULT_INTENTS),
                lines=5,
                visible=True,
                elem_id="intent-candidate-intents"
            )
            def toggle_intent_area(use_custom):
                return gr.update(visible=use_custom)
            use_custom_intents.change(toggle_intent_area, inputs=use_custom_intents, outputs=intents_area)
            detect_btn = gr.Button("Detect Intent", variant="primary")
            model_dropdown = gr.Dropdown(
                INTENT_MODELS,
                value=DEFAULT_MODEL,
                label="Model",
                interactive=True,
                elem_id="intent-model-dropdown"
            )
            custom_instructions = gr.Textbox(
                label="Custom Instructions (optional)",
                lines=2,
                placeholder="Add any custom instructions for the model...",
                elem_id="intent-custom-instructions"
            )
        # Right column: output/result
        with gr.Column(scale=1):
            output_box = gr.Textbox(
                label="Detected Intent",
                lines=1,
                interactive=False,
                elem_id="intent-output"
            )
#             gr.Markdown("""
# **Instructions:**
# - Enter your text and (optionally) custom intent labels.
# - Use the checkbox to switch between custom intent list or LLM auto-detect mode.
# - Add any custom instructions for the LLM if needed.
# """)
        # Logic for button
        def run_intent_detection(text, model, use_custom, intents, custom_instructions):
            candidate_intents = [s.strip() for s in intents.split("\n") if s.strip()] if use_custom else None
            return intent_detection(
                text=text,
                model=model,
                candidate_intents=candidate_intents,
                custom_instructions=custom_instructions,
                use_llm=True
            )
        detect_btn.click(
            run_intent_detection,
            inputs=[input_text, model_dropdown, use_custom_intents, intents_area, custom_instructions],
            outputs=output_box
        )