Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Articles

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:

  1. Monitor a folder for new text files or documents.
  2. Summarize the content using AI.
  3. 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

  1. Folder Monitoring:

    • Uses the
      watchdog
      library to watch a specific folder for new
      .txt
      files.
    • Triggers a function when a new file is added.
  2. AI Summarization:

    • Reads the content of the file.
    • Uses a transformer-based summarization model (e.g., BART or T5) to generate a concise summary.
  3. 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
    PyPDF2
    or
    python-docx
    ).
  • Email Styling: Use
    MIMEText
    with HTML for formatted email content.
  • Additional Automation: Extend functionality to upload summaries to a database or shared cloud storage.

 

caa January 26 2025 22 reads 0 comments Print

0 comments

Leave a Comment

Please Login to Post a Comment.
  • No Comments have been Posted.

Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 4
Members Online 0

Total Members: 14
Newest Member: Frank_nKansas