codelion commited on
Commit
f2687d8
·
verified ·
1 Parent(s): c28bdaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -6
app.py CHANGED
@@ -7,15 +7,59 @@ import base64
7
  import ast
8
  import math
9
 
 
 
 
 
 
 
 
 
 
10
  # Function to process and visualize log probs
11
  def visualize_logprobs(json_input):
12
  try:
13
- # Try to parse as JSON first
14
  try:
15
- data = json.loads(json_input)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  except json.JSONDecodeError:
17
  # If JSON fails, try to parse as Python literal (e.g., with single quotes)
18
- data = ast.literal_eval(json_input)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  # Ensure data is a list or dictionary with 'content'
21
  if isinstance(data, dict) and 'content' in data:
@@ -29,17 +73,17 @@ def visualize_logprobs(json_input):
29
  tokens = []
30
  logprobs = []
31
  for entry in content:
32
- if entry['logprob'] is not None and math.isfinite(entry['logprob']):
33
  tokens.append(entry['token'])
34
  logprobs.append(entry['logprob'])
35
 
36
  # Prepare data for the table
37
  table_data = []
38
  for entry in content:
39
- if entry['logprob'] is not None and math.isfinite(entry['logprob']):
40
  token = entry['token']
41
  logprob = entry['logprob']
42
- top_logprobs = entry['top_logprobs'] or {}
43
 
44
  # Filter out non-finite (e.g., -inf, inf, nan) log probs from top_logprobs
45
  finite_top_logprobs = {k: v for k, v in top_logprobs.items() if math.isfinite(v)}
 
7
  import ast
8
  import math
9
 
10
+ # Function to safely convert string representations of infinity
11
+ def parse_infinity(value):
12
+ if isinstance(value, str):
13
+ if value.lower() == '-infinity' or value.lower() == '-inf':
14
+ return float('-inf')
15
+ elif value.lower() == 'infinity' or value.lower() == 'inf':
16
+ return float('inf')
17
+ return value
18
+
19
  # Function to process and visualize log probs
20
  def visualize_logprobs(json_input):
21
  try:
22
+ # Try to parse as JSON first, handling string representations of infinity
23
  try:
24
+ # Attempt to load JSON, replacing -inf with "-Infinity" if needed
25
+ def replace_inf(s):
26
+ import re
27
+ return re.sub(r'-inf', '"-Infinity"', re.sub(r'inf', '"Infinity"', s))
28
+
29
+ data = json.loads(replace_inf(json_input))
30
+ # Convert string "Infinity" or "-Infinity" back to float if needed
31
+ if isinstance(data, dict) and 'content' in data:
32
+ for entry in data['content']:
33
+ if 'logprob' in entry:
34
+ entry['logprob'] = parse_infinity(entry['logprob'])
35
+ if 'top_logprobs' in entry:
36
+ entry['top_logprobs'] = {k: parse_infinity(v) for k, v in entry['top_logprobs'].items()}
37
+ elif isinstance(data, list):
38
+ for entry in data:
39
+ if 'logprob' in entry:
40
+ entry['logprob'] = parse_infinity(entry['logprob'])
41
+ if 'top_logprobs' in entry:
42
+ entry['top_logprobs'] = {k: parse_infinity(v) for k, v in entry['top_logprobs'].items()}
43
+
44
  except json.JSONDecodeError:
45
  # If JSON fails, try to parse as Python literal (e.g., with single quotes)
46
+ try:
47
+ data = ast.literal_eval(json_input)
48
+ # Ensure -inf is handled as float('-inf')
49
+ if isinstance(data, dict) and 'content' in data:
50
+ for entry in data['content']:
51
+ if 'logprob' in entry and isinstance(entry['logprob'], str):
52
+ entry['logprob'] = parse_infinity(entry['logprob'])
53
+ if 'top_logprobs' in entry:
54
+ entry['top_logprobs'] = {k: parse_infinity(v) for k, v in entry['top_logprobs'].items()}
55
+ elif isinstance(data, list):
56
+ for entry in data:
57
+ if 'logprob' in entry and isinstance(entry['logprob'], str):
58
+ entry['logprob'] = parse_infinity(entry['logprob'])
59
+ if 'top_logprobs' in entry:
60
+ entry['top_logprobs'] = {k: parse_infinity(v) for k, v in entry['top_logprobs'].items()}
61
+ except (SyntaxError, ValueError) as e:
62
+ raise ValueError(f"Malformed input: {str(e)}")
63
 
64
  # Ensure data is a list or dictionary with 'content'
65
  if isinstance(data, dict) and 'content' in data:
 
73
  tokens = []
74
  logprobs = []
75
  for entry in content:
76
+ if 'logprob' in entry and entry['logprob'] is not None and math.isfinite(entry['logprob']):
77
  tokens.append(entry['token'])
78
  logprobs.append(entry['logprob'])
79
 
80
  # Prepare data for the table
81
  table_data = []
82
  for entry in content:
83
+ if 'logprob' in entry and entry['logprob'] is not None and math.isfinite(entry['logprob']):
84
  token = entry['token']
85
  logprob = entry['logprob']
86
+ top_logprobs = entry.get('top_logprobs', {})
87
 
88
  # Filter out non-finite (e.g., -inf, inf, nan) log probs from top_logprobs
89
  finite_top_logprobs = {k: v for k, v in top_logprobs.items() if math.isfinite(v)}