File size: 4,805 Bytes
3b6b0ab
 
 
 
 
 
4c7678c
3b6b0ab
e5dbfbb
3b6b0ab
 
11151b3
4c7678c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b6b0ab
 
 
 
 
 
 
 
 
 
 
 
 
 
baf0f08
df4edbd
baf0f08
df4edbd
c98b0d9
baf0f08
c52862c
3b6b0ab
 
 
11151b3
3b6b0ab
7cf7fc4
3b6b0ab
11151b3
3b6b0ab
df4edbd
3b6b0ab
 
 
 
 
ec5f144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b6b0ab
 
 
 
 
 
 
ec5f144
c98b0d9
df4edbd
3de119e
 
 
 
 
c98b0d9
4d60c3f
3b6b0ab
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
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()