redhairedshanks1 commited on
Commit
d36787a
Β·
1 Parent(s): 252fdc4

Create intent_classifier.py

Browse files
Files changed (1) hide show
  1. services/intent_classifier.py +217 -0
services/intent_classifier.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Intent Classification Service
3
+ Determines user intent to make the chatbot behave naturally
4
+ """
5
+
6
+ import re
7
+ from typing import Dict, Any
8
+
9
+
10
+ class IntentClassifier:
11
+ """Classifies user intents to determine appropriate bot response"""
12
+
13
+ # Keywords that indicate pipeline creation intent
14
+ PIPELINE_KEYWORDS = [
15
+ "extract", "summarize", "translate", "classify", "detect",
16
+ "analyze", "process", "generate", "create pipeline", "build pipeline",
17
+ "run", "execute", "perform", "do", "get", "find", "identify",
18
+ "table", "text", "image", "signature", "stamp", "ner", "entities"
19
+ ]
20
+
21
+ # Casual chat patterns
22
+ CASUAL_PATTERNS = [
23
+ r"^(hi|hello|hey|greetings|good morning|good afternoon|good evening)",
24
+ r"^(how are you|what's up|wassup)",
25
+ r"^(thanks|thank you|appreciate)",
26
+ r"^(bye|goodbye|see you|later)",
27
+ r"^(ok|okay|cool|nice|great|awesome)",
28
+ r"^(what can you do|what do you do|help|capabilities)",
29
+ r"^(who are you|what are you)"
30
+ ]
31
+
32
+ # Question patterns that need informational response
33
+ QUESTION_PATTERNS = [
34
+ r"^(what|how|why|when|where|who|can you|do you|are you|is it)",
35
+ r"(help|explain|tell me|show me)"
36
+ ]
37
+
38
+ @staticmethod
39
+ def classify_intent(user_message: str) -> Dict[str, Any]:
40
+ """
41
+ Classify user intent from their message
42
+
43
+ Returns:
44
+ {
45
+ "intent": "casual_chat" | "question" | "pipeline_request" | "approval" | "rejection",
46
+ "confidence": float (0-1),
47
+ "requires_pipeline": bool,
48
+ "suggested_response_type": "friendly" | "informational" | "pipeline_generation"
49
+ }
50
+ """
51
+ message = user_message.strip().lower()
52
+
53
+ # Empty message
54
+ if not message:
55
+ return {
56
+ "intent": "casual_chat",
57
+ "confidence": 1.0,
58
+ "requires_pipeline": False,
59
+ "suggested_response_type": "friendly"
60
+ }
61
+
62
+ # Approval/Rejection patterns (for pipeline confirmation)
63
+ if message in ["approve", "yes", "y", "ok", "okay", "proceed", "go ahead", "do it"]:
64
+ return {
65
+ "intent": "approval",
66
+ "confidence": 1.0,
67
+ "requires_pipeline": False,
68
+ "suggested_response_type": "execute_pipeline"
69
+ }
70
+
71
+ if message in ["reject", "no", "n", "cancel", "stop", "don't"]:
72
+ return {
73
+ "intent": "rejection",
74
+ "confidence": 1.0,
75
+ "requires_pipeline": False,
76
+ "suggested_response_type": "friendly"
77
+ }
78
+
79
+ # Casual chat patterns
80
+ for pattern in IntentClassifier.CASUAL_PATTERNS:
81
+ if re.search(pattern, message, re.IGNORECASE):
82
+ return {
83
+ "intent": "casual_chat",
84
+ "confidence": 0.9,
85
+ "requires_pipeline": False,
86
+ "suggested_response_type": "friendly"
87
+ }
88
+
89
+ # Question patterns (informational)
90
+ is_question = False
91
+ for pattern in IntentClassifier.QUESTION_PATTERNS:
92
+ if re.search(pattern, message, re.IGNORECASE):
93
+ is_question = True
94
+ break
95
+
96
+ # Check for pipeline keywords
97
+ pipeline_keyword_count = sum(
98
+ 1 for keyword in IntentClassifier.PIPELINE_KEYWORDS
99
+ if keyword in message
100
+ )
101
+
102
+ # If has pipeline keywords, it's likely a pipeline request
103
+ if pipeline_keyword_count > 0:
104
+ return {
105
+ "intent": "pipeline_request",
106
+ "confidence": min(0.6 + (pipeline_keyword_count * 0.1), 1.0),
107
+ "requires_pipeline": True,
108
+ "suggested_response_type": "pipeline_generation",
109
+ "keyword_matches": pipeline_keyword_count
110
+ }
111
+
112
+ # Questions without pipeline keywords
113
+ if is_question:
114
+ return {
115
+ "intent": "question",
116
+ "confidence": 0.8,
117
+ "requires_pipeline": False,
118
+ "suggested_response_type": "informational"
119
+ }
120
+
121
+ # Default: treat as casual if short, otherwise might be pipeline request
122
+ if len(message.split()) < 3:
123
+ return {
124
+ "intent": "casual_chat",
125
+ "confidence": 0.6,
126
+ "requires_pipeline": False,
127
+ "suggested_response_type": "friendly"
128
+ }
129
+
130
+ # Longer messages without clear intent - ask for clarification
131
+ return {
132
+ "intent": "unclear",
133
+ "confidence": 0.4,
134
+ "requires_pipeline": False,
135
+ "suggested_response_type": "clarification"
136
+ }
137
+
138
+ @staticmethod
139
+ def get_friendly_response(intent: str, user_message: str = "") -> str:
140
+ """Generate friendly chatbot responses for non-pipeline intents"""
141
+
142
+ message_lower = user_message.lower().strip()
143
+
144
+ # Greetings
145
+ if re.search(r"^(hi|hello|hey)", message_lower):
146
+ return "Hello! πŸ‘‹ I'm MasterLLM, your AI document processing assistant. Upload a document and tell me what you'd like to do with it!"
147
+
148
+ # How are you
149
+ if "how are you" in message_lower:
150
+ return "I'm doing great, thank you! πŸ€– Ready to help you process documents. Upload a file to get started!"
151
+
152
+ # Thanks
153
+ if re.search(r"^(thanks|thank you)", message_lower):
154
+ return "You're welcome! 😊 Let me know if you need anything else!"
155
+
156
+ # Goodbye
157
+ if re.search(r"^(bye|goodbye)", message_lower):
158
+ return "Goodbye! πŸ‘‹ Feel free to come back anytime you need document processing help!"
159
+
160
+ # Capabilities question
161
+ if "what can you do" in message_lower or "capabilities" in message_lower:
162
+ return """I can help you with various document processing tasks:
163
+
164
+ πŸ“„ **Text Operations:**
165
+ - Extract text from PDFs and images
166
+ - Summarize documents
167
+ - Translate to different languages
168
+ - Classify text content
169
+ - Extract named entities (NER)
170
+
171
+ πŸ“Š **Table Operations:**
172
+ - Extract tables from documents
173
+ - Analyze tabular data
174
+
175
+ πŸ–ΌοΈ **Image Operations:**
176
+ - Describe images
177
+ - Detect signatures
178
+ - Detect stamps
179
+
180
+ πŸ”§ **How to use:**
181
+ 1. Upload a document (PDF or image)
182
+ 2. Tell me what you want to do (e.g., "extract text and summarize")
183
+ 3. I'll create a pipeline for you to approve
184
+ 4. Watch the magic happen! ✨"""
185
+
186
+ # Who are you
187
+ if "who are you" in message_lower or "what are you" in message_lower:
188
+ return "I'm MasterLLM πŸ€–, an AI-powered document processing orchestrator. I use advanced AI models (Bedrock Claude & Google Gemini) to understand your requests and automatically create processing pipelines for your documents!"
189
+
190
+ # Help
191
+ if message_lower in ["help", "?"] or "help me" in message_lower:
192
+ return """Here's how to use me:
193
+
194
+ 1️⃣ **Upload Document**: Click the upload button and select a PDF or image
195
+ 2️⃣ **Describe Task**: Tell me what you want (e.g., "extract text from pages 1-5 and summarize")
196
+ 3️⃣ **Review Pipeline**: I'll show you the processing plan
197
+ 4️⃣ **Approve**: Type 'approve' to execute or 'reject' to cancel
198
+ 5️⃣ **Get Results**: Watch real-time progress and get your results!
199
+
200
+ **Example requests:**
201
+ - "extract text and summarize"
202
+ - "get tables from pages 2-4"
203
+ - "translate to Spanish"
204
+ - "detect signatures and stamps"
205
+
206
+ Need anything else?"""
207
+
208
+ # Unclear intent
209
+ if intent == "unclear":
210
+ return "I'm not sure what you'd like me to do. Could you please:\n- Upload a document first, or\n- Tell me what processing task you need (e.g., 'extract text', 'summarize', 'translate')\n\nType 'help' to see what I can do!"
211
+
212
+ # Default friendly response
213
+ return "I'm here to help! Upload a document and tell me what you'd like to do with it. Type 'help' if you need examples! 😊"
214
+
215
+
216
+ # Singleton instance
217
+ intent_classifier = IntentClassifier()