Preparing your learning space...
100% through Automation for FDEs tutorials
Advanced Automation Systems covers sophisticated automation approaches that integrate multiple technologies, create intelligent workflows, and build comprehensive end-to-end solutions—culminating in the creation of sophisticated AI-powered automation systems.
Building End-to-End AI Automation involves creating comprehensive automation systems that combine traditional workflow automation, API integrations, data processing, and artificial intelligence to solve complex business problems with minimal human intervention.
Why it is useful: End-to-End AI Automation creates sophisticated systems that can handle complex, variable processes requiring judgment, learning, and adaptation—going beyond simple rule-based automation to deliver true intelligent automation that improves over time.
import sqlite3
import logging
import re
import os
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
import joblib
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# ======================
# LAYER 1: DATA INGESTION
# ======================
class DataIngestionLayer:
"""Handles collecting data from various sources"""
def __init__(self, db_path="support_system.db"):
self.db_path = db_path
self.init_database()
def init_database(self):
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS support_tickets (
ticket_id INTEGER PRIMARY KEY AUTOINCREMENT,
customer_email TEXT,
subject TEXT,
description TEXT,
category TEXT,
priority TEXT,
status TEXT DEFAULT 'open',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
conn.commit()
conn.close()
def ingest_email_ticket(self, email_data):
"""Ingest a support ticket from email"""
try:
customer_email = email_data.get('From', '').strip()
subject = email_data.get('Subject', '').strip()
description = email_data.get('Body', '')
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO support_tickets (customer_email, subject, description)
VALUES (?, ?, ?)
''', (customer_email, subject, description))
ticket_id = cursor.lastrowid
conn.commit()
conn.close()
logger.info(f"Ingested email ticket: {ticket_id}")
return ticket_id
except Exception as e:
logger.error(f"Error ingesting email: {e}")
return None
# ======================
# LAYER 2: PROCESSING & STORAGE
# ======================
class ProcessingStorageLayer:
"""Handles data cleaning, transformation, and storage"""
def __init__(self, db_path="support_system.db"):
self.db_path = db_path
def clean_text(self, text):
"""Clean and normalize text for ML"""
if not text:
return ""
text = text.lower()
text = re.sub(r'\s+', ' ', text)
text = re.sub(r'[^a-zA-Z0-9\s\.\,\!\?]', ' ', text)
return text.strip()
def extract_features(self, ticket_id):
"""Extract features from ticket for ML processing"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute(
'SELECT subject, description FROM support_tickets WHERE ticket_id = ?',
(ticket_id,)
)
result = cursor.fetchone()
conn.close()
if not result:
return None
subject, description = result
full_text = f"{subject} {description}"
cleaned = self.clean_text(full_text)
return {
'text_length': len(cleaned),
'word_count': len(cleaned.split()),
'cleaned_text': cleaned
}
# ======================
# LAYER 3: AI/ML LAYER
# ======================
class AIMLayer:
"""Handles machine learning models for classification"""
def __init__(self, model_path="ticket_classifier.pkl"):
self.model_path = model_path
self.pipeline = None
self.categories = ['Technical', 'Billing', 'Feature', 'Account', 'General']
self.load_or_create_model()
def load_or_create_model(self):
if os.path.exists(self.model_path):
try:
model_data = joblib.load(self.model_path)
self.pipeline = model_data['pipeline']
self.categories = model_data['categories']
logger.info("Loaded existing ML model")
except:
self.create_model()
else:
self.create_model()
def create_model(self):
self.pipeline = Pipeline([
('vectorizer', TfidfVectorizer(max_features=5000, stop_words='english')),
('classifier', MultinomialNB())
])
logger.info("Created new ML model")
def train(self):
"""Train model with ticket data"""
conn = sqlite3.connect("support_system.db")
cursor = conn.cursor()
cursor.execute('SELECT subject, description, category FROM support_tickets WHERE category IS NOT NULL')
results = cursor.fetchall()
conn.close()
if len(results) < 10:
# Use sample data if insufficient real data
training_data = [
("Login error", "Cannot access account", "Account"),
("Invoice question", "Bill amount incorrect", "Billing"),
("Feature request", "Add dark mode", "Feature"),
("Bug report", "App crashes on startup", "Technical"),
("General question", "What are your hours?", "General")
]
else:
training_data = [(s, d, c) for s, d, c in results]
texts = [f"{s} {d}" for s, d, _ in training_data]
labels = [c for _, _, c in training_data]
cleaned = [self.clean_text(t) for t in texts]
self.pipeline.fit(cleaned, labels)
# Save model
joblib.dump({'pipeline': self.pipeline, 'categories': self.categories}, self.model_path)
logger.info("Model trained and saved")
def clean_text(self, text):
if not text:
return ""
text = text.lower()
text = re.sub(r'\s+', ' ', text)
text = re.sub(r'[^a-zA-Z0-9\s\.\,\!\?]', ' ', text)
return text.strip()
def predict(self, ticket_id):
"""Predict category for a ticket"""
conn = sqlite3.connect("support_system.db")
cursor = conn.cursor()
cursor.execute(
'SELECT subject, description FROM support_tickets WHERE ticket_id = ?',
(ticket_id,)
)
result = cursor.fetchone()
conn.close()
if not result:
return {'category': self.categories[0], 'confidence': 0.0}
subject, description = result
cleaned = self.clean_text(f"{subject} {description}")
try:
pred = self.pipeline.predict([cleaned])[0]
probs = self.pipeline.predict_proba([cleaned])[0]
conf = max(probs)
return {'category': pred, 'confidence': conf}
except:
return {'category': self.categories[0], 'confidence': 0.0}
# ======================
# LAYER 4: DECISION & ORCHESTRATION
# ======================
class DecisionOrchestrationLayer:
"""Makes decisions and orchestrates actions based on AI outputs"""
def __init__(self, db_path="support_system.db"):
self.db_path = db_path
self.ai = AIMLayer()
def process_ticket(self, ticket_id):
"""Process ticket through decision layer"""
# Get AI prediction
ml_result = self.ai.predict(ticket_id)
category = ml_result['category']
confidence = ml_result['confidence']
# Determine priority
priority = self.determine_priority(category)
# Update ticket
self.update_ticket(ticket_id, category, priority)
# Execute actions
actions = self.execute_actions(ticket_id, category, priority, confidence)
logger.info(f"Processed ticket {ticket_id}: {category}, {priority}, actions: {actions}")
return actions
def determine_priority(self, category):
priority_map = {
'Technical': 'high',
'Account': 'high',
'Billing': 'medium',
'Feature': 'low',
'General': 'low'
}
return priority_map.get(category, 'medium')
def update_ticket(self, ticket_id, category, priority):
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
UPDATE support_tickets
SET category = ?, priority = ?, status = 'triaged'
WHERE ticket_id = ?
''', (category, priority, ticket_id))
conn.commit()
conn.close()
def execute_actions(self, ticket_id, category, priority, confidence):
actions = []
if confidence < 0.6:
actions.append("flagged_for_review")
if category == 'Technical' and priority == 'high':
actions.append("escalated_to_specialist")
if category == 'Billing':
actions.append("routing_to_billing_team")
if category == 'Account':
actions.append("triggering_password_reset")
return actions
# ======================
# LAYER 5: ACTION LAYER
# ======================
class ActionLayer:
"""Executes automated actions"""
def send_notification(self, channel, message):
logger.info(f"[{channel}] {message}")
# In production: integrate with Slack, Email, PagerDuty, etc.
def create_task(self, ticket_id, assignee, description):
logger.info(f"Created task for {assignee}: {description}")
# In production: integrate with Jira, Asana, etc.
# ======================
# MAIN ORCHESTRATOR
# ======================
class IntelligentSupportSystem:
"""End-to-end AI-powered support automation"""
def __init__(self):
self.ingestion = DataIngestionLayer()
self.processing = ProcessingStorageLayer()
self.ai = AIMLayer()
self.orchestration = DecisionOrchestrationLayer()
self.action = ActionLayer()
def process_email(self, email_data):
"""Process incoming email through full pipeline"""
# Ingest
ticket_id = self.ingestion.ingest_email_ticket(email_data)
if not ticket_id:
return None
# Process with AI
self.orchestration.process_ticket(ticket_id)
return ticket_id
def retrain(self):
"""Retrain model with latest data"""
self.ai.train()
def get_metrics(self):
"""Get system health metrics"""
conn = sqlite3.connect("support_system.db")
cursor = conn.cursor()
cursor.execute('SELECT status, COUNT(*) FROM support_tickets GROUP BY status')
stats = dict(cursor.fetchall())
conn.close()
return stats
# Usage
if __name__ == "__main__":
system = IntelligentSupportSystem()
# Train initial model
system.ai.train()
# Process sample email
ticket = system.process_email({
'From': 'user@company.com',
'Subject': 'Urgent: Cannot login to dashboard',
'Body': 'Getting 403 error when trying to access the admin panel. Need this fixed ASAP.'
})
print(f"Processed ticket: {ticket}")
print(f"System metrics: {system.get_metrics()}")
Save your progress and earn XP for completing tutorials.
4 questions · Pass with 70%+
1Which layer in an end-to-end AI automation system makes decisions based on AI output?
2In the support system example, why does load_or_create_model() need import os?
3What is the purpose of the "feedback and learning loop" component?
4Which is the recommended architecture for keeping system components decoupled and independently scalable?
Technology
Forward Deployed Engineer
Lesson group
Automation for FDEs
Progress
100% complete