Update app.py
Browse files57 unmodified lines585960616263646566676814 unmodified lines83848586878889909192939451 unmodified lines14614714814915015115272 unmodified lines22522622722822923023123223323423557 unmodified linesreturn float(value)return 0.0 # Default for any other type# Function to create an empty Plotly figuredef create_empty_figure(title):return go.Figure().update_layout(title=title, xaxis_title="", yaxis_title="", showlegend=False)# Function to process and visualize the full log probs with dynamic top_logprobs, handling Nonedef visualize_logprobs(json_input):try:# Parse the input (handles both JSON and Python dictionaries)14 unmodified linesfor entry in content:logprob = ensure_float(entry.get("logprob", None))if math.isfinite(logprob) and logprob >= -100000:tokens.append(entry["token"])logprobs.append(logprob)# Get top_logprobs, default to empty dict if Nonetop_probs = entry.get("top_logprobs", {})if top_probs is None:logger.debug("top_logprobs is None for token: %s, using empty dict", entry["token"])top_probs = {} # Default to empty dict for None# Ensure all values in top_logprobs are floats and create a list of tuplesfinite_top_probs = []51 unmodified linesfor i, entry in enumerate(content):logprob = ensure_float(entry.get("logprob", None))if math.isfinite(logprob) and logprob >= -100000 and "top_logprobs" in entry:token = entry["token"]top_logprobs = entry.get("top_logprobs", {})if top_logprobs is None:logger.debug("top_logprobs is None for token: %s, using empty dict", token)72 unmodified lineslogger.error("Visualization failed: %s", str(e))return (create_empty_figure("Log Probabilities of Generated Tokens"), None, "No finite log probabilities to display.", create_empty_figure("Top Token Log Probabilities"), create_empty_figure("Significant Probability Drops"))# Gradio interface with full dataset visualization and dynamic top_logprobswith gr.Blocks(title="Log Probability Visualizer") as app:gr.Markdown("# Log Probability Visualizer")gr.Markdown("Paste your JSON or Python dictionary log prob data below to visualize all tokens at once. Fixed filter ≥ -100000, dynamic number of top_logprobs.")with gr.Row():57 unmodified lines58596061626364656667686970717273747514 unmodified lines9091929394959697989910010110251 unmodified lines15415515615715815916072 unmodified lines23323423523623723823924024124224357 unmodified linesreturn float(value)return 0.0 # Default for any other type# Function to get or generate a token value (default to "Unknown" if missing)def get_token(entry):token = entry.get("token", "Unknown")if token == "Unknown":logger.warning("Missing 'token' key for entry: %s, using 'Unknown'", entry)return token# Function to create an empty Plotly figuredef create_empty_figure(title):return go.Figure().update_layout(title=title, xaxis_title="", yaxis_title="", showlegend=False)# Function to process and visualize the full log probs with dynamic top_logprobs, handling missing tokensdef visualize_logprobs(json_input):try:# Parse the input (handles both JSON and Python dictionaries)14 unmodified linesfor entry in content:logprob = ensure_float(entry.get("logprob", None))if math.isfinite(logprob) and logprob >= -100000:token = get_token(entry) # Safely get token, defaulting to "Unknown" if missingtokens.append(token)logprobs.append(logprob)# Get top_logprobs, default to empty dict if Nonetop_probs = entry.get("top_logprobs", {})if top_probs is None:logger.debug("top_logprobs is None for token: %s, using empty dict", token)top_probs = {} # Default to empty dict for None# Ensure all values in top_logprobs are floats and create a list of tuplesfinite_top_probs = []51 unmodified linesfor i, entry in enumerate(content):logprob = ensure_float(entry.get("logprob", None))if math.isfinite(logprob) and logprob >= -100000 and "top_logprobs" in entry:token = get_token(entry) # Safely get token, defaulting to "Unknown" if missingtop_logprobs = entry.get("top_logprobs", {})if top_logprobs is None:logger.debug("top_logprobs is None for token: %s, using empty dict", token)72 unmodified lineslogger.error("Visualization failed: %s", str(e))return (create_empty_figure("Log Probabilities of Generated Tokens"), None, "No finite log probabilities to display.", create_empty_figure("Top Token Log Probabilities"), create_empty_figure("Significant Probability Drops"))# Gradio interface with full dataset visualization, dynamic top_logprobs, and handling missing tokenswith gr.Blocks(title="Log Probability Visualizer") as app:gr.Markdown("# Log Probability Visualizer")gr.Markdown("Paste your JSON or Python dictionary log prob data below to visualize all tokens at once. Fixed filter ≥ -100000, dynamic number of top_logprobs, handles missing 'token'.")with gr.Row():
app.py+14-6