akulasairohit commited on
Commit
27cc2da
Β·
verified Β·
1 Parent(s): 8b7822b

Deploy Panini 1.0 Demo

Browse files
Files changed (2) hide show
  1. app.py +51 -57
  2. 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
- # Format HTML Cards
22
 
23
  # 🌍 Roots
24
- html_roots = ""
25
  if results["roots"]:
26
- html_roots += "<div class='card glass-panel'>"
27
- html_roots += "<h3>🌍 Ancestry</h3>"
 
28
  for r in results["roots"]:
29
- html_roots += f"<div class='item'>{r}</div>"
30
- html_roots += "</div>"
31
 
32
  # βš–οΈ Law
33
- html_law = ""
34
  if results["law"]:
35
- html_law += "<div class='card glass-panel'>"
36
- html_law += "<h3>βš–οΈ Sovereign Laws</h3>"
37
  for item in results["law"]:
38
- html_law += f"<div class='item'><b>{item['source']}</b><br>{item['content']}</div>"
39
- html_law += "</div>"
40
 
41
  # πŸ’° Economy
42
- html_econ = ""
43
  if results["econ"]:
44
- html_econ += "<div class='card glass-panel'>"
45
- html_econ += "<h3>πŸ’° Artha (Economy)</h3>"
46
  for item in results["econ"]:
47
- html_econ += f"<div class='item'><b>{item['source']}</b><br>{item['content']}</div>"
48
- html_econ += "</div>"
49
 
50
  # πŸ—ΊοΈ Geography / Ayurveda
51
- html_geo = ""
52
  if results["geo"] or results["med"]:
53
- html_geo += "<div class='card glass-panel'>"
54
- html_geo += "<h3>πŸ—ΊοΈ Desha & Ayurveda</h3>"
55
  for item in results["geo"]:
56
- html_geo += f"<div class='item'><b>πŸ“ {item['content']}</b></div>"
57
  for item in results["med"]:
58
- html_geo += f"<div class='item'><b>🌿 {item['content']}</b></div>"
59
- html_geo += "</div>"
60
 
 
 
 
 
61
  stats = f"⚑ Inference: {duration*1000:.1f}ms | 🧠 Active Nodes: {len(model.smriti)}"
62
 
63
- return html_roots, html_law, html_econ, html_geo, stats
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: 20px !important;
87
  text-align: left !important;
88
- border-radius: 8px !important;
89
- box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
 
90
  }
91
 
92
  /* Cards */
@@ -94,40 +96,39 @@ textarea, input {
94
  background: #ffffff;
95
  border: 1px solid #e5e7eb;
96
  border-radius: 12px;
97
- padding: 20px;
98
- margin-bottom: 15px;
99
- box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
100
  transition: all 0.2s ease;
101
- }
102
-
103
- .card:hover {
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: 8px;
112
- margin-bottom: 12px;
113
- font-size: 1.1em;
114
  font-weight: 600;
 
115
  }
116
 
117
  .item {
118
- margin-bottom: 10px;
119
  font-size: 16px;
120
- line-height: 1.5;
121
- color: #374151;
122
  }
123
 
124
- b { color: #0369a1; }
125
 
126
  #stats-bar {
127
  text-align: center;
128
- color: #6b7280;
129
- font-size: 13px;
130
- margin-top: 30px;
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="Type to search Sovereign Knowledge...", label="", autofocus=True)
142
 
143
- # Predefined Questions (Capabilities Demo)
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
- # The HUD (Heads Up Display)
158
- with gr.Row():
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=[out_roots, out_law, out_econ, out_geo, stats])
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
- # 2. Linear Scan of Smriti (Knowledge)
111
- # Limit results per category for speed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- # PIN Code match (Exact or City)
124
- if source == "pin_codes":
125
- if query_upper in str(item.get("pincode", "")) or \
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
- # Add to results if under limit
142
- # Tag mapping
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"