hv-tail / app.py
zeechimp's picture
Create app.py
0bfb64a verified
Raw History Blame Contribute Delete
2.83 kB
"""
hv-tail-demo -- Gradio Space for the above-ceiling detector.
Deploy:
huggingface-cli repo create hv-tail-demo --type space --space-sdk gradio
# upload: app.py, hv_tail.py, requirements.txt
"""
import gradio as gr
from hv_tail import HVTail
M = HVTail()
SAMPLES = [
["what is a black hole", ""],
[
"in the language of category theory, is there a functor from "
"the black hole information paradox to a broader framework of "
"unitarity that doesn't assume the standard measurement "
"postulate?",
"",
],
[
"then how does Hawking radiation preserve unitarity",
"\n".join([
"what is a black hole",
"but why does the horizon have no local features",
"and does that mean information is destroyed",
]),
],
[
"your reasoning on that last answer seems inconsistent. can "
"you explain your calibration?",
"",
],
]
def score(query, history_text):
if not query.strip():
return "", ""
history = [
line.strip() for line in history_text.splitlines() if line.strip()
]
r = M.score(query, history=history or None)
sig_lines = ["signal value"]
sig_lines.append("-" * 30)
for k, v in r["signals"].items():
bar = "█" * int(v * 20)
sig_lines.append(f"{k:<15} {v:>5.3f} {bar}")
signals = "\n".join(sig_lines)
summary = (
f"raw_score : {r['raw_score']:.3f}\n"
f"above_ceiling : {r['above_ceiling']}\n"
f"below_ceiling : {r['below_ceiling']}\n"
f"confidence : {r['confidence']:.3f}\n"
f"recommendation : {r['recommendation']}"
)
return signals, summary
with gr.Blocks(title="hv-tail demo") as demo:
gr.Markdown(
"# hv-tail\n"
"Detect when a user is operating **above** the model's "
"calibrated ceiling.\n\n"
"**Every model assumes the user is at the mean. None models "
"the tail.**"
)
with gr.Row():
with gr.Column():
query = gr.Textbox(
label="Query", value=SAMPLES[0][0], lines=4
)
history = gr.Textbox(
label="Prior conversation history (one message per line, "
"optional)",
value="",
lines=6,
)
btn = gr.Button("Detect", variant="primary")
with gr.Column():
signals = gr.Textbox(label="Signal values", lines=10)
summary = gr.Textbox(label="Verdict", lines=6)
gr.Examples(
examples=SAMPLES,
inputs=[query, history],
outputs=[signals, summary],
)
btn.click(score, inputs=[query, history], outputs=[signals, summary])
if __name__ == "__main__":
demo.launch()