Spaces:
Running
Running
Improved NLP Logic#2
Browse files- nlp_service.py +20 -10
nlp_service.py
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
import json
|
3 |
from model_setup import zero_shot, ner # Assuming model_setup.py exists and is correct
|
4 |
from utils import parse_entities # Assuming utils.py exists and is correct
|
5 |
-
from config import CATEGORY_KEYWORDS # Import categories from config
|
6 |
|
7 |
def analyze_text(text: str) -> dict:
|
8 |
"""
|
@@ -38,31 +38,41 @@ def analyze_text(text: str) -> dict:
|
|
38 |
"error": str(e)
|
39 |
}
|
40 |
|
41 |
-
# Step 2: Check
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
return {
|
46 |
-
"status": "fallback_required",
|
47 |
-
"message": "Intent requires further processing (
|
48 |
"original_text": text,
|
49 |
-
"classified_intent":
|
50 |
}
|
51 |
|
52 |
# Step 3: Entity extraction (for non-fallback intents)
|
53 |
try:
|
54 |
entities = ner(text)
|
55 |
print(f"NLP Service: NER entities: {entities}")
|
56 |
-
|
|
|
57 |
print(f"NLP Service: Parsed entities: Amount={amount}, Currency={currency}, Item={item}")
|
58 |
except Exception as e:
|
59 |
print(f"NLP Service: Error during entity extraction: {e}")
|
60 |
# Decide if you want to return an error or proceed with partial data
|
61 |
amount, currency, item = None, None, None # Default to None on error
|
|
|
|
|
|
|
62 |
|
63 |
# Step 4: Category matching using config.py
|
64 |
category = "Misc" # Default
|
65 |
-
text_lower = text.lower()
|
66 |
item_lower = item.lower() if item else ""
|
67 |
|
68 |
# Check intent first for Income/Investment categories
|
|
|
2 |
import json
|
3 |
from model_setup import zero_shot, ner # Assuming model_setup.py exists and is correct
|
4 |
from utils import parse_entities # Assuming utils.py exists and is correct
|
5 |
+
from config import CATEGORY_KEYWORDS, QUERY_KEYWORDS # Import categories and query keywords from config
|
6 |
|
7 |
def analyze_text(text: str) -> dict:
|
8 |
"""
|
|
|
38 |
"error": str(e)
|
39 |
}
|
40 |
|
41 |
+
# Step 2: Check for Query Keywords or Query Intent
|
42 |
+
text_lower = text.lower()
|
43 |
+
is_query_keyword_present = any(kw in text_lower for kw in QUERY_KEYWORDS)
|
44 |
+
|
45 |
+
if intent == "query" or is_query_keyword_present:
|
46 |
+
if is_query_keyword_present and intent != "query":
|
47 |
+
print(f"NLP Service: Query keyword detected, overriding initial intent '{intent}'. Fallback triggered.")
|
48 |
+
else:
|
49 |
+
print(f"NLP Service: Intent classified as '{intent}' or query keyword found. Fallback route triggered.")
|
50 |
+
|
51 |
+
# Return fallback status
|
52 |
return {
|
53 |
+
"status": "fallback_required",
|
54 |
+
"message": "Intent requires further processing (query detected).",
|
55 |
"original_text": text,
|
56 |
+
"classified_intent": "query" # Standardize to query if fallback is triggered
|
57 |
}
|
58 |
|
59 |
# Step 3: Entity extraction (for non-fallback intents)
|
60 |
try:
|
61 |
entities = ner(text)
|
62 |
print(f"NLP Service: NER entities: {entities}")
|
63 |
+
# --- FIX: Pass the original 'text' as 'full_text' ---
|
64 |
+
amount, currency, item = parse_entities(entities, full_text=text)
|
65 |
print(f"NLP Service: Parsed entities: Amount={amount}, Currency={currency}, Item={item}")
|
66 |
except Exception as e:
|
67 |
print(f"NLP Service: Error during entity extraction: {e}")
|
68 |
# Decide if you want to return an error or proceed with partial data
|
69 |
amount, currency, item = None, None, None # Default to None on error
|
70 |
+
# Optionally, log the traceback for debugging
|
71 |
+
import traceback
|
72 |
+
traceback.print_exc()
|
73 |
|
74 |
# Step 4: Category matching using config.py
|
75 |
category = "Misc" # Default
|
|
|
76 |
item_lower = item.lower() if item else ""
|
77 |
|
78 |
# Check intent first for Income/Investment categories
|