Infinitode Pty Ltd commited on
Commit
bd9041f
·
verified ·
1 Parent(s): 1e7c561

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -33
app.py CHANGED
@@ -9,11 +9,17 @@ from sklearn.linear_model import LogisticRegression
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)
@@ -23,45 +29,48 @@ def extract_features(password):
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
  )
 
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
+ except Exception as e:
18
+ print(f"Error loading model/vectorizer: {e}")
19
+ model = None
20
+ vectorizer = None
21
+
22
+ # Function to extract features
23
  def extract_features(password):
24
  features = {}
25
  features['length'] = len(password)
 
29
  features['special'] = sum(1 for c in password if not c.isalnum())
30
  return features
31
 
32
+ # Function to predict password strength
33
+ def predict_password_strength(password):
34
+ if not model or not vectorizer:
35
+ return ["", "", "Model or vectorizer not loaded correctly"]
 
 
 
36
 
37
+ try:
38
+ # Extract features from the input password
39
+ features = extract_features(password)
40
 
41
+ # Transform the input password using the trained vectorizer
42
+ password_vectorized = vectorizer.transform([password])
43
+ password_vectorized = hstack((password_vectorized, np.array(list(features.values())).reshape(1, -1)))
 
 
 
 
 
 
 
 
 
44
 
45
+ # Make a prediction using the trained model
46
+ prediction = model.predict(password_vectorized)[0]
47
+ if prediction == 0:
48
+ text = "Password is very weak."
49
+ elif prediction == 1:
50
+ text = "Password is weak."
51
+ elif prediction == 2:
52
+ text = "Password is average."
53
+ elif prediction == 3:
54
+ text = "Password is strong."
55
+ elif prediction == 4:
56
+ text = "Password is very strong."
57
+ else:
58
+ text = "Unknown strength level."
59
 
60
+ return [password, prediction, text]
61
  except Exception as e:
62
+ return [password, "", f"Error during prediction: {e}"]
63
 
64
+ # Gradio Interface
65
  demo = gr.Interface(
66
  fn=predict_password_strength,
67
+ inputs=gr.Textbox('Hello123', label='Password', info='The password to check the strength of', max_lines=1),
68
+ outputs=gr.Dataframe(
69
+ row_count=(1, "fixed"),
70
+ col_count=(3, "fixed"),
71
+ headers=["Password", "Prediction", "Strength_Text"],
72
+ label="Password Strength Analysis"
73
+ ),
 
 
74
  title='Helix - Password Strength Analyzer',
75
  description='A password strength analyzer, trained on over 5 million different passwords.'
76
  )