File size: 2,378 Bytes
556524e
72e2b6e
 
 
556524e
 
 
 
 
 
 
72e2b6e
 
 
 
 
 
 
7fa2eda
 
 
72e2b6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fa2eda
 
 
72e2b6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fa2eda
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
import importlib.util
import sys
from pathlib import Path

# load api_client directly from file to avoid 'app' package conflict
_client_path = Path(__file__).parent.parent / "api_client.py"
_spec = importlib.util.spec_from_file_location("api_client", _client_path)
_module = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_module)
api_predict = _module.api_predict
require_api = _module.require_api

import pandas as pd
import streamlit as st

require_api()

st.title("Predict Intent")
st.caption(
    "Test the intent classifier live, switching between models or letting the A/B router decide."
)

col1, col2 = st.columns([3, 1])

with col1:
    text = st.text_input("Enter a query", placeholder="e.g. what is my account balance")

with col2:
    model_choice = st.selectbox(
        "Model",
        options=["A/B Router", "Classical (LogReg)", "SVM", "Transformer (DistilBERT)"],
    )

model_map = {
    "A/B Router": None,
    "Classical (LogReg)": "classical",
    "SVM": "svm",
    "Transformer (DistilBERT)": "transformer",
}

if st.button("Predict", type="primary", disabled=not text):
    with st.spinner("predicting..."):
        result = api_predict(text, model_map[model_choice])

    col_a, col_b, col_c = st.columns(3)
    col_a.metric("Intent", result["intent"])
    col_b.metric("Confidence", f"{result['confidence']:.2%}")
    col_c.metric("Latency", f"{result['latency_ms']:.1f} ms")

    if result["is_oos"]:
        st.warning("This query was flagged as out-of-scope (low confidence).")

    if result.get("ab_variant"):
        st.info(
            f"Served by A/B variant **{result['ab_variant']}** using model `{result['model_used']}`"
        )
    else:
        st.info(f"Served by model `{result['model_used']}`")

    st.subheader("Top 5 Predictions")
    df = pd.DataFrame(result["top5"])
    df["confidence"] = df["confidence"].astype(float)
    st.bar_chart(df.set_index("intent")["confidence"], horizontal=True)

st.divider()
st.caption("Sample queries to try:")
samples = [
    "what is my account balance",
    "book a flight to new york",
    "set an alarm for 7am",
    "tell me about quantum physics",
    "write me a poem about the ocean",
]
cols = st.columns(len(samples))
for col, sample in zip(cols, samples):
    col.code(sample, language=None)