SlothSync:Hacktoberfest 第 3 週的本地優先 AI 檔案同步工具
SlothSync: A Local‑First AI‑Powered File Sync Tool for Hacktoberfest Week 3
SlothSync 是一款本地優先的 AI 檔案同步及備份工具,可監察目錄變更、生成摘要,並把檔案同步至指定備份位置。它透過 Ollama 在裝置上使用 gemma2:2b,事件經 5 s 防抖後批次處理,摘要寫入 .slothsync 日誌;資料不會上雲,亦毋須支付按請求計算的 API 費用。
Hacktoberfest Open-Source AI Challenge Week 3: Slow and Steady Submission 🦥
SlothSync
SlothSync is a lightweight, local‑first AI‑powered file synchronization and backup tool that helps you automatically keep your important documents in sync across multiple devices without ever sending your data to the cloud.
Why SlothSync?
-
Privacy first – All AI inference runs on‑device using an open‑weight model (e.g.,
gemma2:2bvia Ollama). No data ever leaves your machine. - Zero cost – No per‑request API fees. The only requirement is a local model runner.
- Save time – Detects new or changed files, generates concise change summaries, and can even suggest better folder structures.
Features
- Watch one or more directories for changes (create, modify, delete).
- Summarise changes with a local LLM and store the summary in a hidden
.slothsynclog. - Sync the changes to a user‑specified backup location (USB drive, external HDD, or another folder).
- Simple CLI with optional TUI (text UI) powered by
rich.
Quick Start
# Install Ollama and the Gemma 2B model (once)
curl -fsSL https://ollama.com/install.sh | sh
ollama pull gemma2:2b
# Clone the repository and install dependencies
git clone https://github.com/YourUsername/slothsync.git
cd slothsync
pip install -r requirements.txt
# Run SlothSync – watch ~/Documents and backup to /Volumes/BackupDrive
python3 slothsync.py --watch ~/Documents --backup /Volumes/BackupDrive
How It Works
-
File Watcher – Uses
watchdogto monitor filesystem events. - Change Collector – Accumulates a batch of events (debounced 5 s).
- LLM Summariser – Sends a concise description of the batch to the local Gemma model.
- Sync Engine – Copies changed files to the backup location while preserving timestamps.
-
Log – Stores the LLM summary in
.slothsync/logs/<timestamp>.md.
Architecture
flowchart TD
A[File System] -->|watchdog| B[Event Collector]
B --> C[Batch (5s)]
C --> D[Local Gemma Summariser]
D --> E[Change Summary]
C --> F[Sync Engine]
F --> G[Backup Destination]
E --> H[Log (.slothsync)]
style A fill:#f9f,stroke:#333,stroke-width:2px
style G fill:#bbf,stroke:#333,stroke-width:2pxCode (core script)
import argparse, shutil, time, os
from pathlib import Path
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class ChangeHandler(FileSystemEventHandler):
def __init__(self, batch, interval=5):
self.batch = batch
self.interval = interval
self.last_event = time.time()
def on_any_event(self, event):
now = time.time()
self.batch.append(event.src_path)
self.last_event = now
def summarise_changes(paths):
# Placeholder – in a real version we call local Gemma via Ollama
return f"Detected {len(paths)} file changes: " + ", ".join([os.path.basename(p) for p in paths[:5]])
def sync_files(paths, backup_dir):
for p in set(paths):
src = Path(p)
if src.is_file():
rel = src.relative_to(src.anchor)
dst = Path(backup_dir) / rel
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src, dst)
def main(watch_dir, backup_dir):
batch = []
handler = ChangeHandler(batch)
observer = Observer()
observer.schedule(handler, watch_dir, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
if batch and time.time() - handler.last_event > handler.interval:
summary = summarise_changes(batch)
log_dir = Path(watch_dir) / ".slothsync" / "logs"
log_dir.mkdir(parents=True, exist_ok=True)
(log_dir / f"{int(time.time())}.md").write_text(summary)
sync_files(batch, backup_dir)
batch.clear()
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
p = argparse.ArgumentParser()
p.add_argument("--watch", required=True)
p.add_argument("--backup", required=True)
args = p.parse_args()
main(args.watch, args.backup)
Why Open Innovation Matters
- Privacy – Freelancers often sign NDAs; a local‑first model ensures client data never leaves the machine.
- Cost – Running an open‑weight model locally eliminates per‑request API fees, keeping the tool free for anyone.
-
Extensibility – Users can swap the model (e.g.,
gemma2:9b) for more sophisticated summarisation without changing code.
Lazy Payoff
Instead of manually scanning folders and writing backup scripts, SlothSync automatically watches, summarises, and backs up – saving countless hours of repetitive work.
Prize Category
Best Use of Gemma – The core summarisation engine runs on the open‑weight Gemma model entirely offline.
This project was built for the **Hacktoberfest Open‑Source AI Challenge: Week 3 – Slow and Steady.
來源:DEV Community · dev.to