Download example.py from zeechimp/hv-mode: direct link, hf CLI and curl.
- Browser
- Download file 2.41 kB
-
https://huggingface.co/zeechimp/hv-mode/resolve/main/example.py
- Command line
-
hf download hf://zeechimp/hv-mode/example.py
-
curl -L -o example.py https://huggingface.co/zeechimp/hv-mode/resolve/main/example.py
2.41 kB
| """ | |
| Minimal usage example for hv-mode. | |
| python example.py | |
| Writes seven files to ./hv_mode_output/ plus an interactive mode menu, | |
| then opens the menu in your browser. | |
| """ | |
| import os | |
| import webbrowser | |
| from hv_mode import HVMode | |
| TEXT = """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.""" | |
| QUERY = "show me a picture of what a black hole is" | |
| def main() -> None: | |
| out_dir = "./hv_mode_output" | |
| os.makedirs(out_dir, exist_ok=True) | |
| m = HVMode() | |
| # 1. Auto mode (based on the query) | |
| out = m.respond(TEXT, query=QUERY) | |
| print(f"auto-selected mode: {out['mode']} ({out['mime']})") | |
| print(f"mode distribution : {out['mode_distribution']}") | |
| # 2. Content negotiation | |
| out_svg = m.respond(TEXT, accept="image/svg+xml") | |
| print(f"Accept svg : {out_svg['mode']}") | |
| out_html = m.respond(TEXT, accept="text/html") | |
| print(f"Accept html : {out_html['mode']}") | |
| # 3. Write all seven variants | |
| meaning = m.extract(TEXT) | |
| ext_map = { | |
| "text/plain": "txt", | |
| "text/html": "html", | |
| "image/svg+xml": "svg", | |
| } | |
| print() | |
| for mode in m.router.available(): | |
| content, mime = m.render(meaning, mode) | |
| ext = ext_map.get(mime, "bin") | |
| path = os.path.join(out_dir, f"{mode}.{ext}") | |
| with open(path, "w", encoding="utf-8") as f: | |
| f.write(content) | |
| print(f"wrote {path}") | |
| # 4. Interactive mode menu | |
| menu_path = os.path.join(out_dir, "mode_menu.html") | |
| with open(menu_path, "w", encoding="utf-8") as f: | |
| f.write(m.mode_menu(TEXT, QUERY)) | |
| print(f"wrote {menu_path}") | |
| # 5. Open in browser (best effort) | |
| try: | |
| webbrowser.open(f"file://{os.path.abspath(menu_path)}") | |
| except Exception: | |
| pass | |
| if __name__ == "__main__": | |
| main() |