|
import pandas as pd |
|
import numpy as np |
|
import joblib |
|
import gradio as gr |
|
import os |
|
import tempfile |
|
|
|
|
|
os.environ["GRADIO_TEMP"] = tempfile.mkdtemp() |
|
|
|
|
|
model = joblib.load("password_checker_model.pkl") |
|
|
|
|
|
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) |
|
|
|
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] |
|
|
|
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)}" |
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch() |