File size: 2,766 Bytes
44f9354
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import pandas as pd
import numpy as np
import joblib
import gradio as gr
import os
import tempfile

# Set a custom directory for Gradio's temporary files
os.environ["GRADIO_TEMP"] = tempfile.mkdtemp()

# Load model
model = joblib.load("password_checker_model.pkl")

# Mapping of numerical predictions to descriptive labels
STRENGTH_MAPPING = {
    0: "Weak",
    1: "Fairly Strong",
    2: "Strong"
}

def extract_password_features(password):
    """Extract features from the password for model prediction."""
    features = {
        'length': len(password),
        'has upper': int(any(c.isupper() for c in password)),
        'has lower': int(any(c.islower() for c in password)),
        'has digit': int(any(c.isdigit() for c in password)),
        'has symbol': int(any(not c.isalnum() for c in password)),
        'count upper': sum(1 for c in password if c.isupper()),
        'count lower': sum(1 for c in password if c.islower()),
        'count digits': sum(1 for c in password if c.isdigit()),
        'count symbols': sum(1 for c in password if not c.isalnum()),
    }
    return pd.DataFrame([features])

def check_password_strength(password):
    """Predict the strength of the password using the loaded model."""
    try:
        if not password:
            return "Error: Password cannot be empty."
        features_df = extract_password_features(password)
        # Verify that feature names match the model's expectations
        if list(features_df.columns) != list(model.feature_names_in_):
            return f"Error: Feature names mismatch. Expected {model.feature_names_in_}, got {list(features_df.columns)}"
        prediction = model.predict(features_df)[0]
        # Map numerical prediction to descriptive label
        if prediction not in STRENGTH_MAPPING:
            return f"Error: Invalid prediction value {prediction}. Expected values are {list(STRENGTH_MAPPING.keys())}."
        return f"Password Strength: {STRENGTH_MAPPING[prediction]}"
    except Exception as e:
        return f"Error: {str(e)}"

# Gradio Interface
with gr.Blocks(title="Password Strength Checker") as iface:
    gr.Markdown(
        """
        # Password Strength Checker
        Enter a password to evaluate its strength. The model will classify it as **Weak**, **Fairly Strong**, or **Strong**.
        """
    )
    password_input = gr.Textbox(
        label="Enter Password",
        placeholder="Type your password here...",
        type="password"
    )
    output = gr.Textbox(label="Predicted Strength")
    submit_button = gr.Button("Check Strength")
    
    submit_button.click(
        fn=check_password_strength,
        inputs=password_input,
        outputs=output
    )

# Launch the interface
if __name__ == "__main__":
    iface.launch()