|
import gradio as gr |
|
|
|
|
|
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) |
|
|
|
|
|
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('?') |
|
else: |
|
decoded_message.append(num) |
|
return ''.join(decoded_message) |
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
def gradio_interface(text, operation): |
|
if operation == "Encode": |
|
return reverse_and_encode(text) |
|
elif operation == "Decode": |
|
return reverse_and_decode(text) |
|
|
|
|
|
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." |
|
) |
|
|
|
|
|
interface.launch() |