zeechimp commited on
Commit
b07cee2
·
verified ·
1 Parent(s): 9d3d906

Create example.py

Browse files
Files changed (1) hide show
  1. example.py +77 -0
example.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Minimal usage example for hv-mode.
3
+
4
+ python example.py
5
+
6
+ Writes seven files to ./hv_mode_output/ plus an interactive mode menu,
7
+ then opens the menu in your browser.
8
+ """
9
+
10
+ import os
11
+ import webbrowser
12
+
13
+ from hv_mode import HVMode
14
+
15
+
16
+ TEXT = """A black hole is a region of spacetime where gravity is so strong \
17
+ that nothing - no particles or even electromagnetic radiation such as light - \
18
+ can escape from it. The theory of general relativity predicts that a \
19
+ sufficiently compact mass can deform spacetime to form a black hole. The \
20
+ boundary of the region from which no escape is possible is called the event \
21
+ horizon. Although the event horizon has profound effects on the fate of an \
22
+ object that crosses it, it has no locally detectable features. A black hole \
23
+ acts as a perfect black body, and moreover, it emits Hawking radiation. Note: \
24
+ results may vary depending on the observer's frame of reference."""
25
+
26
+ QUERY = "show me a picture of what a black hole is"
27
+
28
+
29
+ def main() -> None:
30
+ out_dir = "./hv_mode_output"
31
+ os.makedirs(out_dir, exist_ok=True)
32
+
33
+ m = HVMode()
34
+
35
+ # 1. Auto mode (based on the query)
36
+ out = m.respond(TEXT, query=QUERY)
37
+ print(f"auto-selected mode: {out['mode']} ({out['mime']})")
38
+ print(f"mode distribution : {out['mode_distribution']}")
39
+
40
+ # 2. Content negotiation
41
+ out_svg = m.respond(TEXT, accept="image/svg+xml")
42
+ print(f"Accept svg : {out_svg['mode']}")
43
+
44
+ out_html = m.respond(TEXT, accept="text/html")
45
+ print(f"Accept html : {out_html['mode']}")
46
+
47
+ # 3. Write all seven variants
48
+ meaning = m.extract(TEXT)
49
+ ext_map = {
50
+ "text/plain": "txt",
51
+ "text/html": "html",
52
+ "image/svg+xml": "svg",
53
+ }
54
+ print()
55
+ for mode in m.router.available():
56
+ content, mime = m.render(meaning, mode)
57
+ ext = ext_map.get(mime, "bin")
58
+ path = os.path.join(out_dir, f"{mode}.{ext}")
59
+ with open(path, "w", encoding="utf-8") as f:
60
+ f.write(content)
61
+ print(f"wrote {path}")
62
+
63
+ # 4. Interactive mode menu
64
+ menu_path = os.path.join(out_dir, "mode_menu.html")
65
+ with open(menu_path, "w", encoding="utf-8") as f:
66
+ f.write(m.mode_menu(TEXT, QUERY))
67
+ print(f"wrote {menu_path}")
68
+
69
+ # 5. Open in browser (best effort)
70
+ try:
71
+ webbrowser.open(f"file://{os.path.abspath(menu_path)}")
72
+ except Exception:
73
+ pass
74
+
75
+
76
+ if __name__ == "__main__":
77
+ main()