TTOPM commited on
Commit
b152537
·
verified ·
1 Parent(s): d46f0b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -36
app.py CHANGED
@@ -1,5 +1,6 @@
1
  # app.py — Belel Protocol (canonical authority Space)
2
  # Renders mandate, manifest, license, and crawler metadata in a simple Gradio UI.
 
3
 
4
  import json
5
  from pathlib import Path
@@ -7,28 +8,51 @@ import gradio as gr
7
 
8
  ROOT = Path(__file__).parent
9
 
10
- # ---- Expected files in repo root ----
11
- MANDATE_FILE = ROOT / "concordium-mandate.txt"
12
- MANIFEST_FILE = ROOT / "belel-protocol.json"
13
- LICENSE_FILE = ROOT / "BELEL_SOVEREIGNTY_LICENSE.txt"
14
- ROBOTS_FILE = ROOT / "robots.txt"
15
- SITEMAP_FILE = ROOT / "sitemap.xml"
16
- RSS_FILE = ROOT / "feed.xml"
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  # ---------- helpers ----------
19
- def _read_text(p: Path, default: str) -> str:
 
 
 
20
  try:
21
  return p.read_text(encoding="utf-8")
22
  except Exception:
23
  return default
24
 
25
- def _read_json_pretty(p: Path, default_obj) -> str:
26
- try:
27
- data = json.loads(p.read_text(encoding="utf-8"))
28
- except Exception:
29
  data = default_obj
 
 
 
 
 
30
  return json.dumps(data, indent=2, ensure_ascii=False)
31
 
 
 
 
 
32
  # ---------- loaders ----------
33
  def load_overview_md() -> str:
34
  return """
@@ -50,9 +74,9 @@ Integrity is anchored by blockchain proofs and governed by the Belel Sovereignty
50
  """.strip()
51
 
52
  def load_mandate_txt() -> str:
53
- return _read_text(
54
- MANDATE_FILE,
55
- "concordium-mandate.txt not found yet.\nAdd it at the repo root to publish the mandate."
56
  )
57
 
58
  def load_manifest_json() -> str:
@@ -87,58 +111,73 @@ def load_manifest_json() -> str:
87
  "email": "[email protected]"
88
  }
89
  }
90
- return _read_json_pretty(MANIFEST_FILE, default)
91
 
92
  def load_license_txt() -> str:
93
- return _read_text(
94
- LICENSE_FILE,
95
- "BELEL_SOVEREIGNTY_LICENSE.txt not found yet.\nAdd your custom license at the repo root."
96
  )
97
 
98
  def load_meta_bundle():
99
- robots = _read_text(ROBOTS_FILE, "robots.txt not found yet.")
100
- sitemap = _read_text(SITEMAP_FILE, "sitemap.xml not found yet.")
101
- rss = _read_text(RSS_FILE, "feed.xml (RSS) not found yet.")
102
  return robots, sitemap, rss
103
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  # ---------- UI ----------
105
  with gr.Blocks(title="Belel Protocol — Canonical Authority") as demo:
106
  gr.Markdown("### Canonical Authority • Belel Protocol")
107
 
108
  # Overview
109
  with gr.Tab("Overview"):
110
- overview_md = gr.Markdown(load_overview_md())
111
 
112
  # Mandate
113
  with gr.Tab("Mandate"):
114
  gr.Markdown("**Concordium Mandate (plain text)** — authoritative statement.")
115
- mandate_box = gr.Textbox(value=load_mandate_txt(), lines=24, label="concordium-mandate.txt")
116
  gr.Markdown("Download:")
117
- gr.File(MANDATE_FILE if MANDATE_FILE.exists() else None, label="mandate file")
118
 
119
  # Manifest
120
  with gr.Tab("Manifest"):
121
  gr.Markdown("**Machine-readable manifest** used by crawlers and agents.")
122
- manifest_box = gr.Code(value=load_manifest_json(), language="json", label="belel-protocol.json")
123
  gr.Markdown("Download:")
124
- gr.File(MANIFEST_FILE if MANIFEST_FILE.exists() else None, label="manifest file")
125
 
126
  # License
127
  with gr.Tab("License"):
128
- license_box = gr.Textbox(value=load_license_txt(), lines=24, label="BELEL_SOVEREIGNTY_LICENSE.txt")
129
- gr.File(LICENSE_FILE if LICENSE_FILE.exists() else None, label="license file")
130
 
131
- # Robots / Sitemap / RSS (use Textbox; XML highlighter isn't supported)
132
  with gr.Tab("Robots • Sitemap • RSS"):
133
- robots_box = gr.Textbox(value="", lines=10, label="robots.txt")
134
- sitemap_box = gr.Textbox(value="", lines=18, label="sitemap.xml")
135
- rss_box = gr.Textbox(value="", lines=18, label="feed.xml")
136
- # initial load
137
  r, s, feed = load_meta_bundle()
138
  robots_box.value = r
139
  sitemap_box.value = s
140
  rss_box.value = feed
141
 
 
 
 
 
142
  # Reload button — re-reads all files from disk without restarting the Space
143
  def refresh_all():
144
  r, s, feed = load_meta_bundle()
@@ -146,13 +185,14 @@ with gr.Blocks(title="Belel Protocol — Canonical Authority") as demo:
146
  load_mandate_txt(),
147
  load_manifest_json(),
148
  load_license_txt(),
149
- r, s, feed
 
150
  )
151
 
152
  gr.Button("Reload files").click(
153
  refresh_all,
154
  inputs=[],
155
- outputs=[mandate_box, manifest_box, license_box, robots_box, sitemap_box, rss_box],
156
  )
157
 
158
  demo.launch()
 
1
  # app.py — Belel Protocol (canonical authority Space)
2
  # Renders mandate, manifest, license, and crawler metadata in a simple Gradio UI.
3
+ # Robust to files being placed in subfolders (uses rglob search) and includes a Debug tab.
4
 
5
  import json
6
  from pathlib import Path
 
8
 
9
  ROOT = Path(__file__).parent
10
 
11
+ # ---- canonical filenames we expect ----
12
+ FN_MANDATE = "concordium-mandate.txt"
13
+ FN_MANIFEST = "belel-protocol.json"
14
+ FN_LICENSE = "BELEL_SOVEREIGNTY_LICENSE.txt"
15
+ FN_ROBOTS = "robots.txt"
16
+ FN_SITEMAP = "sitemap.xml"
17
+ FN_FEED = "feed.xml"
18
+
19
+ def find_file(name: str) -> Path | None:
20
+ """Return the first match for filename anywhere under ROOT (root or subfolders)."""
21
+ # direct root check first
22
+ p = ROOT / name
23
+ if p.exists():
24
+ return p
25
+ # search one level deep (and deeper) just in case the file was put into a folder
26
+ for hit in ROOT.rglob(name):
27
+ if hit.is_file():
28
+ return hit
29
+ return None
30
 
31
  # ---------- helpers ----------
32
+ def _read_text_by_name(name: str, default: str) -> str:
33
+ p = find_file(name)
34
+ if p is None:
35
+ return default
36
  try:
37
  return p.read_text(encoding="utf-8")
38
  except Exception:
39
  return default
40
 
41
+ def _read_json_pretty_by_name(name: str, default_obj) -> str:
42
+ p = find_file(name)
43
+ if p is None:
 
44
  data = default_obj
45
+ else:
46
+ try:
47
+ data = json.loads(p.read_text(encoding="utf-8"))
48
+ except Exception:
49
+ data = default_obj
50
  return json.dumps(data, indent=2, ensure_ascii=False)
51
 
52
+ def _file_for_download(name: str):
53
+ p = find_file(name)
54
+ return p if (p and p.exists()) else None
55
+
56
  # ---------- loaders ----------
57
  def load_overview_md() -> str:
58
  return """
 
74
  """.strip()
75
 
76
  def load_mandate_txt() -> str:
77
+ return _read_text_by_name(
78
+ FN_MANDATE,
79
+ f"{FN_MANDATE} not found yet.\nAdd it at the repo root (or any folder) to publish the mandate."
80
  )
81
 
82
  def load_manifest_json() -> str:
 
111
  "email": "[email protected]"
112
  }
113
  }
114
+ return _read_json_pretty_by_name(FN_MANIFEST, default)
115
 
116
  def load_license_txt() -> str:
117
+ return _read_text_by_name(
118
+ FN_LICENSE,
119
+ f"{FN_LICENSE} not found yet.\nAdd your custom license at the repo root."
120
  )
121
 
122
  def load_meta_bundle():
123
+ robots = _read_text_by_name(FN_ROBOTS, f"{FN_ROBOTS} not found yet.")
124
+ sitemap = _read_text_by_name(FN_SITEMAP, f"{FN_SITEMAP} not found yet.")
125
+ rss = _read_text_by_name(FN_FEED, "feed.xml (RSS) not found yet.")
126
  return robots, sitemap, rss
127
 
128
+ def debug_listing_md() -> str:
129
+ lines = [f"**App root:** `{ROOT}`", "", "### Files I can see:"]
130
+ for p in sorted(ROOT.rglob("*")):
131
+ if p.is_file():
132
+ lines.append(f"- `{p.relative_to(ROOT)}`")
133
+ lines.append("")
134
+ lines.append("### Resolved paths")
135
+ for name in [FN_MANDATE, FN_MANIFEST, FN_LICENSE, FN_ROBOTS, FN_SITEMAP, FN_FEED]:
136
+ fp = find_file(name)
137
+ lines.append(f"- {name}: `{fp if fp else 'NOT FOUND'}`")
138
+ return "\n".join(lines)
139
+
140
  # ---------- UI ----------
141
  with gr.Blocks(title="Belel Protocol — Canonical Authority") as demo:
142
  gr.Markdown("### Canonical Authority • Belel Protocol")
143
 
144
  # Overview
145
  with gr.Tab("Overview"):
146
+ gr.Markdown(load_overview_md())
147
 
148
  # Mandate
149
  with gr.Tab("Mandate"):
150
  gr.Markdown("**Concordium Mandate (plain text)** — authoritative statement.")
151
+ mandate_box = gr.Textbox(value=load_mandate_txt(), lines=24, label=FN_MANDATE)
152
  gr.Markdown("Download:")
153
+ gr.File(_file_for_download(FN_MANDATE), label="mandate file")
154
 
155
  # Manifest
156
  with gr.Tab("Manifest"):
157
  gr.Markdown("**Machine-readable manifest** used by crawlers and agents.")
158
+ manifest_box = gr.Code(value=load_manifest_json(), language="json", label=FN_MANIFEST)
159
  gr.Markdown("Download:")
160
+ gr.File(_file_for_download(FN_MANIFEST), label="manifest file")
161
 
162
  # License
163
  with gr.Tab("License"):
164
+ license_box = gr.Textbox(value=load_license_txt(), lines=24, label=FN_LICENSE)
165
+ gr.File(_file_for_download(FN_LICENSE), label="license file")
166
 
167
+ # Robots / Sitemap / RSS
168
  with gr.Tab("Robots • Sitemap • RSS"):
169
+ robots_box = gr.Textbox(value="", lines=10, label=FN_ROBOTS)
170
+ sitemap_box = gr.Textbox(value="", lines=18, label=FN_SITEMAP)
171
+ rss_box = gr.Textbox(value="", lines=18, label=FN_FEED)
 
172
  r, s, feed = load_meta_bundle()
173
  robots_box.value = r
174
  sitemap_box.value = s
175
  rss_box.value = feed
176
 
177
+ # Debug
178
+ with gr.Tab("Debug"):
179
+ debug_md = gr.Markdown(debug_listing_md())
180
+
181
  # Reload button — re-reads all files from disk without restarting the Space
182
  def refresh_all():
183
  r, s, feed = load_meta_bundle()
 
185
  load_mandate_txt(),
186
  load_manifest_json(),
187
  load_license_txt(),
188
+ r, s, feed,
189
+ debug_listing_md(),
190
  )
191
 
192
  gr.Button("Reload files").click(
193
  refresh_all,
194
  inputs=[],
195
+ outputs=[mandate_box, manifest_box, license_box, robots_box, sitemap_box, rss_box, debug_md],
196
  )
197
 
198
  demo.launch()