From 2542bc6907edc5aef6d34646013e124e3fafb218 Mon Sep 17 00:00:00 2001 From: BigDong Date: Fri, 25 Oct 2024 15:50:33 +0800 Subject: [PATCH] [Feature] Support results saving as md format table (#1638) --- opencompass/summarizers/default.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/opencompass/summarizers/default.py b/opencompass/summarizers/default.py index 93dab27a..8a0da5b2 100644 --- a/opencompass/summarizers/default.py +++ b/opencompass/summarizers/default.py @@ -299,16 +299,34 @@ class DefaultSummarizer: raw_txts = '\n'.join(raw_txts) return raw_txts + @staticmethod + def _format_md_table(table): + table_head_str = '| ' + ' | '.join(table[0]) + ' |\n' + table_mid_list = ['-----' for _ in range(len(table[0]))] + table_mid_str = '|' + ' | '.join(table_mid_list) + '|\n' + + md_table_str = table_head_str + table_mid_str + for row in table[1:]: + curr_str = '| ' + ' | '.join(row) + ' |\n' + md_table_str += curr_str + return md_table_str + def _output_to_file(self, output_path, time_str, table, raw_txts): # output to file if output_path is None: output_path = osp.join(self.work_dir, 'summary', f'summary_{time_str}.txt') output_csv_path = osp.join(self.work_dir, 'summary', f'summary_{time_str}.csv') + output_md_path = osp.join(self.work_dir, 'summary', f'summary_{time_str}.md') else: output_csv_path = output_path.replace('.txt', '.csv') + output_md_path = output_path.replace('.txt', '.md') output_dir = osp.split(output_path)[0] mmengine.mkdir_or_exist(output_dir) + + # process md table + md_table = self._format_md_table(table) + with open(output_path, 'w', encoding='utf-8') as f: text = f'{time_str}\n' + \ 'tabulate format\n' + \ @@ -320,6 +338,10 @@ class DefaultSummarizer: '^' * 128 + '\n' + \ '\n'.join([','.join(row) for row in table]) + '\n' + \ '$' * 128 + '\n\n' + \ + 'markdown format\n' + \ + '^' * 128 + '\n' + \ + md_table + '\n' + \ + '$' * 128 + '\n' + \ '-' * 128 + ' THIS IS A DIVIDER ' + '-' * 128 + '\n\n' + \ 'raw format\n' + \ '^' * 128 + '\n' + \ @@ -332,6 +354,11 @@ class DefaultSummarizer: f.write('\n'.join([','.join(row) for row in table]) + '\n') self.logger.info(f'write csv to {osp.abspath(output_csv_path)}') + with open(output_md_path, 'w', encoding='utf-8') as f: + f.write(md_table) + print(f'\n\nThe markdown format results is as below:\n\n{md_table}') + self.logger.info(f'write markdown summary to {osp.abspath(output_md_path)}') + def summarize( self, output_path: str = None,