Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,24 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import re
|
| 3 |
|
| 4 |
-
# 1. Helper function for labels
|
| 5 |
def get_label(is_met, text):
|
| 6 |
-
color = "#28a745" if is_met else "#dc3545"
|
| 7 |
symbol = "β
" if is_met else "β"
|
| 8 |
return f"<b style='color:{color}; font-size: 1.1em;'>{symbol} {text}</b>"
|
| 9 |
|
| 10 |
-
# 2.
|
| 11 |
def render_html(password=""):
|
|
|
|
| 12 |
has_upper = re.search(r"[A-Z]", password)
|
| 13 |
has_lower = re.search(r"[a-z]", password)
|
| 14 |
has_num = re.search(r"\d", password)
|
| 15 |
has_spec = re.search(r"[!@#$%^&*(),.?\":{}|<>]", password)
|
| 16 |
has_len = len(password) >= 8
|
| 17 |
|
|
|
|
| 18 |
checklist_html = f"""
|
| 19 |
-
<div style="border: 1px solid #
|
| 20 |
<p style="margin-top:0;"><b>Password Requirements:</b></p>
|
| 21 |
{get_label(has_len, "8+ Characters")} |
|
| 22 |
{get_label(has_upper, "Capital (A-Z)")} |
|
|
@@ -25,22 +27,28 @@ def render_html(password=""):
|
|
| 25 |
{get_label(has_spec, "Special (!@#)")}
|
| 26 |
</div>
|
| 27 |
"""
|
|
|
|
| 28 |
score = sum([bool(has_len), bool(has_upper), bool(has_lower), bool(has_num), bool(has_spec)])
|
| 29 |
strength_pct = (score / 5) * 100
|
|
|
|
| 30 |
return checklist_html, strength_pct
|
| 31 |
|
| 32 |
-
# 3. Visibility
|
| 33 |
def toggle_visibility(is_visible):
|
| 34 |
new_type = "text" if is_visible else "password"
|
| 35 |
new_label = "Hide Password ποΈ" if is_visible else "Show Password ποΈβπ¨οΈ"
|
| 36 |
return gr.update(type=new_type), new_label, not is_visible
|
| 37 |
|
| 38 |
-
#
|
|
|
|
|
|
|
|
|
|
| 39 |
custom_css = ".gradio-container { max-width: 800px; margin: auto; }"
|
| 40 |
|
| 41 |
-
with gr.Blocks() as demo:
|
| 42 |
gr.Markdown("# π‘οΈ SmartPass Validator")
|
| 43 |
-
gr.Markdown("
|
|
|
|
| 44 |
visible_state = gr.State(True)
|
| 45 |
|
| 46 |
with gr.Row():
|
|
@@ -49,7 +57,7 @@ with gr.Blocks() as demo: # Moved theme/css to launch()
|
|
| 49 |
label="Your Password",
|
| 50 |
placeholder="Start typing...",
|
| 51 |
type="password",
|
| 52 |
-
buttons=["copy"] #
|
| 53 |
)
|
| 54 |
show_btn = gr.Button("Show Password ποΈβπ¨οΈ", variant="secondary")
|
| 55 |
|
|
@@ -59,12 +67,41 @@ with gr.Blocks() as demo: # Moved theme/css to launch()
|
|
| 59 |
minimum=0, maximum=100, value=0, interactive=False
|
| 60 |
)
|
| 61 |
|
|
|
|
| 62 |
initial_html, _ = render_html("")
|
| 63 |
checklist_area = gr.HTML(value=initial_html)
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
#
|
| 70 |
demo.launch(theme=gr.themes.Soft(), css=custom_css)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import re
|
| 3 |
|
| 4 |
+
# 1. Helper function for requirement labels
|
| 5 |
def get_label(is_met, text):
|
| 6 |
+
color = "#28a745" if is_met else "#dc3545" # Green if met, Red if not
|
| 7 |
symbol = "β
" if is_met else "β"
|
| 8 |
return f"<b style='color:{color}; font-size: 1.1em;'>{symbol} {text}</b>"
|
| 9 |
|
| 10 |
+
# 2. Function to build the checklist HTML and calculate strength
|
| 11 |
def render_html(password=""):
|
| 12 |
+
# Define Regex rules
|
| 13 |
has_upper = re.search(r"[A-Z]", password)
|
| 14 |
has_lower = re.search(r"[a-z]", password)
|
| 15 |
has_num = re.search(r"\d", password)
|
| 16 |
has_spec = re.search(r"[!@#$%^&*(),.?\":{}|<>]", password)
|
| 17 |
has_len = len(password) >= 8
|
| 18 |
|
| 19 |
+
# Adaptive background for Light/Dark mode compatibility
|
| 20 |
checklist_html = f"""
|
| 21 |
+
<div style="border: 1px solid #888; padding: 15px; border-radius: 10px; background-color: rgba(128, 128, 128, 0.05);">
|
| 22 |
<p style="margin-top:0;"><b>Password Requirements:</b></p>
|
| 23 |
{get_label(has_len, "8+ Characters")} |
|
| 24 |
{get_label(has_upper, "Capital (A-Z)")} |
|
|
|
|
| 27 |
{get_label(has_spec, "Special (!@#)")}
|
| 28 |
</div>
|
| 29 |
"""
|
| 30 |
+
|
| 31 |
score = sum([bool(has_len), bool(has_upper), bool(has_lower), bool(has_num), bool(has_spec)])
|
| 32 |
strength_pct = (score / 5) * 100
|
| 33 |
+
|
| 34 |
return checklist_html, strength_pct
|
| 35 |
|
| 36 |
+
# 3. Visibility and Dark Mode Logic
|
| 37 |
def toggle_visibility(is_visible):
|
| 38 |
new_type = "text" if is_visible else "password"
|
| 39 |
new_label = "Hide Password ποΈ" if is_visible else "Show Password ποΈβπ¨οΈ"
|
| 40 |
return gr.update(type=new_type), new_label, not is_visible
|
| 41 |
|
| 42 |
+
# JavaScript snippet to toggle the .dark class on the body
|
| 43 |
+
toggle_dark_js = """() => { document.body.classList.toggle('dark'); }"""
|
| 44 |
+
|
| 45 |
+
# 4. Build the UI
|
| 46 |
custom_css = ".gradio-container { max-width: 800px; margin: auto; }"
|
| 47 |
|
| 48 |
+
with gr.Blocks() as demo:
|
| 49 |
gr.Markdown("# π‘οΈ SmartPass Validator")
|
| 50 |
+
gr.Markdown("Check your password strength and toggle between light and dark modes.")
|
| 51 |
+
|
| 52 |
visible_state = gr.State(True)
|
| 53 |
|
| 54 |
with gr.Row():
|
|
|
|
| 57 |
label="Your Password",
|
| 58 |
placeholder="Start typing...",
|
| 59 |
type="password",
|
| 60 |
+
buttons=["copy"] # Gradio 6 standard for copy button
|
| 61 |
)
|
| 62 |
show_btn = gr.Button("Show Password ποΈβπ¨οΈ", variant="secondary")
|
| 63 |
|
|
|
|
| 67 |
minimum=0, maximum=100, value=0, interactive=False
|
| 68 |
)
|
| 69 |
|
| 70 |
+
# Initialize with the red checklist visible immediately
|
| 71 |
initial_html, _ = render_html("")
|
| 72 |
checklist_area = gr.HTML(value=initial_html)
|
| 73 |
|
| 74 |
+
# Quick Settings Panel
|
| 75 |
+
with gr.Row():
|
| 76 |
+
with gr.Column():
|
| 77 |
+
gr.Markdown("### βοΈ Quick Settings")
|
| 78 |
+
with gr.Row():
|
| 79 |
+
dark_btn = gr.Button("π Dark Mode / βοΈ Light Mode", variant="secondary")
|
| 80 |
+
reset_btn = gr.Button("ποΈ Clear Everything", variant="stop")
|
| 81 |
+
|
| 82 |
+
# --- Event Triggers ---
|
| 83 |
+
pass_input.change(
|
| 84 |
+
fn=render_html,
|
| 85 |
+
inputs=pass_input,
|
| 86 |
+
outputs=[checklist_area, strength_meter]
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
show_btn.click(
|
| 90 |
+
toggle_visibility,
|
| 91 |
+
inputs=[visible_state],
|
| 92 |
+
outputs=[pass_input, show_btn, visible_state]
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# Dark Mode Toggle using JavaScript
|
| 96 |
+
dark_btn.click(None, None, None, js=toggle_dark_js)
|
| 97 |
+
|
| 98 |
+
# Reset Button Logic
|
| 99 |
+
reset_btn.click(
|
| 100 |
+
lambda: ["", initial_html, 0],
|
| 101 |
+
None,
|
| 102 |
+
[pass_input, checklist_area, strength_meter]
|
| 103 |
+
)
|
| 104 |
|
| 105 |
if __name__ == "__main__":
|
| 106 |
+
# Launch with theme and css moved to launch()
|
| 107 |
demo.launch(theme=gr.themes.Soft(), css=custom_css)
|