SilverSpeak / app.py
acmc's picture
Update app.py
df4edbd verified
"""
Gradio app with a big textbox for input and a big textbox for output.
Also show tho checkboxes for replace_chars and replace_spaces.
"""
import gradio as gr
import random
from silverspeak import (
random_attack as random_homoglyphs_attack,
)
from silverspeak.homoglyphs.utils import TypesOfHomoglyphs
def random_deletion_attack(original_text, percentage=0.1, random_seed=42):
"""
Inserts random deletion characters into the text.
"""
# TODO: Include this in the SilverSpeak library
# Use the unicode DEL character
DELETION_CHAR = "\u007F"
# Insert the deletion character in random places in the text. The number of characters to insert is a percentage of the length of the text.
random.seed(random_seed)
# Insert the deletion characters
text = original_text
for i in range(int(len(text) * percentage)):
position = random.randint(0, len(text))
text = text[:position] + DELETION_CHAR + text[position:]
return text
EXAMPLE_TEXT_1 = "Dr. Capy Cosmos, a capybara unlike any other, astounded the scientific community with his groundbreaking research in astrophysics. With his keen sense of observation and unparalleled ability to interpret cosmic data, he uncovered new insights into the mysteries of black holes and the origins of the universe. As he peered through telescopes with his large, round eyes, fellow researchers often remarked that it seemed as if the stars themselves whispered their secrets directly to him. Dr. Cosmos not only became a beacon of inspiration to aspiring scientists but also proved that intellect and innovation can be found in the most unexpected of creatures."
EXAMPLE_TEXT_2 = "What are the standards required of offered properties? Properties need to be habitable and must meet certain health and safety standards, which the local authority can discuss with you. These standards have been agreed by the Department of Housing, Local Government and Heritage. The local authority will assess your property to make sure it meets the standards. If the property does not meet the standards, the local authority will explain why and can discuss what could be done to bring the property up to standard. Some properties may not be suitable for all those in need of accommodation, due to location or other reasons. However, every effort will be made by the local authority to ensure that offered properties are matched to appropriate beneficiaries."
inputs = [
gr.Textbox(lines=10, label="Input text"),
gr.Checkbox(label="Homoglyphs", value=True),
gr.Slider(
minimum=0,
maximum=0.3,
step=0.01,
value=0.05,
label="Percentage of characters to replace",
),
gr.Dropdown(
value=['identical'],
multiselect=True,
choices=[x.value for x in TypesOfHomoglyphs],
label="Types of homoglyphs to use",
),
gr.Checkbox(label="Zero-Width", value=False, visible=False),
]
outputs = gr.Textbox(label="Output text", show_copy_button=True)
title = "Silverspeak"
description = """Here, we utilize homoglyph attacks to evade AI-generated content detection systems.
For more information, please refer to the research paper: [Creo, Aldan and Shushanta Pudasaini. “Evading AI-Generated Content Detectors using Homoglyphs.” (2024)](https://arxiv.org/abs/2109.06255)."""
def attack_fn(text, replace_chars, percentage, types_of_homoglyphs, replace_spaces):
if replace_chars:
text = random_homoglyphs_attack(text=text, percentage=percentage, types_of_homoglyphs_to_use=[x for x in TypesOfHomoglyphs if x.value in types_of_homoglyphs])
if replace_spaces:
text = random_deletion_attack(original_text=text, percentage=percentage)
return text
theme = gr.themes.Soft(
primary_hue="fuchsia",
secondary_hue="cyan",
neutral_hue="gray",
radius_size="none",
font=[
gr.themes.GoogleFont("IBM Plex Sans"),
"ui-sans-serif",
"system-ui",
"sans-serif",
],
font_mono=[
gr.themes.GoogleFont("IBM Plex Mono"),
"ui-monospace",
"Consolas",
"monospace",
],
)
gr.Interface(
fn=attack_fn,
inputs=inputs,
outputs=outputs,
title=title,
description=description,
allow_flagging="never",
theme=theme,
examples=[
[EXAMPLE_TEXT_1, True, 0.05, ['identical']],
# [EXAMPLE_TEXT_1, True, 0.05, [TypesOfHomoglyphs.IDENTICAL], False],
# [EXAMPLE_TEXT_1, False, 0.05, [TypesOfHomoglyphs.IDENTICAL], True],
# [EXAMPLE_TEXT_2, True, 0.1, [TypesOfHomoglyphs.IDENTICAL], False],
# [EXAMPLE_TEXT_1, True, 0.03, [TypesOfHomoglyphs.IDENTICAL], True],
# [EXAMPLE_TEXT_2, False, 0.05, [TypesOfHomoglyphs.IDENTICAL], True],
],
examples_per_page=3,
).launch()