Spaces:
Runtime error
Runtime error
| import random | |
| import string | |
| import gradio as gr | |
| def generator(pass_length, option, percent_syb, percent_num): | |
| length = int(pass_length) | |
| percent_symbol = int(percent_syb) | |
| percent_number = int(percent_num) | |
| number_count = max(1, int(length * percent_number / 100)) | |
| symbol_count = max(1, int(length * percent_symbol / 100)) | |
| if option == "Letters + Numbers + Symbols": | |
| letter_count = max(0, length - number_count - symbol_count) | |
| if option == "Letters + Numbers": | |
| symbol_count = 0 | |
| letter_count = max(0, length - number_count) | |
| if option == "Letters + Symbols": | |
| number_count = 0 | |
| letter_count = max(0, length - symbol_count) | |
| if option == "Letters only": | |
| number_count = 0 | |
| symbol_count = 0 | |
| letter_count = max(0, length) | |
| num_list = random.choices(string.digits, k=number_count) | |
| syb_list = random.choices(string.punctuation, k=symbol_count) | |
| str_list = random.choices(string.ascii_letters, k=letter_count) | |
| pass_list = str_list + num_list + syb_list | |
| random.shuffle(pass_list) | |
| password = "".join(pass_list) | |
| count_result = ( | |
| f"Letters: {letter_count}, Symbols: {symbol_count}, Numbers: {number_count}" | |
| ) | |
| password_result = password | |
| return count_result, password_result | |
| with gr.Blocks(title="Day04 Password Generator") as demo: | |
| gr.Markdown("# Password Generator") | |
| with gr.Column(): | |
| with gr.Row(): | |
| password_length = gr.Slider( | |
| minimum=8, maximum=32, step=1, label="Length of your password" | |
| ) | |
| opt_selector = gr.Dropdown( | |
| choices=( | |
| "Letters + Numbers", | |
| "Letters + Symbols", | |
| "Letters + Numbers + Symbols", | |
| "Letters only", | |
| ), | |
| label="Password should include", | |
| value="Letters only", | |
| ) | |
| with gr.Row(): | |
| per_sym = gr.Slider( | |
| minimum=5, maximum=40, step=5, label="Percentage of symbols in password" | |
| ) | |
| per_num = gr.Slider( | |
| minimum=5, maximum=40, step=5, label="Percentage of numbers in password" | |
| ) | |
| with gr.Row(): | |
| count_area = gr.Textbox(label="Character Count", lines=1) | |
| password_area = gr.Textbox(label="Your Password", lines=1) | |
| generate_btn = gr.Button("Generate", variant="primary") | |
| generate_btn.click( # pylint: disable=no-member | |
| fn=generator, | |
| inputs=(password_length, opt_selector, per_sym, per_num), | |
| outputs=(count_area, password_area), | |
| ) | |
| demo.launch() | |