adarsh
commited on
Commit
·
46c79fb
1
Parent(s):
d571561
init for app
Browse files- README.md +2 -13
- app.py +117 -0
- pages/page_1.py +10 -0
- pages/page_2.py +0 -0
- requirements.txt +70 -0
README.md
CHANGED
@@ -1,13 +1,2 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
emoji: 😻
|
4 |
-
colorFrom: gray
|
5 |
-
colorTo: indigo
|
6 |
-
sdk: streamlit
|
7 |
-
sdk_version: 1.33.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
license: mit
|
11 |
-
---
|
12 |
-
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
### Dark Pattern Detection using Fine Tuned BERT Model
|
2 |
+
A web app built using streamlit powered by CogniGuard project
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
4 |
+
|
5 |
+
from tqdm import tqdm # Import tqdm for progress bar
|
6 |
+
|
7 |
+
import time # for time taken calc
|
8 |
+
|
9 |
+
# Load pre-trained model
|
10 |
+
label_dict = {"Urgency": 0, "Not Dark Pattern": 1, "Scarcity": 2, "Misdirection": 3, "Social Proof": 4, "Obstruction": 5, "Sneaking": 6, "Forced Action": 7}
|
11 |
+
model = BertForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=len(label_dict))
|
12 |
+
|
13 |
+
# Load fine-tuned weights
|
14 |
+
fine_tuned_model_path = r"models\finetuned_BERT_epoch_5.model"
|
15 |
+
model.load_state_dict(torch.load(fine_tuned_model_path, map_location=torch.device('cpu')))
|
16 |
+
|
17 |
+
# Preprocess the new text
|
18 |
+
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True)
|
19 |
+
|
20 |
+
# Function to map numeric label to dark pattern name
|
21 |
+
def get_dark_pattern_name(label):
|
22 |
+
reverse_label_dict = {v: k for k, v in label_dict.items()}
|
23 |
+
return reverse_label_dict[label]
|
24 |
+
|
25 |
+
def find_dark_pattern(text_predict):
|
26 |
+
encoded_text = tokenizer.encode_plus(
|
27 |
+
text_predict,
|
28 |
+
add_special_tokens=True,
|
29 |
+
return_attention_mask=True,
|
30 |
+
pad_to_max_length=True,
|
31 |
+
max_length=256,
|
32 |
+
return_tensors='pt'
|
33 |
+
)
|
34 |
+
|
35 |
+
# Making the predictions
|
36 |
+
model.eval()
|
37 |
+
|
38 |
+
with torch.no_grad():
|
39 |
+
inputs = {
|
40 |
+
'input_ids': encoded_text['input_ids'],
|
41 |
+
'attention_mask': encoded_text['attention_mask']
|
42 |
+
}
|
43 |
+
outputs = model(**inputs)
|
44 |
+
|
45 |
+
predictions = outputs.logits
|
46 |
+
|
47 |
+
# Post-process the predictions
|
48 |
+
probabilities = torch.nn.functional.softmax(predictions, dim=1)
|
49 |
+
predicted_label = torch.argmax(probabilities, dim=1).item()
|
50 |
+
|
51 |
+
return get_dark_pattern_name(predicted_label)
|
52 |
+
|
53 |
+
# Streamlit app
|
54 |
+
def main():
|
55 |
+
|
56 |
+
# navigation
|
57 |
+
st.page_link("app.py", label="Home", icon="🏠")
|
58 |
+
st.page_link("pages/page_1.py", label="Training Metrics", icon="1️⃣")
|
59 |
+
# st.page_link("pages/page_2.py", label="Page 2", icon="2️⃣")
|
60 |
+
st.page_link("https://github.com/4darsh-Dev/CogniGaurd", label="GitHub", icon="🌎")
|
61 |
+
# Set page title
|
62 |
+
st.title("Dark Pattern Detector")
|
63 |
+
|
64 |
+
# Display welcome message
|
65 |
+
st.write("Welcome to Dark Pattern Detector powered by CogniGuard")
|
66 |
+
|
67 |
+
#
|
68 |
+
st.write("#### Built with Fine-Tuned BERT and Hugging Face Transformers")
|
69 |
+
|
70 |
+
|
71 |
+
# Get user input
|
72 |
+
text_to_predict = st.text_input("Enter the text to find Dark Pattern")
|
73 |
+
|
74 |
+
if st.button("Predict"):
|
75 |
+
# Record the start time
|
76 |
+
start_time = time.time()
|
77 |
+
|
78 |
+
# Add a simple progress message
|
79 |
+
st.write("Predicting Dark Pattern...")
|
80 |
+
|
81 |
+
|
82 |
+
progress_bar = st.progress(0)
|
83 |
+
|
84 |
+
for i in tqdm(range(10), desc="Predicting", unit="prediction"):
|
85 |
+
predicted_darkp = find_dark_pattern(text_to_predict)
|
86 |
+
progress_bar.progress((i + 1) * 10)
|
87 |
+
time.sleep(0.5) # Simulate some processing time
|
88 |
+
|
89 |
+
# Record the end time
|
90 |
+
end_time = time.time()
|
91 |
+
|
92 |
+
# Calculate the total time taken
|
93 |
+
total_time = end_time - start_time
|
94 |
+
|
95 |
+
# Display the predicted dark pattern and total time taken
|
96 |
+
st.write(f"Result: {predicted_darkp}")
|
97 |
+
st.write(f"Total Time Taken: {total_time:.2f} seconds")
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
|
102 |
+
# Add footer
|
103 |
+
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)
|
104 |
+
|
105 |
+
# Add page visit count
|
106 |
+
page_visits = st.session_state.get('page_visits', 0)
|
107 |
+
page_visits += 1
|
108 |
+
st.session_state['page_visits'] = page_visits
|
109 |
+
|
110 |
+
# Display page visit count
|
111 |
+
st.markdown(f'<p style="text-align:center;">Page Visits: {page_visits}</p>', unsafe_allow_html=True)
|
112 |
+
|
113 |
+
|
114 |
+
# Run the app
|
115 |
+
if __name__ == "__main__":
|
116 |
+
main()
|
117 |
+
|
pages/page_1.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
def main():
|
4 |
+
st.title("Training Metrics")
|
5 |
+
|
6 |
+
|
7 |
+
if __name__ == "__main__":
|
8 |
+
main()
|
9 |
+
|
10 |
+
|
pages/page_2.py
ADDED
File without changes
|
requirements.txt
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiohttp==3.9.3
|
2 |
+
aiosignal==1.3.1
|
3 |
+
altair==5.3.0
|
4 |
+
asgiref==3.8.1
|
5 |
+
attrs==23.2.0
|
6 |
+
blinker==1.8.1
|
7 |
+
cachetools==5.3.3
|
8 |
+
certifi==2024.2.2
|
9 |
+
charset-normalizer==3.3.2
|
10 |
+
click==8.1.7
|
11 |
+
colorama==0.4.6
|
12 |
+
datasets==2.18.0
|
13 |
+
dill==0.3.8
|
14 |
+
Django==5.0.3
|
15 |
+
filelock==3.13.4
|
16 |
+
frozenlist==1.4.1
|
17 |
+
fsspec==2024.2.0
|
18 |
+
gitdb==4.0.11
|
19 |
+
GitPython==3.1.43
|
20 |
+
huggingface-hub==0.22.2
|
21 |
+
idna==3.6
|
22 |
+
Jinja2==3.1.3
|
23 |
+
jsonschema==4.21.1
|
24 |
+
jsonschema-specifications==2023.12.1
|
25 |
+
markdown-it-py==3.0.0
|
26 |
+
MarkupSafe==2.1.5
|
27 |
+
mdurl==0.1.2
|
28 |
+
mpmath==1.3.0
|
29 |
+
multidict==6.0.5
|
30 |
+
multiprocess==0.70.16
|
31 |
+
mysqlclient==2.2.4
|
32 |
+
networkx==3.3
|
33 |
+
numpy==1.26.4
|
34 |
+
packaging==24.0
|
35 |
+
pandas==2.2.1
|
36 |
+
pillow==10.3.0
|
37 |
+
protobuf==4.25.3
|
38 |
+
pyarrow==15.0.2
|
39 |
+
pyarrow-hotfix==0.6
|
40 |
+
pydeck==0.9.0b1
|
41 |
+
Pygments==2.17.2
|
42 |
+
python-dateutil==2.9.0.post0
|
43 |
+
python-dotenv==1.0.1
|
44 |
+
pytz==2024.1
|
45 |
+
PyYAML==6.0.1
|
46 |
+
referencing==0.35.0
|
47 |
+
regex==2024.4.16
|
48 |
+
requests==2.31.0
|
49 |
+
rich==13.7.1
|
50 |
+
rpds-py==0.18.0
|
51 |
+
safetensors==0.4.3
|
52 |
+
six==1.16.0
|
53 |
+
smmap==5.0.1
|
54 |
+
sqlparse==0.4.4
|
55 |
+
streamlit==1.33.0
|
56 |
+
sympy==1.12
|
57 |
+
tenacity==8.2.3
|
58 |
+
tokenizers==0.19.1
|
59 |
+
toml==0.10.2
|
60 |
+
toolz==0.12.1
|
61 |
+
torch==2.2.2
|
62 |
+
tornado==6.4
|
63 |
+
tqdm==4.66.2
|
64 |
+
transformers==4.40.1
|
65 |
+
typing_extensions==4.11.0
|
66 |
+
tzdata==2024.1
|
67 |
+
urllib3==2.2.1
|
68 |
+
watchdog==4.0.0
|
69 |
+
xxhash==3.4.1
|
70 |
+
yarl==1.9.4
|