Skip to content

Commit

Permalink
website: Use pyscript fetch instead of js fetch
Browse files Browse the repository at this point in the history
* Also add some CSS styling
  • Loading branch information
tedgravlin committed Dec 2, 2023
1 parent 98de729 commit 98b33e3
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 49 deletions.
1 change: 0 additions & 1 deletion .gitattributes

This file was deleted.

7 changes: 5 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

<head>
<title>Domain Validator</title>
<!-- Import CSS -->
<link rel="stylesheet" href="main.css">
<!-- Import pyscript -->
<script type="module" src="https://pyscript.net/releases/2023.11.2/core.js"></script>
</head>

Expand All @@ -13,9 +16,9 @@ <h1>Neural Network Domain Validator</h1>
<label for="url-input">Website URL</label>
<input type="text" id="url-input" placeholder="Website URL">
<button py-click="get_url">Check URL</button>
<p id="progress-text"></p>
<p id="progress-text">Loading pyscript. Please wait...</p>
<p id="result-text"></p>
<script type="py" src="main.py" config="./pyscript.json"></script>
<script type="py" src="./main.py" config="./pyscript.json"></script>
</body>

</html>
3 changes: 3 additions & 0 deletions docs/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
text-align: center;
}
104 changes: 60 additions & 44 deletions docs/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pandas as pd
import joblib
from pyscript import document
from pyscript import display
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
Expand All @@ -9,72 +10,86 @@
from sklearn.preprocessing import StandardScaler
from scipy.sparse import hstack
from js import XMLHttpRequest
from js import window
#from js import window
import io
from os.path import exists
import os
import re
#import re
import csv

# Get the progress text HTML element
progress_text = document.querySelector("#progress-text")
progress_text.innerText = "Pyscipt loaded."

# Gets the model and returns it
def load_model():

progress_text.innerText = "Removing old model..."

# Delete the model files if they already exist
if (exists('model.pkl')):
os.remove('model.pkl')
if (exists('tfidf.pkl')):
os.remove('tfidf.pkl')

# Check if the current domain is an IP address (hosted locally) or a real domain (hosted on GH pages)
if (re.search("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$", window.location.hostname)):
model_url = 'http://127.0.0.1:5500/models/model.pkl'
tfidf_url = 'http://127.0.0.1:5500/models/tfidf.pkl'
else:
model_url = 'https://github.com/tedgravlin/neural-network-domain-validator/blob/a792b3c04d16be88eca95f3d6948d25f020722e2/models/model.pkl'
tfidf_url = 'https://github.com/tedgravlin/neural-network-domain-validator/blob/a792b3c04d16be88eca95f3d6948d25f020722e2/models/tfidf.pkl'
# if (exists('model.pkl')):
# os.remove('model.pkl')
# if (exists('tfidf.pkl')):
# os.remove('tfidf.pkl')

# model_url = 'http://127.0.0.1:5500/models/model.pkl'
# tfidf_url = 'http://127.0.0.1:5500/models/tfidf.pkl'

progress_text.innerText = "Fetching model.pkl..."
# progress_text.innerText = "Fetching model.pkl..."

# # Get model.pkl
# model_req = XMLHttpRequest.new()
# model_req.open("GET", model_url, False)
# model_req.send(None)
# model_response = model_req.response.encode()

# Get model.pkl
model_req = XMLHttpRequest.new()
model_req.open("GET", model_url, False)
model_req.send(None)
model_response = (model_req.response)
# progress_text.innerText = "Writing to model.pkl..."

progress_text.innerText = "Writing to model.pkl..."
# # Write the contents of the model response to model.pkl
# with open('model.pkl', 'wb') as model_file:
# model_file.write(model_response)

# Write the contents of the model response to model.pkl
with open('model.pkl', 'w') as model_file:
model_file.write(model_response)
# progress_text.innerText = "Fetching tfidf.pkl..."

progress_text.innerText = "Fetching tfidf.pkl..."
# # Get tfidfk.pkl
# tfidf_req = XMLHttpRequest.new()
# tfidf_req.open("GET", tfidf_url, False)
# tfidf_req.send(None)
# tfidf_response = tfidf_req.response.encode()

# Get tfidfk.pkl
tfidf_req = XMLHttpRequest.new()
tfidf_req.open("GET", tfidf_url, False)
tfidf_req.send(None)
tfidf_response = (tfidf_req.response)
# progress_text.innerText = "Writing to tfidf.pkl..."

progress_text.innerText = "Writing to tfidf.pkl..."
# # Write the contents of the tfidf response to tfidf.pkl
# with open('tfidf.pkl', 'wb') as tfidf_file:
# tfidf_file.write(tfidf_response)

# Write the contents of the tfidf response to tfidf.pkl
with open('tfidf.pkl', 'w') as tfidf_file:
tfidf_file.write(tfidf_response)
display("model file size", os.path.getsize("model.pkl"))
display("tfidf file size", os.path.getsize("tfidf.pkl"))

#model = joblib.load(model_file)
#tfidf = joblib.load(tfidf_file)
model = joblib.load('model.pkl')
tfidf = joblib.load('tfidf.pkl')

progress_text.innerText = "Model load complete."

return exists('model.pkl'), exists('tfidf.pkl')
return model, tfidf

def test_model(model, tfidf):
# Delete the test dataset if it already exists
if (exists('testdataset.csv')):
os.remove('testdataset.csv')

# Fetch testdataset.csv
req = XMLHttpRequest.new()
req.open("GET", 'http://127.0.0.1:5500/dataset/testdataset.csv',True)
req.send(None)
response = (req.response)

# Write the contents of the testdataset response to testdataset.csv
with open('testdataset.csv', 'w') as testdataset_file:
testdataset_file.write(response)

# Get the test dataset CSV file
test_dataset = pd.read_csv("./dataset/testdataset.csv")
test_dataset = pd.read_csv("testdataset.csv")

# Turn the test dataset into a pandas data frame
dataframe = pd.DataFrame(test_dataset)
Expand Down Expand Up @@ -107,18 +122,19 @@ def test_model(model, tfidf):
if (prediction[label] == y.to_list()[label]):
correct_count = correct_count + 1
count = count + 1
print("ACTUAL:", y.to_list())
print("CORRECT PREDICTIONS:", correct_count,"/",count)
print("ACCURACY OF THIS TEST:", (correct_count/count) * 100)
display("ACTUAL:", y.to_list())
display("CORRECT PREDICTIONS:", correct_count,"/",count)
display("ACCURACY OF THIS TEST:", (correct_count/count) * 100)

def get_url(event):
# Load the model from storage
model = load_model()
#test_model(model, tfidf)
model, tfidf = load_model()
# Test the model with the test dataset
test_model(model, tfidf)
input_text = document.querySelector("#url-input")
url = input_text.value
output_text = document.querySelector("#result-text")
output_text.innerText = model
#output_text.innerText = model



Expand Down
12 changes: 10 additions & 2 deletions docs/pyscript.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"packages": ["pandas", "joblib", "scikit-learn"]
}
"packages": [
"pandas",
"joblib",
"scikit-learn"
],
"files": {
"../models/model.pkl": "model.pkl",
"../models/tfidf.pkl": "tfidf.pkl"
}

}

0 comments on commit 98b33e3

Please sign in to comment.