AI automation sample program
Here’s a generic AI-driven automation sample program showcasing how to use AI for automating a repetitive task. This example focuses on automating document summarization and email sending, a practical use case across industries.
AI Document Summarization and Email Automation
Use Case:
- Monitor a folder for new text files or documents.
- Summarize the content using AI.
- Send an email with the summarized content.
Code Example
import os
import smtplib
from email.mime.text import MIMEText
from transformers import pipeline
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
# Load AI summarizer
summarizer = pipeline("summarization")
# Email configuration
SMTP_SERVER = "smtp.example.com"
EMAIL_FROM = "your_email@example.com"
EMAIL_PASSWORD = "your_password"
EMAIL_TO = "recipient@example.com"
def summarize_text(file_path):
"""Read and summarize the content of a text file."""
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
summary = summarizer(content, max_length=100, min_length=50, do_sample=False)
return summary[0]['summary_text']
def send_email(subject, body):
"""Send an email with the given subject and body."""
msg = MIMEText(body)
msg["Subject"] = subject
msg["From"] = EMAIL_FROM
msg["To"] = EMAIL_TO
with smtplib.SMTP_SSL(SMTP_SERVER, 465) as server:
server.login(EMAIL_FROM, EMAIL_PASSWORD)
server.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
print("Email sent!")
class DocumentHandler(FileSystemEventHandler):
"""Handles new file events in the watched folder."""
def on_created(self, event):
if event.is_directory:
return
if event.src_path.endswith(".txt"):
print(f"New file detected: {event.src_path}")
summary = summarize_text(event.src_path)
print(f"Summary: {summary}")
send_email("Document Summary", summary)
def start_monitoring(folder_path):
"""Start monitoring a folder for new text files."""
observer = Observer()
event_handler = DocumentHandler()
observer.schedule(event_handler, folder_path, recursive=False)
observer.start()
print(f"Monitoring folder: {folder_path}")
try:
while True:
pass
except KeyboardInterrupt:
observer.stop()
observer.join()
# Main
if __name__ == "__main__":
# Replace with the path of the folder to monitor
FOLDER_TO_MONITOR = "path_to_folder"
start_monitoring(FOLDER_TO_MONITOR)
How It Works
-
Folder Monitoring:
- Uses the
library to watch a specific folder for newwatchdog
files..txt
- Triggers a function when a new file is added.
- Uses the
-
AI Summarization:
- Reads the content of the file.
- Uses a transformer-based summarization model (e.g., BART or T5) to generate a concise summary.
-
Email Automation:
- Sends the summarized content to a specified recipient via SMTP.
Dependencies
Install the required Python libraries:
pip install transformers watchdog smtplib
Customizations
- Input File Format: Modify the file reader to support PDF or Word documents (using libraries like
orPyPDF2
).python-docx
- Email Styling: Use
with HTML for formatted email content.MIMEText
- Additional Automation: Extend functionality to upload summaries to a database or shared cloud storage.
No Comments have been Posted.