SilverDragon9 commited on
Commit
a5404f3
·
verified ·
1 Parent(s): 41e37db

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import joblib
4
+ import gradio as gr
5
+ import os
6
+ import tempfile
7
+
8
+ # Set a custom directory for Gradio's temporary files
9
+ os.environ["GRADIO_TEMP"] = tempfile.mkdtemp()
10
+
11
+ # Load model
12
+ model = joblib.load("password_checker_model.pkl")
13
+
14
+ # Mapping of numerical predictions to descriptive labels
15
+ STRENGTH_MAPPING = {
16
+ 0: "Weak",
17
+ 1: "Fairly Strong",
18
+ 2: "Strong"
19
+ }
20
+
21
+ def extract_password_features(password):
22
+ """Extract features from the password for model prediction."""
23
+ features = {
24
+ 'length': len(password),
25
+ 'has upper': int(any(c.isupper() for c in password)),
26
+ 'has lower': int(any(c.islower() for c in password)),
27
+ 'has digit': int(any(c.isdigit() for c in password)),
28
+ 'has symbol': int(any(not c.isalnum() for c in password)),
29
+ 'count upper': sum(1 for c in password if c.isupper()),
30
+ 'count lower': sum(1 for c in password if c.islower()),
31
+ 'count digits': sum(1 for c in password if c.isdigit()),
32
+ 'count symbols': sum(1 for c in password if not c.isalnum()),
33
+ }
34
+ return pd.DataFrame([features])
35
+
36
+ def check_password_strength(password):
37
+ """Predict the strength of the password using the loaded model."""
38
+ try:
39
+ if not password:
40
+ return "Error: Password cannot be empty."
41
+ features_df = extract_password_features(password)
42
+ # Verify that feature names match the model's expectations
43
+ if list(features_df.columns) != list(model.feature_names_in_):
44
+ return f"Error: Feature names mismatch. Expected {model.feature_names_in_}, got {list(features_df.columns)}"
45
+ prediction = model.predict(features_df)[0]
46
+ # Map numerical prediction to descriptive label
47
+ if prediction not in STRENGTH_MAPPING:
48
+ return f"Error: Invalid prediction value {prediction}. Expected values are {list(STRENGTH_MAPPING.keys())}."
49
+ return f"Password Strength: {STRENGTH_MAPPING[prediction]}"
50
+ except Exception as e:
51
+ return f"Error: {str(e)}"
52
+
53
+ # Gradio Interface
54
+ with gr.Blocks(title="Password Strength Checker") as iface:
55
+ gr.Markdown(
56
+ """
57
+ # Password Strength Checker
58
+ Enter a password to evaluate its strength. The model will classify it as **Weak**, **Fairly Strong**, or **Strong**.
59
+ """
60
+ )
61
+ password_input = gr.Textbox(
62
+ label="Enter Password",
63
+ placeholder="Type your password here...",
64
+ type="password"
65
+ )
66
+ output = gr.Textbox(label="Predicted Strength")
67
+ submit_button = gr.Button("Check Strength")
68
+
69
+ submit_button.click(
70
+ fn=check_password_strength,
71
+ inputs=password_input,
72
+ outputs=output
73
+ )
74
+
75
+ # Launch the interface
76
+ if __name__ == "__main__":
77
+ iface.launch()