hassan773 commited on
Commit
da7d829
Β·
verified Β·
1 Parent(s): c711e68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -30
app.py CHANGED
@@ -1,22 +1,20 @@
1
  import gradio as gr
2
  import re
3
 
4
- # 1. Helper function to generate colored HTML 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
- # Create the horizontal checklist layout
20
  checklist_html = f"""
21
  <div style="border: 1px solid #ddd; padding: 15px; border-radius: 10px; background-color: #fafafa;">
22
  <p style="margin-top:0;"><b>Password Requirements:</b></p>
@@ -27,25 +25,22 @@ def render_html(password=""):
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. Toggle Visibility 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
- # 4. Build the UI
43
  custom_css = ".gradio-container { max-width: 800px; margin: auto; }"
44
 
45
- with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
46
  gr.Markdown("# πŸ›‘οΈ SmartPass Validator")
47
  gr.Markdown("The checklist below shows what you need for a secure password.")
48
-
49
  visible_state = gr.State(True)
50
 
51
  with gr.Row():
@@ -54,35 +49,22 @@ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
54
  label="Your Password",
55
  placeholder="Start typing...",
56
  type="password",
57
- show_copy_button=True
58
  )
59
  show_btn = gr.Button("Show Password πŸ‘οΈβ€πŸ—¨οΈ", variant="secondary")
60
 
61
  with gr.Column(scale=2):
62
  strength_meter = gr.Slider(
63
  label="Overall Strength Meter (%)",
64
- minimum=0,
65
- maximum=100,
66
- value=0,
67
- interactive=False
68
  )
69
 
70
- # Initialize with the red checklist visible at start
71
  initial_html, _ = render_html("")
72
  checklist_area = gr.HTML(value=initial_html)
73
 
74
- # Event Triggers
75
- pass_input.change(
76
- fn=render_html,
77
- inputs=pass_input,
78
- outputs=[checklist_area, strength_meter]
79
- )
80
-
81
- show_btn.click(
82
- toggle_visibility,
83
- inputs=[visible_state],
84
- outputs=[pass_input, show_btn, visible_state]
85
- )
86
 
87
  if __name__ == "__main__":
88
- demo.launch()
 
 
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. Checklist logic
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 #ddd; padding: 15px; border-radius: 10px; background-color: #fafafa;">
20
  <p style="margin-top:0;"><b>Password Requirements:</b></p>
 
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 toggle
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
+ # 4. UI Build
39
  custom_css = ".gradio-container { max-width: 800px; margin: auto; }"
40
 
41
+ with gr.Blocks() as demo: # Moved theme/css to launch()
42
  gr.Markdown("# πŸ›‘οΈ SmartPass Validator")
43
  gr.Markdown("The checklist below shows what you need for a secure password.")
 
44
  visible_state = gr.State(True)
45
 
46
  with gr.Row():
 
49
  label="Your Password",
50
  placeholder="Start typing...",
51
  type="password",
52
+ buttons=["copy"] # Changed show_copy_button to buttons=["copy"]
53
  )
54
  show_btn = gr.Button("Show Password πŸ‘οΈβ€πŸ—¨οΈ", variant="secondary")
55
 
56
  with gr.Column(scale=2):
57
  strength_meter = gr.Slider(
58
  label="Overall Strength Meter (%)",
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
+ pass_input.change(fn=render_html, inputs=pass_input, outputs=[checklist_area, strength_meter])
66
+ show_btn.click(toggle_visibility, inputs=[visible_state], outputs=[pass_input, show_btn, visible_state])
 
 
 
 
 
 
 
 
 
 
67
 
68
  if __name__ == "__main__":
69
+ # Parameters moved here for Gradio 6 compatibility
70
+ demo.launch(theme=gr.themes.Soft(), css=custom_css)