Here's a high-level design for the agentic flow you described:
Agent 1: Chapter Reader and Prompt Generator
- Reads chapters of a book (e.g., using a PDF parser or an e-book API)
- Extracts relevant information from each chapter (e.g., key points, summaries, quotes)
- Creates prompts for video creation based on the extracted information (e.g., "Create a 2-minute video summarizing Chapter 1: Introduction")
- Stores the prompts in a database or a message queue (e.g., RabbitMQ, Apache Kafka)
Agent 2: Video Generator
- Consumes the prompts from the database or message queue
- Uses AI video generation tools (e.g., Lumen5, Raw Shorts, or custom-built solutions using libraries like OpenCV, PyTorch) to create videos based on the prompts
- Stores the generated videos in an Amazon S3 bucket
- Updates the database or message queue with the video metadata (e.g., video ID, chapter number, video URL)
Agent 3: Notification Agent
- Monitors the database or message queue for new video metadata
- Sends an email notification to the user when a new video is generated for a particular chapter
- Includes the video URL or a link to the S3 bucket in the email notification
Here's a possible implementation using Python and various libraries:
Agent 1: Chapter Reader and Prompt Generator
import PyPDF2
import sqlite3
# Read chapters from a PDF book
def read_chapters(book_path):
pdf = PyPDF2.PdfFileReader(book_path)
chapters = []
for page in range(pdf.numPages):
text = pdf.getPage(page).extractText()
chapters.append(text)
return chapters
# Create prompts for video creation
def create_prompts(chapters):
prompts = []
for chapter in chapters:
prompt = f"Create a 2-minute video summarizing Chapter {chapters.index(chapter) + 1}: {chapter[:50]}"
prompts.append(prompt)
return prompts
# Store prompts in a SQLite database
def store_prompts(prompts):
conn = sqlite3.connect("prompts.db")
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS prompts (id INTEGER PRIMARY KEY, prompt TEXT)")
for prompt in prompts:
c.execute("INSERT INTO prompts (prompt) VALUES (?)", (prompt,))
conn.commit()
conn.close()
# Run the agent
book_path = "path/to/book.pdf"
chapters = read_chapters(book_path)
prompts = create_prompts(chapters)
store_prompts(prompts)
Agent 2: Video Generator
import os
import boto3
from lumen5 import Lumen5
# Consume prompts from the database
def get_prompts():
conn = sqlite3.connect("prompts.db")
c = conn.cursor()
c.execute("SELECT * FROM prompts")
prompts = c.fetchall()
conn.close()
return prompts
# Create videos using Lumen5
def create_videos(prompts):
lumen5 = Lumen5()
for prompt in prompts:
video = lumen5.create_video(prompt[1])
video_id = video["id"]
chapter_number = prompt[1].split(":")[0].split()[-1]
video_url = f"https://s3.amazonaws.com/your-bucket-name/{video_id}.mp4"
# Store video metadata in the database
store_video_metadata(video_id, chapter_number, video_url)
# Store video metadata in the database
def store_video_metadata(video_id, chapter_number, video_url):
conn = sqlite3.connect("videos.db")
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS videos (id INTEGER PRIMARY KEY, video_id TEXT, chapter_number INTEGER, video_url TEXT)")
c.execute("INSERT INTO videos (video_id, chapter_number, video_url) VALUES (?, ?, ?)", (video_id, chapter_number, video_url))
conn.commit()
conn.close()
# Upload video to S3
def upload_video_to_s3(video_id, video_file):
s3 = boto3.client("s3")
s3.upload_file(video_file, "your-bucket-name", f"{video_id}.mp4")
# Run the agent
prompts = get_prompts()
create_videos(prompts)
Agent 3: Notification Agent
import smtplib
from email.mime.text import MIMEText
import sqlite3
# Monitor the database for new video metadata
def get_new_videos():
conn = sqlite3.connect("videos.db")
c = conn.cursor()
c.execute("SELECT * FROM videos WHERE notified = 0")
new_videos = c.fetchall()
conn.close()
return new_videos
# Send email notification
def send_notification(video):
msg = MIMEText(f"Video generated for Chapter {video[2]}: {video[3]}")
msg["Subject"] = "New Video Available"
msg["From"] = "your-email@example.com"
msg["To"] = "user-email@example.com"
server = smtplib.SMTP("your-smtp-server")
server.sendmail("your-email@example.com", "user-email@example.com", msg.as_string())
server.quit()
# Update the database to mark the video as notified
def update_video_notified(video_id):
conn = sqlite3.connect("videos.db")
c = conn.cursor()
c.execute("UPDATE videos SET notified = 1 WHERE id = ?", (video_id,))
conn.commit()
conn.close()
# Run the agent
new_videos = get_new_videos()
for video in new_videos:
send_notification(video)
update_video_notified(video[0])
Note that this is a simplified example and you'll need to modify the code to fit your specific requirements. Additionally, you'll need to set up the necessary infrastructure, such as the database, S3 bucket, and email server.