-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintelligent_csv_agent.py
More file actions
621 lines (527 loc) Β· 25 KB
/
Copy pathintelligent_csv_agent.py
File metadata and controls
621 lines (527 loc) Β· 25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
"""
Truly Intelligent CSV AI Agent
Uses AI to understand natural language and analyze CSV data without hardcoded rules
"""
import pandas as pd
import numpy as np
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse
from typing import Dict, List, Any
import uvicorn
import re
from collections import Counter
import json
class IntelligentCSVAnalyzer:
def __init__(self):
"""Initialize with CSV data and create intelligent analysis capabilities"""
try:
self.trainer_df = pd.read_csv('trainer_details.csv')
self.session_df = pd.read_csv('trainer_session_details.csv')
print(f"β
Loaded {len(self.trainer_df)} trainers and {len(self.session_df)} sessions")
# Create comprehensive data understanding
self.analyze_data_structure()
except Exception as e:
print(f"β Error loading data: {e}")
self.trainer_df = pd.DataFrame()
self.session_df = pd.DataFrame()
def analyze_data_structure(self):
"""Analyze the data to understand what information is available"""
self.data_insights = {
'trainer_columns': list(self.trainer_df.columns),
'session_columns': list(self.session_df.columns),
'domains': list(self.trainer_df['Domain'].unique()) if 'Domain' in self.trainer_df.columns else [],
'locations': list(self.session_df['Location'].unique()) if 'Location' in self.session_df.columns else [],
'topics': list(self.session_df['Topic'].unique()) if 'Topic' in self.session_df.columns else [],
'modes': list(self.session_df['Mode'].unique()) if 'Mode' in self.session_df.columns else [],
'trainer_names': list(self.trainer_df['Name'].unique()) if 'Name' in self.trainer_df.columns else []
}
print(f"π Data Analysis Complete:")
print(f" Domains: {len(self.data_insights['domains'])}")
print(f" Locations: {len(self.data_insights['locations'])}")
print(f" Topics: {len(self.data_insights['topics'])}")
def understand_question_intent(self, question: str) -> Dict[str, Any]:
"""Use AI-like logic to understand what the user is asking"""
question_lower = question.lower().strip()
# Analyze question structure and intent
intent = {
'type': 'unknown',
'target': None, # trainers, sessions, specific person
'filter_by': {}, # location, domain, experience, etc.
'action': None, # count, list, find, compare
'keywords': []
}
# Extract keywords from question
words = re.findall(r'\b\w+\b', question_lower)
intent['keywords'] = [w for w in words if len(w) > 2]
# Determine action type
if any(word in question_lower for word in ['how many', 'count', 'total', 'number']):
intent['action'] = 'count'
elif any(word in question_lower for word in ['show', 'list', 'display', 'all']):
intent['action'] = 'list'
elif any(word in question_lower for word in ['find', 'who', 'which', 'search']):
intent['action'] = 'find'
elif any(word in question_lower for word in ['compare', 'vs', 'versus', 'difference']):
intent['action'] = 'compare'
else:
intent['action'] = 'find' # default
# Determine target
if any(word in question_lower for word in ['trainer', 'trainers', 'instructor', 'teacher', 'people']):
intent['target'] = 'trainers'
elif any(word in question_lower for word in ['session', 'sessions', 'class', 'course']):
intent['target'] = 'sessions'
else:
# Try to infer from context
if intent['action'] == 'count':
intent['target'] = 'trainers' # Most count questions are about trainers
else:
intent['target'] = 'both'
# Extract filters intelligently
self.extract_filters(question_lower, intent)
return intent
def extract_filters(self, question: str, intent: Dict[str, Any]):
"""Intelligently extract filters from the question"""
# Location filters - check against known locations
for location in self.data_insights['locations']:
if location.lower() in question:
intent['filter_by']['location'] = location
break
# Also check for common location variations
location_variations = {
'tamilnadu': ['Trichy', 'Chennai'],
'tamil nadu': ['Trichy', 'Chennai'],
'tn': ['Trichy', 'Chennai'],
'mumbai': ['Mumbai'],
'bangalore': ['Bangalore'],
'delhi': ['Delhi'],
'pune': ['Pune'],
'kolkata': ['Kolkata']
}
for variation, actual_locations in location_variations.items():
if variation in question:
intent['filter_by']['location_group'] = actual_locations
break
# Domain filters - check against known domains
for domain in self.data_insights['domains']:
domain_words = domain.lower().split()
if any(word in question for word in domain_words):
intent['filter_by']['domain'] = domain
break
# Topic filters - check against known topics
for topic in self.data_insights['topics']:
if topic.lower() in question:
intent['filter_by']['topic'] = topic
break
# Experience filters
if any(word in question for word in ['experienced', 'senior', 'expert']):
intent['filter_by']['experience'] = 'high'
elif any(word in question for word in ['junior', 'beginner', 'new']):
intent['filter_by']['experience'] = 'low'
# Cost filters
if any(word in question for word in ['cheap', 'affordable', 'low cost']):
intent['filter_by']['cost'] = 'low'
elif any(word in question for word in ['expensive', 'premium', 'high cost']):
intent['filter_by']['cost'] = 'high'
# Mode filters
if any(word in question for word in ['online', 'remote', 'virtual']):
intent['filter_by']['mode'] = 'Online'
elif any(word in question for word in ['offline', 'in-person', 'physical']):
intent['filter_by']['mode'] = 'Offline'
def execute_intelligent_query(self, intent: Dict[str, Any]) -> str:
"""Execute the query based on understood intent"""
try:
if intent['action'] == 'count':
return self.handle_count_query(intent)
elif intent['action'] == 'list':
return self.handle_list_query(intent)
elif intent['action'] == 'find':
return self.handle_find_query(intent)
elif intent['action'] == 'compare':
return self.handle_compare_query(intent)
else:
return self.handle_general_query(intent)
except Exception as e:
return f"β Error analyzing your question: {str(e)}"
def handle_count_query(self, intent: Dict[str, Any]) -> str:
"""Handle counting queries intelligently"""
if intent['target'] == 'trainers':
df = self.trainer_df.copy()
# Apply filters
if 'location_group' in intent['filter_by']:
# Count trainers who conduct sessions in these locations
locations = intent['filter_by']['location_group']
location_sessions = self.session_df[
self.session_df['Location'].isin(locations)
]
unique_trainers = location_sessions['Trainer Name'].nunique()
response = f"π **Count Result:**\n\n"
response += f"π§βπ« **{unique_trainers} trainers** conduct sessions in {', '.join(locations)}\n\n"
# Add breakdown by location
response += "**Breakdown by location:**\n```\n"
for loc in locations:
loc_count = location_sessions[location_sessions['Location'] == loc]['Trainer Name'].nunique()
session_count = len(location_sessions[location_sessions['Location'] == loc])
response += f"{loc:<12}: {loc_count} trainers, {session_count} sessions\n"
response += "```"
return response
elif 'domain' in intent['filter_by']:
domain = intent['filter_by']['domain']
count = len(df[df['Domain'] == domain])
return f"π **{count} trainers** specialize in {domain}"
else:
total = len(df)
return f"π **Total: {total} trainers** in the database"
elif intent['target'] == 'sessions':
df = self.session_df.copy()
# Apply filters
if 'location' in intent['filter_by']:
location = intent['filter_by']['location']
count = len(df[df['Location'] == location])
return f"π **{count} sessions** in {location}"
elif 'topic' in intent['filter_by']:
topic = intent['filter_by']['topic']
count = len(df[df['Topic'] == topic])
return f"π **{count} sessions** on {topic}"
else:
total = len(df)
return f"π **Total: {total} sessions** in the database"
return "β Could not determine what to count. Please be more specific."
def handle_find_query(self, intent: Dict[str, Any]) -> str:
"""Handle find/search queries intelligently"""
results = []
if intent['target'] in ['trainers', 'both']:
trainer_results = self.filter_trainers(intent['filter_by'])
if not trainer_results.empty:
results.append(('trainers', trainer_results))
if intent['target'] in ['sessions', 'both']:
session_results = self.filter_sessions(intent['filter_by'])
if not session_results.empty:
results.append(('sessions', session_results))
if not results:
return f"β No results found matching your criteria. Available options:\n" + \
f"Domains: {', '.join(self.data_insights['domains'][:5])}\n" + \
f"Locations: {', '.join(self.data_insights['locations'][:5])}"
return self.format_results(results, intent)
def filter_trainers(self, filters: Dict[str, Any]) -> pd.DataFrame:
"""Apply filters to trainer data"""
df = self.trainer_df.copy()
if 'domain' in filters:
df = df[df['Domain'] == filters['domain']]
if 'experience' in filters:
if filters['experience'] == 'high':
df = df[df['Experience (yrs)'] >= 10]
elif filters['experience'] == 'low':
df = df[df['Experience (yrs)'] <= 5]
if 'cost' in filters:
if filters['cost'] == 'low':
df = df[df['Per Day Charges (βΉ)'] <= df['Per Day Charges (βΉ)'].median()]
elif filters['cost'] == 'high':
df = df[df['Per Day Charges (βΉ)'] >= df['Per Day Charges (βΉ)'].median()]
# Location filter for trainers (based on where they conduct sessions)
if 'location_group' in filters:
locations = filters['location_group']
session_trainers = self.session_df[
self.session_df['Location'].isin(locations)
]['Trainer Name'].unique()
df = df[df['Name'].isin(session_trainers)]
return df
def filter_sessions(self, filters: Dict[str, Any]) -> pd.DataFrame:
"""Apply filters to session data"""
df = self.session_df.copy()
if 'location' in filters:
df = df[df['Location'] == filters['location']]
if 'location_group' in filters:
df = df[df['Location'].isin(filters['location_group'])]
if 'topic' in filters:
df = df[df['Topic'] == filters['topic']]
if 'mode' in filters:
df = df[df['Mode'] == filters['mode']]
return df
def format_results(self, results: List, intent: Dict[str, Any]) -> str:
"""Format results in a user-friendly way"""
response = "π **Search Results:**\n\n"
for result_type, df in results:
if result_type == 'trainers':
response += f"π§βπ« **TRAINERS ({len(df)} found):**\n"
response += "```\n"
response += f"{'Name':<15} {'Domain':<15} {'Experience':<12} {'Daily Rate':<12}\n"
response += "-" * 60 + "\n"
for _, row in df.head(5).iterrows():
name = str(row['Name'])[:14]
domain = str(row['Domain'])[:14]
exp = f"{row['Experience (yrs)']} yrs"
rate = f"βΉ{row['Per Day Charges (βΉ)']}"
response += f"{name:<15} {domain:<15} {exp:<12} {rate:<12}\n"
response += "```\n\n"
elif result_type == 'sessions':
response += f"π
**SESSIONS ({len(df)} found):**\n"
response += "```\n"
response += f"{'Topic':<20} {'Trainer':<15} {'Location':<12} {'Mode':<8}\n"
response += "-" * 60 + "\n"
for _, row in df.head(5).iterrows():
topic = str(row['Topic'])[:19]
trainer = str(row['Trainer Name'])[:14]
location = str(row['Location'])[:11]
mode = str(row['Mode'])[:7]
response += f"{topic:<20} {trainer:<15} {location:<12} {mode:<8}\n"
response += "```\n\n"
return response.strip()
def handle_list_query(self, intent: Dict[str, Any]) -> str:
"""Handle listing queries"""
return self.handle_find_query(intent) # Same logic
def handle_compare_query(self, intent: Dict[str, Any]) -> str:
"""Handle comparison queries"""
return "π Comparison feature coming soon! Try asking about specific trainers or domains."
def handle_general_query(self, intent: Dict[str, Any]) -> str:
"""Handle general queries"""
return self.handle_find_query(intent)
def answer_question(self, question: str) -> str:
"""Main method to answer any question using AI analysis"""
print(f"π§ AI Processing: {question}")
if not question or question.strip() == "":
return "β Please ask a question about trainers or sessions."
# Handle greetings
if question.lower().strip() in ['hello', 'hi', 'hey']:
return f"π Hello! I can help you analyze your trainer data. Try asking:\n" + \
f"β’ 'How many trainers are from Tamil Nadu?'\n" + \
f"β’ 'Show me blockchain experts'\n" + \
f"β’ 'Find affordable DevOps trainers'\n" + \
f"β’ 'List all online sessions'"
# Use AI to understand the question
intent = self.understand_question_intent(question)
print(f"π― Understood intent: {intent}")
# Execute intelligent query
result = self.execute_intelligent_query(intent)
return result
# Initialize the intelligent agent
agent = IntelligentCSVAnalyzer()
# FastAPI app
app = FastAPI(title="Intelligent CSV AI Agent", version="2.0.0")
@app.get("/", response_class=HTMLResponse)
async def serve_index():
"""Serve the main page"""
return HTMLResponse("""
<!DOCTYPE html>
<html>
<head>
<title>π§ Intelligent CSV AI Agent</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 900px;
margin: 0 auto;
background: white;
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
overflow: hidden;
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 30px;
text-align: center;
}
.header h1 { font-size: 2.2rem; margin-bottom: 10px; }
.header p { font-size: 1.1rem; opacity: 0.9; }
.features {
background: #f8f9fa;
padding: 20px;
border-bottom: 1px solid #eee;
}
.feature-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
}
.feature-item {
background: white;
padding: 15px;
border-radius: 10px;
text-align: center;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.chat-container {
height: 400px;
overflow-y: auto;
padding: 20px;
background: #f8f9fa;
}
.message {
margin: 15px 0;
padding: 15px;
border-radius: 10px;
max-width: 85%;
}
.user-message {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
margin-left: auto;
text-align: right;
}
.bot-message {
background: white;
border: 1px solid #ddd;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.input-section {
padding: 20px;
background: white;
border-top: 1px solid #eee;
}
.input-container {
display: flex;
gap: 10px;
}
input {
flex: 1;
padding: 15px;
border: 2px solid #ddd;
border-radius: 25px;
font-size: 16px;
outline: none;
}
input:focus {
border-color: #667eea;
}
button {
padding: 15px 25px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 25px;
cursor: pointer;
font-weight: bold;
}
button:hover {
transform: translateY(-2px);
}
pre {
background: #f8f9fa;
padding: 10px;
border-radius: 5px;
overflow-x: auto;
font-family: 'Courier New', monospace;
font-size: 12px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>π§ Intelligent CSV AI Agent</h1>
<p>Ask me anything in natural language - I'll understand and analyze your data!</p>
</div>
<div class="features">
<div class="feature-grid">
<div class="feature-item">
<strong>π― Smart Understanding</strong><br>
<small>No hardcoded rules - I understand your intent</small>
</div>
<div class="feature-item">
<strong>π Data Analysis</strong><br>
<small>Intelligent filtering and counting</small>
</div>
<div class="feature-item">
<strong>π£οΈ Natural Language</strong><br>
<small>Ask questions like you would to a human</small>
</div>
<div class="feature-item">
<strong>π Context Aware</strong><br>
<small>Understands locations, domains, and more</small>
</div>
</div>
</div>
<div class="chat-container" id="chatContainer">
<div class="message bot-message">
<strong>π§ AI Agent:</strong> Hello! I'm an intelligent AI that can understand your questions and analyze your CSV data. Ask me anything about trainers and sessions - I'll figure out what you mean!
</div>
</div>
<div class="input-section">
<div class="input-container">
<input type="text" id="questionInput" placeholder="Ask me anything about your data..." onkeypress="handleKeyPress(event)">
<button onclick="sendQuestion()">Ask AI</button>
</div>
</div>
</div>
<script>
function addMessage(message, isUser = false) {
const container = document.getElementById('chatContainer');
const messageDiv = document.createElement('div');
messageDiv.className = 'message ' + (isUser ? 'user-message' : 'bot-message');
if (isUser) {
messageDiv.innerHTML = '<strong>π€ You:</strong> ' + message;
} else {
messageDiv.innerHTML = '<strong>π§ AI Agent:</strong><br>' +
message.replace(/\\n/g, '<br>')
.replace(/```([\\s\\S]*?)```/g, '<pre>$1</pre>')
.replace(/\\*\\*(.*?)\\*\\*/g, '<strong>$1</strong>');
}
container.appendChild(messageDiv);
container.scrollTop = container.scrollHeight;
}
function handleKeyPress(event) {
if (event.key === 'Enter') {
sendQuestion();
}
}
async function sendQuestion() {
const input = document.getElementById('questionInput');
const question = input.value.trim();
if (!question) return;
addMessage(question, true);
input.value = '';
try {
const response = await fetch('/ask', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ question: question })
});
const data = await response.json();
if (data && data.answer) {
addMessage(data.answer);
} else {
addMessage('β No response received from server');
}
} catch (error) {
addMessage('β Error: ' + error.message);
}
}
</script>
</body>
</html>
""")
@app.post("/ask")
async def ask_question(request: Dict[str, str]):
"""Answer questions using intelligent analysis"""
question = request.get("question", "").strip()
if not question:
raise HTTPException(status_code=400, detail="No question provided")
try:
answer = agent.answer_question(question)
return {"question": question, "answer": answer, "status": "success"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {
"status": "healthy",
"agent_type": "intelligent_ai",
"trainers_loaded": len(agent.trainer_df),
"sessions_loaded": len(agent.session_df),
"domains": len(agent.data_insights['domains']),
"locations": len(agent.data_insights['locations'])
}
if __name__ == "__main__":
print("π§ Starting Intelligent CSV AI Agent...")
print("π― AI-powered natural language understanding")
print("π Smart data analysis without hardcoded rules")
print("π Access at: http://localhost:8005")
uvicorn.run(app, host="0.0.0.0", port=8005, log_level="error")