Spaces:
Running
Running
Commit ·
3076398
1
Parent(s): 69f9284
Consolidate format_stats into single function
Browse filesMerge 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>
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
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 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":
|
| 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,
|