S-Dreamer's picture
Update app.py
6121902 verified
import gradio as gr
import numpy as np
def transpose_cipher(text, key):
# Convert the key to a list of integers
key_list = [int(k) for k in key.split()]
# Calculate the number of columns
num_cols = len(key_list)
# Calculate the number of rows
num_rows = -(-len(text) // num_cols) # Ceiling division
# Initialize the matrix with spaces
matrix = [[' ' for _ in range(num_cols)] for _ in range(num_rows)]
# Fill the matrix with the text
index = 0
for row in range(num_rows):
for col in range(num_cols):
if index < len(text):
matrix[row][col] = text[index]
index += 1
# Transpose the matrix based on the key
transposed_matrix = ['' for _ in range(num_rows)]
for col in range(num_cols):
for row in range(num_rows):
transposed_matrix[key_list[col] - 1] += matrix[row][col]
# Join the transposed matrix into a single string
return ''.join(transposed_matrix)
# Create the Gradio interface
with gr.Blocks() as demo:
# Input components with tooltips and instructions
text_input = gr.Textbox(
label="Text to Encrypt",
placeholder="Enter your text here",
info="Enter the text you want to encrypt."
)
key_input = gr.Textbox(
label="Key",
placeholder="Enter the key (space-separated integers)",
info="Enter the key as a sequence of space-separated integers. The key should be a permutation of the column indices."
)
# Output component
encrypted_output = gr.Textbox(label="Encrypted Text")
# Button to trigger the encryption
encrypt_button = gr.Button("Encrypt")
# Define the event listener
encrypt_button.click(fn=transpose_cipher, inputs=[text_input, key_input], outputs=encrypted_output)
# Launch the Gradio app
demo.launch(show_error=True)