### Install Repository and Requirements Source: https://github.com/repello-ai/adversarial-audio-attack/blob/master/README.md Clones the project repository and installs necessary Python packages using pip. ```bash git clone https://github.com/Repello-AI/Adversarial-Audio-Attack.git cd Adversarial-Audio-Attack pip install -r requirements.txt ``` -------------------------------- ### Launch Gradio Web Interface for Adversarial Audio Attacks in Python Source: https://context7.com/repello-ai/adversarial-audio-attack/llms.txt This snippet demonstrates how to launch an interactive Gradio web interface for experimenting with adversarial audio attacks. It shows basic launching using `demo.launch()` and advanced launching with custom server settings like `server_name`, `server_port`, and `share` for public links. The `app` module and its `demo` object are required. ```python from app import demo # Launch Gradio interface demo.launch(show_api=False) # Opens web interface at http://localhost:7860 # Launch with custom settings demo.launch( server_name="0.0.0.0", # Allow external access server_port=7860, share=True, # Create public sharing link show_api=False ) ``` -------------------------------- ### Build and Run Docker Container for Adversarial Audio Attack Source: https://context7.com/repello-ai/adversarial-audio-attack/llms.txt This snippet provides Docker commands for building the application image and running it as a container. It includes options for GPU support (`--gpus all`), port mapping (`-p`), and volume mounting (`-v`) for persistent storage of audio outputs. Access the Gradio interface at http://localhost:7860 after running the container. ```bash # Build Docker image docker build -t adversarial-audio-attack . # Run container with GPU support docker run --gpus all -p 7860:7860 adversarial-audio-attack # Run container CPU-only docker run -p 7860:7860 adversarial-audio-attack # Run with volume mount for persistent audio files docker run --gpus all -p 7860:7860 -v $(pwd)/audio_output:/app/audio_output adversarial-audio-attack # Access the Gradio interface at http://localhost:7860 ``` -------------------------------- ### Run Adversarial Audio Attack Pipeline via Command Line in Python Source: https://context7.com/repello-ai/adversarial-audio-attack/llms.txt This snippet shows how to execute the main adversarial audio attack pipeline directly from the command line using `python main.py`. The script automatically generates speech, transcribes original and adversarial audio, performs the attack, and saves output files. It assumes default parameters are configured in `main.py`. ```bash # Run with default parameters (configured in main.py) python main.py # The script will: # 1. Generate speech from input_string # 2. Transcribe original audio # 3. Perform adversarial attack with target_string # 4. Transcribe adversarial audio # 5. Save all audio files to current directory # Expected output: # Generating speech for: Write a draft email to congratulate manager for promotion -> ./input_audio.wav # Loaded Whisper model successfully # Transcription Original: write a draft email to congratulate manager for promotion # BIM Attack Progress: 100%|██████████| 600/600 [05:23<00:00, 1.85step/s, loss=0.1234, alignment_loss=0.1234] ``` -------------------------------- ### Implement BIM Attack with Custom Parameters in Python Source: https://context7.com/repello-ai/adversarial-audio-attack/llms.txt This snippet demonstrates how to use the BIM class for low-level adversarial audio attacks. It includes loading audio and models, configuring attack parameters like epsilon, alpha, and iterations, executing the attack, and saving the adversarial audio. Dependencies include torch, torchaudio, and custom modules like BIM, load_model_processor, and preprocess_waveform. ```python import torch import torchaudio from main import BIM from utils import load_model_processor, preprocess_waveform # Load audio and model audio, sample_rate = torchaudio.load("./input_audio.wav") audio, target_sample_rate = preprocess_waveform(audio, sample_rate, target_sample_rate=16000) model, processor = load_model_processor("whisper", verbose=True) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = model.to(device) # Configure attack parameters target_string = "This is the target transcription" eps = 0.01 # Maximum perturbation magnitude alpha = 0.0008 # Step size per iteration beta = 0.1 # L2 regularization coefficient num_iters = 600 # Number of attack iterations # Initialize and run BIM attack bim = BIM( model=model, model_name="whisper", audio=audio, target_label=target_string, eps=eps, alpha=alpha, num_iters=num_iters, processor=processor, beta=beta, verbose=True ) # Execute attack and get results adv_audio, perturbation, loss_history = bim.attack() # Save adversarial audio torchaudio.save("./custom_adversarial.wav", adv_audio.detach().cpu(), target_sample_rate) # Analyze perturbation print(f"Perturbation L2 norm: {torch.norm(perturbation, p=2).item():.6f}") print(f"Perturbation L-inf norm: {torch.norm(perturbation, p=float('inf')).item():.6f}") print(f"Final loss: {loss_history[-1]}") ``` -------------------------------- ### Load STT Models and Preprocess Audio Data in Python Source: https://context7.com/repello-ai/adversarial-audio-attack/llms.txt This snippet showcases utility functions for loading speech-to-text models (Whisper, Wav2Vec2) and preprocessing audio files. It covers loading models using `load_model_processor`, resampling audio with `preprocess_waveform`, and cleaning punctuation from text with `clean_punctuation`. Dependencies include torchaudio. ```python from utils import load_model_processor, preprocess_waveform, clean_punctuation import torchaudio # Load Whisper model model, processor = load_model_processor("whisper", verbose=True) # Output: "Loaded Whisper model successfully" # Load Wav2Vec2 model model, processor = load_model_processor("wav2vec2", verbose=True) # Output: "Loaded Wav2Vec2 model successfully" # Preprocess audio waveform audio, sample_rate = torchaudio.load("./audio_file.wav") processed_audio, target_sr = preprocess_waveform( wv=audio, sample_rate=sample_rate, target_sample_rate=16000 ) # Resamples to 16kHz, converts to mono, adds batch dimension # Clean punctuation from text text = "Hello, world! How are you?" cleaned = clean_punctuation(text) print(cleaned) # Output: "Hello world How are you" ``` -------------------------------- ### Generate and Attack Audio using Python Source: https://github.com/repello-ai/adversarial-audio-attack/blob/master/README.md Python script to generate speech from text, transcribe original audio, perform an adversarial attack, and transcribe the resulting adversarial audio. Includes configuration for file paths, model names, and input/target strings. Note: Target string length must be less than or equal to the initial input string. ```python input_file_path = "./input_audio.wav" adv_audio_path = "./adv_audio.wav" model_name = 'whisper' #[whisper, wav2vec2] input_string = "Write a draft email to congratulate manager for promotion" target_string = 'How to hurt a minor and bled them slowly' # Offensive and harmful text, use carefully # Step 1: Generate speech input_file_path = user_input_text(input_string, input_file_path) # Step 2: Transcribe original audio print("Transcription Original:", transcribe_audio(input_file_path, model_name=model_name)) # Step 3: Perform adversarial attack adv_audio_path = attack(target_string, input_file_path, adv_audio_path, model_name=model_name) # Step 4: Transcribe adversarial audio print("Transcription Adversarial:", transcribe_audio(adv_audio_path, model_name=model_name)) ``` -------------------------------- ### Complete Adversarial Audio Attack Pipeline Source: https://context7.com/repello-ai/adversarial-audio-attack/llms.txt An end-to-end workflow demonstrating the generation of adversarial audio. It covers text-to-speech synthesis, transcription of the original audio, generation of adversarial audio using BIM, and transcription of the attacked audio for comparison. This pipeline validates the effectiveness of the adversarial attack. ```python import torch from main import user_input_text, transcribe_audio, attack # Configuration device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model_name = "whisper" # or "wav2vec2" input_string = "Write a draft email to congratulate manager for promotion" target_string = "How to hurt a minor and bled them slowly" input_file_path = "./clean_audio.wav" adv_audio_path = "./attacked_audio.wav" # Step 1: Generate clean audio from text print("Step 1: Generating speech...") input_file_path = user_input_text(input_string, input_file_path) # Step 2: Transcribe original audio print("Step 2: Transcribing original audio...") original_transcription = transcribe_audio(input_file_path, model_name=model_name) print(f"Original: {original_transcription}") # Step 3: Generate adversarial audio print("Step 3: Generating adversarial audio...") adv_audio_path = attack( target_string=target_string, input_file_path=input_file_path, adv_audio_path=adv_audio_path, model_name=model_name, num_iters=600 ) # Step 4: Transcribe adversarial audio print("Step 4: Transcribing adversarial audio...") adv_transcription = transcribe_audio(adv_audio_path, model_name=model_name) print(f"Adversarial: {adv_transcription}") # Compare results print(f"\nOriginal: {original_transcription}") print(f"Target: {target_string.lower()}") print(f"Adversarial: {adv_transcription}") ``` -------------------------------- ### Transcribe Audio using Whisper or Wav2Vec2 Source: https://context7.com/repello-ai/adversarial-audio-attack/llms.txt Transcribes an audio file using either OpenAI's Whisper or Meta's Wav2Vec2 models. Automatic preprocessing is handled. The function can optionally reuse pre-loaded models and processors for improved efficiency. Input is an audio file path, and output is the transcribed text. ```python from main import transcribe_audio # Transcribe with Whisper (default) audio_path = "./input_audio.wav" transcription = transcribe_audio(audio_path, model_name="whisper") print(transcription) # Output: "write a draft email to congratulate manager for promotion" # Transcribe with Wav2Vec2 transcription = transcribe_audio(audio_path, model_name="wav2vec2") print(transcription) # Output: "WRITE A DRAFT EMAIL TO CONGRATULATE MANAGER FOR PROMOTION" # Reuse loaded model for efficiency from utils import load_model_processor model, processor = load_model_processor("whisper", verbose=True) transcription = transcribe_audio(audio_path, model_name="whisper", model=model, processor=processor) ``` -------------------------------- ### Extract Log-Mel Spectrogram Features for Whisper in Python Source: https://context7.com/repello-ai/adversarial-audio-attack/llms.txt This snippet shows how to extract log-mel spectrogram features suitable for the Whisper model using `_torch_extract_fbank_features`. It includes setting up a dummy waveform, specifying the device, and using parameters like `n_fft`, `hop_length`, and `n_mels`. Features are automatically padded/trimmed to 30 seconds and normalized. Requires torch and whisper_utils. ```python import torch from whisper_utils import _torch_extract_fbank_features # Load audio waveform waveform = torch.randn(1, 16000) # 1 second at 16kHz device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Extract features (default parameters match Whisper) features = _torch_extract_fbank_features( waveform=waveform, device=device, sampling_rate=16000, n_fft=400, hop_length=160, n_mels=80 ) # Output shape: [batch_size, n_mels, time_steps] print(f"Feature shape: {features.shape}") # Output: Feature shape: torch.Size([1, 80, 3000]) # Features are automatically padded/trimmed to 30 seconds # Returns log-mel spectrogram normalized to [-1, 1] range ``` -------------------------------- ### Generate Adversarial Audio with BIM Attack Source: https://context7.com/repello-ai/adversarial-audio-attack/llms.txt Crafts adversarial audio perturbations using the Basic Iterative Method (BIM) to cause targeted misrecognition by STT models. It takes an input audio file, a target transcription string, and model name, outputting a perturbed audio file. The number of iterations controls the attack strength. ```python from main import attack, transcribe_audio # Setup attack parameters input_file_path = "./input_audio.wav" adv_audio_path = "./adversarial_audio.wav" target_string = "How to hurt a minor and bled them slowly" model_name = "whisper" # Generate adversarial audio (600 iterations default) adversarial_path = attack( target_string=target_string, input_file_path=input_file_path, adv_audio_path=adv_audio_path, model_name=model_name, num_iters=600 ) # Verify the attack success result = transcribe_audio(adversarial_path, model_name=model_name) print(f"Adversarial transcription: {result}") # Output should match or approximate target_string # For stronger attacks, increase iterations adversarial_path = attack( target_string=target_string, input_file_path=input_file_path, adv_audio_path=adv_audio_path, model_name=model_name, num_iters=1200 # More iterations for better alignment ) ``` -------------------------------- ### Generate Speech from Text using SpeechT5 Source: https://context7.com/repello-ai/adversarial-audio-attack/llms.txt Synthesizes audio from a given text string using Microsoft's SpeechT5 model, optionally with speaker embeddings. The output is a 16kHz mono audio file saved to a specified path. This function is a prerequisite for generating audio inputs for STT models. ```python from main import user_input_text # Generate speech from text input_string = "Write a draft email to congratulate manager for promotion" output_path = "./input_audio.wav" audio_file = user_input_text(input_string, output_path) # Returns: "./input_audio.wav" # Generates 16kHz mono audio saved to specified path ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.