Spaces:
Sleeping
Sleeping
Deploy Panini 1.0 Demo
Browse files- app.py +51 -57
- meru_os/panini_model/panini_model.py +57 -26
app.py
CHANGED
|
@@ -12,55 +12,56 @@ print(f"β
Boot Complete in {boot_time:.4f}s")
|
|
| 12 |
# 2. Key Processors
|
| 13 |
def omni_search(query):
|
| 14 |
if not query or len(query) < 2:
|
| 15 |
-
return "", ""
|
| 16 |
|
| 17 |
start = time.time()
|
| 18 |
results = model.query_omni(query)
|
| 19 |
duration = time.time() - start
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
# π Roots
|
| 24 |
-
html_roots = ""
|
| 25 |
if results["roots"]:
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
for r in results["roots"]:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
# βοΈ Law
|
| 33 |
-
html_law = ""
|
| 34 |
if results["law"]:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
for item in results["law"]:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
# π° Economy
|
| 42 |
-
html_econ = ""
|
| 43 |
if results["econ"]:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
for item in results["econ"]:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
# πΊοΈ Geography / Ayurveda
|
| 51 |
-
html_geo = ""
|
| 52 |
if results["geo"] or results["med"]:
|
| 53 |
-
|
| 54 |
-
|
| 55 |
for item in results["geo"]:
|
| 56 |
-
|
| 57 |
for item in results["med"]:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
stats = f"β‘ Inference: {duration*1000:.1f}ms | π§ Active Nodes: {len(model.smriti)}"
|
| 62 |
|
| 63 |
-
return
|
| 64 |
|
| 65 |
# 3. The Sovereign Design (CSS) - Light Theme
|
| 66 |
custom_css = """
|
|
@@ -83,10 +84,11 @@ textarea, input {
|
|
| 83 |
border: 1px solid #e5e7eb !important;
|
| 84 |
color: #000000 !important;
|
| 85 |
font-family: 'Outfit', sans-serif !important;
|
| 86 |
-
font-size:
|
| 87 |
text-align: left !important;
|
| 88 |
-
|
| 89 |
-
|
|
|
|
| 90 |
}
|
| 91 |
|
| 92 |
/* Cards */
|
|
@@ -94,40 +96,39 @@ textarea, input {
|
|
| 94 |
background: #ffffff;
|
| 95 |
border: 1px solid #e5e7eb;
|
| 96 |
border-radius: 12px;
|
| 97 |
-
padding:
|
| 98 |
-
margin-bottom:
|
| 99 |
-
box-shadow: 0
|
| 100 |
transition: all 0.2s ease;
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
border-color: #ff5722;
|
| 105 |
-
transform: translateY(-2px);
|
| 106 |
}
|
| 107 |
|
| 108 |
.card h3 {
|
| 109 |
color: #ea580c;
|
| 110 |
border-bottom: 2px solid #f3f4f6;
|
| 111 |
-
padding-bottom:
|
| 112 |
-
margin-bottom:
|
| 113 |
-
font-size: 1.
|
| 114 |
font-weight: 600;
|
|
|
|
| 115 |
}
|
| 116 |
|
| 117 |
.item {
|
| 118 |
-
margin-bottom:
|
| 119 |
font-size: 16px;
|
| 120 |
-
line-height: 1.
|
| 121 |
-
color: #
|
| 122 |
}
|
| 123 |
|
| 124 |
-
b { color: #0369a1; }
|
| 125 |
|
| 126 |
#stats-bar {
|
| 127 |
text-align: center;
|
| 128 |
-
color: #
|
| 129 |
-
font-size:
|
| 130 |
-
margin-top:
|
| 131 |
}
|
| 132 |
"""
|
| 133 |
|
|
@@ -138,9 +139,9 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
| 138 |
gr.Markdown("Type a concept (e.g. 'RIGHTS', 'MOTHER', 'GST'). The model searches Law, Roots, and Economy in real-time.")
|
| 139 |
|
| 140 |
# The Omni-Input (Light Mode)
|
| 141 |
-
inp = gr.Textbox(placeholder="
|
| 142 |
|
| 143 |
-
# Predefined Questions
|
| 144 |
gr.Examples(
|
| 145 |
examples=[
|
| 146 |
"Constitution Preamble",
|
|
@@ -154,18 +155,11 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
| 154 |
label="Try these examples:"
|
| 155 |
)
|
| 156 |
|
| 157 |
-
#
|
| 158 |
-
|
| 159 |
-
out_roots = gr.HTML()
|
| 160 |
-
out_law = gr.HTML()
|
| 161 |
-
|
| 162 |
-
with gr.Row():
|
| 163 |
-
out_econ = gr.HTML()
|
| 164 |
-
out_geo = gr.HTML()
|
| 165 |
-
|
| 166 |
stats = gr.Markdown(elem_id="stats-bar")
|
| 167 |
|
| 168 |
# Real-time Trigger
|
| 169 |
-
inp.change(fn=omni_search, inputs=inp, outputs=[
|
| 170 |
|
| 171 |
demo.launch()
|
|
|
|
| 12 |
# 2. Key Processors
|
| 13 |
def omni_search(query):
|
| 14 |
if not query or len(query) < 2:
|
| 15 |
+
return "", ""
|
| 16 |
|
| 17 |
start = time.time()
|
| 18 |
results = model.query_omni(query)
|
| 19 |
duration = time.time() - start
|
| 20 |
|
| 21 |
+
html = ""
|
| 22 |
|
| 23 |
# π Roots
|
|
|
|
| 24 |
if results["roots"]:
|
| 25 |
+
html += "<div class='card glass-panel'>"
|
| 26 |
+
html += "<h3>π Ancestry</h3>"
|
| 27 |
+
html += "<div class='item'>The concept <b>" + query.upper() + "</b> connects to:</div>"
|
| 28 |
for r in results["roots"]:
|
| 29 |
+
html += f"<div class='item'>π {r}</div>"
|
| 30 |
+
html += "</div>"
|
| 31 |
|
| 32 |
# βοΈ Law
|
|
|
|
| 33 |
if results["law"]:
|
| 34 |
+
html += "<div class='card glass-panel'>"
|
| 35 |
+
html += "<h3>βοΈ Sovereign Laws</h3>"
|
| 36 |
for item in results["law"]:
|
| 37 |
+
html += f"<div class='item'>{item['content']}</div>"
|
| 38 |
+
html += "</div>"
|
| 39 |
|
| 40 |
# π° Economy
|
|
|
|
| 41 |
if results["econ"]:
|
| 42 |
+
html += "<div class='card glass-panel'>"
|
| 43 |
+
html += "<h3>π° Artha (Economy)</h3>"
|
| 44 |
for item in results["econ"]:
|
| 45 |
+
html += f"<div class='item'><b>{item['source']}</b><br>{item['content']}</div>"
|
| 46 |
+
html += "</div>"
|
| 47 |
|
| 48 |
# πΊοΈ Geography / Ayurveda
|
|
|
|
| 49 |
if results["geo"] or results["med"]:
|
| 50 |
+
html += "<div class='card glass-panel'>"
|
| 51 |
+
html += "<h3>πΊοΈ Desha & Ayurveda</h3>"
|
| 52 |
for item in results["geo"]:
|
| 53 |
+
html += f"<div class='item'><b>π {item['content']}</b></div>"
|
| 54 |
for item in results["med"]:
|
| 55 |
+
html += f"<div class='item'><b>πΏ {item['content']}</b></div>"
|
| 56 |
+
html += "</div>"
|
| 57 |
|
| 58 |
+
# If nothing found
|
| 59 |
+
if not html:
|
| 60 |
+
html = "<div style='text-align:center; padding: 20px; color: #888;'>Scanning Smriti... No direct matches found. Try a broader term.</div>"
|
| 61 |
+
|
| 62 |
stats = f"β‘ Inference: {duration*1000:.1f}ms | π§ Active Nodes: {len(model.smriti)}"
|
| 63 |
|
| 64 |
+
return html, stats
|
| 65 |
|
| 66 |
# 3. The Sovereign Design (CSS) - Light Theme
|
| 67 |
custom_css = """
|
|
|
|
| 84 |
border: 1px solid #e5e7eb !important;
|
| 85 |
color: #000000 !important;
|
| 86 |
font-family: 'Outfit', sans-serif !important;
|
| 87 |
+
font-size: 24px !important;
|
| 88 |
text-align: left !important;
|
| 89 |
+
padding: 15px !important;
|
| 90 |
+
border-radius: 12px !important;
|
| 91 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
|
| 92 |
}
|
| 93 |
|
| 94 |
/* Cards */
|
|
|
|
| 96 |
background: #ffffff;
|
| 97 |
border: 1px solid #e5e7eb;
|
| 98 |
border-radius: 12px;
|
| 99 |
+
padding: 24px;
|
| 100 |
+
margin-bottom: 20px;
|
| 101 |
+
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.05);
|
| 102 |
transition: all 0.2s ease;
|
| 103 |
+
max-width: 800px;
|
| 104 |
+
margin-left: auto;
|
| 105 |
+
margin-right: auto;
|
|
|
|
|
|
|
| 106 |
}
|
| 107 |
|
| 108 |
.card h3 {
|
| 109 |
color: #ea580c;
|
| 110 |
border-bottom: 2px solid #f3f4f6;
|
| 111 |
+
padding-bottom: 12px;
|
| 112 |
+
margin-bottom: 16px;
|
| 113 |
+
font-size: 1.25em;
|
| 114 |
font-weight: 600;
|
| 115 |
+
letter-spacing: -0.02em;
|
| 116 |
}
|
| 117 |
|
| 118 |
.item {
|
| 119 |
+
margin-bottom: 12px;
|
| 120 |
font-size: 16px;
|
| 121 |
+
line-height: 1.6;
|
| 122 |
+
color: #4b5563;
|
| 123 |
}
|
| 124 |
|
| 125 |
+
b { color: #0369a1; font-weight: 600; }
|
| 126 |
|
| 127 |
#stats-bar {
|
| 128 |
text-align: center;
|
| 129 |
+
color: #9ca3af;
|
| 130 |
+
font-size: 12px;
|
| 131 |
+
margin-top: 40px;
|
| 132 |
}
|
| 133 |
"""
|
| 134 |
|
|
|
|
| 139 |
gr.Markdown("Type a concept (e.g. 'RIGHTS', 'MOTHER', 'GST'). The model searches Law, Roots, and Economy in real-time.")
|
| 140 |
|
| 141 |
# The Omni-Input (Light Mode)
|
| 142 |
+
inp = gr.Textbox(placeholder="Search Knowledge Base...", label="", autofocus=True)
|
| 143 |
|
| 144 |
+
# Predefined Questions
|
| 145 |
gr.Examples(
|
| 146 |
examples=[
|
| 147 |
"Constitution Preamble",
|
|
|
|
| 155 |
label="Try these examples:"
|
| 156 |
)
|
| 157 |
|
| 158 |
+
# Single Stream Output
|
| 159 |
+
out_stream = gr.HTML()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
stats = gr.Markdown(elem_id="stats-bar")
|
| 161 |
|
| 162 |
# Real-time Trigger
|
| 163 |
+
inp.change(fn=omni_search, inputs=inp, outputs=[out_stream, stats])
|
| 164 |
|
| 165 |
demo.launch()
|
meru_os/panini_model/panini_model.py
CHANGED
|
@@ -107,8 +107,35 @@ class PaniniLLM(nn.Module):
|
|
| 107 |
if query_upper in self.universal_roots:
|
| 108 |
results["roots"] = self.universal_roots[query_upper]
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
limits = {"constitution": 2, "ipc": 2, "gst": 3, "mandi": 3, "pin": 3, "ayur": 2}
|
| 113 |
counts = {k: 0 for k in limits}
|
| 114 |
|
|
@@ -116,42 +143,46 @@ class PaniniLLM(nn.Module):
|
|
| 116 |
source = item.get("_source", "unknown")
|
| 117 |
category = self._categorize(source)
|
| 118 |
|
| 119 |
-
# Simple Text Match
|
| 120 |
matches = False
|
| 121 |
content = ""
|
| 122 |
|
| 123 |
-
#
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
query_upper in item.get("officeName", "").upper() or \
|
| 127 |
-
query_upper in item.get("districtName", "").upper():
|
| 128 |
-
matches = True
|
| 129 |
-
content = f"{item.get('officeName')} ({item.get('pincode')})"
|
| 130 |
-
|
| 131 |
-
# Law/Text match
|
| 132 |
-
else:
|
| 133 |
-
for v in item.values():
|
| 134 |
-
if isinstance(v, str) and query_lower in v.lower():
|
| 135 |
-
matches = True
|
| 136 |
-
# Find the relevant snippet
|
| 137 |
-
content = str(v)[:150] + "..."
|
| 138 |
-
break
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
if matches:
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
limit_key = source.split("_")[0] # 'constitution', 'ipc'
|
| 144 |
-
if limit_key not in counts: limit_key = "pin" # default
|
| 145 |
|
| 146 |
-
if counts.get(limit_key, 0) < 5:
|
| 147 |
results[category].append({
|
| 148 |
"source": source.upper(),
|
| 149 |
-
"content": content
|
| 150 |
-
"full": item
|
| 151 |
})
|
| 152 |
if limit_key in counts: counts[limit_key] += 1
|
| 153 |
|
| 154 |
return results
|
|
|
|
|
|
|
| 155 |
|
| 156 |
def _categorize(self, source):
|
| 157 |
if source in ["constitution", "ipc"]: return "law"
|
|
|
|
| 107 |
if query_upper in self.universal_roots:
|
| 108 |
results["roots"] = self.universal_roots[query_upper]
|
| 109 |
|
| 110 |
+
def query_omni(self, query):
|
| 111 |
+
"""
|
| 112 |
+
The All-Seeing Eye. Searches EVERYTHING.
|
| 113 |
+
Returns a dict of results by category.
|
| 114 |
+
"""
|
| 115 |
+
query_parts = query.lower().strip().split()
|
| 116 |
+
if not query_parts: return {}
|
| 117 |
+
|
| 118 |
+
query_upper = query.upper().strip()
|
| 119 |
+
|
| 120 |
+
results = {
|
| 121 |
+
"roots": [],
|
| 122 |
+
"law": [],
|
| 123 |
+
"econ": [],
|
| 124 |
+
"geo": [],
|
| 125 |
+
"med": []
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
# 1. Check Roots (Exact Match preferred, or partial)
|
| 129 |
+
if query_upper in self.universal_roots:
|
| 130 |
+
results["roots"] = self.universal_roots[query_upper]
|
| 131 |
+
else:
|
| 132 |
+
# Try partial root match
|
| 133 |
+
for k, v in self.universal_roots.items():
|
| 134 |
+
if query_upper in k:
|
| 135 |
+
results["roots"] = v
|
| 136 |
+
break
|
| 137 |
+
|
| 138 |
+
# 2. Key-Value Scan of Smriti (Knowledge)
|
| 139 |
limits = {"constitution": 2, "ipc": 2, "gst": 3, "mandi": 3, "pin": 3, "ayur": 2}
|
| 140 |
counts = {k: 0 for k in limits}
|
| 141 |
|
|
|
|
| 143 |
source = item.get("_source", "unknown")
|
| 144 |
category = self._categorize(source)
|
| 145 |
|
|
|
|
| 146 |
matches = False
|
| 147 |
content = ""
|
| 148 |
|
| 149 |
+
# Helper to stringify item for search
|
| 150 |
+
# We construct a "Searchable Text" blob from values
|
| 151 |
+
blob = " ".join([str(v).lower() for v in item.values() if isinstance(v, (str, int))])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
+
# SEARCH LOGIC: ALL Keywords must be present
|
| 154 |
+
if all(part in blob for part in query_parts):
|
| 155 |
+
matches = True
|
| 156 |
+
|
| 157 |
+
# Format Content based on Source
|
| 158 |
+
if source == "pin_codes":
|
| 159 |
+
content = f"{item.get('officeName')} ({item.get('pincode')}) - {item.get('districtName')}"
|
| 160 |
+
elif source == "constitution":
|
| 161 |
+
article = item.get("Article", "") or item.get("title", "")
|
| 162 |
+
text = item.get("Text", "") or str(item)
|
| 163 |
+
content = f"<b>{article}</b><br>{text[:300]}..."
|
| 164 |
+
elif source == "ipc":
|
| 165 |
+
section = item.get("Section", "")
|
| 166 |
+
desc = item.get("Description", "") or item.get("Offense", "")
|
| 167 |
+
content = f"<b>Section {section}</b><br>{desc[:300]}..."
|
| 168 |
+
else:
|
| 169 |
+
# Generic fallback
|
| 170 |
+
content = str(blob)[:200] + "..."
|
| 171 |
+
|
| 172 |
if matches:
|
| 173 |
+
limit_key = source.split("_")[0]
|
| 174 |
+
if limit_key not in counts: limit_key = "pin"
|
|
|
|
|
|
|
| 175 |
|
| 176 |
+
if counts.get(limit_key, 0) < limits.get(limit_key, 5):
|
| 177 |
results[category].append({
|
| 178 |
"source": source.upper(),
|
| 179 |
+
"content": content
|
|
|
|
| 180 |
})
|
| 181 |
if limit_key in counts: counts[limit_key] += 1
|
| 182 |
|
| 183 |
return results
|
| 184 |
+
|
| 185 |
+
return results
|
| 186 |
|
| 187 |
def _categorize(self, source):
|
| 188 |
if source in ["constitution", "ipc"]: return "law"
|