|
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(): |
|
|
|
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" |
|
) |
|
|
|
with gr.Column(scale=1): |
|
output_box = gr.Textbox( |
|
label="Detected Intent", |
|
lines=1, |
|
interactive=False, |
|
elem_id="intent-output" |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
) |
|
|