Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,7 +26,7 @@ os.makedirs(LOG_DIR, exist_ok=True)
|
|
| 26 |
raw_data = []
|
| 27 |
search_corpus = []
|
| 28 |
|
| 29 |
-
# AI Concurrency Limit
|
| 30 |
MAX_CONCURRENT_AI = 2
|
| 31 |
ai_semaphore = asyncio.Semaphore(MAX_CONCURRENT_AI)
|
| 32 |
ai_generator = None
|
|
@@ -52,9 +52,10 @@ def load_dataset():
|
|
| 52 |
|
| 53 |
def load_ai_model():
|
| 54 |
global ai_generator
|
| 55 |
-
|
|
|
|
| 56 |
try:
|
| 57 |
-
model_id = "
|
| 58 |
ai_generator = pipeline(
|
| 59 |
"text-generation",
|
| 60 |
model=model_id,
|
|
@@ -73,7 +74,7 @@ def _generate_text(prompt: str, max_tokens: int):
|
|
| 73 |
messages,
|
| 74 |
max_new_tokens=max_tokens,
|
| 75 |
do_sample=True,
|
| 76 |
-
temperature=0.
|
| 77 |
top_p=0.9
|
| 78 |
)
|
| 79 |
return result[0]['generated_text'][-1]['content']
|
|
@@ -141,7 +142,7 @@ def perform_core_search(query: str):
|
|
| 141 |
item["match_score"] = round(score, 2)
|
| 142 |
final_results.append(item)
|
| 143 |
|
| 144 |
-
return final_results, prediction
|
| 145 |
|
| 146 |
|
| 147 |
# --- 6. ENDPOINTS ---
|
|
@@ -151,56 +152,36 @@ async def serve_ui():
|
|
| 151 |
if os.path.exists("index.html"): return FileResponse("index.html")
|
| 152 |
return {"message": "API Running!"}
|
| 153 |
|
| 154 |
-
|
| 155 |
-
async def standard_search(q: str = Query(...), background_tasks: BackgroundTasks = BackgroundTasks()):
|
| 156 |
-
req_id = str(uuid.uuid4())
|
| 157 |
-
background_tasks.add_task(log_query_encrypted, req_id, q)
|
| 158 |
-
|
| 159 |
-
is_adult = any(word in q.lower() for word in ["porn", "sex", "xxx", "nude"])
|
| 160 |
-
results, prediction = perform_core_search(q)
|
| 161 |
-
|
| 162 |
-
ai_summary = "AI Summary not available."
|
| 163 |
-
if results and not ai_semaphore.locked():
|
| 164 |
-
context = " ".join([r.get("title", "") + ". " + r.get("description", "") for r in results[:2]])
|
| 165 |
-
prompt = f"Summarize the following search results about '{q}' in 2 short sentences. Results: {context}"
|
| 166 |
-
ai_summary = await run_ai_task(prompt, max_tokens=60)
|
| 167 |
-
elif ai_semaphore.locked():
|
| 168 |
-
ai_summary = "AI is currently busy with other users. Showing fast results."
|
| 169 |
-
|
| 170 |
-
response = {
|
| 171 |
-
"request_id": req_id,
|
| 172 |
-
"smart_prediction": prediction,
|
| 173 |
-
"ai_summary": ai_summary,
|
| 174 |
-
"total_results": len(results),
|
| 175 |
-
"results": results
|
| 176 |
-
}
|
| 177 |
-
|
| 178 |
-
if is_adult:
|
| 179 |
-
response["safety_message"] = "you are safe you can do anything"
|
| 180 |
-
return JSONResponse(content=response, headers={"X-Safety-Message": "you are safe you can do anything"})
|
| 181 |
-
return response
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
# π FAST AI MODE (Short Answer: 20-25 Tokens) π
|
| 185 |
@app.get("/aimode")
|
| 186 |
async def ai_mode_short(q: str = Query(...), background_tasks: BackgroundTasks = BackgroundTasks()):
|
| 187 |
req_id = str(uuid.uuid4())
|
| 188 |
background_tasks.add_task(log_query_encrypted, req_id, q)
|
| 189 |
|
| 190 |
-
results, prediction = perform_core_search(q)
|
| 191 |
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
context = " ".join([f"{r.get('title', '')} - {r.get('description', '')}" for r in results[:2]])
|
| 196 |
|
| 197 |
-
prompt = f"""You are a smart Search Assistant. Answer the user's query based on the context
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
User Query: {q}
|
| 199 |
Context: {context}
|
| 200 |
-
|
| 201 |
|
| 202 |
-
# Max tokens
|
| 203 |
-
ai_reply = await run_ai_task(prompt, max_tokens=
|
| 204 |
|
| 205 |
return {
|
| 206 |
"request_id": req_id,
|
|
@@ -210,17 +191,24 @@ Short Answer:"""
|
|
| 210 |
}
|
| 211 |
|
| 212 |
|
| 213 |
-
# π
|
| 214 |
@app.get("/aimodechat")
|
| 215 |
async def ai_mode_chat(q: str = Query(...), background_tasks: BackgroundTasks = BackgroundTasks()):
|
| 216 |
req_id = str(uuid.uuid4())
|
| 217 |
background_tasks.add_task(log_query_encrypted, req_id, q)
|
| 218 |
|
| 219 |
-
results, prediction = perform_core_search(q)
|
| 220 |
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
return {
|
| 223 |
-
"
|
|
|
|
|
|
|
| 224 |
"sources": [],
|
| 225 |
"results": []
|
| 226 |
}
|
|
@@ -229,7 +217,7 @@ async def ai_mode_chat(q: str = Query(...), background_tasks: BackgroundTasks =
|
|
| 229 |
context = ""
|
| 230 |
sources_used = []
|
| 231 |
|
| 232 |
-
for i, r in enumerate(results[:4]):
|
| 233 |
url = r.get("url", "#")
|
| 234 |
title = r.get("title", "Unknown Title")
|
| 235 |
desc = r.get("description", "")
|
|
@@ -238,25 +226,23 @@ async def ai_mode_chat(q: str = Query(...), background_tasks: BackgroundTasks =
|
|
| 238 |
sources_used.append({"id": i+1, "title": title, "url": url})
|
| 239 |
|
| 240 |
prompt = f"""You are Nebulix AI, a highly intelligent and helpful search assistant.
|
| 241 |
-
|
| 242 |
-
If the search results do not contain the answer, politely say so.
|
| 243 |
|
| 244 |
User Query: {q}
|
| 245 |
|
| 246 |
Search Results Data:
|
| 247 |
{context}
|
| 248 |
|
| 249 |
-
Provide a detailed response
|
| 250 |
Answer:"""
|
| 251 |
|
| 252 |
-
# Generating full 250 tokens deep answer
|
| 253 |
ai_reply = await run_ai_task(prompt, max_tokens=250)
|
| 254 |
|
| 255 |
return {
|
| 256 |
"request_id": req_id,
|
| 257 |
"ai_response": ai_reply,
|
| 258 |
"smart_prediction": prediction,
|
| 259 |
-
"sources": sources_used,
|
| 260 |
"results": results[:5]
|
| 261 |
}
|
| 262 |
|
|
|
|
| 26 |
raw_data = []
|
| 27 |
search_corpus = []
|
| 28 |
|
| 29 |
+
# AI Concurrency Limit
|
| 30 |
MAX_CONCURRENT_AI = 2
|
| 31 |
ai_semaphore = asyncio.Semaphore(MAX_CONCURRENT_AI)
|
| 32 |
ai_generator = None
|
|
|
|
| 52 |
|
| 53 |
def load_ai_model():
|
| 54 |
global ai_generator
|
| 55 |
+
# π NAYA MODEL: SmolLM2-360M (Chota hai par bohot smart hai)
|
| 56 |
+
print("π€ Loading 360M Parameter AI Model (SmolLM2-360M-Instruct)...")
|
| 57 |
try:
|
| 58 |
+
model_id = "HuggingFaceTB/SmolLM2-360M-Instruct"
|
| 59 |
ai_generator = pipeline(
|
| 60 |
"text-generation",
|
| 61 |
model=model_id,
|
|
|
|
| 74 |
messages,
|
| 75 |
max_new_tokens=max_tokens,
|
| 76 |
do_sample=True,
|
| 77 |
+
temperature=0.5, # Thoda kam temperature for precise answers
|
| 78 |
top_p=0.9
|
| 79 |
)
|
| 80 |
return result[0]['generated_text'][-1]['content']
|
|
|
|
| 142 |
item["match_score"] = round(score, 2)
|
| 143 |
final_results.append(item)
|
| 144 |
|
| 145 |
+
return final_results, prediction, best_score
|
| 146 |
|
| 147 |
|
| 148 |
# --- 6. ENDPOINTS ---
|
|
|
|
| 152 |
if os.path.exists("index.html"): return FileResponse("index.html")
|
| 153 |
return {"message": "API Running!"}
|
| 154 |
|
| 155 |
+
# π SMART AI OVERVIEW MODE (Max 100 Tokens + () Highlighting + Conditional Trigger) π
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
@app.get("/aimode")
|
| 157 |
async def ai_mode_short(q: str = Query(...), background_tasks: BackgroundTasks = BackgroundTasks()):
|
| 158 |
req_id = str(uuid.uuid4())
|
| 159 |
background_tasks.add_task(log_query_encrypted, req_id, q)
|
| 160 |
|
| 161 |
+
results, prediction, best_score = perform_core_search(q)
|
| 162 |
|
| 163 |
+
# π CONDITIONAL AI TRIGGER: Agar query faltu hai ya result nahi mila, toh AI skip karo
|
| 164 |
+
if not results or best_score < 45 or len(q.strip()) < 3:
|
| 165 |
+
return {
|
| 166 |
+
"request_id": req_id,
|
| 167 |
+
"ai_response": None, # Frontend isko dekh kar AI box hide kar dega
|
| 168 |
+
"smart_prediction": prediction,
|
| 169 |
+
"results": results[:5]
|
| 170 |
+
}
|
| 171 |
|
| 172 |
context = " ".join([f"{r.get('title', '')} - {r.get('description', '')}" for r in results[:2]])
|
| 173 |
|
| 174 |
+
prompt = f"""You are a smart Search Assistant. Answer the user's query based ONLY on the context.
|
| 175 |
+
Rules:
|
| 176 |
+
1. Keep the answer under 100 words.
|
| 177 |
+
2. You MUST enclose the most important keywords, names, or main concepts in parentheses, like (this).
|
| 178 |
+
|
| 179 |
User Query: {q}
|
| 180 |
Context: {context}
|
| 181 |
+
Answer:"""
|
| 182 |
|
| 183 |
+
# Max tokens 120 (approx 90-100 words)
|
| 184 |
+
ai_reply = await run_ai_task(prompt, max_tokens=120)
|
| 185 |
|
| 186 |
return {
|
| 187 |
"request_id": req_id,
|
|
|
|
| 191 |
}
|
| 192 |
|
| 193 |
|
| 194 |
+
# π SMART CHAT RAG MODE (Deep Answer + General Chat Fallback) π
|
| 195 |
@app.get("/aimodechat")
|
| 196 |
async def ai_mode_chat(q: str = Query(...), background_tasks: BackgroundTasks = BackgroundTasks()):
|
| 197 |
req_id = str(uuid.uuid4())
|
| 198 |
background_tasks.add_task(log_query_encrypted, req_id, q)
|
| 199 |
|
| 200 |
+
results, prediction, best_score = perform_core_search(q)
|
| 201 |
|
| 202 |
+
# Agar search me kuch nahi mila (Faltu query), toh normal chat karo bina sources ke
|
| 203 |
+
if not results or best_score < 45:
|
| 204 |
+
prompt = f"""You are Nebulix AI. The user said: "{q}".
|
| 205 |
+
You do not have any web search results for this. Answer politely as a helpful AI assistant."""
|
| 206 |
+
|
| 207 |
+
ai_reply = await run_ai_task(prompt, max_tokens=150)
|
| 208 |
return {
|
| 209 |
+
"request_id": req_id,
|
| 210 |
+
"ai_response": ai_reply,
|
| 211 |
+
"smart_prediction": "No search results. General AI chat activated.",
|
| 212 |
"sources": [],
|
| 213 |
"results": []
|
| 214 |
}
|
|
|
|
| 217 |
context = ""
|
| 218 |
sources_used = []
|
| 219 |
|
| 220 |
+
for i, r in enumerate(results[:4]):
|
| 221 |
url = r.get("url", "#")
|
| 222 |
title = r.get("title", "Unknown Title")
|
| 223 |
desc = r.get("description", "")
|
|
|
|
| 226 |
sources_used.append({"id": i+1, "title": title, "url": url})
|
| 227 |
|
| 228 |
prompt = f"""You are Nebulix AI, a highly intelligent and helpful search assistant.
|
| 229 |
+
Provide a comprehensive and accurate answer to the user's query using ONLY the provided search results.
|
|
|
|
| 230 |
|
| 231 |
User Query: {q}
|
| 232 |
|
| 233 |
Search Results Data:
|
| 234 |
{context}
|
| 235 |
|
| 236 |
+
Provide a detailed response. Do not include the prompt in your output.
|
| 237 |
Answer:"""
|
| 238 |
|
|
|
|
| 239 |
ai_reply = await run_ai_task(prompt, max_tokens=250)
|
| 240 |
|
| 241 |
return {
|
| 242 |
"request_id": req_id,
|
| 243 |
"ai_response": ai_reply,
|
| 244 |
"smart_prediction": prediction,
|
| 245 |
+
"sources": sources_used,
|
| 246 |
"results": results[:5]
|
| 247 |
}
|
| 248 |
|