Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -18,21 +18,26 @@ attack = TextFoolerJin2019.build(model_wrapper)
|
|
18 |
|
19 |
# Function to run attack
|
20 |
def run_attack(input_text):
|
21 |
-
|
22 |
-
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
36 |
|
37 |
# Gradio UI
|
38 |
gr.Interface(fn=run_attack,
|
|
|
18 |
|
19 |
# Function to run attack
|
20 |
def run_attack(input_text):
|
21 |
+
try:
|
22 |
+
# Attempt to modify the input text
|
23 |
+
result = attack.attack(input_text, ground_truth_output=1)
|
24 |
|
25 |
+
# Initialize a message to describe what happened
|
26 |
+
attack_info = ""
|
27 |
+
skipped_info = ""
|
28 |
+
|
29 |
+
for goal_function_result in result:
|
30 |
+
if goal_function_result.goal_status == textattack.shared.AttackResultStatus.SUCCEEDED:
|
31 |
+
attack_info += f"Attack Succeeded!\nOriginal Text: {input_text}\nModified Text: {goal_function_result.attacked_text.text}\n"
|
32 |
+
elif goal_function_result.goal_status == textattack.shared.AttackResultStatus.SKIPPED:
|
33 |
+
skipped_info += f"Skipped Text: {goal_function_result.attacked_text.text}\n"
|
34 |
|
35 |
+
# Format the output
|
36 |
+
output = f"TextAttack Results:\n{attack_info}\nSkipped Details:\n{skipped_info}"
|
37 |
+
return output
|
38 |
+
|
39 |
+
except Exception as e:
|
40 |
+
return f"An error occurred: {str(e)}"
|
41 |
|
42 |
# Gradio UI
|
43 |
gr.Interface(fn=run_attack,
|