178 lines
5.5 KiB
Python
Executable File
178 lines
5.5 KiB
Python
Executable File
import os
|
||
import json
|
||
import orjson
|
||
from pathlib import Path
|
||
import numpy as np
|
||
|
||
|
||
def load_orjson(path: str | Path):
|
||
path = Path(path)
|
||
with path.open("rb") as f: # باید باینری باز بشه برای orjson
|
||
return orjson.loads(f.read())
|
||
|
||
|
||
def save_orjson(path, data):
|
||
with open(path, "wb") as f:
|
||
f.write(orjson.dumps(data, option=orjson.OPT_INDENT_2 | orjson.OPT_NON_STR_KEYS))
|
||
|
||
|
||
def make_data(
|
||
all_data, system_prompt, assistant_prompt=None
|
||
):
|
||
api_batch = []
|
||
for item in all_data:
|
||
|
||
# user_prompt = f"Input: `{item.get('sanad')}`"
|
||
api_batch.append(
|
||
{
|
||
"id": str(item.get("id")),
|
||
"user_prompt": str(item.get("user_prompt")),
|
||
"system_prompt": system_prompt,
|
||
"assistant_prompt": assistant_prompt,
|
||
}
|
||
)
|
||
return api_batch
|
||
|
||
|
||
SYS_PROMPT = """You are an expert question generator for legal and domain-specific texts.
|
||
|
||
You are given two statements:
|
||
- Statement A: {correct_text}
|
||
- Statement B: {wrong_text}
|
||
|
||
Your goal:
|
||
1. Create ONE precise, highly technical question that tests the understanding of the *meaning, reasoning, or principle* in Statement A.
|
||
2. Statement B must NOT correctly answer the question.
|
||
3. Do NOT use or refer to any article numbers, law identifiers, dates, or numeric references. Ignore all numbers in the text.
|
||
4. Focus on the logic, condition, duty, or conceptual relationship described in Statement A.
|
||
5. The question should sound natural, professional, and fully in Persian.
|
||
6. Avoid any direct quotation or reference to Statement A’s wording — rephrase into a conceptual question.
|
||
|
||
Output format (JSON):
|
||
{
|
||
"question": "your generated conceptual question"
|
||
}"""
|
||
|
||
|
||
AST_PROMPT = '''
|
||
|
||
'''
|
||
|
||
|
||
def cosine_similarity(vec1, vec2):
|
||
"""
|
||
محاسبه شباهت کسینوسی بین دو بردار
|
||
"""
|
||
vec1 = np.array(vec1)
|
||
vec2 = np.array(vec2)
|
||
|
||
dot_product = np.dot(vec1, vec2)
|
||
norm1 = np.linalg.norm(vec1)
|
||
norm2 = np.linalg.norm(vec2)
|
||
|
||
if norm1 == 0 or norm2 == 0:
|
||
return 0.0 # جلوگیری از تقسیم بر صفر
|
||
|
||
similarity = dot_product / (norm1 * norm2)
|
||
return similarity
|
||
|
||
|
||
# 'id', 'domain_id', 'domain_name', 'content', 'content_with_fullpath', 'embed'
|
||
|
||
# نمونه تست
|
||
# file_path = '/home1/ava3/project/aidataparser_test/qan_dataset/input/embdings.json'
|
||
# raw_data = load_orjson(file_path)
|
||
# embed_data = {item['id']: item['embed'] for item in raw_data}
|
||
|
||
# map_path = '/home1/ava3/project/aidataparser_test/qan_dataset/input/mapping_embed.json'
|
||
# map_data = load_orjson(map_path)
|
||
|
||
# print(f'raw_data {raw_data[0].keys()}')
|
||
# same_map= []
|
||
# for map_ in map_data:
|
||
# embed_id_1 = map_[0]
|
||
# embed_id_2 = map_[1]
|
||
# print(f'embed_id_2 {embed_id_2}')
|
||
# print(f'embed_id_1 {embed_id_1}')
|
||
# embed_1 = embed_data[embed_id_1]
|
||
# embed_2 = embed_data[embed_id_2]
|
||
# number = cosine_similarity(embed_1, embed_2)
|
||
# if number >= 0.9:
|
||
# same_map.append(map_)
|
||
|
||
# print(f'number of same_map {len(same_map)}')
|
||
# save_orjson(
|
||
# data=same_map,
|
||
# path='./same_mapping.json'
|
||
# )
|
||
|
||
|
||
if __name__ == '__main__':
|
||
############################################## Make Data #####################################################
|
||
in_data = load_orjson('/home1/ava3/init_mahdi/project/aiDataParser/task/qan_jokar/data_17300.json')
|
||
o_data = load_orjson('/home1/ava3/init_mahdi/project/aiDataParser/task/qan_dataset_old/finall_data.json')
|
||
o_data = {i['id']: i for i in o_data}
|
||
#### 17306
|
||
print(f'o_data1 {len(o_data)}')
|
||
print(f'in_data {len(in_data)}')
|
||
|
||
|
||
f_data = []
|
||
for i, item in enumerate(in_data, start=1):
|
||
_id = item.get('id')
|
||
q_data = o_data.get(_id, None)
|
||
if q_data:
|
||
item['question'] = q_data['question']
|
||
item['ai_code_version'] = q_data['ai_code_version']
|
||
f_data.append(item)
|
||
|
||
print(f'f_data {len(f_data)}')
|
||
save_orjson(
|
||
data=f_data,
|
||
path='/home1/ava3/init_mahdi/project/aiDataParser/task/qan_dataset_old/qan_dataset_17300_v1.json'
|
||
)
|
||
###################################################################################################
|
||
# raw_data = load_orjson(path='/home1/ava3/project/aidataparser_test/qan_dataset/input/embdings.json')
|
||
# raw_data_map = load_orjson(path='/home1/ava3/project/aidataparser_test/qan_dataset/input/mapping_embed.json')
|
||
# print(f'raw_data_map {len(raw_data_map)}')
|
||
|
||
# is_a = False
|
||
# is_b = False
|
||
# user_prompt_list = []
|
||
# for map_ in raw_data_map:
|
||
# correct = map_[0]
|
||
# incorrect = map_[1]
|
||
# for i_data in raw_data:
|
||
# if i_data.get('id') == correct:
|
||
# is_a = True
|
||
# correct_data = i_data.get('content')
|
||
# if i_data.get('id') == incorrect:
|
||
# incorrect_data = i_data.get('content')
|
||
# is_b = True
|
||
# if is_a and is_b:
|
||
# user_prompt_list.append(
|
||
# {
|
||
# 'id': incorrect, #
|
||
# "user_prompt":f'''"correct_text": "{incorrect_data }",\n"wrong_text": "{ correct_data}"'''}
|
||
# )
|
||
# break
|
||
# # break
|
||
|
||
# print(f'--- user_prompt_list : {len(user_prompt_list)}')
|
||
# data = make_data(
|
||
# all_data=user_prompt_list,
|
||
# system_prompt=SYS_PROMPT,
|
||
# )
|
||
|
||
# save_orjson(
|
||
# data=data,
|
||
# path='/home1/ava3/project/aidataparser_test/qan_dataset/output/all_data_p2.json'
|
||
# )
|
||
|
||
# python3 -m aiDataParser.task.qan_dataset_old.normalize_data
|
||
|
||
|
||
|
||
|
||
|