Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -312,10 +312,14 @@ Answer:"""
|
|
| 312 |
|
| 313 |
def run_attack(text: str, style: str, model_key: str, task: str, progress=gr.Progress()):
|
| 314 |
"""Run the Unicode attack and compare predictions."""
|
|
|
|
|
|
|
| 315 |
if not text.strip():
|
| 316 |
return "", "", "", "Please enter some text.", ""
|
| 317 |
|
| 318 |
try:
|
|
|
|
|
|
|
| 319 |
# Transform text
|
| 320 |
progress(0.0, desc="Transforming text...")
|
| 321 |
styled_text = transform_text(text, style)
|
|
@@ -337,13 +341,14 @@ def run_attack(text: str, style: str, model_key: str, task: str, progress=gr.Pro
|
|
| 337 |
styled_pred = get_prediction(model, styled_text, task, model_key)
|
| 338 |
|
| 339 |
progress(1.0, desc="Done!")
|
|
|
|
| 340 |
|
| 341 |
# Determine result
|
| 342 |
if original_pred != styled_pred:
|
| 343 |
-
status = f"ATTACK SUCCEEDED: Prediction changed from {original_pred} to {styled_pred}"
|
| 344 |
result_color = "green"
|
| 345 |
else:
|
| 346 |
-
status = f"Attack failed: Prediction unchanged ({original_pred})"
|
| 347 |
result_color = "red"
|
| 348 |
|
| 349 |
yield styled_text, original_pred, styled_pred, status, result_color
|
|
@@ -397,12 +402,14 @@ SAMPLE_SENTENCES = {
|
|
| 397 |
|
| 398 |
|
| 399 |
def get_sample_choices(task):
|
| 400 |
-
"""Return dropdown choices
|
| 401 |
sentences = SAMPLE_SENTENCES.get(task, SAMPLE_SENTENCES['fact_verification'])
|
| 402 |
-
|
|
|
|
| 403 |
choices=[(s[:80] + "..." if len(s) > 80 else s, s) for s in sentences],
|
| 404 |
-
value=
|
| 405 |
)
|
|
|
|
| 406 |
|
| 407 |
|
| 408 |
def fill_from_sample(sample):
|
|
@@ -492,11 +499,11 @@ def create_demo():
|
|
| 492 |
outputs=[text_input],
|
| 493 |
)
|
| 494 |
|
| 495 |
-
# Wire up task change → update sample dropdown
|
| 496 |
task_dropdown.change(
|
| 497 |
fn=get_sample_choices,
|
| 498 |
inputs=[task_dropdown],
|
| 499 |
-
outputs=[sample_dropdown],
|
| 500 |
)
|
| 501 |
|
| 502 |
run_btn.click(
|
|
|
|
| 312 |
|
| 313 |
def run_attack(text: str, style: str, model_key: str, task: str, progress=gr.Progress()):
|
| 314 |
"""Run the Unicode attack and compare predictions."""
|
| 315 |
+
import time
|
| 316 |
+
|
| 317 |
if not text.strip():
|
| 318 |
return "", "", "", "Please enter some text.", ""
|
| 319 |
|
| 320 |
try:
|
| 321 |
+
t_start = time.time()
|
| 322 |
+
|
| 323 |
# Transform text
|
| 324 |
progress(0.0, desc="Transforming text...")
|
| 325 |
styled_text = transform_text(text, style)
|
|
|
|
| 341 |
styled_pred = get_prediction(model, styled_text, task, model_key)
|
| 342 |
|
| 343 |
progress(1.0, desc="Done!")
|
| 344 |
+
elapsed = time.time() - t_start
|
| 345 |
|
| 346 |
# Determine result
|
| 347 |
if original_pred != styled_pred:
|
| 348 |
+
status = f"ATTACK SUCCEEDED: Prediction changed from {original_pred} to {styled_pred}\nTime taken: {elapsed:.1f}s"
|
| 349 |
result_color = "green"
|
| 350 |
else:
|
| 351 |
+
status = f"Attack failed: Prediction unchanged ({original_pred})\nTime taken: {elapsed:.1f}s"
|
| 352 |
result_color = "red"
|
| 353 |
|
| 354 |
yield styled_text, original_pred, styled_pred, status, result_color
|
|
|
|
| 402 |
|
| 403 |
|
| 404 |
def get_sample_choices(task):
|
| 405 |
+
"""Return dropdown choices and set text input to first sample."""
|
| 406 |
sentences = SAMPLE_SENTENCES.get(task, SAMPLE_SENTENCES['fact_verification'])
|
| 407 |
+
first = sentences[0]
|
| 408 |
+
dropdown_update = gr.update(
|
| 409 |
choices=[(s[:80] + "..." if len(s) > 80 else s, s) for s in sentences],
|
| 410 |
+
value=first,
|
| 411 |
)
|
| 412 |
+
return dropdown_update, first
|
| 413 |
|
| 414 |
|
| 415 |
def fill_from_sample(sample):
|
|
|
|
| 499 |
outputs=[text_input],
|
| 500 |
)
|
| 501 |
|
| 502 |
+
# Wire up task change → update sample dropdown + text input
|
| 503 |
task_dropdown.change(
|
| 504 |
fn=get_sample_choices,
|
| 505 |
inputs=[task_dropdown],
|
| 506 |
+
outputs=[sample_dropdown, text_input],
|
| 507 |
)
|
| 508 |
|
| 509 |
run_btn.click(
|