Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion background.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,52 @@ function getRecommendationFromConfig(concern, config, userQA) {

return {
focusLabel: selected.focus_label || concern,
recommendation: selected.recommendation || "",
recommendation: selected.recommendations || [],
latestQuestion: userQA[userQA.length - 1] || ""
};
}



async function logInteraction(question, detected_concern) {
const entry = {
question,
detected_concern,
timestamp: new Date().toISOString()
};

const data = await chrome.storage.local.get(["interactions"]);
const interactions = data.interactions || [];

interactions.push(entry);

await chrome.storage.local.set({ interactions });
}


chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {

if (request.action === "logQA") {
(async () => {
const entry = {
question: request.userQA?.[0],
detected_concern: "general_privacy",
timestamp: new Date().toISOString()
};

const data = await chrome.storage.local.get(["interactions"]);
const interactions = data.interactions || [];

interactions.push(entry);

await chrome.storage.local.set({ interactions });

console.log("Logged interaction:", entry);
})();

return true;
}

if (request.action === "detectGDPR") {
detectGDPRFromIP();
sendResponse({ status: "started" });
Expand Down Expand Up @@ -183,6 +222,22 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
personalization = getRecommendationFromConfig(inferredConcern, config, userQA);
}


//ADDED
await chrome.storage.local.get(["eval_logs"]).then(async (data) => {
const logs = data.eval_logs || [];

logs.push({
question: request.userQA,
concern: inferredConcern,
recommendation: personalization?.recommendation,
focusLabel: personalization?.focusLabel,
timestamp: new Date().toISOString()
});

await chrome.storage.local.set({ eval_logs: logs });
});

let prompt = `
You are a legal AI assistant.

Expand Down
27 changes: 21 additions & 6 deletions display_logs.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import json
import os
from encryption import decrypt_file, encrypt_file

LOG_FILE = "user_interactions.json"

def display_interactions():
"""
Display all logged user interactions.
"""
if os.path.exists(LOG_FILE):
with open(LOG_FILE, "r") as file:
data = json.load(file)
try:
# Step 1: Decrypt file
decrypt_file(LOG_FILE)

# Step 2: Read JSON
with open(LOG_FILE, "r") as file:
data = json.load(file)

# Step 3: Print interactions
for interaction in data["interactions"]:
print(interaction)

# Step 4: Re-encrypt file
encrypt_file(LOG_FILE)

except Exception as e:
print(f"Error reading interactions: {e}")
else:
print("No interactions logged yet. Run initialize_schema() first.")
print("No interactions logged yet. Run initialize_schema() first.")


if __name__ == "__main__":
display_interactions()
Binary file removed encoders.joblib
Binary file not shown.
4 changes: 2 additions & 2 deletions encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def encrypt_file(file_path):
file.write(encrypted_data)
print(f"File '{file_path}' encrypted successfully.")

# Decrypt a file
# Decrypt a file
def decrypt_file(file_path):
cipher = load_key()
if os.path.exists(file_path):
Expand All @@ -42,4 +42,4 @@ def decrypt_file(file_path):
# If decryption fails, assume the file is already in plaintext
print(f"File '{file_path}' is not encrypted or decryption failed: {e}")
else:
print(f"File '{file_path}' does not exist. Cannot decrypt.")
print(f"File '{file_path}' does not exist. Cannot decrypt.")
94 changes: 74 additions & 20 deletions logger.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,84 @@
import json
import os # Import the os module
import os
from datetime import datetime
from encryption import decrypt_file, encrypt_file

LOG_FILE = "user_interactions.json"

def log_interaction(policy_id, action, setting_changed=None, previous_value=None, new_value=None, context=None):
def log_interaction(question, detected_concern):
"""
Log a user interaction with the privacy policy.
Log a user Q&A interaction (Option A).
"""
log_entry = {
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"policy_id": policy_id,
"action": action,
"setting_changed": setting_changed,
"previous_value": previous_value,
"new_value": new_value,
"context": context
}

# Append the log entry to the JSON file
if os.path.exists(LOG_FILE):
with open(LOG_FILE, "r+") as file:

if not os.path.exists(LOG_FILE):
print("Log file not found. Initialize it first.")
return

try:
# Step 1: Decrypt file (this modifies file in place)
decrypt_file(LOG_FILE)

# Step 2: Load JSON data
with open(LOG_FILE, "r") as file:
data = json.load(file)
data["interactions"].append(log_entry)
file.seek(0)

# Step 3: Create new entry
new_entry = {
"question": question,
"detected_concern": detected_concern,
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}

# Step 4: Append
data["interactions"].append(new_entry)

# Step 5: Save updated JSON
with open(LOG_FILE, "w") as file:
json.dump(data, file, indent=4)

# Step 6: Re-encrypt
encrypt_file(LOG_FILE)

print("Interaction logged successfully.")
else:
print("Schema not initialized. Run initialize_schema() first.")

except Exception as e:
print(f"Logging error: {e}")


# Optional: test it manually
if __name__ == "__main__":
log_interaction(
"How can I protect my account?",
"account_security"
)

# Decrypt, load, append, and re-encrypt the file
# try:
# if os.path.exists(LOG_FILE):
# decrypted_data = decrypt_file(LOG_FILE)
# data = json.loads(decrypted_data)
# else:
# print("Schema not initialized. Run initialize_schema() first.")
# return

# data["interactions"].append(log_entry)

# # Re-encrypt and save
# encrypted_data = encrypt_file(json.dumps(data, indent=4))
# with open(LOG_FILE, "wb") as file: # Write bytes for encrypted data
# file.write(encrypted_data)

# print("Interaction logged and encrypted successfully.")
# except Exception as e:
# print(f"Logging error: {e}")

# Append the log entry to the JSON file
#if os.path.exists(LOG_FILE):
#with open(LOG_FILE, "r+") as file:
#data = json.load(file)
#data["interactions"].append(log_entry)
#file.seek(0)
#json.dump(data, file, indent=4)
#print("Interaction logged successfully.")
#else:
#print("Schema not initialized. Run initialize_schema() first.")
63 changes: 37 additions & 26 deletions model_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,54 @@
"n_estimators": 50
},
"scores": {
"accuracy": 1.0,
"precision_macro": 1.0,
"recall_macro": 1.0,
"f1_macro": 1.0
"accuracy": 0.1,
"precision_macro": 0.08333333333333333,
"recall_macro": 0.08333333333333333,
"f1_macro": 0.08333333333333333,
"precision_at_1": 1.0,
"precision_at_3": 0.3333333333333333,
"precision_at_5": 0.25
},
"classification_report": {
"0": {
"precision": 1.0,
"recall": 1.0,
"f1-score": 1.0,
"support": 2.0
"precision": 0.3333333333333333,
"recall": 0.3333333333333333,
"f1-score": 0.3333333333333333,
"support": 3.0
},
"1": {
"precision": 1.0,
"recall": 1.0,
"f1-score": 1.0,
"support": 2.0
"precision": 0.0,
"recall": 0.0,
"f1-score": 0.0,
"support": 3.0
},
"accuracy": 1.0,
"2": {
"precision": 0.0,
"recall": 0.0,
"f1-score": 0.0,
"support": 3.0
},
"3": {
"precision": 0.0,
"recall": 0.0,
"f1-score": 0.0,
"support": 1.0
},
"accuracy": 0.1,
"macro avg": {
"precision": 1.0,
"recall": 1.0,
"f1-score": 1.0,
"support": 4.0
"precision": 0.08333333333333333,
"recall": 0.08333333333333333,
"f1-score": 0.08333333333333333,
"support": 10.0
},
"weighted avg": {
"precision": 1.0,
"recall": 1.0,
"f1-score": 1.0,
"support": 4.0
"precision": 0.1,
"recall": 0.1,
"f1-score": 0.1,
"support": 10.0
}
},
"features": [
"policy_id",
"action",
"previous_value",
"new_value",
"context"
"question"
]
}
Loading