Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,22 @@ def encode_message(message):
|
|
11 |
encoded_message.append(char)
|
12 |
return ' '.join(encoded_message)
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
# Function to reverse the letters in each word
|
15 |
def reverse_and_encode(input_text):
|
16 |
words = input_text.split()
|
@@ -19,16 +35,29 @@ def reverse_and_encode(input_text):
|
|
19 |
encoded_message = encode_message(reversed_message)
|
20 |
return encoded_message
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
# Gradio interface
|
23 |
-
def gradio_interface(
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
# Create the Gradio interface
|
27 |
-
interface = gr.Interface(
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
32 |
|
33 |
# Launch the interface
|
34 |
interface.launch()
|
|
|
11 |
encoded_message.append(char)
|
12 |
return ' '.join(encoded_message)
|
13 |
|
14 |
+
# Function to decode a message from numbers
|
15 |
+
def decode_message(encoded_message):
|
16 |
+
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
17 |
+
numbers = encoded_message.split()
|
18 |
+
decoded_message = []
|
19 |
+
for num in numbers:
|
20 |
+
if num.isdigit():
|
21 |
+
index = int(num) - 1
|
22 |
+
if 0 <= index < len(alphabet):
|
23 |
+
decoded_message.append(alphabet[index])
|
24 |
+
else:
|
25 |
+
decoded_message.append('?') # For numbers out of range
|
26 |
+
else:
|
27 |
+
decoded_message.append(num)
|
28 |
+
return ''.join(decoded_message)
|
29 |
+
|
30 |
# Function to reverse the letters in each word
|
31 |
def reverse_and_encode(input_text):
|
32 |
words = input_text.split()
|
|
|
35 |
encoded_message = encode_message(reversed_message)
|
36 |
return encoded_message
|
37 |
|
38 |
+
# Function to decode and reverse the letters in each word
|
39 |
+
def reverse_and_decode(encoded_message):
|
40 |
+
decoded_message = decode_message(encoded_message)
|
41 |
+
words = decoded_message.split()
|
42 |
+
reversed_words = [word[::-1] for word in words]
|
43 |
+
reversed_message = ' '.join(reversed_words)
|
44 |
+
return reversed_message
|
45 |
+
|
46 |
# Gradio interface
|
47 |
+
def gradio_interface(text, operation):
|
48 |
+
if operation == "Encode":
|
49 |
+
return reverse_and_encode(text)
|
50 |
+
elif operation == "Decode":
|
51 |
+
return reverse_and_decode(text)
|
52 |
|
53 |
# Create the Gradio interface
|
54 |
+
interface = gr.Interface(
|
55 |
+
fn=gradio_interface,
|
56 |
+
inputs=[gr.inputs.Textbox(label="Input Text"), gr.inputs.Radio(["Encode", "Decode"], label="Operation")],
|
57 |
+
outputs="text",
|
58 |
+
title="Reverse and Encode/Decode",
|
59 |
+
description="Choose 'Encode' to reverse each word and convert it to numbers, or 'Decode' to convert from numbers back to text and reverse each word."
|
60 |
+
)
|
61 |
|
62 |
# Launch the interface
|
63 |
interface.launch()
|