chuckfinca Claude Opus 4.6 (1M context) commited on
Commit
3076398
·
1 Parent(s): 69f9284

Consolidate format_stats into single function

Browse files

Merge format_stats and format_stats_from_trace — one function
handles both Trace objects and dicts.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +19 -23
app.py CHANGED
@@ -156,31 +156,27 @@ def save_uploaded_files(file_paths: list[str]) -> Path:
156
  return workspace
157
 
158
 
159
- def format_stats(trace: object) -> str:
160
- cached = trace.cached_tokens
161
- cache_str = f" ({cached} cached)" if cached else ""
162
- model_name = trace.model.split("/")[-1] if trace.model else ""
163
- parts = [
164
- model_name,
165
- f"{trace.prompt_tokens + trace.completion_tokens:,} tokens{cache_str}",
166
- f"{len(trace.tool_calls)} tool calls",
167
- f"{trace.wall_time_s:.1f}s",
168
- ]
169
- if trace.cost:
170
- parts.append(f"${trace.cost:.4f}")
171
- return " · ".join(parts)
172
-
 
 
 
 
173
 
174
- def format_stats_from_trace(trace: dict) -> str:
175
- cached = trace.get("cached_tokens", 0)
176
  cache_str = f" ({cached} cached)" if cached else ""
177
- model = trace.get("model", "")
178
  model_name = model.split("/")[-1] if model else ""
179
- prompt = trace.get("prompt_tokens", 0)
180
- completion = trace.get("completion_tokens", 0)
181
- tool_calls = trace.get("tool_calls", [])
182
- wall = trace.get("wall_time_s", 0)
183
- cost = trace.get("cost")
184
  parts = [
185
  model_name,
186
  f"{prompt + completion:,} tokens{cache_str}",
@@ -396,7 +392,7 @@ def build_app() -> gr.Blocks:
396
  "question": data.get("question", ""),
397
  "answer": clean_answer,
398
  "sources": sources,
399
- "stats": format_stats_from_trace(trace),
400
  "source_tag": data.get("source", ""),
401
  "trace_html": trace_html,
402
  "filename": safe_name,
 
156
  return workspace
157
 
158
 
159
+ def format_stats(trace) -> str:
160
+ """Format trace stats for display. Accepts a Trace object or dict."""
161
+ if isinstance(trace, dict):
162
+ cached = trace.get("cached_tokens", 0)
163
+ model = trace.get("model", "")
164
+ prompt = trace.get("prompt_tokens", 0)
165
+ completion = trace.get("completion_tokens", 0)
166
+ tool_calls = trace.get("tool_calls", [])
167
+ wall = trace.get("wall_time_s", 0)
168
+ cost = trace.get("cost")
169
+ else:
170
+ cached = trace.cached_tokens
171
+ model = trace.model
172
+ prompt = trace.prompt_tokens
173
+ completion = trace.completion_tokens
174
+ tool_calls = trace.tool_calls
175
+ wall = trace.wall_time_s
176
+ cost = trace.cost
177
 
 
 
178
  cache_str = f" ({cached} cached)" if cached else ""
 
179
  model_name = model.split("/")[-1] if model else ""
 
 
 
 
 
180
  parts = [
181
  model_name,
182
  f"{prompt + completion:,} tokens{cache_str}",
 
392
  "question": data.get("question", ""),
393
  "answer": clean_answer,
394
  "sources": sources,
395
+ "stats": format_stats(trace),
396
  "source_tag": data.get("source", ""),
397
  "trace_html": trace_html,
398
  "filename": safe_name,