Spaces:
Sleeping
Sleeping
Update tools_engine.py
Browse files- tools_engine.py +20 -11
tools_engine.py
CHANGED
|
@@ -1,27 +1,36 @@
|
|
| 1 |
"""
|
| 2 |
-
Nexari Tools Engine (
|
| 3 |
Author: Piyush
|
| 4 |
-
Description:
|
| 5 |
"""
|
| 6 |
|
| 7 |
from duckduckgo_search import DDGS
|
| 8 |
from transformers import pipeline
|
| 9 |
|
| 10 |
print(">>> Tools: Loading Intent Classification Model...")
|
| 11 |
-
# Loading the Brain
|
| 12 |
intent_classifier = pipeline("zero-shot-classification", model="typeform/distilbert-base-uncased-mnli")
|
| 13 |
|
| 14 |
def analyze_intent(user_text):
|
| 15 |
"""
|
| 16 |
-
Decides
|
| 17 |
"""
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
candidate_labels = [
|
| 20 |
"internet search",
|
| 21 |
"general conversation",
|
| 22 |
-
"coding
|
| 23 |
-
"checking time
|
| 24 |
-
"mathematical calculation"
|
| 25 |
]
|
| 26 |
|
| 27 |
try:
|
|
@@ -31,8 +40,8 @@ def analyze_intent(user_text):
|
|
| 31 |
|
| 32 |
print(f">>> Brain: Detected '{top_intent}' ({confidence:.2f})")
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
if confidence > 0.
|
| 36 |
return top_intent
|
| 37 |
except Exception as e:
|
| 38 |
print(f"Intent Error: {e}")
|
|
@@ -41,7 +50,7 @@ def analyze_intent(user_text):
|
|
| 41 |
|
| 42 |
def perform_web_search(user_text):
|
| 43 |
"""
|
| 44 |
-
Executes search only when triggered
|
| 45 |
"""
|
| 46 |
try:
|
| 47 |
clean_query = user_text.lower()
|
|
|
|
| 1 |
"""
|
| 2 |
+
Nexari Tools Engine (Smart Override Edition)
|
| 3 |
Author: Piyush
|
| 4 |
+
Description: Prevents silly mistakes on short messages by using a 'Hardcoded Logic Layer' before the Neural Network.
|
| 5 |
"""
|
| 6 |
|
| 7 |
from duckduckgo_search import DDGS
|
| 8 |
from transformers import pipeline
|
| 9 |
|
| 10 |
print(">>> Tools: Loading Intent Classification Model...")
|
|
|
|
| 11 |
intent_classifier = pipeline("zero-shot-classification", model="typeform/distilbert-base-uncased-mnli")
|
| 12 |
|
| 13 |
def analyze_intent(user_text):
|
| 14 |
"""
|
| 15 |
+
Decides intent with a Safety Layer for short greetings to prevent hallucination.
|
| 16 |
"""
|
| 17 |
+
text_lower = user_text.lower().strip()
|
| 18 |
+
|
| 19 |
+
# === LAYER 1: HARDCODED SAFETY (The Fix for 'Hi' -> Searching) ===
|
| 20 |
+
# Agar user bas hello bol raha hai, to AI brain mat lagao. Direct chat karo.
|
| 21 |
+
direct_chat_triggers = ["hi", "hello", "hey", "hlo", "hola", "namaste", "what is your name", "who are you"]
|
| 22 |
+
|
| 23 |
+
# Agar input EXACTLY inme se ek hai, ya start hota hai
|
| 24 |
+
if text_lower in direct_chat_triggers or any(text_lower.startswith(t + " ") for t in direct_chat_triggers):
|
| 25 |
+
print(f">>> Brain Override: Detected Greeting/Identity -> Force 'general conversation'")
|
| 26 |
+
return "general conversation"
|
| 27 |
+
|
| 28 |
+
# === LAYER 2: NEURAL NETWORK DECISION ===
|
| 29 |
candidate_labels = [
|
| 30 |
"internet search",
|
| 31 |
"general conversation",
|
| 32 |
+
"coding request",
|
| 33 |
+
"checking time"
|
|
|
|
| 34 |
]
|
| 35 |
|
| 36 |
try:
|
|
|
|
| 40 |
|
| 41 |
print(f">>> Brain: Detected '{top_intent}' ({confidence:.2f})")
|
| 42 |
|
| 43 |
+
# Confidence Threshold badha diya (0.4 -> 0.5) taaki ghalat search na kare
|
| 44 |
+
if confidence > 0.5:
|
| 45 |
return top_intent
|
| 46 |
except Exception as e:
|
| 47 |
print(f"Intent Error: {e}")
|
|
|
|
| 50 |
|
| 51 |
def perform_web_search(user_text):
|
| 52 |
"""
|
| 53 |
+
Executes search only when triggered.
|
| 54 |
"""
|
| 55 |
try:
|
| 56 |
clean_query = user_text.lower()
|