Spaces:
Sleeping
Sleeping
File size: 4,048 Bytes
a005c19 |
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 |
import json
from sheet_manager.sheet_crud.sheet_crud import SheetManager
import json
from typing import Optional, Dict
def update_benchmark_json(
model_name: str,
benchmark_data: dict,
worksheet_name: str = "metric",
target_column: str = "benchmark" # ํ๊ฒ ์นผ๋ผ ํ๋ผ๋ฏธํฐ ์ถ๊ฐ
):
"""
ํน์ ๋ชจ๋ธ์ ๋ฒค์น๋งํฌ ๋ฐ์ดํฐ๋ฅผ JSON ํํ๋ก ์
๋ฐ์ดํธํฉ๋๋ค.
Args:
model_name (str): ์
๋ฐ์ดํธํ ๋ชจ๋ธ ์ด๋ฆ
benchmark_data (dict): ์
๋ฐ์ดํธํ ๋ฒค์น๋งํฌ ๋ฐ์ดํฐ ๋์
๋๋ฆฌ
worksheet_name (str): ์์
ํ ์ํฌ์ํธ ์ด๋ฆ (๊ธฐ๋ณธ๊ฐ: "metric")
target_column (str): ์
๋ฐ์ดํธํ ํ๊ฒ ์นผ๋ผ ์ด๋ฆ (๊ธฐ๋ณธ๊ฐ: "benchmark")
"""
sheet_manager = SheetManager(worksheet_name=worksheet_name)
# ๋์
๋๋ฆฌ๋ฅผ JSON ๋ฌธ์์ด๋ก ๋ณํ
json_str = json.dumps(benchmark_data, ensure_ascii=False)
# ๋ชจ๋ธ๋ช
์ ๊ธฐ์ค์ผ๋ก ์ง์ ๋ ์นผ๋ผ ์
๋ฐ์ดํธ
row = sheet_manager.update_cell_by_condition(
condition_column="Model name", # ๋ชจ๋ธ๋ช
์ด ์๋ ์นผ๋ผ
condition_value=model_name, # ์ฐพ์ ๋ชจ๋ธ๋ช
target_column=target_column, # ์
๋ฐ์ดํธํ ํ๊ฒ ์นผ๋ผ
target_value=json_str # ์
๋ฐ์ดํธํ JSON ๊ฐ
)
if row:
print(f"Successfully updated {target_column} data for model: {model_name}")
else:
print(f"Model {model_name} not found in the sheet")
def get_benchmark_dict(
model_name: str,
worksheet_name: str = "metric",
target_column: str = "benchmark",
save_path: Optional[str] = None
) -> Dict:
"""
์ํธ์์ ํน์ ๋ชจ๋ธ์ ๋ฒค์น๋งํฌ JSON ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ ๋์
๋๋ฆฌ๋ก ๋ณํํฉ๋๋ค.
Args:
model_name (str): ๊ฐ์ ธ์ฌ ๋ชจ๋ธ ์ด๋ฆ
worksheet_name (str): ์์
ํ ์ํฌ์ํธ ์ด๋ฆ (๊ธฐ๋ณธ๊ฐ: "metric")
target_column (str): ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์นผ๋ผ ์ด๋ฆ (๊ธฐ๋ณธ๊ฐ: "benchmark")
save_path (str, optional): ๋์
๋๋ฆฌ๋ฅผ ์ ์ฅํ JSON ํ์ผ ๊ฒฝ๋ก
Returns:
Dict: ๋ฒค์น๋งํฌ ๋ฐ์ดํฐ ๋์
๋๋ฆฌ. ๋ฐ์ดํฐ๊ฐ ์๊ฑฐ๋ JSON ํ์ฑ ์คํจ์ ๋น ๋์
๋๋ฆฌ ๋ฐํ
"""
sheet_manager = SheetManager(worksheet_name=worksheet_name)
try:
# ๋ชจ๋ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
data = sheet_manager.sheet.get_all_records()
# ํด๋น ๋ชจ๋ธ ์ฐพ๊ธฐ
target_row = next(
(row for row in data if row.get("Model name") == model_name),
None
)
if not target_row:
print(f"Model {model_name} not found in the sheet")
return {}
# ํ๊ฒ ์นผ๋ผ์ JSON ๋ฌธ์์ด ๊ฐ์ ธ์ค๊ธฐ
json_str = target_row.get(target_column)
if not json_str:
print(f"No data found in {target_column} for model: {model_name}")
return {}
# JSON ๋ฌธ์์ด์ ๋์
๋๋ฆฌ๋ก ๋ณํ
result_dict = json.loads(json_str)
# ๊ฒฐ๊ณผ ์ ์ฅ (save_path๊ฐ ์ ๊ณต๋ ๊ฒฝ์ฐ)
if save_path:
with open(save_path, 'w', encoding='utf-8') as f:
json.dump(result_dict, f, ensure_ascii=False, indent=2)
print(f"Successfully saved dictionary to: {save_path}")
return result_dict
except json.JSONDecodeError:
print(f"Failed to parse JSON data for model: {model_name}")
return {}
except Exception as e:
print(f"Error occurred: {str(e)}")
return {}
def str2json(json_str):
"""
๋ฌธ์์ด์ JSON ๊ฐ์ฒด๋ก ๋ณํํฉ๋๋ค.
Args:
json_str (str): JSON ํ์์ ๋ฌธ์์ด
Returns:
dict: ํ์ฑ๋ JSON ๊ฐ์ฒด, ์คํจ์ None
"""
try:
return json.loads(json_str)
except json.JSONDecodeError as e:
print(f"JSON Parsing Error: {e}")
return None
except Exception as e:
print(f"Unexpected Error: {e}")
return None |