Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import time
- import requests
- import smtplib
- from email.message import EmailMessage
- # === Configuration ===
- # Folder to watch
- FOLDER = "./watch"
- # LLM settings
- API_KEY = ""
- API_URL = "http://localhost:8000/v1/chat/completions"
- MODEL = ""
- TEMPERATURE = 0.5
- SYSTEM_PROMPT = "You are a helpful assistant that summarizes documents clearly and concisely."
- # Email settings
- SMTP_SERVER = "smtp.example.com"
- SMTP_PORT = 587
- EMAIL_ADDRESS = "sender@example.com"
- EMAIL_PASSWORD = "email-password"
- SEND_TO = "recipient@example.com"
- # Internal tracking
- PROCESSED = set()
- # === Core Functions ===
- def summarize(content):
- headers = {"Content-Type": "application/json"}
- if API_KEY:
- headers["Authorization"] = f"Bearer {API_KEY}"
- payload = {
- "model": MODEL,
- "messages": [
- {"role": "system", "content": SYSTEM_PROMPT},
- {"role": "user", "content": f"Summarize this file:\n\n{content}"}
- ],
- "temperature": TEMPERATURE
- }
- response = requests.post(API_URL, headers=headers, json=payload)
- response.raise_for_status()
- return response.json()["choices"][0]["message"]["content"]
- def send_email(subject, body):
- msg = EmailMessage()
- msg["Subject"] = subject
- msg["From"] = EMAIL_ADDRESS
- msg["To"] = SEND_TO
- msg.set_content(body)
- with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as smtp:
- smtp.starttls()
- smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
- smtp.send_message(msg)
- print(f"Email sent to {SEND_TO}")
- def run_agent():
- os.makedirs(FOLDER, exist_ok=True)
- print(f"Watching folder: {FOLDER}")
- while True:
- files = [f for f in os.listdir(FOLDER) if f.endswith(".txt")]
- for filename in files:
- path = os.path.join(FOLDER, filename)
- if path in PROCESSED:
- continue
- with open(path, "r") as f:
- content = f.read()
- print(f"Processing: {filename}")
- try:
- summary = summarize(content)
- send_email(f"Summary of {filename}", summary)
- PROCESSED.add(path)
- except Exception as e:
- print(f"Error processing {filename}: {e}")
- time.sleep(5)
- if __name__ == "__main__":
- run_agent()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement