|
@@ -0,0 +1,76 @@
|
|
|
+#!/bin/bash
|
|
|
+
|
|
|
+# Error handling
|
|
|
+set -eo pipefail
|
|
|
+trap 'echo "❌ Error occurred on line $LINENO. Exiting..."; exit 1' ERR
|
|
|
+
|
|
|
+# Function to display error messages
|
|
|
+error_msg() {
|
|
|
+ echo "❌ Error: $1" >&2
|
|
|
+ exit 1
|
|
|
+}
|
|
|
+
|
|
|
+# Check if input file is provided
|
|
|
+if [ $# -ne 1 ]; then
|
|
|
+ error_msg "Usage: $0 <csv_file>"
|
|
|
+fi
|
|
|
+
|
|
|
+INPUT_FILE="$1"
|
|
|
+
|
|
|
+# Validate input file exists and is readable
|
|
|
+[ ! -f "$INPUT_FILE" ] && error_msg "Input file '$INPUT_FILE' does not exist!"
|
|
|
+[ ! -r "$INPUT_FILE" ] && error_msg "Input file '$INPUT_FILE' is not readable!"
|
|
|
+
|
|
|
+# Create temporary file for processing
|
|
|
+TEMP_FILE=$(mktemp)
|
|
|
+trap 'rm -f "$TEMP_FILE"' EXIT
|
|
|
+
|
|
|
+# Check if ollama is installed
|
|
|
+command -v ollama >/dev/null 2>&1 || error_msg "ollama is not installed!"
|
|
|
+
|
|
|
+# Process the CSV file
|
|
|
+while IFS=, read -r chargeName remainder || [ -n "$chargeName" ]; do
|
|
|
+ # Skip empty lines
|
|
|
+ [ -z "$chargeName" ] && continue
|
|
|
+
|
|
|
+ # Remove any trailing/leading whitespace
|
|
|
+ chargeName=$(echo "$chargeName" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
|
|
|
+
|
|
|
+ # Skip if first column is empty
|
|
|
+ [ -z "$chargeName" ] && continue
|
|
|
+
|
|
|
+ echo "🔄 Processing charge: $chargeName"
|
|
|
+
|
|
|
+ # Call ollama and capture output, then remove commas
|
|
|
+ aiDesc=$(printf '%s' "$chargeName" | ollama run granite3.1-dense:latest "interpret what this credit card charge description might be in simple, plain English. Use no more than three sentences: $chargeName" 2>/dev/null | tr -d ',' || echo "AI processing failed")
|
|
|
+
|
|
|
+ # If AI processing failed, add error message
|
|
|
+ if [ "$aiDesc" = "AI processing failed" ]; then
|
|
|
+ echo "⚠️ Warning: Failed to process '$chargeName'"
|
|
|
+ fi
|
|
|
+
|
|
|
+ # Properly escape the AI description for sed
|
|
|
+ aiDesc_escaped=$(printf '%s\n' "$aiDesc" | sed 's/[\/&]/\\&/g')
|
|
|
+
|
|
|
+ # Write the processed line to temp file
|
|
|
+ printf '%s,%s\n' "$chargeName" "$aiDesc" >> "$TEMP_FILE"
|
|
|
+
|
|
|
+ echo "✅ Processed entry successfully"
|
|
|
+done < "$INPUT_FILE"
|
|
|
+
|
|
|
+# Move temp file to original file
|
|
|
+mv "$TEMP_FILE" "$INPUT_FILE"
|
|
|
+
|
|
|
+# **Apply regex fix to remove stray newline followed by a double quote**
|
|
|
+# _Using perl for reliable multiline regex handling_
|
|
|
+perl -i -0777 -pe 's/\n"/,\n/g' "$INPUT_FILE"
|
|
|
+
|
|
|
+# Display success message with some flair
|
|
|
+cat << "EOF"
|
|
|
+✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
|
|
|
+🎉 CSV Processing Complete! 🎉
|
|
|
+✨ All entries have been processed
|
|
|
+🔍 AI descriptions have been added
|
|
|
+💾 File has been updated successfully
|
|
|
+✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨
|
|
|
+EOF
|