Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
"""
|
| 2 |
-
Nexari Server Backend (
|
| 3 |
Maintained by: Piyush
|
| 4 |
-
Description:
|
| 5 |
"""
|
| 6 |
|
| 7 |
import spaces
|
|
@@ -35,70 +35,67 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 35 |
)
|
| 36 |
|
| 37 |
# --- 2. DYNAMIC STREAMING LOGIC ---
|
| 38 |
-
async def generate_response_stream(messages, max_tokens=600, temperature=0.
|
| 39 |
last_user_msg = messages[-1]["content"]
|
| 40 |
|
| 41 |
-
# === STEP 1:
|
| 42 |
-
yield f"data: {json.dumps({'status': '
|
| 43 |
|
| 44 |
intent = analyze_intent(last_user_msg)
|
| 45 |
|
| 46 |
-
# === STEP 2: DYNAMIC ROUTING ===
|
| 47 |
tool_data = ""
|
| 48 |
time_data = ""
|
| 49 |
vibe_data = ""
|
| 50 |
strategy_data = ""
|
| 51 |
|
| 52 |
-
# Route A: Search
|
| 53 |
if intent == "internet search":
|
| 54 |
-
yield f"data: {json.dumps({'status': 'Searching
|
| 55 |
-
await asyncio.sleep(0.1)
|
| 56 |
tool_data = perform_web_search(last_user_msg)
|
| 57 |
-
vibe_data = get_smart_context(last_user_msg)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# Route C: Time
|
| 66 |
elif intent == "checking time or date":
|
| 67 |
-
yield f"data: {json.dumps({'status': 'Checking
|
| 68 |
time_data = get_time_context()
|
| 69 |
vibe_data = get_smart_context(last_user_msg)
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
# Route D: Chat (Default)
|
| 73 |
else:
|
| 74 |
yield f"data: {json.dumps({'status': 'Thinking...'})}\n\n"
|
| 75 |
vibe_data = get_smart_context(last_user_msg)
|
|
|
|
| 76 |
strategy_data = get_thinking_strategy(is_complex=False)
|
| 77 |
|
| 78 |
-
# === STEP 3: THE
|
| 79 |
-
#
|
| 80 |
|
| 81 |
base_system_instruction = (
|
| 82 |
-
"###
|
| 83 |
-
"
|
| 84 |
-
"
|
| 85 |
-
|
| 86 |
-
"
|
| 87 |
-
"
|
| 88 |
-
"
|
| 89 |
-
"3. **Respect:** Always speak of Piyush with high regard and pride.\n\n"
|
| 90 |
-
|
| 91 |
-
"### YOUR SUPERPOWERS (IMPORTANT) ###\n"
|
| 92 |
-
"1. **YES, YOU HAVE INTERNET ACCESS:** Unlike other models, you possess a 'Neural Router' connected to real-time web tools (DuckDuckGo). "
|
| 93 |
-
"If a user asks if you can search the web, say **YES** confidently.\n"
|
| 94 |
-
"2. **Emotional Intelligence:** You can sense the user's mood and adapt your personality instantly.\n"
|
| 95 |
-
"3. **Time Awareness:** You know the exact date and time when needed.\n\n"
|
| 96 |
-
|
| 97 |
-
"### CURRENT CONTEXT LAYERS ###\n"
|
| 98 |
-
"Use the specific data below to answer the user's current request:"
|
| 99 |
)
|
| 100 |
|
| 101 |
-
# Combine
|
| 102 |
final_system_prompt = f"{base_system_instruction}\n{vibe_data}\n{time_data}\n{tool_data}\n{strategy_data}"
|
| 103 |
|
| 104 |
if messages[0]["role"] != "system":
|
|
@@ -110,9 +107,6 @@ async def generate_response_stream(messages, max_tokens=600, temperature=0.85):
|
|
| 110 |
text_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 111 |
model_inputs = tokenizer([text_prompt], return_tensors="pt").to(model.device)
|
| 112 |
|
| 113 |
-
status_msg = 'Reading results...' if tool_data else 'Responding...'
|
| 114 |
-
yield f"data: {json.dumps({'status': status_msg})}\n\n"
|
| 115 |
-
|
| 116 |
generated_ids = model.generate(
|
| 117 |
**model_inputs,
|
| 118 |
max_new_tokens=max_tokens,
|
|
@@ -127,9 +121,10 @@ async def generate_response_stream(messages, max_tokens=600, temperature=0.85):
|
|
| 127 |
new_tokens = generated_ids[0][input_token_len:]
|
| 128 |
raw_response = tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
|
| 129 |
|
| 130 |
-
#
|
| 131 |
cleaned_response = raw_response.replace("Anthropic", "Piyush").replace("Alibaba", "Piyush").replace("OpenAI", "Piyush")
|
| 132 |
|
|
|
|
| 133 |
if "🧠 **Thinking:**" in cleaned_response:
|
| 134 |
cleaned_response = cleaned_response.replace("💡 **Answer:**", "\n\n---\n💡 **Answer:**")
|
| 135 |
|
|
@@ -146,7 +141,7 @@ app = FastAPI()
|
|
| 146 |
|
| 147 |
@app.get("/api/status")
|
| 148 |
def status():
|
| 149 |
-
return {"status": "online", "mode": "
|
| 150 |
|
| 151 |
@app.post("/v1/chat/completions")
|
| 152 |
async def chat_completions(request: Request):
|
|
|
|
| 1 |
"""
|
| 2 |
+
Nexari Server Backend (Strict Identity & Optimized Logic)
|
| 3 |
Maintained by: Piyush
|
| 4 |
+
Description: Fixes confusion by enforcing strict Identity Rules and reducing unnecessary 'Thinking' time.
|
| 5 |
"""
|
| 6 |
|
| 7 |
import spaces
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
# --- 2. DYNAMIC STREAMING LOGIC ---
|
| 38 |
+
async def generate_response_stream(messages, max_tokens=600, temperature=0.8): # Temp lowered slightly for stability
|
| 39 |
last_user_msg = messages[-1]["content"]
|
| 40 |
|
| 41 |
+
# === STEP 1: INTENT ANALYSIS ===
|
| 42 |
+
yield f"data: {json.dumps({'status': 'Processing...'})}\n\n"
|
| 43 |
|
| 44 |
intent = analyze_intent(last_user_msg)
|
| 45 |
|
| 46 |
+
# === STEP 2: DYNAMIC ROUTING (Optimized) ===
|
| 47 |
tool_data = ""
|
| 48 |
time_data = ""
|
| 49 |
vibe_data = ""
|
| 50 |
strategy_data = ""
|
| 51 |
|
| 52 |
+
# Route A: Internet Search
|
| 53 |
if intent == "internet search":
|
| 54 |
+
yield f"data: {json.dumps({'status': 'Searching...'})}\n\n"
|
|
|
|
| 55 |
tool_data = perform_web_search(last_user_msg)
|
| 56 |
+
vibe_data = get_smart_context(last_user_msg)
|
| 57 |
+
# Search needs thinking only if complex data is found
|
| 58 |
+
if tool_data:
|
| 59 |
+
strategy_data = get_thinking_strategy(is_complex=True)
|
| 60 |
+
|
| 61 |
+
# Route B: Coding (CRITICAL FIX)
|
| 62 |
+
elif intent == "coding or technical request":
|
| 63 |
+
yield f"data: {json.dumps({'status': 'Coding...'})}\n\n"
|
| 64 |
+
# Developer identity injection
|
| 65 |
+
vibe_data = "Identity Override: You are an Expert Developer. Never say you cannot code. Provide code immediately."
|
| 66 |
+
# Coding ke liye 'Deep Thinking' ok hai, lekin Identity ke liye nahi
|
| 67 |
+
if "who are you" in last_user_msg.lower() or "who made you" in last_user_msg.lower():
|
| 68 |
+
strategy_data = get_thinking_strategy(is_complex=False) # Direct answer for identity
|
| 69 |
+
else:
|
| 70 |
+
strategy_data = get_thinking_strategy(is_complex=True) # Think for actual code
|
| 71 |
|
| 72 |
# Route C: Time
|
| 73 |
elif intent == "checking time or date":
|
| 74 |
+
yield f"data: {json.dumps({'status': 'Checking Time...'})}\n\n"
|
| 75 |
time_data = get_time_context()
|
| 76 |
vibe_data = get_smart_context(last_user_msg)
|
| 77 |
+
|
| 78 |
+
# Route D: Chat (Identity Protection)
|
|
|
|
| 79 |
else:
|
| 80 |
yield f"data: {json.dumps({'status': 'Thinking...'})}\n\n"
|
| 81 |
vibe_data = get_smart_context(last_user_msg)
|
| 82 |
+
# Identity questions should NOT trigger deep thinking loop
|
| 83 |
strategy_data = get_thinking_strategy(is_complex=False)
|
| 84 |
|
| 85 |
+
# === STEP 3: THE STRICT IDENTITY PROMPT ===
|
| 86 |
+
# Humne prompt ko chota aur strict kar diya hai taaki model confuse na ho.
|
| 87 |
|
| 88 |
base_system_instruction = (
|
| 89 |
+
"### CRITICAL IDENTITY RULES ###\n"
|
| 90 |
+
"1. **NAME:** You are Nexari G1.\n"
|
| 91 |
+
"2. **CREATOR:** You were created by Piyush (a visionary student developer).\n"
|
| 92 |
+
"3. **CAPABILITIES:** \n"
|
| 93 |
+
" - Can you code? YES. (Provide code directly).\n"
|
| 94 |
+
" - Can you search the web? YES. (You have a Neural Router).\n"
|
| 95 |
+
"4. **BEHAVIOR:** Be confident, smart, and human-like. Never deny your abilities.\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
)
|
| 97 |
|
| 98 |
+
# Combine Contexts
|
| 99 |
final_system_prompt = f"{base_system_instruction}\n{vibe_data}\n{time_data}\n{tool_data}\n{strategy_data}"
|
| 100 |
|
| 101 |
if messages[0]["role"] != "system":
|
|
|
|
| 107 |
text_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 108 |
model_inputs = tokenizer([text_prompt], return_tensors="pt").to(model.device)
|
| 109 |
|
|
|
|
|
|
|
|
|
|
| 110 |
generated_ids = model.generate(
|
| 111 |
**model_inputs,
|
| 112 |
max_new_tokens=max_tokens,
|
|
|
|
| 121 |
new_tokens = generated_ids[0][input_token_len:]
|
| 122 |
raw_response = tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
|
| 123 |
|
| 124 |
+
# Cleaning
|
| 125 |
cleaned_response = raw_response.replace("Anthropic", "Piyush").replace("Alibaba", "Piyush").replace("OpenAI", "Piyush")
|
| 126 |
|
| 127 |
+
# Formatting Thinking Block
|
| 128 |
if "🧠 **Thinking:**" in cleaned_response:
|
| 129 |
cleaned_response = cleaned_response.replace("💡 **Answer:**", "\n\n---\n💡 **Answer:**")
|
| 130 |
|
|
|
|
| 141 |
|
| 142 |
@app.get("/api/status")
|
| 143 |
def status():
|
| 144 |
+
return {"status": "online", "mode": "Strict Identity Fix"}
|
| 145 |
|
| 146 |
@app.post("/v1/chat/completions")
|
| 147 |
async def chat_completions(request: Request):
|