<jupyter_start><jupyter_text>Weights & Biases Scriptable AlertsUse W&B Alerts to sent yourself a Slack message or email when something happens in your Python script.[Alerts docs](https://docs.wandb.com/app/features/alerts)  1. Install the W&B Library<jupyter_code>!pip install wandb -qqq<jupyter_output><empty_output><jupyter_text>2. Launch a script that triggers alerts<jupyter_code>from datetime import timedelta
import random
import wandb
from wandb import AlertLevel

# Initialize a new run in Weights & Biases
run = wandb.init(project="test_alerts",
                 # Config lets you track run settings in a central place
                 config={
                     "threshold": 0.3, # The minimum acceptable accuracy
                     "max_steps": 1000, # The max number of steps for this run
                 })
config = wandb.config

# Simulating a model training loop
for training_step in range(config.max_steps):

  # Generate a random number for accuracy
  accuracy = round(random.random() + random.random(), 3)
  print(accuracy)
  wandb.log({"Accuracy": accuracy})

  # If the accuracy is below the threshold, fire an alert and stop the run
  if accuracy <= config.threshold:
    wandb.alert(
      title='Low Accuracy',
      text=f'Accuracy {accuracy} at step {training_step} is below the acceptable theshold',
      level=AlertLevel.WARN,
      wait_duration=timedelta(minutes=0)
    )
    break

# Mark the run as finished (useful in Jupyter notebooks)
run.finish()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08300395256916997, 'fraction_numerical': 0.006587615283267457, 'mean_word_length': 3.5208333333333335, 'pattern_counts': {'":': 3, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '647947', 'license': 'Apache-2.0', 'n_tokens_mistral': 458, 'n_tokens_neox': 440, 'n_words': 150, 'path': 'W&B_Scriptable_Alerts.ipynb', 'repo_name': 'sadjadasghari/Colab', 'size': '1518'}