Nexari-Research commited on
Commit
c949f11
·
verified ·
1 Parent(s): 89cc727

Update tools_engine.py

Browse files
Files changed (1) hide show
  1. tools_engine.py +56 -17
tools_engine.py CHANGED
@@ -1,44 +1,83 @@
1
  """
2
- Nexari Tools Engine (Lazy Loading Fix)
 
 
3
  """
 
4
  from duckduckgo_search import DDGS
5
  from transformers import pipeline
6
 
7
- # Global variable, initially None
8
  _intent_classifier = None
9
 
10
- def get_classifier():
 
 
 
11
  global _intent_classifier
12
  if _intent_classifier is None:
13
- print(">>> Tools: Loading Intent Model (Lazy)...")
14
  _intent_classifier = pipeline("zero-shot-classification", model="typeform/distilbert-base-uncased-mnli")
15
  return _intent_classifier
16
 
17
  def analyze_intent(user_text):
 
 
 
18
  text_lower = user_text.lower().strip()
19
 
20
- # Fast Greeting Check
21
- direct_chat = ["hi", "hello", "hey", "who are you", "what is your name"]
22
- if any(text_lower.startswith(t) for t in direct_chat):
23
- return "general_conversation"
 
24
 
 
25
  try:
26
- # Load only when needed
27
- classifier = get_classifier()
28
- candidate_labels = ["internet search", "general conversation", "coding request", "checking time"]
 
 
 
 
 
 
 
29
  result = classifier(user_text, candidate_labels)
 
 
 
 
30
 
31
- if result['scores'][0] > 0.5:
32
- return result['labels'][0]
33
  except Exception as e:
34
  print(f"Intent Error: {e}")
35
 
36
- return "general_conversation"
37
 
38
  def perform_web_search(user_text):
 
 
 
39
  try:
40
- clean_query = user_text.replace("search", "").strip()
 
 
 
 
 
 
 
 
41
  results = DDGS().text(clean_query, max_results=3)
42
- return results if results else None
43
- except:
 
 
 
 
 
 
44
  return None
 
1
  """
2
+ Nexari Tools Engine (Lazy Loading)
3
+ Author: Piyush
4
+ Description: Loads the Intent Brain only when needed to prevent Server Crash.
5
  """
6
+
7
  from duckduckgo_search import DDGS
8
  from transformers import pipeline
9
 
10
+ # Global variable to hold the model (initially empty)
11
  _intent_classifier = None
12
 
13
+ def get_intent_model():
14
+ """
15
+ Loads model only if it's not already loaded.
16
+ """
17
  global _intent_classifier
18
  if _intent_classifier is None:
19
+ print(">>> Tools: Lazy Loading Intent Model...")
20
  _intent_classifier = pipeline("zero-shot-classification", model="typeform/distilbert-base-uncased-mnli")
21
  return _intent_classifier
22
 
23
  def analyze_intent(user_text):
24
+ """
25
+ Decides intent with Safety Layer + Lazy Loading.
26
+ """
27
  text_lower = user_text.lower().strip()
28
 
29
+ # LAYER 1: HARDCODED SAFETY
30
+ direct_chat_triggers = ["hi", "hello", "hey", "hlo", "hola", "namaste", "what is your name", "who are you"]
31
+
32
+ if text_lower in direct_chat_triggers or any(text_lower.startswith(t + " ") for t in direct_chat_triggers):
33
+ return "general conversation"
34
 
35
+ # LAYER 2: LAZY NEURAL NETWORK
36
  try:
37
+ # Load Model Here (On Demand)
38
+ classifier = get_intent_model()
39
+
40
+ candidate_labels = [
41
+ "internet search",
42
+ "general conversation",
43
+ "coding request",
44
+ "checking time"
45
+ ]
46
+
47
  result = classifier(user_text, candidate_labels)
48
+ top_intent = result['labels'][0]
49
+ confidence = result['scores'][0]
50
+
51
+ print(f">>> Brain: Detected '{top_intent}' ({confidence:.2f})")
52
 
53
+ if confidence > 0.5:
54
+ return top_intent
55
  except Exception as e:
56
  print(f"Intent Error: {e}")
57
 
58
+ return "general conversation"
59
 
60
  def perform_web_search(user_text):
61
+ """
62
+ Executes search.
63
+ """
64
  try:
65
+ clean_query = user_text.lower()
66
+ remove_phrases = ["search for", "google", "find", "tell me about", "latest info on", "news about"]
67
+ for phrase in remove_phrases:
68
+ clean_query = clean_query.replace(phrase, "")
69
+
70
+ clean_query = clean_query.strip()
71
+ if len(clean_query) < 2: clean_query = user_text
72
+
73
+ print(f">>> Action: Searching Web for '{clean_query}'...")
74
  results = DDGS().text(clean_query, max_results=3)
75
+
76
+ if results:
77
+ # Return raw list
78
+ return results
79
+ return None
80
+
81
+ except Exception as e:
82
+ print(f"Search Error: {e}")
83
  return None