mirror of
https://github.com/hanzoai/synth.git
synced 2026-10-30 18:20:12 +00:00
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
import json
|
|
|
|
class Data:
|
|
"""
|
|
Data class for game/corpus
|
|
@param question: question of the game/corpus
|
|
@param answer: answer of the game/corpus
|
|
@param difficulty: difficulty of the game/corpus, from 1 to 10
|
|
"""
|
|
def __init__(self, question: str, answer: str, difficulty: int = 1, metadata: dict = None, **kwargs):
|
|
self.question = question
|
|
self.answer = answer
|
|
self.difficulty = difficulty
|
|
self.metadata = metadata
|
|
self.gpt_response = ""
|
|
|
|
def to_json(self):
|
|
return {
|
|
"question": self.question,
|
|
"answer": self.answer,
|
|
"difficulty": self.difficulty,
|
|
"metadata": self.metadata,
|
|
"gpt_response": self.gpt_response
|
|
}
|
|
|
|
def to_json_str(self):
|
|
return json.dumps(self.to_json(), ensure_ascii=False)
|
|
|
|
@classmethod
|
|
def from_json_str(cls, json_str):
|
|
json_data = json.loads(json_str)
|
|
return cls(**json_data)
|
|
|
|
@classmethod
|
|
def from_json_dict(cls, json_dict):
|
|
instance = cls(**json_dict)
|
|
if 'gpt_response' in json_dict:
|
|
instance.gpt_response = json_dict['gpt_response']
|
|
return instance
|
|
|
|
@classmethod
|
|
def from_jsonl_file(cls, file_path):
|
|
data_list = []
|
|
with open(file_path, "r") as f:
|
|
for line in f:
|
|
json_data = json.loads(line)
|
|
instance = cls(**json_data)
|
|
if 'gpt_response' in json_data:
|
|
instance.gpt_response = json_data['gpt_response']
|
|
data_list.append(instance)
|
|
return data_list |