Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
|
| 4 |
+
def transpose_cipher(text, key):
|
| 5 |
+
# Convert the key to a list of integers
|
| 6 |
+
key_list = [int(k) for k in key.split()]
|
| 7 |
+
# Calculate the number of columns
|
| 8 |
+
num_cols = len(key_list)
|
| 9 |
+
# Calculate the number of rows
|
| 10 |
+
num_rows = -(-len(text) // num_cols) # Ceiling division
|
| 11 |
+
# Initialize the matrix with spaces
|
| 12 |
+
matrix = [[' ' for _ in range(num_cols)] for _ in range(num_rows)]
|
| 13 |
+
# Fill the matrix with the text
|
| 14 |
+
index = 0
|
| 15 |
+
for row in range(num_rows):
|
| 16 |
+
for col in range(num_cols):
|
| 17 |
+
if index < len(text):
|
| 18 |
+
matrix[row][col] = text[index]
|
| 19 |
+
index += 1
|
| 20 |
+
# Transpose the matrix based on the key
|
| 21 |
+
transposed_matrix = ['' for _ in range(num_rows)]
|
| 22 |
+
for col in range(num_cols):
|
| 23 |
+
for row in range(num_rows):
|
| 24 |
+
transposed_matrix[key_list[col] - 1] += matrix[row][col]
|
| 25 |
+
# Join the transposed matrix into a single string
|
| 26 |
+
return ''.join(transposed_matrix)
|
| 27 |
|
| 28 |
+
# Create the Gradio interface
|
| 29 |
+
with gr.Blocks() as demo:
|
| 30 |
+
# Input components with tooltips and instructions
|
| 31 |
+
text_input = gr.Textbox(
|
| 32 |
+
label="Text to Encrypt",
|
| 33 |
+
placeholder="Enter your text here",
|
| 34 |
+
info="Enter the text you want to encrypt."
|
| 35 |
+
)
|
| 36 |
+
key_input = gr.Textbox(
|
| 37 |
+
label="Key",
|
| 38 |
+
placeholder="Enter the key (space-separated integers)",
|
| 39 |
+
info="Enter the key as a sequence of space-separated integers. The key should be a permutation of the column indices."
|
| 40 |
+
)
|
| 41 |
+
# Output component
|
| 42 |
+
encrypted_output = gr.Textbox(label="Encrypted Text")
|
| 43 |
+
# Button to trigger the encryption
|
| 44 |
+
encrypt_button = gr.Button("Encrypt")
|
| 45 |
+
# Define the event listener
|
| 46 |
+
encrypt_button.click(fn=transpose_cipher, inputs=[text_input, key_input], outputs=encrypted_output)
|
| 47 |
|
| 48 |
+
# Launch the Gradio app
|
| 49 |
+
demo.launch(show_error=True)
|
|
|