Antigravity AI commited on
Commit ·
be8bb08
1
Parent(s): 53046f8
Run merged model directly on HF Space CPU
Browse files
app.py
CHANGED
|
@@ -1,13 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import matplotlib
|
| 4 |
matplotlib.use("Agg")
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
import pypdf, csv
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 9 |
MODEL_ID = "sf0Jmn/kaal-7b-merged"
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
TAGLINE = "The only Multi-Agent Reasoning engine built to solve the future backward."
|
| 12 |
FALLBACK = "I am KAAL. I specialize in solving the future backward using calibrated scientific insights, not general conversation. Let's get back to the future."
|
| 13 |
GLOBAL_HISTORY = []
|
|
@@ -23,23 +37,32 @@ def get_logo_b64():
|
|
| 23 |
LOGO_B64 = get_logo_b64()
|
| 24 |
LOGO_HTML = f'<div style="text-align:center;margin-bottom:30px;width:100%;"><img src="data:image/png;base64,{LOGO_B64}" style="height:188px;display:block;margin:0 auto;"/><p style="color:#00f2ff;font-size:22px;font-weight:800;margin-top:15px;">{TAGLINE}</p></div>' if LOGO_B64 else f'<div style="text-align:center;margin-bottom:30px;"><p style="color:#00f2ff;font-size:32px;font-weight:900;">KAAL FORESIGHT</p><p style="color:#00ff88;">{TAGLINE}</p></div>'
|
| 25 |
|
| 26 |
-
def call_agent(prompt, sys_msg, max_tokens=
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
return f"ERROR: {str(e)}"
|
| 41 |
|
| 42 |
-
def hard_trim(text, max_words=
|
| 43 |
words = text.split()
|
| 44 |
if len(words) <= max_words: return text.strip()
|
| 45 |
candidate = " ".join(words[:max_words])
|
|
@@ -55,8 +78,8 @@ def dedupe(text):
|
|
| 55 |
seen.add(k); out.append(s.strip())
|
| 56 |
return " ".join(out)
|
| 57 |
|
| 58 |
-
def compress_context(text, query, max_chunks=
|
| 59 |
-
if len(text.split()) <
|
| 60 |
words = text.split()
|
| 61 |
chunks = [" ".join(words[i:i+chunk_size]) for i in range(0, len(words), chunk_size)]
|
| 62 |
query_words = set(re.sub(r'[^\w\s]', '', query.lower()).split()) - {
|
|
@@ -82,16 +105,7 @@ def read_file_context(files, query=""):
|
|
| 82 |
content = compress_context(raw, query)
|
| 83 |
elif ext == 'csv':
|
| 84 |
with open(path, 'r', errors='ignore') as h:
|
| 85 |
-
content = "\n".join([",".join(r) for r in list(csv.reader(h))[:
|
| 86 |
-
elif ext in ['xlsx', 'xls']:
|
| 87 |
-
try:
|
| 88 |
-
import openpyxl
|
| 89 |
-
wb = openpyxl.load_workbook(path, read_only=True, data_only=True)
|
| 90 |
-
content = ""
|
| 91 |
-
for ws in wb.worksheets:
|
| 92 |
-
for row in ws.iter_rows(max_row=300, values_only=True):
|
| 93 |
-
content += ",".join([str(c or "") for c in row]) + "\n"
|
| 94 |
-
except: content = "[Excel file detected]"
|
| 95 |
elif ext in ['png', 'jpg', 'jpeg']:
|
| 96 |
content = f"[Image uploaded: {name}]"
|
| 97 |
else:
|
|
@@ -137,32 +151,41 @@ def run_kaal(query, context):
|
|
| 137 |
yield "COMPLETE", FALLBACK, "▸ System redirected.", build_plot(series, labels)
|
| 138 |
return
|
| 139 |
|
| 140 |
-
evidence_block = f"EVIDENCE (PRIMARY):\n{context[:
|
| 141 |
-
yield "INITIALIZING", "Initializing...", "▸ System wake-up...", build_plot(series, labels)
|
| 142 |
|
| 143 |
log = "▸ Architect: Synthesizing thesis...\n"
|
| 144 |
push(series, labels, "A-Init", Architect=90, Contrarian=10, Analyst=8, Synthesizer=5)
|
| 145 |
-
yield "ARCHITECTING", "Building thesis...", log, build_plot(series, labels)
|
| 146 |
-
thesis = dedupe(hard_trim(call_agent(
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
log += "▸ Contrarian: Stress-testing assumptions...\n"
|
| 149 |
push(series, labels, "C-Init", Architect=40, Contrarian=95, Analyst=15, Synthesizer=5)
|
| 150 |
-
yield "CONFLICTING", "Attacking assumptions...", log, build_plot(series, labels)
|
| 151 |
-
attack = dedupe(hard_trim(call_agent(
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
log += "▸ Analyst: Reconciling divergence...\n"
|
| 154 |
push(series, labels, "R-Init", Architect=20, Contrarian=30, Analyst=98, Synthesizer=15)
|
| 155 |
-
yield "ANALYZING", "Reconciling logic...", log, build_plot(series, labels)
|
| 156 |
-
recon = dedupe(hard_trim(call_agent(
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
log += "▸ Synthesizer: Writing final strategic report...\n"
|
| 159 |
push(series, labels, "S-Init", Architect=15, Contrarian=15, Analyst=30, Synthesizer=100)
|
| 160 |
-
yield "SYNTHESIZING", "Delivering final report...", log, build_plot(series, labels)
|
| 161 |
|
| 162 |
report = call_agent(
|
| 163 |
f"TOPIC: {query}\nFINDINGS: {recon}\nTHESIS: {thesis}",
|
| 164 |
-
"You are KAAL, a calibrated foresight intelligence. Write a strategic report in the style of a senior research analyst
|
| 165 |
-
max_tokens=
|
| 166 |
)
|
| 167 |
report = dedupe(report)
|
| 168 |
last = max(report.rfind('.'), report.rfind('!'), report.rfind('?'))
|
|
@@ -210,13 +233,13 @@ with gr.Blocks(title="KAAL Foresight", css=CSS) as demo:
|
|
| 210 |
<div style="color:#4ade80;font-weight:800;letter-spacing:1px;margin-bottom:15px;text-transform:uppercase;font-size:12px;">Omni Stack Platform</div>
|
| 211 |
<ul style="list-style:none;padding:0;margin:0;">
|
| 212 |
<li style="margin-bottom:15px;font-size:13px;"><span style="color:#22d3ee;font-weight:700;">• Knowledge Agent Arbitration Layer:</span><br/>Core orchestration engine.</li>
|
| 213 |
-
<li style="margin-bottom:15px;font-size:13px;"><span style="color:#22d3ee;font-weight:700;">• AMD MI300X
|
| 214 |
<li style="font-size:13px;"><span style="color:#22d3ee;font-weight:700;">• Trained on Substrate-v1:</span><br/>2024-2026 scientific data.</li>
|
| 215 |
</ul></div>""")
|
| 216 |
with gr.Column(scale=4):
|
| 217 |
with gr.Row():
|
| 218 |
q_in = gr.Textbox(label="Make a Forecast", placeholder="What will the global energy landscape look like in 2050?", lines=4)
|
| 219 |
-
f_in = gr.File(label="Evidence Upload (PDF, CSV,
|
| 220 |
btn = gr.Button("DE-RISK THE CENTURY", variant="primary", elem_classes="action-btn")
|
| 221 |
stat_box = gr.Markdown("### SYSTEM: READY")
|
| 222 |
with gr.Tabs():
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import re, os, base64, random
|
| 3 |
import matplotlib
|
| 4 |
matplotlib.use("Agg")
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
import pypdf, csv
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
|
|
|
|
| 10 |
MODEL_ID = "sf0Jmn/kaal-7b-merged"
|
| 11 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 12 |
+
|
| 13 |
+
print("Loading KAAL model...")
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 15 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
+
MODEL_ID,
|
| 17 |
+
token=HF_TOKEN,
|
| 18 |
+
torch_dtype=torch.float32,
|
| 19 |
+
device_map="cpu",
|
| 20 |
+
low_cpu_mem_usage=True,
|
| 21 |
+
)
|
| 22 |
+
model.eval()
|
| 23 |
+
print("KAAL ready!")
|
| 24 |
+
|
| 25 |
TAGLINE = "The only Multi-Agent Reasoning engine built to solve the future backward."
|
| 26 |
FALLBACK = "I am KAAL. I specialize in solving the future backward using calibrated scientific insights, not general conversation. Let's get back to the future."
|
| 27 |
GLOBAL_HISTORY = []
|
|
|
|
| 37 |
LOGO_B64 = get_logo_b64()
|
| 38 |
LOGO_HTML = f'<div style="text-align:center;margin-bottom:30px;width:100%;"><img src="data:image/png;base64,{LOGO_B64}" style="height:188px;display:block;margin:0 auto;"/><p style="color:#00f2ff;font-size:22px;font-weight:800;margin-top:15px;">{TAGLINE}</p></div>' if LOGO_B64 else f'<div style="text-align:center;margin-bottom:30px;"><p style="color:#00f2ff;font-size:32px;font-weight:900;">KAAL FORESIGHT</p><p style="color:#00ff88;">{TAGLINE}</p></div>'
|
| 39 |
|
| 40 |
+
def call_agent(prompt, sys_msg, max_tokens=300, temperature=0.3):
|
| 41 |
try:
|
| 42 |
+
messages = [{"role": "system", "content": sys_msg},
|
| 43 |
+
{"role": "user", "content": prompt}]
|
| 44 |
+
text = tokenizer.apply_chat_template(
|
| 45 |
+
messages, tokenize=False, add_generation_prompt=True
|
| 46 |
+
)
|
| 47 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 48 |
+
with torch.no_grad():
|
| 49 |
+
out = model.generate(
|
| 50 |
+
**inputs,
|
| 51 |
+
max_new_tokens=max_tokens,
|
| 52 |
+
temperature=max(temperature, 0.01),
|
| 53 |
+
do_sample=True,
|
| 54 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 55 |
+
repetition_penalty=1.3,
|
| 56 |
+
)
|
| 57 |
+
response = tokenizer.decode(
|
| 58 |
+
out[0][inputs["input_ids"].shape[1]:],
|
| 59 |
+
skip_special_tokens=True
|
| 60 |
+
).strip()
|
| 61 |
+
return re.sub(r'(?i)^(system|assistant|user|architect|contrarian|analyst|synthesizer):\s*', '', response).strip()
|
| 62 |
except Exception as e:
|
| 63 |
return f"ERROR: {str(e)}"
|
| 64 |
|
| 65 |
+
def hard_trim(text, max_words=200):
|
| 66 |
words = text.split()
|
| 67 |
if len(words) <= max_words: return text.strip()
|
| 68 |
candidate = " ".join(words[:max_words])
|
|
|
|
| 78 |
seen.add(k); out.append(s.strip())
|
| 79 |
return " ".join(out)
|
| 80 |
|
| 81 |
+
def compress_context(text, query, max_chunks=5, chunk_size=300):
|
| 82 |
+
if len(text.split()) < 800: return text
|
| 83 |
words = text.split()
|
| 84 |
chunks = [" ".join(words[i:i+chunk_size]) for i in range(0, len(words), chunk_size)]
|
| 85 |
query_words = set(re.sub(r'[^\w\s]', '', query.lower()).split()) - {
|
|
|
|
| 105 |
content = compress_context(raw, query)
|
| 106 |
elif ext == 'csv':
|
| 107 |
with open(path, 'r', errors='ignore') as h:
|
| 108 |
+
content = "\n".join([",".join(r) for r in list(csv.reader(h))[:100]])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
elif ext in ['png', 'jpg', 'jpeg']:
|
| 110 |
content = f"[Image uploaded: {name}]"
|
| 111 |
else:
|
|
|
|
| 151 |
yield "COMPLETE", FALLBACK, "▸ System redirected.", build_plot(series, labels)
|
| 152 |
return
|
| 153 |
|
| 154 |
+
evidence_block = f"EVIDENCE (PRIMARY):\n{context[:20000]}\n\nQUERY: {query}" if context else f"QUERY: {query}"
|
| 155 |
+
yield "INITIALIZING", "Initializing... (CPU mode — please wait 2-3 mins per step)", "▸ System wake-up...", build_plot(series, labels)
|
| 156 |
|
| 157 |
log = "▸ Architect: Synthesizing thesis...\n"
|
| 158 |
push(series, labels, "A-Init", Architect=90, Contrarian=10, Analyst=8, Synthesizer=5)
|
| 159 |
+
yield "ARCHITECTING", "Building thesis... (please wait)", log, build_plot(series, labels)
|
| 160 |
+
thesis = dedupe(hard_trim(call_agent(
|
| 161 |
+
evidence_block,
|
| 162 |
+
"You are the Architect. Construct a 4-line strategic thesis. Direct and data-backed. No preamble.",
|
| 163 |
+
max_tokens=180), 80))
|
| 164 |
|
| 165 |
log += "▸ Contrarian: Stress-testing assumptions...\n"
|
| 166 |
push(series, labels, "C-Init", Architect=40, Contrarian=95, Analyst=15, Synthesizer=5)
|
| 167 |
+
yield "CONFLICTING", "Attacking assumptions... (please wait)", log, build_plot(series, labels)
|
| 168 |
+
attack = dedupe(hard_trim(call_agent(
|
| 169 |
+
f"THESIS: {thesis}",
|
| 170 |
+
"You are the Contrarian. Identify 3 weaknesses. Sharp and numbered. No preamble.",
|
| 171 |
+
max_tokens=120), 60))
|
| 172 |
|
| 173 |
log += "▸ Analyst: Reconciling divergence...\n"
|
| 174 |
push(series, labels, "R-Init", Architect=20, Contrarian=30, Analyst=98, Synthesizer=15)
|
| 175 |
+
yield "ANALYZING", "Reconciling logic... (please wait)", log, build_plot(series, labels)
|
| 176 |
+
recon = dedupe(hard_trim(call_agent(
|
| 177 |
+
f"THESIS: {thesis}\nCRITIQUE: {attack}",
|
| 178 |
+
"You are the Analyst. Reconcile into 4 findings. Precise. No preamble.",
|
| 179 |
+
max_tokens=150), 70))
|
| 180 |
|
| 181 |
log += "▸ Synthesizer: Writing final strategic report...\n"
|
| 182 |
push(series, labels, "S-Init", Architect=15, Contrarian=15, Analyst=30, Synthesizer=100)
|
| 183 |
+
yield "SYNTHESIZING", "Delivering final report... (please wait)", log, build_plot(series, labels)
|
| 184 |
|
| 185 |
report = call_agent(
|
| 186 |
f"TOPIC: {query}\nFINDINGS: {recon}\nTHESIS: {thesis}",
|
| 187 |
+
"You are KAAL, a calibrated foresight intelligence. Write a strategic report in the style of a senior research analyst. Structure: 2-sentence macro opening. Three numbered findings with projections and confidence levels. One closing sentence beginning with 'The convergence of these dynamics suggests'. PhD-level rigor. Specific numbers. End at a complete sentence. No bold or markdown headers.",
|
| 188 |
+
max_tokens=400, temperature=0.25
|
| 189 |
)
|
| 190 |
report = dedupe(report)
|
| 191 |
last = max(report.rfind('.'), report.rfind('!'), report.rfind('?'))
|
|
|
|
| 233 |
<div style="color:#4ade80;font-weight:800;letter-spacing:1px;margin-bottom:15px;text-transform:uppercase;font-size:12px;">Omni Stack Platform</div>
|
| 234 |
<ul style="list-style:none;padding:0;margin:0;">
|
| 235 |
<li style="margin-bottom:15px;font-size:13px;"><span style="color:#22d3ee;font-weight:700;">• Knowledge Agent Arbitration Layer:</span><br/>Core orchestration engine.</li>
|
| 236 |
+
<li style="margin-bottom:15px;font-size:13px;"><span style="color:#22d3ee;font-weight:700;">• AMD MI300X Fine-tuned:</span><br/>ROCm 7.0, 532 training examples.</li>
|
| 237 |
<li style="font-size:13px;"><span style="color:#22d3ee;font-weight:700;">• Trained on Substrate-v1:</span><br/>2024-2026 scientific data.</li>
|
| 238 |
</ul></div>""")
|
| 239 |
with gr.Column(scale=4):
|
| 240 |
with gr.Row():
|
| 241 |
q_in = gr.Textbox(label="Make a Forecast", placeholder="What will the global energy landscape look like in 2050?", lines=4)
|
| 242 |
+
f_in = gr.File(label="Evidence Upload (PDF, CSV, Image)", file_count="multiple")
|
| 243 |
btn = gr.Button("DE-RISK THE CENTURY", variant="primary", elem_classes="action-btn")
|
| 244 |
stat_box = gr.Markdown("### SYSTEM: READY")
|
| 245 |
with gr.Tabs():
|