OnyxlMunkey's picture
Text Transformer: Docker app for HF Space
0f5ca68
import gradio as gr
import base64
import markdown
import urllib.parse
import binascii
# --- Logic ---
MAX_INPUT_LENGTH = 100000 # Limit input size to prevent freezing
def validate_input(text):
if not text:
return False, "Input cannot be empty."
if len(text) > MAX_INPUT_LENGTH:
return False, f"Input too long (max {MAX_INPUT_LENGTH} characters)."
return True, ""
# Leet
leet_map = {
'A': '4', 'B': '8', 'E': '3', 'G': '6', 'L': '1', 'O': '0', 'S': '5', 'T': '7', 'Z': '2'
}
reverse_leet_map = {v: k for k, v in leet_map.items()}
def to_leet(text):
valid, msg = validate_input(text)
if not valid: return msg
return "".join(leet_map.get(c.upper(), c) for c in text)
def from_leet(text):
valid, msg = validate_input(text)
if not valid: return msg
return "".join(reverse_leet_map.get(c.upper(), c) for c in text)
# Morse
morse_code_map = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.', 'H': '....',
'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---', 'P': '.--.',
'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
'Y': '-.--', 'Z': '--..',
'1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...',
'8': '---..', '9': '----.', '0': '-----',
' ': '/'
}
reverse_morse_map = {v: k for k, v in morse_code_map.items()}
def to_morse(text):
valid, msg = validate_input(text)
if not valid: return msg
morse = []
for char in text.upper():
morse.append(morse_code_map.get(char, '?'))
return " ".join(morse)
def from_morse(text):
valid, msg = validate_input(text)
if not valid: return msg
decoded = []
for code in text.split(' '):
if code == '/':
decoded.append(' ')
else:
decoded.append(reverse_morse_map.get(code, ''))
return "".join(decoded)
# Binary
def to_binary(text):
valid, msg = validate_input(text)
if not valid: return msg
return " ".join(format(ord(char), '08b') for char in text)
def from_binary(text):
valid, msg = validate_input(text)
if not valid: return msg
try:
# Remove spaces and validate binary chars
clean_text = text.replace(" ", "")
if not all(c in '01' for c in clean_text):
return "Error: Input contains non-binary characters."
# Ensure 8-bit chunks if possible, or just parse what we have
# If separated by spaces, process each chunk
if " " in text:
return "".join(chr(int(b, 2)) for b in text.split())
else:
# Process as stream of 8 bits
chars = []
for i in range(0, len(clean_text), 8):
byte = clean_text[i:i+8]
if len(byte) == 8:
chars.append(chr(int(byte, 2)))
return "".join(chars)
except ValueError:
return "Error: Invalid Binary input."
# Base64
def to_base64(text):
valid, msg = validate_input(text)
if not valid: return msg
return base64.b64encode(text.encode('utf-8')).decode('utf-8')
def from_base64(text):
valid, msg = validate_input(text)
if not valid: return msg
try:
# Check for valid base64 chars
if not all(c.isalnum() or c in '+/=' for c in text.strip()):
return "Error: Input contains invalid Base64 characters."
return base64.b64decode(text).decode('utf-8')
except (binascii.Error, UnicodeDecodeError):
return "Error: Invalid Base64 input or padding."
# Hex
def to_hex(text):
valid, msg = validate_input(text)
if not valid: return msg
return " ".join(format(ord(char), 'x') for char in text)
def from_hex(text):
valid, msg = validate_input(text)
if not valid: return msg
try:
clean_text = text.replace(" ", "")
# Validate hex chars
if not all(c.lower() in '0123456789abcdef' for c in clean_text):
return "Error: Input contains non-hex characters."
return binascii.unhexlify(clean_text).decode('utf-8')
except (binascii.Error, ValueError):
return "Error: Invalid Hex input."
# Markdown
def to_markdown_html(text):
valid, msg = validate_input(text)
if not valid: return msg
return markdown.markdown(text)
# URL
def to_url_encoded(text):
valid, msg = validate_input(text)
if not valid: return msg
return urllib.parse.quote(text)
def from_url_encoded(text):
valid, msg = validate_input(text)
if not valid: return msg
try:
return urllib.parse.unquote(text)
except Exception:
return "Error: Invalid URL encoded input."
# --- Interface ---
with gr.Blocks(theme=gr.themes.Soft(), title="T3XT TR4N5F0RM3R") as demo:
gr.Markdown(
"""
# T3XT TR4N5F0RM3R ⚡
Your one-stop tool for text conversions.
"""
)
with gr.Tabs():
# Morse Code Tab
with gr.TabItem("Morse Code"):
with gr.Row():
morse_input = gr.Textbox(label="Input", placeholder="Enter text or Morse code...", lines=5)
morse_output = gr.Textbox(label="Output", lines=5)
with gr.Row():
btn_to_morse = gr.Button("Text -> Morse")
btn_from_morse = gr.Button("Morse -> Text")
btn_to_morse.click(to_morse, inputs=morse_input, outputs=morse_output)
btn_from_morse.click(from_morse, inputs=morse_input, outputs=morse_output)
# Leet Speak Tab
with gr.TabItem("Leet Speak"):
with gr.Row():
leet_input = gr.Textbox(label="Input", placeholder="Enter text...", lines=5)
leet_output = gr.Textbox(label="Output", lines=5)
with gr.Row():
btn_to_leet = gr.Button("Text -> Leet")
btn_from_leet = gr.Button("Leet -> Text")
btn_to_leet.click(to_leet, inputs=leet_input, outputs=leet_output)
btn_from_leet.click(from_leet, inputs=leet_input, outputs=leet_output)
# Binary Tab
with gr.TabItem("Binary"):
with gr.Row():
binary_input = gr.Textbox(label="Input", placeholder="Enter text...", lines=5)
binary_output = gr.Textbox(label="Output", lines=5)
with gr.Row():
btn_to_binary = gr.Button("Text -> Binary")
btn_from_binary = gr.Button("Binary -> Text")
btn_to_binary.click(to_binary, inputs=binary_input, outputs=binary_output)
btn_from_binary.click(from_binary, inputs=binary_input, outputs=binary_output)
# Base64 Tab
with gr.TabItem("Base64"):
with gr.Row():
b64_input = gr.Textbox(label="Input", placeholder="Enter text...", lines=5)
b64_output = gr.Textbox(label="Output", lines=5)
with gr.Row():
btn_to_b64 = gr.Button("Text -> Base64")
btn_from_b64 = gr.Button("Base64 -> Text")
btn_to_b64.click(to_base64, inputs=b64_input, outputs=b64_output)
btn_from_b64.click(from_base64, inputs=b64_input, outputs=b64_output)
# Hex Tab
with gr.TabItem("Hex"):
with gr.Row():
hex_input = gr.Textbox(label="Input", placeholder="Enter text...", lines=5)
hex_output = gr.Textbox(label="Output", lines=5)
with gr.Row():
btn_to_hex = gr.Button("Text -> Hex")
btn_from_hex = gr.Button("Hex -> Text")
btn_to_hex.click(to_hex, inputs=hex_input, outputs=hex_output)
btn_from_hex.click(from_hex, inputs=hex_input, outputs=hex_output)
# Markdown Tab
with gr.TabItem("Markdown"):
with gr.Row():
md_input = gr.Textbox(label="Markdown Input", placeholder="# Hello World...", lines=10)
md_output = gr.Textbox(label="HTML Output", lines=10)
btn_to_html = gr.Button("Convert to HTML")
btn_to_html.click(to_markdown_html, inputs=md_input, outputs=md_output)
# URL Encoding Tab
with gr.TabItem("URL Encode"):
with gr.Row():
url_input = gr.Textbox(label="Input", placeholder="https://example.com/foo bar", lines=5)
url_output = gr.Textbox(label="Output", lines=5)
with gr.Row():
btn_url_enc = gr.Button("Encode")
btn_url_dec = gr.Button("Decode")
btn_url_enc.click(to_url_encoded, inputs=url_input, outputs=url_output)
btn_url_dec.click(from_url_encoded, inputs=url_input, outputs=url_output)
if __name__ == "__main__":
demo.launch()