Strictly adhere to PEP-8 standards, ensuring code is clean, well-structured, and follows Python's style guide for readability.
Use appropriate modules like typing for type hints, ensuring code clarity, maintainability, and leveraging Python's standard libraries effectively.
Focus on time, space, robustness, scalability; write optimized, efficient code that can handle large datasets and scale seamlessly.
Implement comprehensive error handling, catching exceptions gracefully, providing meaningful error messages, and ensuring code robustness under unexpected conditions.
Ensure the developer agent writes advanced, maintainable Python code, balancing performance and readability while adhering to best practices.
TASK : robustness , scable , generalised , code , zero hard coding
Visualize top k probable tokens from a single token_id input using Plotly. Create a loop to generate and visualize 30 words, updating the bar chart dynamically for each new token. Highlight top tokens with distinct colors and include labels, title, and legend.
reference code: plot should one by one :
decoding strategies:
Greedy Decoding
Beam Search
Top-k Sampling
Top-p (Nucleus) Sampling
Temperature Sampling
Random Sampling
Diverse Beam Search
Assistant
Sure! Below is a comprehensive, robust, and scalable Python script that visualizes the top k probable tokens from a given token_id input using Plotly. The script includes a loop to generate and visualize 30 tokens, updating the bar chart dynamically for each new token. It highlights the top tokens with distinct colors and includes labels, titles, and a legend. The code adheres strictly to PEP-8 standards, utilizes type hints from the typing module, and incorporates robust error handling to ensure maintainability and scalability.
The script is generalized with no hard-coded values, allowing for easy adjustments of parameters such as k, the number of tokens to generate, and the decoding strategy. Additionally, it includes placeholder functions for different decoding strategies, which can be implemented as needed.
To keep the script self-contained, a mock function simulates token probability distributions. In a real-world scenario, you would replace this mock function with actual model predictions, such as those from a language model like GPT.
python
import time
from typing import List, Tuple, Callable, Dict, Any
import plotly.graph_objs as go
from plotly.subplots import make_subplots
import random
import string
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class TokenVisualizer:
"""
A class to visualize the top k probable tokens from a token_id input using Plotly.
It generates and visualizes tokens dynamically in a loop.
"""
def __init__(
self,
k: int = 5,
num_tokens: int = 30,
update_interval: float = 1.0,
decoding_strategy: str = "greedy",
) -> None:
"""
Initializes the TokenVisualizer.
Args:
k (int): Number of top tokens to display.
num_tokens (int): Number of tokens to generate and visualize.
update_interval (float): Time interval between updates in seconds.
decoding_strategy (str): Decoding strategy to use.
"""
self.k = k
self.num_tokens = num_tokens
self.update_interval = update_interval
self.decoding_strategy = decoding_strategy.lower()
self.decoding_functions: Dict[str, Callable[[Any], Tuple[str, float]]] = {
"greedy": self.greedy_decoding,
"beam_search": self.beam_search,
"top_k_sampling": self.top_k_sampling,
"top_p_sampling": self.top_p_sampling,
"temperature_sampling": self.temperature_sampling,
"random_sampling": self.random_sampling,
"diverse_beam_search": self.diverse_beam_search,
}
self.validate_parameters()
def validate_parameters(self) -> None:
"""Validates the initialization parameters."""
if self.k <= 0:
logger.error("Parameter k must be positive.")
raise ValueError("Parameter k must be positive.")
if self.num_tokens <= 0:
logger.error("Parameter num_tokens must be positive.")
raise ValueError("Parameter num_tokens must be positive.")
if self.decoding_strategy not in self.decoding_functions:
logger.error(
f"Decoding strategy '{self.decoding_strategy}' is not supported."
)
raise ValueError(
f"Decoding strategy '{self.decoding_strategy}' is not supported."
)
def generate_mock_probabilities(self) -> Dict[str, float]:
"""
Generates mock token probabilities for demonstration purposes.
Returns:
Dict[str, float]: A dictionary mapping tokens to their probabilities.
"""
tokens = [
''.join(random.choices(string.ascii_lowercase, k=3)) for _ in range(50)
]
probabilities = [random.random() for _ in range(50)]
total = sum(probabilities)
normalized_probs = [p / total for p in probabilities]
token_probs = dict(zip(tokens, normalized_probs))
return token_probs
def greedy_decoding(self, token_probs: Dict[str, float]) -> Tuple[str, float]:
"""
Greedy decoding strategy: selects the token with the highest probability.
Args:
token_probs (Dict[str, float]): Token probabilities.
Returns:
Tuple[str, float]: Selected token and its probability.
"""
token, prob = max(token_probs.items(), key=lambda item: item[1])
return token, prob
def beam_search(self, token_probs: Dict[str, float]) -> Tuple[str, float]:
"""
Placeholder for Beam Search decoding strategy.
Args:
token_probs (Dict[str, float]): Token probabilities.
Returns:
Tuple[str, float]: Selected token and its probability.
"""
# Implement Beam Search logic here
return self.greedy_decoding(token_probs)
def top_k_sampling(self, token_probs: Dict[str, float], top_k: int = 5) -> Tuple[str, float]:
"""
Top-k Sampling decoding strategy.
Args:
token_probs (Dict[str, float]): Token probabilities.
top_k (int): Number of top tokens to consider.
Returns:
Tuple[str, float]: Selected token and its probability.
"""
sorted_tokens = sorted(token_probs.items(), key=lambda item: item[1], reverse=True)
top_k_tokens = sorted_tokens[:top_k]
tokens, probs = zip(*top_k_tokens)
normalized_probs = [p / sum(probs) for p in probs]
selected_token = random.choices(tokens, weights=normalized_probs, k=1)[0]
selected_prob = dict(top_k_tokens)[selected_token]
return selected_token, selected_prob
def top_p_sampling(self, token_probs: Dict[str, float], top_p: float = 0.9) -> Tuple[str, float]:
"""
Top-p (Nucleus) Sampling decoding strategy.
Args:
token_probs (Dict[str, float]): Token probabilities.
top_p (float): Cumulative probability threshold.
Returns:
Tuple[str, float]: Selected token and its probability.
"""
sorted_tokens = sorted(token_probs.items(), key=lambda item: item[1], reverse=True)
cumulative_prob = 0.0
top_p_tokens = []
for token, prob in sorted_tokens:
cumulative_prob += prob
top_p_tokens.append((token, prob))
if cumulative_prob >= top_p:
break
tokens, probs = zip(*top_p_tokens)
normalized_probs = [p / sum(probs) for p in probs]
selected_token = random.choices(tokens, weights=normalized_probs, k=1)[0]
selected_prob = dict(top_p_tokens)[selected_token]
return selected_token, selected_prob
def temperature_sampling(
self, token_probs: Dict[str, float], temperature: float = 1.0
) -> Tuple[str, float]:
"""
Temperature Sampling decoding strategy.
Args:
token_probs (Dict[str, float]): Token probabilities.
temperature (float): Temperature parameter.
Returns:
Tuple[str, float]: Selected token and its probability.
"""
if temperature <= 0:
logger.error("Temperature must be greater than 0.")
raise ValueError("Temperature must be greater than 0.")
tokens, probs = zip(*token_probs.items())
adjusted_probs = [p ** (1 / temperature) for p in probs]
total = sum(adjusted_probs)
normalized_probs = [p / total for p in adjusted_probs]
selected_token = random.choices(tokens, weights=normalized_probs, k=1)[0]
selected_prob = token_probs[selected_token]
return selected_token, selected_prob
def random_sampling(self, token_probs: Dict[str, float]) -> Tuple[str, float]:
"""
Random Sampling decoding strategy.
Args:
token_probs (Dict[str, float]): Token probabilities.
Returns:
Tuple[str, float]: Selected token and its probability.
"""
tokens, probs = zip(*token_probs.items())
selected_token = random.choices(tokens, weights=probs, k=1)[0]
selected_prob = token_probs[selected_token]
return selected_token, selected_prob
def diverse_beam_search(self, token_probs: Dict[str, float]) -> Tuple[str, float]:
"""
Placeholder for Diverse Beam Search decoding strategy.
Args:
token_probs (Dict[str, float]): Token probabilities.
Returns:
Tuple[str, float]: Selected token and its probability.
"""
# Implement Diverse Beam Search logic here
return self.greedy_decoding(token_probs)
def get_next_token(self, token_probs: Dict[str, float]) -> Tuple[str, float]:
"""
Selects the next token based on the decoding strategy.
Args:
token_probs (Dict[str, float]): Token probabilities.
Returns:
Tuple[str, float]: Selected token and its probability.
"""
decoding_func = self.decoding_functions[self.decoding_strategy]
try:
if self.decoding_strategy == "top_k_sampling":
return decoding_func(token_probs, top_k=self.k)
elif self.decoding_strategy == "top_p_sampling":
return decoding_func(token_probs, top_p=0.9)
elif self.decoding_strategy == "temperature_sampling":
return decoding_func(token_probs, temperature=1.0)
else:
return decoding_func(token_probs)
except Exception as e:
logger.exception("Error in decoding strategy.")
raise e
def visualize(self) -> None:
"""
Generates and visualizes tokens dynamically using Plotly.
"""
try:
# Initialize the Plotly figure
fig = go.FigureWidget()
fig.update_layout(
title=f"Top {self.k} Probable Tokens",
xaxis_title="Tokens",
yaxis_title="Probability",
showlegend=False,
)
bar = fig.add_bar(x=[], y=[], marker_color=[])
fig.show()
generated_tokens: List[str] = []
for i in range(self.num_tokens):
# Simulate fetching token probabilities
token_probs = self.generate_mock_probabilities()
# Get top k tokens
sorted_tokens = sorted(
token_probs.items(), key=lambda item: item[1], reverse=True
)
top_k = sorted_tokens[: self.k]
tokens, probs = zip(*top_k)
# Update the bar chart
with fig.batch_update():
bar.x = tokens
bar.y = probs
colors = ["red"] + ["blue"] * (self.k - 1)
bar.marker.color = colors
fig.layout.title.text = (
f"Top {self.k} Probable Tokens - Token {i + 1}"
)
# Append the selected token
selected_token, selected_prob = self.get_next_token(token_probs)
generated_tokens.append(selected_token)
logger.info(f"Generated Token {i + 1}: {selected_token} (P={selected_prob:.4f})")
# Wait for the specified interval before next update
time.sleep(self.update_interval)
except Exception as e:
logger.exception("An error occurred during visualization.")
raise e
def main() -> None:
"""
Main function to run the TokenVisualizer.
"""
try:
visualizer = TokenVisualizer(
k=5,
num_tokens=30,
update_interval=0.5,
decoding_strategy="top_k_sampling",
)
visualizer.visualize()
except Exception as e:
logger.error("Failed to run the TokenVisualizer.")
logger.error(str(e))
if __name__ == "__main__":
main()
Explanation of the Code
Imports and Logging Configuration:
The script imports necessary modules, including typing for type hints, plotly for visualization, and logging for debug statements.
Logging is configured to output informational messages, which helps in tracking the script's execution flow and catching potential issues.
TokenVisualizer Class:
Initialization (__init__):
Accepts parameters such as k, num_tokens, update_interval, and decoding_strategy.
Initializes a dictionary mapping decoding strategy names to their corresponding methods.
Validates the input parameters to ensure they are within acceptable ranges and that the chosen decoding strategy is supported.
Mock Probability Generation (generate_mock_probabilities):
Generates a mock distribution of token probabilities. This function creates random tokens and assigns them random probabilities that sum to 1.
In a real-world application, this method should be replaced with actual model predictions.
Decoding Strategies:
Greedy Decoding: Selects the token with the highest probability.
Beam Search: Placeholder for Beam Search strategy.
Top-K Sampling: Samples the next token from the top k tokens based on their probabilities.
Top-P (Nucleus) Sampling: Samples from the smallest possible set of tokens whose cumulative probability exceeds p.
Temperature Sampling: Adjusts the probability distribution based on the temperature parameter before sampling.
Random Sampling: Selects the next token purely based on the probability distribution.
Diverse Beam Search: Placeholder for Diverse Beam Search strategy.
Token Selection (get_next_token):
Selects the next token based on the chosen decoding strategy.
Handles any exceptions that might occur during the token selection process.
Visualization (visualize):
Customization and Extension
Decoding Strategies:
The script includes placeholders for beam_search and diverse_beam_search. These methods can be implemented based on specific requirements or integrated with libraries that provide these decoding strategies.
Integration with Actual Models:
Replace the generate_mock_probabilities method with actual predictions from a language model (e.g., using Hugging Face Transformers) to visualize real token probabilities.
Parameter Adjustments:
You can adjust parameters like k, num_tokens, update_interval, and decoding_strategy either by modifying the main function or by extending the script to accept command-line arguments.
Error Handling Enhancements:
Additional specific exceptions can be caught and handled as needed, especially when integrating with external models or data sources.
Running the Script
To run the script:
Install Dependencies:
Ensure you have Plotly installed. You can install it using pip:
bash
pip install plotly
Execute the Script:
Run the script using Python:
bash
python token_visualizer.py
Replace token_visualizer.py with the name you saved the script as.
Visualization:
A Plotly window will open displaying the bar chart. The chart will update dynamically, showcasing the top k tokens at each iteration. The top token will be highlighted in red, while the others will be in blue.
This script provides a solid foundation for visualizing token probabilities and can be extended or integrated into larger projects as needed.