File size: 2,174 Bytes
76a4c0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
hv-split-demo -- Gradio Space for the interpretation splitter.

Deploy:
    huggingface-cli repo create hv-split-demo --type space --space-sdk gradio
    # upload: app.py, hv_split.py, requirements.txt
"""

import gradio as gr

from hv_split import HVInterpret


M = HVInterpret()

SAMPLES = [
    ["I need to access the bank"],
    ["All that glitters is not gold"],
    ["Why did the CEO resign last week?"],
    ["in the language of category theory, what is an identity?"],
    [
        "why did the current bank say recently that the function she "
        "used in the language of category theory had changed?"
    ],
    ["compute 2 + 2"],
]


def split(query):
    if not query.strip():
        return "", "", ""
    b = M.split(query)
    summary = (
        f"ambiguity_score : {b.ambiguity_score:.3f}\n"
        f"confidence      : {b.confidence:.3f}\n"
        f"sources         : {', '.join(b.sources) or '(none)'}\n"
        f"interpretations : {b.n_interpretations}"
    )
    rows = ["prior    source             reading"]
    rows.append("-" * 70)
    for i in b.interpretations:
        rows.append(f"{i.prior:.3f}  {i.source:<17}  {i.reading}")
    table = "\n".join(rows)
    md = M.render(b, mode="markdown")
    return summary, table, md


with gr.Blocks(title="hv-split demo") as demo:
    gr.Markdown(
        "# hv-split\n"
        "Split an ambiguous query into a ranked bundle of "
        "interpretations.\n\n"
        "**Every generative model picks one reading. This one "
        "refuses to pick.**"
    )
    with gr.Row():
        with gr.Column():
            query = gr.Textbox(
                label="Query", value=SAMPLES[0][0], lines=3
            )
            btn = gr.Button("Split", variant="primary")
        with gr.Column():
            summary = gr.Textbox(label="Summary", lines=5)
            table = gr.Textbox(label="Interpretations", lines=14)
            md = gr.Markdown(label="Markdown")

    gr.Examples(
        examples=SAMPLES,
        inputs=[query],
        outputs=[summary, table, md],
    )

    btn.click(split, inputs=[query], outputs=[summary, table, md])


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