Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,17 @@ import matplotlib.pyplot as plt
|
|
| 4 |
import pandas as pd
|
| 5 |
import io
|
| 6 |
import base64
|
|
|
|
| 7 |
|
| 8 |
# Function to process and visualize log probs
|
| 9 |
def visualize_logprobs(json_input):
|
| 10 |
try:
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Extract tokens and log probs, skipping None values
|
| 15 |
tokens = [entry['token'] for entry in data['content'] if entry['logprob'] is not None]
|
|
@@ -67,10 +72,10 @@ def visualize_logprobs(json_input):
|
|
| 67 |
# Gradio interface
|
| 68 |
with gr.Blocks(title="Log Probability Visualizer") as app:
|
| 69 |
gr.Markdown("# Log Probability Visualizer")
|
| 70 |
-
gr.Markdown("Paste your JSON log prob data below to visualize the tokens and their probabilities.")
|
| 71 |
|
| 72 |
# Input
|
| 73 |
-
json_input = gr.Textbox(label="JSON Input", lines=10, placeholder="Paste your JSON here...")
|
| 74 |
|
| 75 |
# Outputs
|
| 76 |
plot_output = gr.HTML(label="Log Probability Plot")
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
import io
|
| 6 |
import base64
|
| 7 |
+
import ast # For safely evaluating Python literals
|
| 8 |
|
| 9 |
# Function to process and visualize log probs
|
| 10 |
def visualize_logprobs(json_input):
|
| 11 |
try:
|
| 12 |
+
# Try to parse as JSON first
|
| 13 |
+
try:
|
| 14 |
+
data = json.loads(json_input)
|
| 15 |
+
except json.JSONDecodeError:
|
| 16 |
+
# If JSON fails, try to parse as Python literal (e.g., with single quotes)
|
| 17 |
+
data = ast.literal_eval(json_input)
|
| 18 |
|
| 19 |
# Extract tokens and log probs, skipping None values
|
| 20 |
tokens = [entry['token'] for entry in data['content'] if entry['logprob'] is not None]
|
|
|
|
| 72 |
# Gradio interface
|
| 73 |
with gr.Blocks(title="Log Probability Visualizer") as app:
|
| 74 |
gr.Markdown("# Log Probability Visualizer")
|
| 75 |
+
gr.Markdown("Paste your JSON or Python dictionary log prob data below to visualize the tokens and their probabilities.")
|
| 76 |
|
| 77 |
# Input
|
| 78 |
+
json_input = gr.Textbox(label="JSON Input", lines=10, placeholder="Paste your JSON or Python dict here...")
|
| 79 |
|
| 80 |
# Outputs
|
| 81 |
plot_output = gr.HTML(label="Log Probability Plot")
|