Infinitode Pty Ltd commited on
Commit
bc80375
·
verified ·
1 Parent(s): b784405

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -34
app.py CHANGED
@@ -1,56 +1,69 @@
1
  import gradio as gr
2
- import joblib
 
 
3
  from sklearn.feature_extraction.text import TfidfVectorizer
4
- from sklearn.calibration import CalibratedClassifierCV
5
 
6
  # Load model and vectorizer
7
  try:
8
- model = joblib.load("helix-psa.pkl")
9
- vectorizer = joblib.load("helix-psa-vectorizer.pkl")
10
- print("Model and vectorizer loaded successfully.")
11
- except Exception as e:
12
- print("Error loading model or vectorizer:", e)
13
- model, vectorizer = None, None # Assign None so we can check later
14
-
15
- def predictPasswordStrength(password_input):
16
- if model is None or vectorizer is None:
17
- return [["Error: Model or vectorizer not loaded correctly.", "", ""]]
18
-
19
- try:
20
- password_tfidf = vectorizer.transform([password_input])
21
-
22
- # Make predictions
23
- predicted_proba = model.predict_proba(password_tfidf)
24
- predicted_class = int(model.predict(password_tfidf)[0]) # Convert to Python integer
25
- output = ''
26
- if predicted_class == 0:
27
- output = "The password is very weak..."
28
- elif predicted_class == 1:
29
- output = "The password is average."
30
- else:
31
- output = "The password is strong. But alas, it is not unbreakable."
32
-
33
- confidence = float(predicted_proba.max())
34
-
35
- # Return as a list of lists for DataFrame
36
- return [[password_input, output, confidence]]
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  except Exception as e:
39
  return [[f"Error during prediction: {e}", "", ""]]
40
 
41
  demo = gr.Interface(
42
- fn=predictPasswordStrength,
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", "Confidence"],
49
  label="Password Strength Analysis"
50
  )
51
  ],
52
  title='Helix - Password Strength Analyzer',
53
- description='A password strength analyzer, trained on over 10 million different passwords.'
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()