uslap-query / app.py
uslap's picture
Upload app.py with huggingface_hub
3e4a8c7 verified
Raw
History Blame Contribute Delete
4.28 kB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
USLaP โ€” Universal System of Linguistic Accountability and Proof
Query interface backed by the live SQLite database.
"""
import gradio as gr
import subprocess
import sys
import os
# uslap.py + DB live in Code_files/ alongside this app
BASE = os.path.join(os.path.dirname(__file__), "Code_files")
USLAP = os.path.join(BASE, "uslap.py")
DB = os.path.join(BASE, "uslap_database_v3.db")
EXAMPLES = [
["trace silk"],
["trace gold"],
["trace iron"],
["intel baghdad"],
["intel khazar"],
["explain R447"],
["compare ุฑ-ุญ-ู… and ู…-ุฑ-ุญ"],
["tasrif ุฑ-ุญ-ู…"],
["tasrif status"],
]
DESCRIPTION = """
## ุจูุณู’ู…ู ุงู„ู„ูŽู‘ู‡ู ุงู„ุฑูŽู‘ุญู’ู…ูŽูฐู†ู ุงู„ุฑูŽู‘ุญููŠู…ู
**USLaP** traces every word in every language back to its root in Allah's Arabic โ€” the divine language of the Qur'an.
**Direction:** Allah's Arabic โ†’ all downstream languages. Always.
| Command | Example |
|---|---|
| `trace <word>` | `trace silk` |
| `intel <topic>` | `intel baghdad` |
| `explain <root-id>` | `explain R447` |
| `compare <root> and <root>` | `compare ุฑ-ุญ-ู… and ู…-ุฑ-ุญ` |
| `tasrif <root>` | `tasrif ุฑ-ุญ-ู…` |
| `tasrif status` | coverage report |
"""
def strip_hypothesis(text: str, query: str) -> str:
"""Remove any HYPOTHESIS block from output.
Only confirmed DB data is shown on the public Space.
Direction: Allah's Arabic โ†’ downstream. ALWAYS.
Hypothesis mode reverses this without a confirmed chain โ€” blocked here.
"""
lines = text.splitlines()
clean = []
in_hypothesis = False
for line in lines:
if "HYPOTHESIS:" in line or "CANDIDATES:" in line:
in_hypothesis = True
if in_hypothesis:
continue
clean.append(line)
result = "\n".join(clean).strip()
if not result:
return (
f"Not found in DB: '{query}'\n\n"
"Only confirmed entries are shown here.\n"
"Run locally with the full engine for hypothesis mode."
)
return result
def run_query(query: str) -> str:
if not query or not query.strip():
return "Enter a query above."
if not os.path.exists(DB):
return f"Database not found at:\n {DB}\n\nSpace may still be building."
if not os.path.exists(USLAP):
return f"uslap.py not found at:\n {USLAP}"
try:
result = subprocess.run(
[sys.executable, USLAP, query.strip()],
capture_output=True,
text=True,
timeout=30,
cwd=BASE,
)
out = result.stdout.strip()
err = result.stderr.strip()
if not out:
return f"[stderr]\n{err}" if err else "(no output)"
return strip_hypothesis(out, query.strip())
except subprocess.TimeoutExpired:
return "Query timed out (30s)."
except Exception as e:
return f"Error: {e}"
with gr.Blocks(
title="USLaP Query",
theme=gr.themes.Base(
primary_hue="gray",
font=[gr.themes.GoogleFont("IBM Plex Mono"), "monospace"],
),
css="""
#output-box textarea {
font-family: 'IBM Plex Mono', monospace !important;
font-size: 13px;
white-space: pre;
}
footer { display: none !important; }
""",
) as demo:
gr.Markdown(DESCRIPTION)
with gr.Row():
query_box = gr.Textbox(
label="Query",
placeholder="trace silk | intel baghdad | explain R447",
scale=5,
)
run_btn = gr.Button("Run โ–ถ", variant="primary", scale=1)
output_box = gr.Textbox(
label="Output",
lines=28,
max_lines=80,
interactive=False,
elem_id="output-box",
)
gr.Examples(examples=EXAMPLES, inputs=query_box, label="Examples")
run_btn.click(fn=run_query, inputs=query_box, outputs=output_box)
query_box.submit(fn=run_query, inputs=query_box, outputs=output_box)
gr.Markdown(
"Dataset: [uslap/uslap-engine](https://huggingface.co/datasets/uslap/uslap-engine) ยท "
"GitHub: [uslap-protocol/uslap-engine](https://github.com/uslap-protocol/uslap-engine)"
)
if __name__ == "__main__":
demo.launch()