hv-mode / app.py
zeechimp's picture
Create app.py
88c0430 verified
Raw History Blame Contribute Delete
3.68 kB
"""
hv-mode-demo -- Gradio Space for the hv-mode cognitive delivery router.
Deploy:
huggingface-cli repo create hv-mode-demo --type space --space-sdk gradio
# then upload this file + hv_mode.py + requirements.txt
"""
import gradio as gr
from hv_mode import HVMode
MODEL = HVMode()
SAMPLES = [
[
"A black hole is a region of spacetime where gravity is so strong "
"that nothing - no particles or even electromagnetic radiation such "
"as light - can escape from it. The theory of general relativity "
"predicts that a sufficiently compact mass can deform spacetime to "
"form a black hole. The boundary of the region from which no escape "
"is possible is called the event horizon. Although the event horizon "
"has profound effects on the fate of an object that crosses it, it "
"has no locally detectable features. A black hole acts as a perfect "
"black body, and moreover, it emits Hawking radiation. Note: results "
"may vary depending on the observer's frame of reference.",
"show me a picture of what a black hole is",
],
[
"To change a flat tire: first, park on level ground and engage the "
"parking brake. Second, loosen the lug nuts slightly with the wrench. "
"Third, jack up the car until the tire clears the ground. Fourth, "
"remove the lug nuts and take off the tire. Fifth, mount the spare "
"tire and hand-tighten the lug nuts. Finally, lower the car and "
"tighten the lug nuts fully. Note: check the spare's pressure first.",
"how do I change a flat tire",
],
[
"Approach A costs 40 USD and takes 3 hours. Approach B costs 120 USD "
"and takes 1 hour. Approach C costs 60 USD and takes 2 hours. Mean "
"cost is about 73 USD.",
"compare the options in color",
],
]
def render(llm_text: str, query: str, mode: str):
if not llm_text.strip():
return "<p style='color:#888'>Enter some text above.</p>", ""
meaning = MODEL.extract(llm_text)
if mode == "auto":
mode = MODEL.best_mode(query or llm_text, meaning=meaning)
content, mime = MODEL.render(meaning, mode)
dist = MODEL.detect(query or llm_text, meaning=meaning)
dist_str = "\n".join(
f"{m:<14} {p:.3f}" for m, p in
sorted(dist.items(), key=lambda kv: -kv[1])
)
return content, dist_str
with gr.Blocks(title="hv-mode demo") as demo:
gr.Markdown(
"# hv-mode\n"
"LLM text in, reader-mode-matched output out.\n\n"
"**Storage is not delivery.**"
)
with gr.Row():
with gr.Column():
text = gr.Textbox(
label="LLM response",
lines=10,
value=SAMPLES[0][0],
)
query = gr.Textbox(
label="Original query (optional)",
value=SAMPLES[0][1],
)
mode = gr.Dropdown(
choices=[
"auto", "text", "card_deck", "svg_tree",
"svg_strip", "pacman", "color_field", "rhythm",
],
value="auto",
label="Output mode",
)
btn = gr.Button("Render", variant="primary")
with gr.Column():
output = gr.HTML(label="Rendered output")
dist = gr.Textbox(label="Mode distribution", lines=8)
gr.Examples(
examples=SAMPLES,
inputs=[text, query],
outputs=[output, dist],
)
btn.click(render, inputs=[text, query, mode], outputs=[output, dist])
if __name__ == "__main__":
demo.launch()