File size: 2,197 Bytes
29b1ca6
 
 
 
 
 
 
 
 
 
 
 
 
fe609cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29b1ca6
 
 
 
 
 
 
 
fe609cb
 
 
 
 
 
 
 
29b1ca6
fe609cb
 
 
 
 
29b1ca6
 
fe609cb
 
 
 
 
 
 
29b1ca6
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import gradio as gr

# Function to encode a message to numbers
def encode_message(message):
    alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    encoded_message = []
    for char in message.upper():
        if char in alphabet:
            encoded_message.append(str(alphabet.index(char) + 1))
        else:
            encoded_message.append(char)
    return ' '.join(encoded_message)

# Function to decode a message from numbers
def decode_message(encoded_message):
    alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    numbers = encoded_message.split()
    decoded_message = []
    for num in numbers:
        if num.isdigit():
            index = int(num) - 1
            if 0 <= index < len(alphabet):
                decoded_message.append(alphabet[index])
            else:
                decoded_message.append('?')  # For numbers out of range
        else:
            decoded_message.append(num)
    return ''.join(decoded_message)

# Function to reverse the letters in each word
def reverse_and_encode(input_text):
    words = input_text.split()
    reversed_words = [word[::-1] for word in words]
    reversed_message = ' '.join(reversed_words)
    encoded_message = encode_message(reversed_message)
    return encoded_message

# Function to decode and reverse the letters in each word
def reverse_and_decode(encoded_message):
    decoded_message = decode_message(encoded_message)
    words = decoded_message.split()
    reversed_words = [word[::-1] for word in words]
    reversed_message = ' '.join(reversed_words)
    return reversed_message

# Gradio interface
def gradio_interface(text, operation):
    if operation == "Encode":
        return reverse_and_encode(text)
    elif operation == "Decode":
        return reverse_and_decode(text)

# Create the Gradio interface
interface = gr.Interface(
    fn=gradio_interface,
    inputs=[gr.inputs.Textbox(label="Input Text"), gr.inputs.Radio(["Encode", "Decode"], label="Operation")],
    outputs="text",
    title="Reverse and Encode/Decode",
    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."
)

# Launch the interface
interface.launch()