AI Automation·20 min read

AI Automation with Python: Complete Practical Guide

Lyubo
Lyubo·
AI Automation with Python: Complete Practical Guide

Master AI automation with Python. Build intelligent workflows, automate data processing, and create smart systems that work 24/7.

AI Automation with Python: Complete Practical Guide 🤖

TL;DR – Learn to build powerful AI automation systems with Python. From data processing to intelligent decision-making, this guide covers everything you need to create production-ready automated workflows.

Table of Contents

  1. Introduction to AI Automation
  2. Setting Up Your Environment
  3. Building Your First AI Automation
  4. Advanced Automation Patterns
  5. Production Deployment

Introduction to AI Automation {#introduction}

AI automation combines artificial intelligence with workflow automation to create systems that can:

  • Make intelligent decisions
  • Process data automatically
  • Learn from patterns
  • Adapt to changing conditions

Key Benefits

  • 24/7 Operation: Never stops working
  • Consistent Quality: No human errors
  • Scalability: Handle increasing workloads
  • Cost Efficiency: Reduce manual labor

Setting Up Your Environment {#setup}

1# Create virtual environment
2python -m venv ai-automation
3source ai-automation/bin/activate # Linux/Mac
4# ai-automation\Scripts\activate # Windows
5
6# Install core packages
7pip install pandas numpy scikit-learn
8pip install openai langchain
9pip install schedule celery redis
10pip install fastapi uvicorn

Building Your First AI Automation {#first-automation}

Project: Intelligent Email Classifier

1import pandas as pd
2from sklearn.feature_extraction.text import TfidfVectorizer
3from sklearn.naive_bayes import MultinomialNB
4from sklearn.pipeline import Pipeline
5import pickle
6import schedule
7import time
8
9class EmailClassifier:
10 def __init__(self):
11 self.model = Pipeline([
12 ('tfidf', TfidfVectorizer(max_features=5000)),
13 ('classifier', MultinomialNB())
14 ])
15 self.categories = ['urgent', 'normal', 'spam', 'promotional']
16
17 def train(self, emails_df):
18 """Train the classifier"""
19 X = emails_df['content']
20 y = emails_df['category']
21 self.model.fit(X, y)
22
23 # Save model
24 with open('email_classifier.pkl', 'wb') as f:
25 pickle.dump(self.model, f)
26
27 def classify(self, email_content):
28 """Classify a single email"""
29 prediction = self.model.predict([email_content])[0]
30 confidence = self.model.predict_proba([email_content]).max()
31 return prediction, confidence
32
33 def process_inbox(self):
34 """Automated inbox processing"""
35 # Connect to email service (simplified)
36 emails = self.fetch_new_emails()
37
38 for email in emails:
39 category, confidence = self.classify(email['content'])
40
41 if confidence > 0.8:
42 self.move_email(email['id'], category)
43 self.log_action(email['id'], category, confidence)
44
45 def fetch_new_emails(self):
46 # Implement email fetching logic
47 pass
48
49 def move_email(self, email_id, category):
50 # Implement email moving logic
51 pass
52
53 def log_action(self, email_id, category, confidence):
54 print(f"Email {email_id} classified as {category} (confidence: {confidence:.2f})")
55
56# Schedule automation
57classifier = EmailClassifier()
58schedule.every(5).minutes.do(classifier.process_inbox)
59
60while True:
61 schedule.run_pending()
62 time.sleep(1)

Project: Smart Data Pipeline

1import pandas as pd
2import numpy as np
3from datetime import datetime, timedelta
4import logging
5
6class SmartDataPipeline:
7 def __init__(self):
8 self.setup_logging()
9 self.anomaly_threshold = 2.0
10
11 def setup_logging(self):
12 logging.basicConfig(
13 level=logging.INFO,
14 format='%(asctime)s - %(levelname)s - %(message)s',
15 handlers=[
16 logging.FileHandler('pipeline.log'),
17 logging.StreamHandler()
18 ]
19 )
20 self.logger = logging.getLogger(__name__)
21
22 def extract_data(self, source):
23 """Extract data from various sources"""
24 try:
25 if source.endswith('.csv'):
26 data = pd.read_csv(source)
27 elif source.endswith('.json'):
28 data = pd.read_json(source)
29 else:
30 # Database connection
31 data = self.fetch_from_database(source)
32
33 self.logger.info(f"Extracted {len(data)} records from {source}")
34 return data
35 except Exception as e:
36 self.logger.error(f"Extraction failed: {e}")
37 return None
38
39 def detect_anomalies(self, data, column):
40 """AI-powered anomaly detection"""
41 values = data[column].values
42 mean = np.mean(values)
43 std = np.std(values)
44
45 # Z-score based detection
46 z_scores = np.abs((values - mean) / std)
47 anomalies = z_scores > self.anomaly_threshold
48
49 if anomalies.any():
50 self.logger.warning(f"Found {anomalies.sum()} anomalies in {column}")
51 self.alert_anomalies(data[anomalies], column)
52
53 return data[~anomalies] # Return clean data
54
55 def transform_data(self, data):
56 """Intelligent data transformation"""
57 # Auto-detect data types
58 for column in data.columns:
59 if data[column].dtype == 'object':
60 # Try to convert to datetime
61 try:
62 data[column] = pd.to_datetime(data[column])
63 except:
64 pass
65
66 # Handle missing values intelligently
67 for column in data.columns:
68 if data[column].isnull().any():
69 if data[column].dtype in ['int64', 'float64']:
70 # Use median for numeric
71 data[column].fillna(data[column].median(), inplace=True)
72 else:
73 # Use mode for categorical
74 data[column].fillna(data[column].mode()[0], inplace=True)
75
76 return data
77
78 def load_data(self, data, destination):
79 """Load processed data"""
80 try:
81 if destination.endswith('.csv'):
82 data.to_csv(destination, index=False)
83 elif destination.endswith('.json'):
84 data.to_json(destination, orient='records')
85 else:
86 self.save_to_database(data, destination)
87
88 self.logger.info(f"Loaded {len(data)} records to {destination}")
89 except Exception as e:
90 self.logger.error(f"Loading failed: {e}")
91
92 def run_pipeline(self, source, destination):
93 """Run the complete pipeline"""
94 self.logger.info("Starting data pipeline")
95
96 # Extract
97 data = self.extract_data(source)
98 if data is None:
99 return False
100
101 # Transform with anomaly detection
102 for column in data.select_dtypes(include=[np.number]).columns:
103 data = self.detect_anomalies(data, column)
104
105 data = self.transform_data(data)
106
107 # Load
108 self.load_data(data, destination)
109
110 self.logger.info("Pipeline completed successfully")
111 return True
112
113# Usage
114pipeline = SmartDataPipeline()
115pipeline.run_pipeline('raw_data.csv', 'processed_data.csv')

Advanced Automation Patterns {#advanced-patterns}

Pattern 1: Self-Healing Systems

1import psutil
2import requests
3from datetime import datetime
4
5class SelfHealingSystem:
6 def __init__(self):
7 self.health_checks = []
8 self.recovery_actions = {}
9
10 def add_health_check(self, name, check_function, recovery_function):
11 """Add a health check with recovery action"""
12 self.health_checks.append({
13 'name': name,
14 'check': check_function,
15 'recovery': recovery_function
16 })
17
18 def monitor_system(self):
19 """Continuously monitor system health"""
20 for check in self.health_checks:
21 try:
22 if not check['check']():
23 print(f"Health check failed: {check['name']}")
24 self.execute_recovery(check['recovery'])
25 except Exception as e:
26 print(f"Error in health check {check['name']}: {e}")
27
28 def execute_recovery(self, recovery_function):
29 """Execute recovery action"""
30 try:
31 recovery_function()
32 print("Recovery action executed successfully")
33 except Exception as e:
34 print(f"Recovery failed: {e}")
35
36# Example health checks
37def check_cpu_usage():
38 return psutil.cpu_percent(interval=1) < 80
39
40def check_memory_usage():
41 return psutil.virtual_memory().percent < 85
42
43def check_api_endpoint():
44 try:
45 response = requests.get('http://localhost:8000/health', timeout=5)
46 return response.status_code == 200
47 except:
48 return False
49
50# Recovery actions
51def restart_service():
52 import subprocess
53 subprocess.run(['systemctl', 'restart', 'myservice'])
54
55def clear_cache():
56 import shutil
57 shutil.rmtree('/tmp/cache', ignore_errors=True)
58
59# Setup monitoring
60monitor = SelfHealingSystem()
61monitor.add_health_check('CPU', check_cpu_usage, lambda: print("CPU usage high"))
62monitor.add_health_check('Memory', check_memory_usage, clear_cache)
63monitor.add_health_check('API', check_api_endpoint, restart_service)

Pattern 2: Intelligent Task Scheduling

1from celery import Celery
2from datetime import datetime, timedelta
3import redis
4
5app = Celery('ai_automation', broker='redis://localhost:6379')
6redis_client = redis.Redis(host='localhost', port=6379, db=0)
7
8@app.task
9def intelligent_task_scheduler():
10 """AI-powered task scheduling"""
11 # Get system metrics
12 cpu_usage = psutil.cpu_percent()
13 memory_usage = psutil.virtual_memory().percent
14
15 # Determine optimal task distribution
16 if cpu_usage < 50 and memory_usage < 60:
17 # System has capacity, schedule more tasks
18 schedule_high_priority_tasks()
19 elif cpu_usage > 80 or memory_usage > 80:
20 # System under stress, defer non-critical tasks
21 defer_low_priority_tasks()
22
23 # Predictive scheduling based on historical data
24 predicted_load = predict_system_load()
25 if predicted_load > 0.8:
26 preemptively_scale_resources()
27
28@app.task
29def process_data_batch(batch_id):
30 """Process a batch of data"""
31 # Implementation here
32 pass
33
34def schedule_high_priority_tasks():
35 """Schedule urgent tasks"""
36 urgent_batches = get_urgent_batches()
37 for batch_id in urgent_batches:
38 process_data_batch.delay(batch_id)
39
40def predict_system_load():
41 """Predict future system load using historical data"""
42 # Simple moving average prediction
43 historical_data = get_historical_load_data()
44 return sum(historical_data[-5:]) / 5

Production Deployment {#deployment}

Docker Configuration

1FROM python:3.10-slim
2
3WORKDIR /app
4
5COPY requirements.txt .
6RUN pip install -r requirements.txt
7
8COPY . .
9
10CMD ["python", "automation_manager.py"]

Kubernetes Deployment

1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: ai-automation
5spec:
6 replicas: 3
7 selector:
8 matchLabels:
9 app: ai-automation
10 template:
11 metadata:
12 labels:
13 app: ai-automation
14 spec:
15 containers:
16 - name: ai-automation
17 image: ai-automation:latest
18 resources:
19 requests:
20 memory: "256Mi"
21 cpu: "250m"
22 limits:
23 memory: "512Mi"
24 cpu: "500m"

Monitoring Setup

1from prometheus_client import Counter, Histogram, start_http_server
2import time
3
4# Metrics
5TASKS_PROCESSED = Counter('automation_tasks_processed_total')
6TASK_DURATION = Histogram('automation_task_duration_seconds')
7ERRORS = Counter('automation_errors_total')
8
9def monitored_task(func):
10 """Decorator to add monitoring to tasks"""
11 def wrapper(*args, **kwargs):
12 start_time = time.time()
13 try:
14 result = func(*args, **kwargs)
15 TASKS_PROCESSED.inc()
16 return result
17 except Exception as e:
18 ERRORS.inc()
19 raise
20 finally:
21 TASK_DURATION.observe(time.time() - start_time)
22 return wrapper
23
24# Start metrics server
25start_http_server(8000)

Conclusion

AI automation with Python opens up endless possibilities for creating intelligent, self-managing systems. Key takeaways:

  1. Start Simple: Begin with basic automation and add AI gradually
  2. Monitor Everything: Use metrics to understand system behavior
  3. Plan for Failures: Implement robust error handling and recovery
  4. Scale Thoughtfully: Design for growth from the beginning

Ready to automate your world with AI? Start with one small process and expand from there! 🚀

Share:
AI AutomationPythonMachine LearningWorkflowsProduction