Hiroaki Ogasawara commited on
Commit
aa428bd
1 Parent(s): 704a0d2

feat: csv report

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +22 -4
  3. utils.py +10 -9
README.md CHANGED
@@ -13,5 +13,5 @@ pinned: false
13
 
14
  ```shell
15
  uv sync
16
- uv run python app.py
17
  ```
 
13
 
14
  ```shell
15
  uv sync
16
+ uv run app.py
17
  ```
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import json
2
  import os
3
  import tempfile
@@ -21,6 +22,7 @@ def process_jsonl_file(jsonl_file_path: str, api_key: str):
21
  file_name_with_ext = os.path.basename(jsonl_file_path)
22
  file_name, _ = os.path.splitext(file_name_with_ext)
23
 
 
24
  with tempfile.NamedTemporaryFile(
25
  delete=False,
26
  prefix=f"{file_name}-report-",
@@ -30,10 +32,25 @@ def process_jsonl_file(jsonl_file_path: str, api_key: str):
30
  ) as temp_file:
31
  temp_file.write(html_content)
32
  output_file = temp_file.name
33
- return output_file, ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  except Exception as e:
36
- return None, e
37
 
38
 
39
  # Gradioデモ
@@ -45,13 +62,14 @@ with gr.Blocks() as reporting:
45
  gr.Markdown("APIキーの発行は[こちら](https://aistudio.google.com/app/apikey)")
46
  process_button = gr.Button("レポートを作成")
47
 
48
- output_file = gr.File(label="セルフ評価レポート")
 
49
  output_text = gr.Textbox(label="システムメッセージ")
50
 
51
  process_button.click(
52
  process_jsonl_file,
53
  inputs=[jsonl_input, api_key_input],
54
- outputs=[output_file, output_text],
55
  )
56
 
57
  llm_jp_3 = "llm-jp/llm-jp-3-1.8b"
 
1
+ import csv
2
  import json
3
  import os
4
  import tempfile
 
22
  file_name_with_ext = os.path.basename(jsonl_file_path)
23
  file_name, _ = os.path.splitext(file_name_with_ext)
24
 
25
+ output_file = None
26
  with tempfile.NamedTemporaryFile(
27
  delete=False,
28
  prefix=f"{file_name}-report-",
 
32
  ) as temp_file:
33
  temp_file.write(html_content)
34
  output_file = temp_file.name
35
+
36
+ output_csv = None
37
+ keys = json_data[0].keys()
38
+ with tempfile.NamedTemporaryFile(
39
+ delete=False,
40
+ prefix=f"{file_name}-report-",
41
+ suffix=".csv",
42
+ mode="w",
43
+ encoding="utf-8",
44
+ ) as temp_file:
45
+ dict_writer = csv.DictWriter(temp_file, fieldnames=keys)
46
+ dict_writer.writeheader()
47
+ dict_writer.writerows(json_data)
48
+ output_csv = temp_file.name
49
+
50
+ return output_file, output_csv, ""
51
 
52
  except Exception as e:
53
+ return None, None, e
54
 
55
 
56
  # Gradioデモ
 
62
  gr.Markdown("APIキーの発行は[こちら](https://aistudio.google.com/app/apikey)")
63
  process_button = gr.Button("レポートを作成")
64
 
65
+ output_file = gr.File(label="セルフ評価レポート(HTML)")
66
+ output_csv = gr.File(label="セルフ評価レポート(CSV)")
67
  output_text = gr.Textbox(label="システムメッセージ")
68
 
69
  process_button.click(
70
  process_jsonl_file,
71
  inputs=[jsonl_input, api_key_input],
72
+ outputs=[output_file, output_csv, output_text],
73
  )
74
 
75
  llm_jp_3 = "llm-jp/llm-jp-3-1.8b"
utils.py CHANGED
@@ -79,15 +79,16 @@ def evaluate(results: list[dict], api_key: str, batch_size: int = 10) -> list[di
79
  )
80
 
81
  for result, score in zip(batch_results, scores):
82
- evaluations.append(
83
- {
84
- "input": result["input"],
85
- "output": result["output"],
86
- "eval_aspect": result.get("eval_aspect"),
87
- "target": result.get("target"),
88
- "score": score,
89
- }
90
- )
 
91
 
92
  return evaluations
93
 
 
79
  )
80
 
81
  for result, score in zip(batch_results, scores):
82
+ evaluation = {
83
+ "input": result["input"],
84
+ "output": result["output"],
85
+ "score": score,
86
+ }
87
+ if "eval_aspect" in result:
88
+ evaluation["eval_aspect"] = result["eval_aspect"]
89
+ if "target" in result:
90
+ evaluation["target"] = result["target"]
91
+ evaluations.append(evaluation)
92
 
93
  return evaluations
94