File size: 12,789 Bytes
2bd094f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ba08e6
2bd094f
 
5ba08e6
2bd094f
 
 
 
5ba08e6
2bd094f
5ba08e6
2bd094f
 
 
 
 
5ba08e6
2bd094f
5ba08e6
2bd094f
 
 
 
 
 
 
5ba08e6
2bd094f
 
 
 
 
5ba08e6
2bd094f
5ba08e6
2bd094f
 
 
 
 
 
5ba08e6
2bd094f
 
5ba08e6
2bd094f
 
 
 
 
 
 
 
 
 
 
 
 
5ba08e6
2bd094f
 
5ba08e6
2bd094f
 
 
 
5ba08e6
2bd094f
 
 
 
5ba08e6
2bd094f
 
5ba08e6
2bd094f
 
 
 
 
 
 
 
5ba08e6
2bd094f
5ba08e6
 
2bd094f
 
 
5ba08e6
2bd094f
 
5ba08e6
2bd094f
 
 
 
5ba08e6
2bd094f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ba08e6
2bd094f
5ba08e6
2bd094f
 
5ba08e6
 
2bd094f
 
 
 
 
 
5ba08e6
2bd094f
 
5ba08e6
 
2bd094f
 
 
 
 
 
 
 
 
 
 
5ba08e6
2bd094f
 
 
 
5ba08e6
2bd094f
 
5ba08e6
2bd094f
 
987829f
 
2bd094f
 
 
 
 
987829f
 
 
2bd094f
 
 
 
 
 
5ba08e6
2bd094f
 
 
 
 
 
 
 
 
 
 
 
 
5ba08e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bd094f
5ba08e6
2bd094f
 
5ba08e6
2bd094f
5ba08e6
 
 
 
 
 
 
 
2bd094f
5ba08e6
2bd094f
 
 
5ba08e6
2bd094f
 
5ba08e6
2bd094f
 
5ba08e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bd094f
5ba08e6
 
 
 
 
2bd094f
 
5ba08e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a695de
2bd094f
92b319a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""
StructEval-T Analyzer
松尾研LLM講義2025 メインコンペ用 推論結果分析ツール
"""

import json
import csv
import io
import re
import traceback
from collections import Counter, defaultdict
from pathlib import Path

import gradio as gr
import pandas as pd

# ---------------------------------------------------------------------------
# 1. Syntax Validators
# ---------------------------------------------------------------------------

def validate_json(text):
    try:
        json.loads(text)
        return True, ""
    except json.JSONDecodeError as e:
        return False, f"JSONDecodeError: {e.msg} (line {e.lineno})"

def validate_yaml(text):
    try:
        import yaml
        yaml.safe_load(text)
        return True, ""
    except Exception as e:
        return False, f"YAMLError: {e}"

def validate_toml(text):
    try:
        import tomllib
        tomllib.loads(text)
        return True, ""
    except Exception as e:
        return False, f"TOMLError: {e}"

def validate_xml(text):
    try:
        import xml.etree.ElementTree as ET
        ET.fromstring(text)
        return True, ""
    except Exception as e:
        return False, f"XMLError: {e}"

def validate_csv(text):
    try:
        reader = csv.reader(io.StringIO(text))
        rows = list(reader)
        if len(rows) == 0:
            return False, "Empty CSV"
        if len(rows) == 1:
            return False, "Only header"
        col_counts = [len(row) for row in rows]
        if len(set(col_counts)) > 1:
            return False, f"Inconsistent cols: {set(col_counts)}"
        return True, ""
    except Exception as e:
        return False, f"CSVError: {e}"

VALIDATORS = {
    "JSON": validate_json,
    "YAML": validate_yaml,
    "TOML": validate_toml,
    "XML": validate_xml,
    "CSV": validate_csv,
}

# ---------------------------------------------------------------------------
# 2. Error Pattern Classifier
# ---------------------------------------------------------------------------

def classify_error_patterns(generation, output_type):
    patterns = []
    if re.search(r"```\w*", generation):
        patterns.append("markdown_block")
    first_line = generation.strip().split("\n")[0] if generation.strip() else ""
    nl_indicators = ["here is", "here's", "below is", "sure", "certainly", "let me"]
    if any(ind in first_line.lower() for ind in nl_indicators):
        patterns.append("natural_language_prefix")
    last_lines = generation.strip().split("\n")[-3:] if generation.strip() else []
    last_text = " ".join(last_lines).lower()
    if any(ind in last_text for ind in ["note:", "explanation:", "this ", "the above"]):
        patterns.append("natural_language_suffix")
    if output_type == "JSON":
        if generation.count("{") + generation.count("[") > generation.count("}") + generation.count("]"):
            patterns.append("truncation")
    elif output_type == "XML":
        open_tags = len(re.findall(r"<[^/!?][^>]*>", generation))
        close_tags = len(re.findall(r"</[^>]+>", generation))
        if open_tags > close_tags + 1:
            patterns.append("truncation")
    if not generation.strip():
        patterns.append("empty_output")
    if re.search(r"<think>|</think>", generation):
        patterns.append("cot_leakage")
    if re.search(r"<tool_call>", generation):
        patterns.append("tool_call_leakage")
    return patterns if patterns else ["unknown"]

# ---------------------------------------------------------------------------
# 3. Core Analysis
# ---------------------------------------------------------------------------

def load_public_150(file_path):
    with open(file_path, "r", encoding="utf-8") as f:
        data = json.load(f)
    return {item["task_id"]: item for item in data}

def analyze_single_inference(inference_data, task_info):
    results = []
    for item in inference_data:
        task_id = item.get("task_id", "")
        generation = item.get("generation", "")
        info = task_info.get(task_id, {})
        output_type = info.get("output_type", "UNKNOWN")
        task_name = info.get("task_name", "UNKNOWN")
        validator = VALIDATORS.get(output_type)
        if validator:
            is_valid, error_msg = validator(generation)
        else:
            is_valid, error_msg = False, f"Unknown format: {output_type}"
        if not is_valid:
            error_patterns = classify_error_patterns(generation, output_type)
        else:
            error_patterns = []
        results.append({
            "task_id": task_id,
            "task_name": task_name,
            "output_type": output_type,
            "is_valid": is_valid,
            "error_msg": error_msg,
            "error_patterns": ",".join(error_patterns) if error_patterns else "",
            "generation_length": len(generation),
            "generation_preview": generation[:200],
        })
    return pd.DataFrame(results)

def compute_summary(df):
    total = len(df)
    valid = int(df["is_valid"].sum())
    summary = {
        "total_tasks": total,
        "parse_success": valid,
        "parse_fail": total - valid,
        "parse_rate": f"{valid / total * 100:.1f}%" if total > 0 else "N/A",
    }
    format_stats = {}
    for fmt in ["JSON", "YAML", "TOML", "XML", "CSV"]:
        fmt_df = df[df["output_type"] == fmt]
        fmt_total = len(fmt_df)
        fmt_valid = int(fmt_df["is_valid"].sum())
        format_stats[fmt] = {
            "total": fmt_total,
            "success": fmt_valid,
            "fail": fmt_total - fmt_valid,
            "rate": f"{fmt_valid / fmt_total * 100:.1f}%" if fmt_total > 0 else "N/A",
        }
    summary["by_format"] = format_stats
    all_patterns = []
    for patterns_str in df[df["is_valid"] == False]["error_patterns"]:
        if patterns_str:
            all_patterns.extend(patterns_str.split(","))
    summary["error_pattern_counts"] = dict(Counter(all_patterns).most_common())
    return summary

# ---------------------------------------------------------------------------
# 4. Main Processing
# ---------------------------------------------------------------------------

def process_files(public_150_file, inference_files):
    if public_150_file is None:
        return "❌ public_150.json をアップロードしてください", "", ""

    if not inference_files:
        return "❌ inference.json を1つ以上アップロードしてください", "", ""

    try:
        pub_path = public_150_file if isinstance(public_150_file, str) else public_150_file.name
        task_info = load_public_150(pub_path)

        all_results = {}
        all_summaries = {}

        for inf_file in inference_files:
            inf_path = inf_file if isinstance(inf_file, str) else inf_file.name
            filename = Path(inf_path).stem
            with open(inf_path, "r", encoding="utf-8") as f:
                inference_data = json.load(f)
            df = analyze_single_inference(inference_data, task_info)
            summary = compute_summary(df)
            all_results[filename] = df
            all_summaries[filename] = summary

        # --- Output 1: Summary ---
        summary_text = "## 📊 分析結果サマリー\n\n"
        for name, s in all_summaries.items():
            summary_text += f"### {name}\n"
            summary_text += f"- パース成功: {s['parse_success']}/{s['total_tasks']} ({s['parse_rate']})\n"
            summary_text += f"- フォーマット別:\n"
            for fmt, fs in s["by_format"].items():
                summary_text += f"  - {fmt}: {fs['success']}/{fs['total']} ({fs['rate']})\n"
            if s["error_pattern_counts"]:
                summary_text += f"- エラーパターン:\n"
                for pattern, count in s["error_pattern_counts"].items():
                    summary_text += f"  - {pattern}: {count}件\n"
            summary_text += "\n"

        # --- Output 2: Comparison table as markdown ---
        comp_lines = ["## 📈 実験比較\n"]
        comp_lines.append("| experiment | total | pass | rate | JSON | YAML | TOML | XML | CSV |")
        comp_lines.append("|---|---|---|---|---|---|---|---|---|")
        for name, df in all_results.items():
            total = len(df)
            valid = int(df["is_valid"].sum())
            rate = f"{valid/total*100:.1f}%" if total > 0 else "N/A"
            fmt_rates = {}
            for fmt in ["JSON", "YAML", "TOML", "XML", "CSV"]:
                fmt_df = df[df["output_type"] == fmt]
                ft = len(fmt_df)
                fv = int(fmt_df["is_valid"].sum())
                fmt_rates[fmt] = f"{fv/ft*100:.1f}%" if ft > 0 else "N/A"
            comp_lines.append(f"| {name} | {total} | {valid} | {rate} | {fmt_rates['JSON']} | {fmt_rates['YAML']} | {fmt_rates['TOML']} | {fmt_rates['XML']} | {fmt_rates['CSV']} |")
        comparison_md = "\n".join(comp_lines)

        # --- Output 3: Error details as markdown ---
        first_name = list(all_results.keys())[0]
        first_df = all_results[first_name]
        error_df = first_df[first_df["is_valid"] == False]

        error_lines = [f"## ❌ エラー詳細 ({first_name})\n"]
        error_lines.append(f"パース失敗: {len(error_df)}件\n")
        error_lines.append("| task_name | output_type | error_patterns | error_msg |")
        error_lines.append("|---|---|---|---|")
        for _, row in error_df.iterrows():
            err_msg_short = str(row['error_msg'])[:60]
            error_lines.append(f"| {row['task_name']} | {row['output_type']} | {row['error_patterns']} | {err_msg_short} |")
        error_md = "\n".join(error_lines)

        return summary_text, comparison_md, error_md

    except Exception as e:
        error_trace = traceback.format_exc()
        return f"❌ エラー:\n```\n{error_trace}\n```", "", ""

# ---------------------------------------------------------------------------
# 5. Gradio App - using only Markdown outputs to avoid Dataframe bugs
# ---------------------------------------------------------------------------

with gr.Blocks(
    title="StructEval-T Analyzer",
    theme=gr.themes.Soft(),
) as demo:
    gr.Markdown(
        """
        # 🔍 StructEval-T Analyzer
        ### 松尾研LLM講義2025 メインコンペ用 推論結果分析ツール

        `inference.json` と `public_150.json` をアップロードすることで、
        モデル出力の構文的正確性(パース可能性)やエラーパターンを分析できます。

        **使い方:**
        1. `public_150.json` をアップロード
        2. 1つ以上の `inference.json` をアップロード(複数ファイル対応・実験比較可能)
        3. 「分析開始」ボタンをクリック
        """
    )

    with gr.Row():
        public_file = gr.File(
            label="public_150.json",
            file_types=[".json"],
            type="filepath",
        )
        inference_files = gr.File(
            label="inference.json(複数可)",
            file_types=[".json"],
            file_count="multiple",
            type="filepath",
        )

    analyze_btn = gr.Button("🔬 分析開始", variant="primary", size="lg")

    with gr.Tabs():
        with gr.Tab("📊 サマリー"):
            summary_output = gr.Markdown()
        with gr.Tab("📈 実験比較"):
            comparison_output = gr.Markdown()
        with gr.Tab("❌ エラー詳細"):
            error_output = gr.Markdown()

    analyze_btn.click(
        fn=process_files,
        inputs=[public_file, inference_files],
        outputs=[summary_output, comparison_output, error_output],
    )

    gr.Markdown(
        """
        ---
        **注意:** このツールは構文的な正確性(パース可能かどうか)のみを検証します。
        運営側の採点基準である `raw_output_metric`(特定キーの存在チェック等)は
        `public_150.json` から削除されているため、完全なスコア再現はできません。

        **エラーパターンの凡例:**
        - `markdown_block`: マークダウンコードブロック(\\`\\`\\`json 等)の混入
        - `natural_language_prefix`: 先頭に自然言語("Here is..."等)が混入
        - `natural_language_suffix`: 末尾に自然言語("Note:"等)が混入
        - `truncation`: 出力の途切れ(閉じ括弧・タグの欠落)
        - `empty_output`: 空の出力
        - `wrong_format`: 要求と異なるフォーマットの出力
        - `cot_leakage`: 思考過程(\\<think\\>等)の混入
        - `tool_call_leakage`: ツールコール(\\<tool_call\\>等)の混入
        - `unknown`: 上記に該当しない構文エラー
        """
    )

if __name__ == "__main__":
    demo.launch()