Spaces:
Sleeping
Sleeping
Infinitode Pty Ltd
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,69 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
|
|
|
|
3 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
4 |
-
from sklearn.
|
5 |
|
6 |
# Load model and vectorizer
|
7 |
try:
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
def
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
except Exception as e:
|
39 |
return [[f"Error during prediction: {e}", "", ""]]
|
40 |
|
41 |
demo = gr.Interface(
|
42 |
-
fn=
|
43 |
inputs=[gr.Textbox('Hello123', label='Password', info='The password to check the strength of', max_lines=1)],
|
44 |
outputs=[
|
45 |
gr.Dataframe(
|
46 |
row_count=(1, "fixed"),
|
47 |
col_count=(3, "fixed"),
|
48 |
-
headers=["Password", "Prediction", "
|
49 |
label="Password Strength Analysis"
|
50 |
)
|
51 |
],
|
52 |
title='Helix - Password Strength Analyzer',
|
53 |
-
description='A password strength analyzer, trained on over
|
54 |
)
|
55 |
|
56 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import pickle
|
3 |
+
import numpy as np'
|
4 |
+
from scipy.sparse import hstack
|
5 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
6 |
+
from sklearn.linear_model import LogisticRegression
|
7 |
|
8 |
# Load model and vectorizer
|
9 |
try:
|
10 |
+
# --- Load and inference code ---
|
11 |
+
with open('password_model.pkl', 'rb') as f:
|
12 |
+
model = pickle.load(f)
|
13 |
+
|
14 |
+
with open('password_vectorizer.pkl', 'rb') as f:
|
15 |
+
vectorizer = pickle.load(f)
|
16 |
+
|
17 |
+
def extract_features(password):
|
18 |
+
features = {}
|
19 |
+
features['length'] = len(password)
|
20 |
+
features['uppercase'] = sum(1 for c in password if c.isupper())
|
21 |
+
features['lowercase'] = sum(1 for c in password if c.islower())
|
22 |
+
features['digits'] = sum(1 for c in password if c.isdigit())
|
23 |
+
features['special'] = sum(1 for c in password if not c.isalnum())
|
24 |
+
return features
|
25 |
+
|
26 |
+
def predict_password_strength(password, vectorizer, model): # Add vectorizer and model as arguments
|
27 |
+
# Extract features from the input password
|
28 |
+
features = extract_features(password)
|
29 |
+
|
30 |
+
# Transform the input password using the trained vectorizer
|
31 |
+
password_vectorized = vectorizer.transform([password])
|
32 |
+
password_vectorized = hstack((password_vectorized, np.array(list(features.values())).reshape(1, -1)))
|
33 |
+
|
34 |
+
text="No password analyzed."
|
35 |
+
|
36 |
+
# Make a prediction using the trained model
|
37 |
+
prediction = model.predict(password_vectorized)[0]
|
38 |
+
if prediction === 0:
|
39 |
+
text = "Password is very weak."
|
40 |
+
elif prediction === 1:
|
41 |
+
text = "Password is weak."
|
42 |
+
elif prediction === 2:
|
43 |
+
text = "Password is average."
|
44 |
+
elif prediction === 3:
|
45 |
+
text = "Password is strong."
|
46 |
+
elif prediction === 4:
|
47 |
+
text = "Password is very strong."
|
48 |
+
|
49 |
+
return password, prediction, text
|
50 |
|
51 |
except Exception as e:
|
52 |
return [[f"Error during prediction: {e}", "", ""]]
|
53 |
|
54 |
demo = gr.Interface(
|
55 |
+
fn=predict_password_strength,
|
56 |
inputs=[gr.Textbox('Hello123', label='Password', info='The password to check the strength of', max_lines=1)],
|
57 |
outputs=[
|
58 |
gr.Dataframe(
|
59 |
row_count=(1, "fixed"),
|
60 |
col_count=(3, "fixed"),
|
61 |
+
headers=["Password", "Prediction", "Strength_Text"],
|
62 |
label="Password Strength Analysis"
|
63 |
)
|
64 |
],
|
65 |
title='Helix - Password Strength Analyzer',
|
66 |
+
description='A password strength analyzer, trained on over 5 million different passwords.'
|
67 |
)
|
68 |
|
69 |
demo.launch()
|