Hehe / app.py
TIMBOVILL's picture
Update app.py
fe609cb verified
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()