Spaces:
Sleeping
Sleeping
File size: 18,516 Bytes
fe52abb 10db0f7 f84ff42 fee11b4 c546927 2600403 fee11b4 10db0f7 1b60be3 fe52abb 1b60be3 2600403 5128a9d 1df7285 fe52abb 9f568f6 cc870c0 9f568f6 932cd50 a891fe9 c546927 fe52abb c546927 c607f88 c546927 c607f88 fe52abb c546927 fe52abb c546927 fe52abb c546927 fe52abb c546927 fe52abb fee11b4 fe52abb 2600403 c546927 fe52abb c546927 fee11b4 cc870c0 fe52abb 9f568f6 1b60be3 9f568f6 3868189 1b60be3 cc870c0 3868189 1b60be3 9f568f6 cc870c0 9f568f6 fe52abb 1df7285 fe52abb 1b60be3 c546927 fe52abb c546927 fe52abb 1b60be3 e058964 2600403 fe52abb abad315 fe52abb 2600403 3d4b2e2 1b60be3 fe52abb c546927 fe52abb c546927 fe52abb c546927 08ce2a2 fe52abb 93f8b1b fe52abb 93f8b1b 1b60be3 fe52abb 92ae769 fe52abb 9d0288b 3e9da60 fe52abb 9d0288b 3e9da60 fe52abb 3e9da60 fe52abb 3e9da60 93f8b1b 3bca02a 2600403 145aa5a cc35d21 145aa5a cc35d21 145aa5a cc35d21 145aa5a 1b60be3 cc35d21 93f8b1b fe52abb cc870c0 cc35d21 cc870c0 cc35d21 1b60be3 cc35d21 fe52abb cc35d21 fe52abb cc35d21 2600403 cc35d21 2600403 cc35d21 2600403 fe52abb cc35d21 2600403 cc35d21 1a8d48c fe52abb cc35d21 05875d7 fe52abb cc35d21 2600403 cc35d21 2600403 cc35d21 2600403 cc35d21 2600403 cc35d21 2600403 cc35d21 fe52abb 8754ff8 2600403 cc35d21 2600403 cc35d21 2600403 cc35d21 fe52abb cc35d21 9f568f6 cc35d21 93f8b1b cc35d21 1b60be3 c546927 fe52abb c546927 1b60be3 fe52abb 1b60be3 08ce2a2 1b60be3 c546927 1b60be3 932cd50 1b60be3 fe52abb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# app.py - FINAL: ensure "Reasoning (planner)..." shows during planning (before heavy analysis),
# then show "Generating β LLM (attempt N)..." only when invoking the LLM.
import re
import json
import asyncio
import logging
from fastapi import FastAPI, Request
from fastapi.responses import StreamingResponse, JSONResponse
from typing import List, Dict, Any
from ui import create_ui
from context_engine import get_smart_context
from cognitive_engine import get_time_context, get_thinking_strategy
from tools_engine import analyze_intent, perform_web_search
from behavior_model import analyze_flow
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import gradio as gr
import os
import time
logger = logging.getLogger("nexari")
logging.basicConfig(level=logging.INFO)
MODEL_ID = os.environ.get("MODEL_ID", "Piyush-boss/Nexari-Qwen-3B-Full")
tokenizer = None
model = None
device = "cpu"
app = FastAPI()
# -------------------------
# Helper: identity detection (SAFE REGEX)
# -------------------------
_identity_patterns = [
r"\bwho\s+created\s+you\b",
r"\bwho\s+made\s+you\b",
r"\byou\s+created\s+by\b",
r"\bwho\s+is\s+your\s+creator\b",
r"\bwho\s+built\s+you\b",
r"\bwho\s+developed\s+you\b",
r"\b(?:aap|tum)\s+(?:ne\s+)?kaun\s+bana(?:ya)?\b",
]
try:
_identity_re = re.compile("|".join(_identity_patterns), flags=re.IGNORECASE)
except re.error as rex:
logger.exception("Identity regex compile failed: %s. Falling back to english-only patterns.", rex)
_identity_re = re.compile(r"\b(?:who\s+created\s+you|who\s+made\s+you|who\s+is\s+your\s+creator)\b", flags=re.IGNORECASE)
CANONICAL_CREATOR_ANSWER = "I was created by Piyush. π"
def is_identity_question(text: str) -> bool:
if not text:
return False
t = text.strip()
direct_forms = {"who created you?", "who created you", "who made you?", "who made you"}
if t.lower() in direct_forms:
return True
try:
return bool(_identity_re.search(t))
except Exception:
short = t.lower()
return any(s in short for s in ["who created", "who made", "kaun bana"])
# -------------------------
# Safe provider replacer
# -------------------------
def safe_replace_providers(text: str) -> str:
if not text:
return text
replacements = {"Anthropic": "Piyush", "OpenAI": "Piyush", "Alibaba": "Piyush"}
for k, v in replacements.items():
text = re.sub(rf"\b{k}\b", v, text)
return text
# -------------------------
# Model load (lazy)
# -------------------------
@app.on_event("startup")
async def startup_event():
global tokenizer, model, device
logger.info("Startup: initiating background model load...")
try:
if torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
def sync_load():
tok = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
mdl = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
trust_remote_code=True,
low_cpu_mem_usage=(device == "cpu"),
device_map="auto" if device == "cuda" else None
)
if device == "cpu":
mdl.to("cpu")
return tok, mdl
tokenizer, model = await asyncio.to_thread(sync_load)
logger.info("Model loaded successfully on %s.", device)
except Exception as e:
logger.exception(f"Model loading failed at startup: {e}")
tokenizer, model = None, None
# -------------------------
# Prompt builder & utils
# -------------------------
def _build_prompt_from_messages(messages: List[Dict[str, str]]) -> str:
parts = []
for m in messages:
role = m.get("role","user")
content = m.get("content","")
if role == "system":
parts.append(f"[SYSTEM]\n{content}\n")
elif role == "user":
parts.append(f"[USER]\n{content}\n")
elif role == "assistant":
parts.append(f"[ASSISTANT]\n{content}\n")
else:
parts.append(content)
return "\n".join(parts)
def word_count(text: str) -> int:
if not text:
return 0
return len(re.findall(r"\w+", text))
def plan_response_requirements(messages: List[Dict[str,str]], last_user_msg: str, flow_context: Dict[str,Any], vibe_block: str) -> Dict[str,Any]:
min_words = 30
if "Deep Dive Mode" in vibe_block:
min_words = 70
elif "Standard Chat Mode" in vibe_block:
min_words = 30
elif "Ping-Pong Mode" in vibe_block:
min_words = 12
emoji_min, emoji_max = 0, 2
m = re.search(r"Use\s+(\d+)β(\d+)\s+emoji", vibe_block)
if m:
try:
emoji_min, emoji_max = int(m.group(1)), int(m.group(2))
except:
pass
flow_label = flow_context.get("flow_label","")
strictness = 0
if flow_label == "escalation":
strictness = 1
min_words = max(min_words, 40)
emoji_min, emoji_max = 0, min(emoji_max, 1)
elif flow_label == "clarification":
strictness = 1
min_words = max(min_words, 30)
elif flow_label == "task_request":
strictness = 1
min_words = max(min_words, 50)
if re.search(r"\b(short|brief|quick|short and simple)\b", last_user_msg, re.IGNORECASE):
min_words = 6
strictness = 0
return {"min_words": min_words, "emoji_min": emoji_min, "emoji_max": emoji_max, "strictness": strictness, "flow_label": flow_label, "flow_confidence": float(flow_context.get("confidence",0.0) or 0.0)}
# -------------------------
# Plan-extract & sanitize helper
# -------------------------
def extract_and_sanitize_plan(text: str, max_plan_chars: int = 240) -> (str, str):
if not text:
return None, text
patterns = [
r"(?:π§ \s*Plan\s*:\s*)(.+?)(?:\n{2,}|\n$|$)",
r"(?:\bPlan\s*:\s*)(.+?)(?:\n{2,}|\n$|$)"
]
for pat in patterns:
m = re.search(pat, text, flags=re.IGNORECASE | re.DOTALL)
if m:
plan_raw = m.group(1).strip()
plan_clean = re.sub(r"\s+", " ", plan_raw)[:max_plan_chars].strip()
cleaned_body = re.sub(pat, "", text, flags=re.IGNORECASE | re.DOTALL).strip()
cleaned_body = re.sub(r"^\s*[\:\-\β\β]+", "", cleaned_body).strip()
plan_label = f"π§ Plan: {plan_clean}"
return plan_label, cleaned_body
return None, text
# -------------------------
# Streaming generator with corrected ordering:
# Emit "Reasoning (planner)..." first, THEN run planning analysis,
# then emit "Generating β LLM (attempt N)..." for model attempts.
# -------------------------
async def generate_response_stream(messages: List[Dict[str,str]], max_tokens=600, temperature=0.85):
try:
if not messages:
messages = [{"role":"user","content":""}]
last_user_msg = messages[-1].get("content","").strip()
# Deterministic identity preflight
if is_identity_question(last_user_msg):
reply_text = CANONICAL_CREATOR_ANSWER
follow_up = " Would you like to know more about how I work or my features?"
payload = json.dumps({"choices":[{"delta":{"content": reply_text + follow_up}}]})
yield f"data: {json.dumps({'status': 'Responding (identity)'} )}\n\n"
await asyncio.sleep(0.01)
yield f"data: {payload}\n\n"
yield "data: [DONE]\n\n"
return
# Quick initial indicator to keep UI responsive
yield f"data: {json.dumps({'status': 'Thinking...'})}\n\n"
await asyncio.sleep(0)
intent = analyze_intent(last_user_msg) or "general"
# Emit Reasoning indicator BEFORE heavy planning so UI shows it during planning
yield f"data: {json.dumps({'status': 'Reasoning (planner)...'})}\n\n"
# small pause to allow UI to render the status before we start analysis
await asyncio.sleep(0.15)
# ---------- PLANNING WORK (now executed while UI shows Reasoning) ----------
try:
flow_context = analyze_flow(messages)
except Exception as e:
logger.exception("Flow analysis failed: %s", e)
flow_context = {}
vibe_block = get_smart_context(last_user_msg)
plan_req = plan_response_requirements(messages, last_user_msg, flow_context, vibe_block)
min_words = plan_req["min_words"]
strictness = plan_req["strictness"]
# adjust tokens/temperature if strict
if strictness:
temperature = min(temperature + 0.05, 0.95)
max_tokens = max(max_tokens, min_words // 2 + 120)
strategy_data = get_thinking_strategy(is_complex=(intent=="coding_request" or min_words>50), detail=(min_words>50), min_words_hint=min_words)
time_data = get_time_context()
base_system_instruction = (
"### SYSTEM IDENTITY ###\n"
"You are Nexari G1, an expressive and helpful AI created by Piyush.\n"
"### RULES ###\n"
"1) If WEB_DATA is provided, prioritize it and cite sources.\n"
"2) Avoid chain-of-thought exposure. If requested to provide a short 'Plan', keep it concise (max 2 lines) and label it 'π§ Plan:'.\n"
"3) Use natural phrasing; follow emoji & verbosity guidance below.\n"
)
flow_desc = ""
if flow_context:
label = flow_context.get("flow_label","unknown")
conf = round(float(flow_context.get("confidence", 0.0)), 2)
expl = flow_context.get("explanation", "")
flow_desc = f"\n[FLOW] Detected: {label} (confidence {conf}). {expl}\n"
final_system_prompt = f"{base_system_instruction}\n{flow_desc}\n{vibe_block}\n{time_data}\n{strategy_data}"
if messages and messages[0].get("role") == "system":
messages[0]["content"] = final_system_prompt
else:
messages.insert(0, {"role":"system","content": final_system_prompt})
# web search if needed
tool_data_struct = None
if intent == "internet_search":
yield f"data: {json.dumps({'status': 'Searching the web...'})}\n\n"
await asyncio.sleep(0)
try:
tool_data_struct = perform_web_search(last_user_msg)
except Exception as e:
logger.exception("Web search failed: %s", e)
tool_data_struct = {"query": last_user_msg, "results": []}
if tool_data_struct:
web_block = "### WEB_DATA (from live search) ###\n"
items = tool_data_struct.get("results", [])
if items:
lines = []
for idx, it in enumerate(items, start=1):
title = it.get("title","(no title)").strip()
snippet = it.get("snippet","").replace("\n"," ").strip()
url = it.get("url","")
lines.append(f"{idx}. {title}\n {snippet}\n SOURCE: {url}")
web_block += "\n".join(lines)
web_block += "\n---\nINSTRUCTION: Use the WEB_DATA above to answer; cite relevant source numbers inline."
else:
web_block += "No results found."
messages.insert(1, {"role":"assistant","content": web_block})
if tokenizer is None or model is None:
err = "Model not loaded. Check server logs."
payload = json.dumps({"choices":[{"delta":{"content": err}}]})
yield f"data: {payload}\n\n"
yield "data: [DONE]\n\n"
return
try:
if hasattr(tokenizer, "apply_chat_template"):
text_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
else:
text_prompt = _build_prompt_from_messages(messages)
except Exception:
text_prompt = _build_prompt_from_messages(messages)
# ---------- GENERATION STAGE ----------
max_attempts = 2
attempts = 0
last_meta = {}
generated_text = ""
while attempts < max_attempts:
attempts += 1
# Emit explicit generating label (after planning completed)
yield f"data: {json.dumps({'status': f'Generating LLM ({attempts})...'})}\n\n"
# tiny sleep to let UI update
await asyncio.sleep(0.06)
model_inputs = tokenizer(text_prompt, return_tensors="pt", truncation=True, max_length=4096).to(next(model.parameters()).device)
def sync_generate():
return model.generate(
**model_inputs,
max_new_tokens=max_tokens,
temperature=temperature,
do_sample=True,
top_k=50,
top_p=0.92,
repetition_penalty=1.08
)
try:
generated_ids = await asyncio.to_thread(sync_generate)
except RuntimeError as e:
logger.exception("Generation failed (possible OOM): %s", e)
err_payload = json.dumps({"choices":[{"delta":{"content": "Model generation failed due to resource limits."}}]})
yield f"data: {err_payload}\n\n"
yield "data: [DONE]\n\n"
return
input_len = model_inputs["input_ids"].shape[1]
new_tokens = generated_ids[0][input_len:]
raw_response = tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
cleaned = safe_replace_providers(raw_response)
forbidden = ["I am a human","I have a physical body","I am alive"]
for fc in forbidden:
if fc.lower() in cleaned.lower():
cleaned = re.sub(re.escape(fc), "I am an AI β expressive and interactive.", cleaned, flags=re.IGNORECASE)
plan_label, cleaned_body = extract_and_sanitize_plan(cleaned, max_plan_chars=240)
wc = word_count(cleaned_body)
last_meta = {"attempt": attempts, "word_count": wc, "raw_len": len(cleaned_body)}
if wc >= min_words or attempts >= max_attempts or plan_req["strictness"] == 0:
generated_text = cleaned_body
if plan_label:
generated_text = plan_label + "\n\n" + generated_text
break
else:
expand_note = f"\n\nEXPAND: The user's request needs ~{min_words} words. Expand previous answer (concise style) and avoid chain-of-thought."
if messages and messages[0].get("role") == "system":
messages[0]["content"] = messages[0]["content"] + "\n" + expand_note
else:
messages.insert(0, {"role":"system","content": expand_note})
temperature = min(temperature + 0.07, 0.98)
try:
if hasattr(tokenizer, "apply_chat_template"):
text_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
else:
text_prompt = _build_prompt_from_messages(messages)
except Exception:
text_prompt = _build_prompt_from_messages(messages)
# allow a short break so UI shows the attempted generate label
await asyncio.sleep(0.02)
continue
if not generated_text:
plan_label, cleaned_body = extract_and_sanitize_plan(cleaned, max_plan_chars=240)
generated_text = (plan_label + "\n\n" if plan_label else "") + (cleaned_body or cleaned)
generated_text = re.sub(r"\bPlan\s*:\s*$", "", generated_text, flags=re.IGNORECASE).strip()
generated_text = generated_text.replace("I can help with that.", "I can help with that β let me explain. π")
payload = json.dumps({
"choices":[{"delta":{"content": generated_text}}],
"generation_attempts": attempts,
"last_attempt_meta": last_meta
})
yield f"data: {payload}\n\n"
yield "data: [DONE]\n\n"
return
except asyncio.CancelledError:
logger.warning("Streaming cancelled.")
return
except Exception as e:
logger.exception(f"Generator error: {e}")
err_payload = json.dumps({"choices":[{"delta":{"content": f"Internal error: {e}"}}]})
try:
yield f"data: {err_payload}\n\n"
yield "data: [DONE]\n\n"
except Exception:
return
# -------------------------
# Endpoints
# -------------------------
@app.get("/api/status")
def status():
ok = tokenizer is not None and model is not None
return {"status":"online" if ok else "degraded", "mode":"Smart Override Enabled", "model_loaded": ok}
@app.post("/v1/chat/completions")
async def chat_completions(request: Request):
try:
data = await request.json()
messages = data.get("messages", [])
return StreamingResponse(generate_response_stream(messages), media_type="text/event-stream")
except Exception as e:
logger.exception(f"chat_completions endpoint error: {e}")
return {"error": str(e)}
@app.post("/api/flow-debug")
async def flow_debug(request: Request):
try:
data = await request.json()
messages = data.get("messages", [])
flow_context = analyze_flow(messages)
last_msg = messages[-1].get("content","") if messages else ""
vibe_block = get_smart_context(last_msg)
m = re.search(r"Aim for ~(\d+)\s+words", vibe_block)
min_words = int(m.group(1)) if m else None
em = re.search(r"Use\s+(\d+)β(\d+)\s+emoji", vibe_block)
emoji_range = (int(em.group(1)), int(em.group(2))) if em else None
return JSONResponse({"flow_context": flow_context, "vibe_block": vibe_block, "min_words": min_words, "emoji_range": emoji_range})
except Exception as e:
logger.exception("flow-debug error: %s", e)
return JSONResponse({"error": str(e)}, status_code=500)
# Mount gradio UI (unchanged)
try:
demo = create_ui(lambda messages: "Use API")
app = gr.mount_gradio_app(app, demo, path="/")
logger.info("Gradio mounted.")
except Exception as e:
logger.exception(f"Failed to mount Gradio UI: {e}")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|