-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
122 lines (84 loc) · 3.71 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import streamlit as st
import torch
from transformers import BertTokenizer, BertForSequenceClassification
from tqdm import tqdm # Import tqdm for progress bar
import time # for time taken calc
# Load pre-trained model
label_dict = {"Urgency": 0, "Not Dark Pattern": 1, "Scarcity": 2, "Misdirection": 3, "Social Proof": 4, "Obstruction": 5, "Sneaking": 6, "Forced Action": 7}
model = BertForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=len(label_dict))
# Load fine-tuned weights
fine_tuned_model_path = "models/finetuned_BERT_5k_epoch_5.model"
model.load_state_dict(torch.load(fine_tuned_model_path, map_location=torch.device('cpu')))
# Preprocess the new text
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True)
# Function to map numeric label to dark pattern name
def get_dark_pattern_name(label):
reverse_label_dict = {v: k for k, v in label_dict.items()}
return reverse_label_dict[label]
def find_dark_pattern(text_predict):
encoded_text = tokenizer.encode_plus(
text_predict,
add_special_tokens=True,
return_attention_mask=True,
pad_to_max_length=True,
max_length=256,
return_tensors='pt'
)
# Making the predictions
model.eval()
with torch.no_grad():
inputs = {
'input_ids': encoded_text['input_ids'],
'attention_mask': encoded_text['attention_mask']
}
outputs = model(**inputs)
predictions = outputs.logits
# Post-process the predictions
probabilities = torch.nn.functional.softmax(predictions, dim=1)
predicted_label = torch.argmax(probabilities, dim=1).item()
return get_dark_pattern_name(predicted_label)
# Streamlit app
def main():
# navigation
st.page_link("app.py", label="Home", icon="🏠")
st.page_link("pages/page_1.py", label="Training Metrics", icon="1️⃣")
# st.page_link("pages/page_2.py", label="Page 2", icon="2️⃣")
st.page_link("https://github.com/4darsh-Dev/CogniGaurd", label="GitHub", icon="🌎")
# Set page title
st.title("Dark Pattern Detector")
# Display welcome message
st.write("Welcome to Dark Pattern Detector powered by CogniGuard")
#
st.write("#### Built with Fine-Tuned BERT and Hugging Face Transformers")
# Get user input
text_to_predict = st.text_input("Enter the text to find Dark Pattern")
if st.button("Predict"):
# Record the start time
start_time = time.time()
# Add a simple progress message
st.write("Predicting Dark Pattern...")
progress_bar = st.progress(0)
for i in tqdm(range(10), desc="Predicting", unit="prediction"):
predicted_darkp = find_dark_pattern(text_to_predict)
progress_bar.progress((i + 1) * 10)
time.sleep(0.5) # Simulate some processing time
# Record the end time
end_time = time.time()
# Calculate the total time taken
total_time = end_time - start_time
# Display the predicted dark pattern and total time taken
st.write(f"Result: {predicted_darkp}")
st.write(f"Total Time Taken: {total_time:.2f} seconds")
# Add footer
st.markdown('<p style="text-align:center;">Made with ❤️ by <a href="https://www.adarshmaurya.onionreads.com">Adarsh Maurya</a></p>', unsafe_allow_html=True)
# Add page visit count
with open("assets/counter.txt", "r") as f:
pVisit = int(f.read())
pVisit += 1
with open("assets/counter.txt", "w") as f:
f.write(str(pVisit))
# Display page visit count
st.markdown(f'<p style="text-align:center;">Page Visits: {pVisit}</p>', unsafe_allow_html=True)
# Run the app
if __name__ == "__main__":
main()