spanofzero commited on
Commit
d824a95
·
verified ·
1 Parent(s): 4539dc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -101
app.py CHANGED
@@ -8,7 +8,7 @@ import time
8
  # Securely retrieve the token from your Space's secrets
9
  HF_TOKEN = os.getenv("HF_TOKEN")
10
 
11
- # Initialize BOTH engines with the exact same base model
12
  client_primary = InferenceClient("Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
13
  client_competitor = InferenceClient("Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
14
 
@@ -21,32 +21,17 @@ class StateController:
21
  self._batch = 10
22
  self._reg = {}
23
  self._rendered = self._build_render()
24
-
25
- self._rom60 = tuple(
26
- tuple((i * j) % 60 for j in range(60))
27
- for i in range(60)
28
- )
29
  self._symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567"
30
 
31
  def _build_render(self) -> str:
32
- return "".join(
33
- " [NODE_120] " if i == 120 else ("<" if i % 10 == 0 else ".")
34
- for i in range(121)
35
- )
36
 
37
  def diagnostic(self) -> str:
38
- for i in range(51):
39
- self._state[i] = i % self._batch
40
  self._reg.clear()
41
  self._reg["STATUS"] = "RESOLVED"
42
- return (
43
- "Diagnostic sequence initiated.\n\n"
44
- "Grid initialized: 5 active blocks.\n\n"
45
- "Rendering 121-point array:\n"
46
- f"{self._rendered}\n\n"
47
- "Executing state resolution:\n"
48
- "System resolved. State array reset to zero."
49
- )
50
 
51
  def generate_receipt(self, a: int, b: int, c: int) -> str:
52
  rom_val = self._rom60[a % 60][b % 60]
@@ -54,106 +39,86 @@ class StateController:
54
  return f"0{self._symbols[checksum_index]}"
55
 
56
  def validate_receipt(self, receipt: str, a: int, b: int, c: int) -> str:
57
- expected_receipt = self.generate_receipt(a, b, c)
58
- if receipt == expected_receipt:
59
  self._state[120] = 1
60
- return f"[NODE_120: ACTIVE] Checksum Validated. Receipt {receipt} matches allocation ({a}, {b}, {c})."
61
- else:
62
- self._state[120] = 0
63
- return f"[NODE_120: INACTIVE] Verification Failed. Expected receipt {expected_receipt}, received {receipt}."
64
 
65
  controller = StateController()
66
 
67
- PRIMARY_SYSTEM_MSG = {"role": "system", "content": "You are a logic-focused inference engine. Utilize strict state-hold memory."}
68
- COMPETITOR_SYSTEM_MSG = {"role": "system", "content": "You are a standard helpful AI assistant."}
69
-
70
- def generate_responses(user_message, primary_history, competitor_history):
71
- clean_message = user_message.strip()
72
- if not clean_message:
73
- yield primary_history, competitor_history, ""
74
- return
75
 
76
- # Add messages to both histories (Dictionary Format for Gradio 6)
77
- primary_history.append({"role": "user", "content": clean_message})
78
- primary_history.append({"role": "assistant", "content": ""})
79
- competitor_history.append({"role": "user", "content": clean_message})
80
- competitor_history.append({"role": "assistant", "content": ""})
81
- yield primary_history, competitor_history, ""
82
 
83
- start_time = time.perf_counter()
84
-
85
- # Intercept Logic (Diagnostic/Verify)
86
- if clean_message.lower() == "run grid diagnostic":
87
- output = controller.diagnostic()
88
- primary_history[-1]["content"] = f"{output}\n\n---\n*Telemetry: {time.perf_counter()-start_time:.4f}s | Source: Local Engine*"
89
- competitor_history[-1]["content"] = "Hardware diagnostics not supported by generic models."
90
- yield primary_history, competitor_history, ""
91
- return
92
 
93
- # Intercept Logic (Checksum Receipt)
94
- verify_match = re.search(r"verify receipt\s+([a-zA-Z0-9]{2})\s+for\s+(\d+),\s*(\d+),\s*(\d+)", clean_message, re.IGNORECASE)
95
- if verify_match:
96
- receipt, a, b, c = verify_match.group(1), int(verify_match.group(2)), int(verify_match.group(3)), int(verify_match.group(4))
97
- output = controller.validate_receipt(receipt, a, b, c)
98
- primary_history[-1]["content"] = f"{output}\n\n---\n*Telemetry: {time.perf_counter()-start_time:.6f}s | Source: Local ROM Math*"
99
- competitor_history[-1]["content"] = "Deterministic verification not supported."
100
- yield primary_history, competitor_history, ""
101
  return
102
 
103
- # 1. PRIMARY ENGINE STREAM
104
  try:
105
- msgs = [PRIMARY_SYSTEM_MSG] + primary_history[:-1]
106
- response = ""
107
- stream = client_primary.chat_completion(messages=msgs, max_tokens=1024, stream=True, temperature=0.1)
108
- for chunk in stream:
109
- response += (chunk.choices[0].delta.content or "")
110
- primary_history[-1]["content"] = response
111
- yield primary_history, competitor_history, ""
112
- primary_history[-1]["content"] += f"\n\n---\n*Telemetry: {time.perf_counter()-start_time:.2f}s | Source: Augmented Kernel*"
113
- yield primary_history, competitor_history, ""
 
 
114
  except Exception as e:
115
- primary_history[-1]["content"] = f"Error: {str(e)}"
116
- yield primary_history, competitor_history, ""
117
 
118
- # 2. COMPETITOR ENGINE STREAM
119
- comp_start = time.perf_counter()
120
- competitor_history[-1]["content"] = "*Connecting to vanilla infrastructure...*"
121
- yield primary_history, competitor_history, ""
122
 
123
  try:
124
- msgs = [COMPETITOR_SYSTEM_MSG] + competitor_history[:-1]
125
- response = ""
126
- stream = client_competitor.chat_completion(messages=msgs, max_tokens=1024, stream=True, temperature=0.7)
127
- for chunk in stream:
128
- response += (chunk.choices[0].delta.content or "")
129
- competitor_history[-1]["content"] = response
130
- yield primary_history, competitor_history, ""
131
- competitor_history[-1]["content"] += f"\n\n---\n*Telemetry: {time.perf_counter()-comp_start:.2f}s | Source: Vanilla Qwen*"
132
- yield primary_history, competitor_history, ""
 
 
133
  except Exception as e:
134
- competitor_history[-1]["content"] = f"Error: {str(e)}"
135
- yield primary_history, competitor_history, ""
 
136
 
137
- custom_css = """
138
- body, .gradio-container { background-color: #110c08 !important; }
139
- footer {display: none !important}
140
- """
141
 
142
- with gr.Blocks() as demo:
143
  gr.Markdown("# [ GLYPH.IO ]\n### Dual-Engine Hardware Benchmark")
144
-
145
- primary_chat = gr.Chatbot(label="Augmented Logic Kernel", height=320, type="messages")
146
-
147
  with gr.Row():
148
- msg_input = gr.Textbox(label="Message", placeholder="Enter scheduling or distribution task...", scale=8)
149
- submit_btn = gr.Button("Execute", scale=1, variant="primary")
150
-
151
- gr.Examples(examples=["Calculate the integer distribution for 120 units across 3 nodes.", "Run grid diagnostic"], inputs=msg_input)
152
-
153
- competitor_chat = gr.Chatbot(label="Vanilla Qwen 2.5", height=320, type="messages")
154
 
155
- msg_input.submit(generate_responses, [msg_input, primary_chat, competitor_chat], [primary_chat, competitor_chat, msg_input])
156
- submit_btn.click(generate_responses, [msg_input, primary_chat, competitor_chat], [primary_chat, competitor_chat, msg_input])
157
 
158
  if __name__ == "__main__":
159
- demo.queue().launch(theme=gr.themes.Soft(primary_hue="orange"), css=custom_css)
 
8
  # Securely retrieve the token from your Space's secrets
9
  HF_TOKEN = os.getenv("HF_TOKEN")
10
 
11
+ # Initialize BOTH engines with the same model
12
  client_primary = InferenceClient("Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
13
  client_competitor = InferenceClient("Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
14
 
 
21
  self._batch = 10
22
  self._reg = {}
23
  self._rendered = self._build_render()
24
+ self._rom60 = tuple(tuple((i * j) % 60 for j in range(60)) for i in range(60))
 
 
 
 
25
  self._symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567"
26
 
27
  def _build_render(self) -> str:
28
+ return "".join(" [NODE_120] " if i == 120 else ("<" if i % 10 == 0 else ".") for i in range(121))
 
 
 
29
 
30
  def diagnostic(self) -> str:
31
+ for i in range(51): self._state[i] = i % self._batch
 
32
  self._reg.clear()
33
  self._reg["STATUS"] = "RESOLVED"
34
+ return f"Diagnostic sequence initiated.\n\nGrid initialized.\n\n{self._rendered}\n\nSystem resolved."
 
 
 
 
 
 
 
35
 
36
  def generate_receipt(self, a: int, b: int, c: int) -> str:
37
  rom_val = self._rom60[a % 60][b % 60]
 
39
  return f"0{self._symbols[checksum_index]}"
40
 
41
  def validate_receipt(self, receipt: str, a: int, b: int, c: int) -> str:
42
+ if receipt == self.generate_receipt(a, b, c):
 
43
  self._state[120] = 1
44
+ return f"[NODE_120: ACTIVE] Checksum Validated. Receipt {receipt} matches."
45
+ self._state[120] = 0
46
+ return f"[NODE_120: INACTIVE] Verification Failed."
 
47
 
48
  controller = StateController()
49
 
50
+ PRIMARY_SYS = {"role": "system", "content": "You are a logic-focused inference engine. Use strict state-hold memory."}
51
+ VANILLA_SYS = {"role": "system", "content": "You are a standard helpful AI assistant."}
 
 
 
 
 
 
52
 
53
+ def generate_responses(message, p_history, c_history):
54
+ msg = message.strip()
55
+ if not msg: yield p_history, c_history, ""
 
 
 
56
 
57
+ # Revert to standard List-of-Lists format
58
+ p_history.append([msg, ""])
59
+ c_history.append([msg, ""])
60
+ yield p_history, c_history, ""
61
+
62
+ start = time.perf_counter()
 
 
 
63
 
64
+ # Logic Intercepts
65
+ if msg.lower() == "run grid diagnostic":
66
+ p_history[-1][1] = f"{controller.diagnostic()}\n\n---\n*Telemetry: {time.perf_counter()-start:.4f}s*"
67
+ c_history[-1][1] = "Hardware diagnostics not supported."
68
+ yield p_history, c_history, ""
 
 
 
69
  return
70
 
71
+ # 1. Primary Engine
72
  try:
73
+ msgs = [PRIMARY_SYS]
74
+ for h in p_history[:-1]:
75
+ msgs.extend([{"role": "user", "content": h[0]}, {"role": "assistant", "content": h[1]}])
76
+ msgs.append({"role": "user", "content": msg})
77
+
78
+ res = ""
79
+ for chunk in client_primary.chat_completion(messages=msgs, max_tokens=1024, stream=True, temperature=0.1):
80
+ res += (chunk.choices[0].delta.content or "")
81
+ p_history[-1][1] = res
82
+ yield p_history, c_history, ""
83
+ p_history[-1][1] += f"\n\n---\n*Telemetry: {time.perf_counter()-start:.2f}s | Augmented*"
84
  except Exception as e:
85
+ p_history[-1][1] = f"Error: {str(e)}"
 
86
 
87
+ # 2. Competitor Engine
88
+ c_start = time.perf_counter()
89
+ c_history[-1][1] = "*Connecting...*"
90
+ yield p_history, c_history, ""
91
 
92
  try:
93
+ msgs = [VANILLA_SYS]
94
+ for h in c_history[:-1]:
95
+ msgs.extend([{"role": "user", "content": h[0]}, {"role": "assistant", "content": h[1]}])
96
+ msgs.append({"role": "user", "content": msg})
97
+
98
+ res = ""
99
+ for chunk in client_competitor.chat_completion(messages=msgs, max_tokens=1024, stream=True, temperature=0.7):
100
+ res += (chunk.choices[0].delta.content or "")
101
+ c_history[-1][1] = res
102
+ yield p_history, c_history, ""
103
+ c_history[-1][1] += f"\n\n---\n*Telemetry: {time.perf_counter()-c_start:.2f}s | Vanilla*"
104
  except Exception as e:
105
+ c_history[-1][1] = f"Error: {str(e)}"
106
+
107
+ yield p_history, c_history, ""
108
 
109
+ custom_css = "body, .gradio-container { background-color: #110c08 !important; } footer {display: none !important}"
 
 
 
110
 
111
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange"), css=custom_css) as demo:
112
  gr.Markdown("# [ GLYPH.IO ]\n### Dual-Engine Hardware Benchmark")
113
+ p_chat = gr.Chatbot(label="Augmented Logic Kernel", height=320)
 
 
114
  with gr.Row():
115
+ txt = gr.Textbox(label="Message", placeholder="Enter task...", scale=8)
116
+ btn = gr.Button("Execute", scale=1, variant="primary")
117
+ gr.Examples(examples=["Calculate the integer distribution for 120 units across 3 nodes.", "Run grid diagnostic"], inputs=txt)
118
+ c_chat = gr.Chatbot(label="Vanilla Qwen 2.5", height=320)
 
 
119
 
120
+ txt.submit(generate_responses, [txt, p_chat, c_chat], [p_chat, c_chat, txt])
121
+ btn.click(generate_responses, [txt, p_chat, c_chat], [p_chat, c_chat, txt])
122
 
123
  if __name__ == "__main__":
124
+ demo.queue().launch()