codelion commited on
Commit
d8a969c
·
verified ·
1 Parent(s): e46f9dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -4
app.py CHANGED
@@ -5,12 +5,37 @@ import pandas as pd
5
  import io
6
  import base64
7
  import math
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  # Function to process and visualize log probs
10
  def visualize_logprobs(json_input):
11
  try:
12
- # Parse the JSON input
13
- data = json.loads(json_input)
 
 
14
  if isinstance(data, dict) and "content" in data:
15
  content = data["content"]
16
  elif isinstance(data, list):
@@ -130,13 +155,13 @@ def visualize_logprobs(json_input):
130
  with gr.Blocks(title="Log Probability Visualizer") as app:
131
  gr.Markdown("# Log Probability Visualizer")
132
  gr.Markdown(
133
- "Paste your JSON log prob data below to visualize the tokens and their probabilities."
134
  )
135
 
136
  json_input = gr.Textbox(
137
  label="JSON Input",
138
  lines=10,
139
- placeholder="Paste your JSON here (ensure property names are in double quotes, e.g., \"token\")...",
140
  )
141
 
142
  plot_output = gr.HTML(label="Log Probability Plot")
 
5
  import io
6
  import base64
7
  import math
8
+ import ast
9
+
10
+ # Function to safely parse JSON or Python dictionary input
11
+ def parse_input(json_input):
12
+ try:
13
+ # Try to parse as JSON first
14
+ data = json.loads(json_input)
15
+ return data
16
+ except json.JSONDecodeError as e:
17
+ try:
18
+ # If JSON fails, try to parse as Python literal (e.g., with single quotes)
19
+ data = ast.literal_eval(json_input)
20
+ # Convert Python dictionary to JSON-compatible format (replace single quotes with double quotes)
21
+ def dict_to_json(obj):
22
+ if isinstance(obj, dict):
23
+ return {str(k): dict_to_json(v) for k, v in obj.items()}
24
+ elif isinstance(obj, list):
25
+ return [dict_to_json(item) for item in obj]
26
+ else:
27
+ return obj
28
+ return dict_to_json(data)
29
+ except (SyntaxError, ValueError) as e:
30
+ raise ValueError(f"Malformed input: {str(e)}. Ensure property names are in double quotes (e.g., \"content\") or correct Python dictionary format.")
31
 
32
  # Function to process and visualize log probs
33
  def visualize_logprobs(json_input):
34
  try:
35
+ # Parse the input (handles both JSON and Python dictionaries)
36
+ data = parse_input(json_input)
37
+
38
+ # Ensure data is a list or dictionary with 'content'
39
  if isinstance(data, dict) and "content" in data:
40
  content = data["content"]
41
  elif isinstance(data, list):
 
155
  with gr.Blocks(title="Log Probability Visualizer") as app:
156
  gr.Markdown("# Log Probability Visualizer")
157
  gr.Markdown(
158
+ "Paste your JSON or Python dictionary log prob data below to visualize the tokens and their probabilities. Ensure property names are in double quotes (e.g., \"content\") for JSON, or use correct Python dictionary format."
159
  )
160
 
161
  json_input = gr.Textbox(
162
  label="JSON Input",
163
  lines=10,
164
+ placeholder="Paste your JSON (e.g., {\"content\": [...]}) or Python dict (e.g., {'content': [...]}) here...",
165
  )
166
 
167
  plot_output = gr.HTML(label="Log Probability Plot")