Skip to content

Commit

Permalink
updated files
Browse files Browse the repository at this point in the history
  • Loading branch information
4darsh-Dev committed Feb 11, 2024
1 parent 7fb99d1 commit 59b2327
Show file tree
Hide file tree
Showing 23 changed files with 957 additions and 435 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.model !text !filter !merge !diff
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ share/
pyvenv.cfg
.env
fine_tuned_models/
fine_tuned_bert/
finetuned_BERT_epoch_1.model
finetuned_BERT_epoch_2.model
finetuned_BERT_epoch_3.model
finetuned_BERT_epoch_4.model
finetuned_BERT_epoch_5.model
22 changes: 18 additions & 4 deletions api/cogniguard/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@
'django.contrib.staticfiles',
]

# REST api settings configuration
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.AllowAny',
],
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
],
}


MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand All @@ -57,6 +68,10 @@
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

# Cors allowed origins
CORS_ALLOW_ALL_ORIGINS = True


# Written by admin
CSRF_HEADER_NAME = 'HTTP_X_CSRFTOKEN'

Expand All @@ -80,7 +95,6 @@

WSGI_APPLICATION = 'cogniguard.wsgi.application'

CORS_ALLOW_ALL_ORIGINS = True


# Database
Expand Down Expand Up @@ -118,9 +132,9 @@

LANGUAGE_CODE = 'en-us'

# CORS settings
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
# # CORS settings
# CORS_ORIGIN_ALLOW_ALL = True
# CORS_ALLOW_CREDENTIALS = True


TIME_ZONE = 'UTC'
Expand Down
9 changes: 7 additions & 2 deletions api/home/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from django.contrib import admin
from django.urls import path, include
from django.urls import path,include
from home import views

urlpatterns = [
path("", views.index, name="home"),
path("faqs/", views.faqs, name="faqs"),
path("popup_detect/", views.popup_detect, name="popup_detect"),
path("report-dp/", views.reportDp, name="report-dp"),
path("about/", views.about, name="about"),
path("terms-conditions/", views.termsConditions, name="terms-conditions"),
path("know-dp/", views.knowDp, name="knowDp"),

]



5 changes: 5 additions & 0 deletions api/home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def index(request):
def faqs(request):
return render(request, "faqs.html")

def termsConditions(request):
return render(request, "termsConditions.html")

def knowDp(request):
return redirect("/")

def popup_detect(request):
# img = data_url_to_image(request.GET.get('img', ''))
Expand Down
4 changes: 2 additions & 2 deletions api/mlApi/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib import admin
from .models import WebsiteTransparencyScore, DarkPatternsData
from .models import WebsiteTransparencyScore, DarkPatternsData,DpRequest

# Register your models here.
admin.site.register(WebsiteTransparencyScore)
admin.site.register(DarkPatternsData)

admin.site.register(DpRequest)
21 changes: 21 additions & 0 deletions api/mlApi/migrations/0003_dprequest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.0.1 on 2024-02-08 18:10

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('mlApi', '0002_darkpatternsdata'),
]

operations = [
migrations.CreateModel(
name='dpRequest',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('url', models.URLField()),
('create_at', models.DateTimeField(auto_now_add=True)),
],
),
]
18 changes: 18 additions & 0 deletions api/mlApi/migrations/0004_rename_create_at_dprequest_created_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.1 on 2024-02-09 06:14

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('mlApi', '0003_dprequest'),
]

operations = [
migrations.RenameField(
model_name='dprequest',
old_name='create_at',
new_name='created_at',
),
]
13 changes: 13 additions & 0 deletions api/mlApi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,16 @@ class DarkPatternsData(models.Model):
def __str__(self):
return f"{self.website_url} - Dark Pattern: {self.dark_pattern_label}"

<<<<<<< HEAD

class DpRequest(models.Model):
url = models.URLField()
created_at = models.DateTimeField(auto_now_add=True)


def __str__(self):
return f"{self.url } at {self.created_at}"


=======
>>>>>>> 7fb99d1719b011518f106d67414c183667809f90
28 changes: 28 additions & 0 deletions api/mlApi/predict_darkp.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,33 @@ def find_dark_pattern(text_predict):

return get_dark_pattern_name(predicted_label)

<<<<<<< HEAD
# # text_to_predict = "Hello my name is adarsh and my friend name is aman , he is only on sale for the next two days. "
# welcomeMsg = """******* Welcome to Dark Pattern Detector ********"""

# text_to_predict = input("Enter the the text to find Dark Pattern : ")
# # text_to_predict = "Hurry only 5 items left otherwise they will be sold out. "


# # Record the start time
# start_time = time.time()

# # Add a simple progress message
# print("Predicting Dark Pattern...")

# # Use tqdm to create a progress bar
# for _ in tqdm(range(10), desc="Predicting", unit="prediction"):
# predicted_darkp = find_dark_pattern(text_to_predict)

# # Record the end time
# end_time = time.time()

# # Calculate the total time taken
# total_time = end_time - start_time

# print(f"Predicted Dark Pattern: {predicted_darkp}")
# print(f"Total Time Taken: {total_time:.2f} seconds")
=======
# text_to_predict = "Hello my name is adarsh and my friend name is aman , he is only on sale for the next two days. "
welcomeMsg = """******* Welcome to Dark Pattern Detector ********"""

Expand All @@ -73,3 +100,4 @@ def find_dark_pattern(text_predict):

print(f"Predicted Dark Pattern: {predicted_darkp}")
print(f"Total Time Taken: {total_time:.2f} seconds")
>>>>>>> 7fb99d1719b011518f106d67414c183667809f90
9 changes: 9 additions & 0 deletions api/mlApi/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import serializers
from .models import DpRequest

class DpRequestSerializer(serializers.ModelSerializer):
class Meta:
model = DpRequest
fields = '__all__'


14 changes: 14 additions & 0 deletions api/mlApi/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
from django.contrib import admin
<<<<<<< HEAD
from django.urls import path, include
=======
from django.urls import path,include
>>>>>>> 7fb99d1719b011518f106d67414c183667809f90
from . import views
# from mlApi.views import csrf_view

urlpatterns = [
path("tp-score/", views.tpScore, name="tpScore"),
<<<<<<< HEAD
# path("dp-data/", views.dpData, name="dpData"),

# Implmentaion for the DRF views
path('dp-request/', views.MessageCreateView.as_view(), name='dp-request'),
path('dp-request-list/', views.MessageListView.as_view(), name='dp-request-list'),


=======
path("dp-data/", views.dpData, name="dpData"),
# path('api/csrf/', csrf_view, name='csrf_endpoint'),
path('csrf/', views.csrf_token_view, name='csrf_token_view'),
>>>>>>> 7fb99d1719b011518f106d67414c183667809f90

]
126 changes: 76 additions & 50 deletions api/mlApi/views.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
from django.shortcuts import render,redirect
from django.shortcuts import render,redirect, get_object_or_404
from django.http import JsonResponse, HttpResponse

# Create your views here.
from .models import WebsiteTransparencyScore, DarkPatternsData
from .models import WebsiteTransparencyScore, DarkPatternsData, DpRequest

from scraping.dp_scrape import get_scrape_data
from scraping.dp_scrape import get_scrape_data,dark_sentence_list

from django.middleware.csrf import get_token
# Setup for DRF views
from rest_framework import generics,permissions

# Import the urllib.parse library
from urllib.parse import urlparse
from .serializers import DpRequestSerializer

import pandas as pd
from .predict_darkp import find_dark_pattern

def csrf_token_view(request):
csrf_token = get_token(request)
return JsonResponse({'csrfToken': csrf_token})


def index(request):
return HttpResponse("Hello, world. You're at Django API Index.")
# For submitting the URL to the model
dpCond = False

class MessageCreateView(generics.CreateAPIView):
dpCond = True
queryset = DpRequest.objects.all()
serializer_class = DpRequestSerializer
permission_classes = [permissions.IsAuthenticated]
http_method_names = ['post']



def tpScore(request):
Expand All @@ -38,55 +42,77 @@ def transparencyCalc(request):

return tScore

def dpData(request):

print("I am executing! ")
# Extract the URL from the request
url = request.GET.get('url', None)
print(url)
# Checking for existing data and then saving the dark patterns data
def dpData(url):

if url is not None:
# Remove URL-encoded double quotes ("%22") from the beginning and end
url = urlparse(url).path
if url:
# Check if the URL exists in DarkPatternsData model
existing_data = DarkPatternsData.objects.filter(website_url=url).exists()

# Check if the scheme is missing and add a default scheme
parsed_url = urlparse(url)
if existing_data:
# If the URL already exists, do nothing
return "Data already exists in the model"

# Remove double quotes from the URL
url = url.strip('"')
else:
# Perform some operations
# For example, scrape data from the URL using get_scrape_data function
scrape_output, sentenceFile = dark_sentence_list(url)
print(scrape_output)

if not parsed_url.scheme:
url = url
newUrl = url
print(f"Final url {newUrl}")
# Opening the sentences file
df = pd.read_csv("sentences.csv")

# Process each sentence
for _, row in df.iterrows():
sentence = row['sentence']
processed_result = find_dark_pattern(sentence)
print(f"{sentence}: {processed_result}")
# Predicting the dark pattern

scraped_text = get_scrape_data(newUrl)


# Save the scraped data to DarkPatternsData model
# dark_patterns_data = DarkPatternsData.objects.create(
# website_url=url,
# dark_pattern_label="Label", # Replace with the appropriate label
# dark_text="Scraped data" # Replace with the scraped data
# )

return "Data saved to DarkPatternsData model"

else:
return "URL parameter is missing in the request"


# For viewing the list of URLs submitted
class MessageListView(generics.ListAPIView):
queryset = DpRequest.objects.all()
serializer_class = DpRequestSerializer
http_method_names = ['get']



# For viewing the details of a specific URL






## You need to ensure that it doesn't call the dpData function if the dpCond is False

# print(scraped_text)
# Fetching urls from model
dpUrls = DpRequest.objects.all()
urlLen = len(dpUrls)
myDpUrl = dpUrls[urlLen-1].url
print(dpUrls)
myOutput = dpData(myDpUrl)
print(myOutput)
dpCond = False


# return "done! succesfully scraping"

# Fine-tune BERT model and classify dark pattern
# predicted_labels = fine_tune_and_classify(scraped_text)

predicted_labels= {"dp_data": "This is the dark pattern data"}

# Save the dark pattern data in the DarkPatternData model
# website_url = url
# for label, dark_text in predicted_labels.items():
# dark_pattern, created = DarkPatternsData.objects.get_or_create(
# website_url=website_url,
# dark_pattern_label=label,
# defaults={'dark_text': dark_text}
# )

# # If the record already exists, update the dark_text field
# if not created:
# dark_pattern.dark_text = dark_text
# dark_pattern.save()

return JsonResponse(predicted_labels)

Binary file modified api/requirements.txt
Binary file not shown.
Loading

0 comments on commit 59b2327

Please sign in to comment.