mirror of
https://github.com/open-compass/opencompass.git
synced 2025-05-30 16:03:24 +08:00
[Fix] temporary files using tempfile (#1186)
Co-authored-by: yaoying <yaoying@kingsoft.com>
This commit is contained in:
parent
5eb8f14d97
commit
749e4cea71
@ -1,6 +1,7 @@
|
|||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import tempfile
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Task: legal document grammar correction
|
Task: legal document grammar correction
|
||||||
@ -30,21 +31,22 @@ def compute_wsjd(data_dict):
|
|||||||
|
|
||||||
now_path = os.path.abspath(os.getcwd())
|
now_path = os.path.abspath(os.getcwd())
|
||||||
utils_path = os.path.abspath(os.path.join(__file__, '..', '..', 'utils'))
|
utils_path = os.path.abspath(os.path.join(__file__, '..', '..', 'utils'))
|
||||||
uid = os.getuid()
|
|
||||||
os.chdir(utils_path)
|
os.chdir(utils_path)
|
||||||
with open(f'/tmp/tmp_pred_{uid}.para', 'w') as f:
|
with tempfile.NamedTemporaryFile(delete=False, mode='w') as tmp_pred_file, \
|
||||||
f.writelines(preds)
|
tempfile.NamedTemporaryFile(delete=False, mode='w') as tmp_gold_file:
|
||||||
with open(f'/tmp/tmp_gold_{uid}.para', 'w') as f:
|
tmp_pred_file.writelines(preds)
|
||||||
f.writelines(golds)
|
tmp_gold_file.writelines(golds)
|
||||||
os.environ['KMP_DUPLICATE_LIB_OK']='True'
|
|
||||||
os.system(f'python3 parallel_to_m2.py -f /tmp/tmp_pred_{uid}.para -o /tmp/tmp_pred_{uid}.para.m2 -g char')
|
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
|
||||||
os.system(f'python3 parallel_to_m2.py -f /tmp/tmp_gold_{uid}.para -o /tmp/tmp_gold_{uid}.para.m2 -g char')
|
os.system(f'python3 parallel_to_m2.py -f {tmp_pred_file.name} -o {tmp_pred_file.name}.m2 -g char')
|
||||||
output = subprocess.check_output(f"python3 compare_m2_for_evaluation.py -hyp /tmp/tmp_pred_{uid}.para.m2 -ref /tmp/tmp_gold_{uid}.para.m2", shell = True)
|
os.system(f'python3 parallel_to_m2.py -f {tmp_gold_file.name} -o {tmp_gold_file.name}.m2 -g char')
|
||||||
|
output = subprocess.check_output(
|
||||||
|
f"python3 compare_m2_for_evaluation.py -hyp {tmp_pred_file.name}.m2 -ref {tmp_gold_file.name}.m2", shell=True)
|
||||||
score = float(output.decode().split('\t')[-1].split('\n')[0])
|
score = float(output.decode().split('\t')[-1].split('\n')[0])
|
||||||
#remove prediction files
|
#remove prediction files
|
||||||
os.remove(f'/tmp/tmp_pred_{uid}.para')
|
os.remove(tmp_pred_file.name)
|
||||||
os.remove(f'/tmp/tmp_gold_{uid}.para')
|
os.remove(tmp_gold_file.name)
|
||||||
os.remove(f'/tmp/tmp_pred_{uid}.para.m2')
|
os.remove(f"{tmp_pred_file.name}.m2")
|
||||||
os.remove(f'/tmp/tmp_gold_{uid}.para.m2')
|
os.remove(f"{tmp_gold_file.name}.m2")
|
||||||
os.chdir(now_path)
|
os.chdir(now_path)
|
||||||
return {"score": score}
|
return {"score": score}
|
||||||
|
Loading…
Reference in New Issue
Block a user