1305 lines
44 KiB
Python
1305 lines
44 KiB
Python
import numbers
|
|
import os
|
|
from dotenv import load_dotenv
|
|
|
|
import json
|
|
import re
|
|
import zipfile
|
|
import pandas as pd
|
|
import asyncio
|
|
import multiprocessing
|
|
import sys
|
|
|
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
|
|
|
|
from app.common.elastic_helper_async import ElasticHelperAsync
|
|
from app.common.elastic_helper import ElasticHelper
|
|
from app.common.general_functions import *
|
|
from app.common.text_normalizer import TextNormalizer
|
|
from app.common.data_helper import DataHelper
|
|
|
|
text_normal = TextNormalizer()
|
|
|
|
|
|
def get_evaluation_data(data):
|
|
result = []
|
|
if data.is_subject_unity_assessment_correct:
|
|
result.append("تایید وجود وحدت موضوعی")
|
|
else:
|
|
result.append("رد وجود وحدت موضوعی")
|
|
if data.is_conflict_detection_correct:
|
|
result.append("تایید تشخیص مخالفت")
|
|
else:
|
|
result.append("رد تشخیص مخالفت")
|
|
|
|
if data.is_conflict_type_detection_correct:
|
|
result.append("تایید تشخیص نوع مخالفت")
|
|
else:
|
|
result.append("رد تشخیص نوع مخالفت")
|
|
|
|
if data.is_relation_type_detection_correct:
|
|
result.append("تایید تشخیص نوع تعارض")
|
|
else:
|
|
result.append("رد تشخیص نوع تعارض")
|
|
return result
|
|
|
|
|
|
## -----------------------------------------------------------------------
|
|
## -----------------------------------------------------------------------
|
|
|
|
|
|
def run_to_procssing(list_files, index_name, file_jumps, host, password):
|
|
results = []
|
|
for file in list_files:
|
|
result = asyncio.run( async_import_data_from_json( file, index_name, file_jumps, host, password))
|
|
results.append(result)
|
|
return results
|
|
|
|
|
|
async def async_pack_files_to_elastic(
|
|
input_path, index_name, file_jumps, host, password
|
|
):
|
|
|
|
list_files = []
|
|
if os.path.isdir(input_path):
|
|
list_files = [
|
|
os.path.join(input_path, f)
|
|
for f in os.listdir(input_path)
|
|
if os.path.isfile(os.path.join(input_path, f))
|
|
]
|
|
elif os.path.isfile(input_path):
|
|
list_files.append(input_path)
|
|
|
|
jump_files = []
|
|
if os.path.exists(file_jumps):
|
|
with open(file_jumps, "r", encoding="utf-8") as f:
|
|
jump_files = [line.strip() for line in f if line.strip()]
|
|
|
|
list_input = []
|
|
for file in list_files:
|
|
if not file in jump_files:
|
|
list_input.append(file)
|
|
|
|
# ----------------------------------------
|
|
# ---- packing by cpu_count --------------
|
|
cpu_count = 8
|
|
list_process = []
|
|
|
|
x = len(list_input)
|
|
y = (x - (x % cpu_count)) / cpu_count
|
|
c = x % cpu_count
|
|
list_spilt = []
|
|
for f in range(0, x, int(y)):
|
|
list_spilt.append(f)
|
|
list_spilt[len(list_spilt) - 1] = list_spilt[len(list_spilt) - 1] + c
|
|
b = 0
|
|
len_list_spilt = len(list_spilt)
|
|
for split in list_spilt:
|
|
if b + 1 < len_list_spilt:
|
|
|
|
print( " pack = ", split, list_spilt[b + 1] , " of total= ", len(list_input))
|
|
proc = multiprocessing.Process(
|
|
target=run_to_procssing,
|
|
args=(
|
|
list_input[split : list_spilt[b + 1]],
|
|
index_name,
|
|
file_jumps,
|
|
host,
|
|
password,
|
|
),
|
|
)
|
|
proc.start()
|
|
list_process.append(proc)
|
|
b = b + 1
|
|
for proc in list_process:
|
|
proc.join()
|
|
print("finish ⏹️✅")
|
|
|
|
def convert_import_data_to_es(item, jump_ids=None):
|
|
_id = item["id"]
|
|
|
|
if jump_ids is not None :
|
|
if _id in jump_ids:
|
|
return {}
|
|
jump_ids.append(_id)
|
|
|
|
previous_info = item["previous_info"]
|
|
next_info = item["next_info"]
|
|
version_key = item["version_key"]
|
|
subject_unity = item["subject_unity"]
|
|
|
|
if subject_unity["main_type"] == "no":
|
|
return {}
|
|
|
|
|
|
qanon_title1 = previous_info["qanon_title"]
|
|
qanon_id1 = previous_info["qanon_id"]
|
|
|
|
qanon_title2 = next_info["qanon_title"]
|
|
qanon_id2 = next_info["qanon_id"]
|
|
|
|
title_type1 = getTitleType(qanon_title1)
|
|
title_type2 = getTitleType(qanon_title2)
|
|
|
|
previous_info["title_type"] = title_type1
|
|
next_info["title_type"] = title_type2
|
|
|
|
conflict_detection = item["conflict_detection"]
|
|
# conflict_type_detection = item["conflict_type_detection"]
|
|
conflict_relation_identification = item["conflict_relation_type"]
|
|
conflict_evaluation = item["conflict_evaluation"]
|
|
conflict_final = item["conflict_final"]
|
|
|
|
subject_unity["ref_key"] = "gemma4"
|
|
|
|
es_data = {
|
|
"id": _id,
|
|
"version_key": version_key,
|
|
"previous_info": previous_info,
|
|
"next_info": next_info,
|
|
"subject_unitys": [subject_unity],
|
|
}
|
|
if conflict_detection:
|
|
conflict_detection["ref_key"] = "openyourmind"
|
|
es_data["conflict_detections"] = [conflict_detection]
|
|
|
|
if conflict_relation_identification:
|
|
conflict_relation_identification["ref_key"] = "openyourmind"
|
|
es_data["conflict_relation_identifications"] = [
|
|
conflict_relation_identification
|
|
]
|
|
|
|
if conflict_evaluation:
|
|
conflict_evaluation["ref_key"] = "qwen3-6"
|
|
es_data["conflict_evaluations"] = [conflict_evaluation]
|
|
|
|
if conflict_final:
|
|
es_data["conflict_finals"] = [conflict_final]
|
|
|
|
return es_data
|
|
|
|
|
|
def import_conflict_to_elastic(elastic, input_path, out_put_index_name, file_jump_ids):
|
|
|
|
jump_ids = []
|
|
if os.path.exists(file_jump_ids):
|
|
with open(file_jump_ids, "r", encoding="utf-8") as f:
|
|
jump_ids = [line.strip() for line in f if line.strip()]
|
|
|
|
list_files = []
|
|
if os.path.isdir(input_path):
|
|
list_files = [
|
|
os.path.join(input_path, f)
|
|
for f in os.listdir(input_path)
|
|
if os.path.isfile(os.path.join(input_path, f))
|
|
]
|
|
elif os.path.isfile(input_path):
|
|
list_files.append(input_path)
|
|
|
|
file_count = 0
|
|
total_import = 0
|
|
for file in list_files:
|
|
|
|
data_all = []
|
|
try:
|
|
data_all = DataHelper.load_orjson(file)
|
|
if isinstance(data_all[0], list):
|
|
data_all = data_all[0]
|
|
|
|
except Exception as e:
|
|
print("********** error read data file json ", file, str(e))
|
|
continue
|
|
|
|
file_count += 1
|
|
print(
|
|
"@" * 5,
|
|
"file start... file_count / count ",
|
|
str(file_count),
|
|
"/",
|
|
len(list_files),
|
|
file,
|
|
)
|
|
|
|
if file in jump_ids:
|
|
continue
|
|
|
|
bulk_data = []
|
|
failed_data = []
|
|
total_count = 0
|
|
|
|
for item in data_all:
|
|
|
|
es_data = convert_import_data_to_es(item, jump_ids)
|
|
|
|
if es_data :
|
|
bulk_data.append({"_id": _id, "_source": es_data})
|
|
|
|
if len(bulk_data) > 200:
|
|
total_count += len(bulk_data)
|
|
print(
|
|
"=" * 10,
|
|
"to_elastic , total="
|
|
+ str(total_count)
|
|
+ " -- "
|
|
+ str(total_import),
|
|
)
|
|
elastic.importBulkList(bulk_data, out_put_index_name)
|
|
bulk_data = []
|
|
|
|
if len(bulk_data) > 0:
|
|
total_count += len(bulk_data)
|
|
print(
|
|
"=" * 10,
|
|
"to_elastic , total="
|
|
+ str(total_count)
|
|
+ " -- "
|
|
+ str(total_import),
|
|
)
|
|
elastic.importBulkList(bulk_data, out_put_index_name)
|
|
bulk_data = []
|
|
|
|
total_import += total_count
|
|
jump_ids.append(file)
|
|
|
|
with open(file_jump_ids, "w") as f:
|
|
f.write("\n".join(jump_ids))
|
|
|
|
|
|
async def async_import_data_from_json(file, index_name, file_jumps, host, password) -> list:
|
|
|
|
time = datetime.datetime.now().timestamp()
|
|
file_name = file.split("/")[-1:][0]
|
|
print("▶️", file_name)
|
|
|
|
elastic = ElasticHelperAsync(
|
|
es_url=host, es_pass=password, es_user="elastic"
|
|
)
|
|
data_all = []
|
|
try:
|
|
with open(file, "rb") as rfile:
|
|
data_all = json.loads(rfile.read())
|
|
if data_all[0] and isinstance(data_all[0], list):
|
|
data_all = data_all[0]
|
|
|
|
|
|
except Exception as e:
|
|
print(f"not read file {file}", str(e))
|
|
|
|
bulk_data = []
|
|
failed_data = []
|
|
total_count = 0
|
|
list_jump = []
|
|
for item in data_all:
|
|
es_data = convert_import_data_to_es(item)
|
|
if es_data:
|
|
bulk_data.append({"_id": _id, "_source": es_data})
|
|
|
|
if len(bulk_data) > 200:
|
|
await elastic.importBulkList(bulk_data, index_name)
|
|
bulk_data = []
|
|
|
|
if len(bulk_data) > 0:
|
|
await elastic.importBulkList(bulk_data, index_name)
|
|
|
|
bulk_data = []
|
|
|
|
|
|
with open(file_jumps, "a", encoding="utf-8") as f:
|
|
f.write(file + "\n")
|
|
|
|
# total_import += total_count
|
|
await elastic.es.close()
|
|
time1 = datetime.datetime.now().timestamp()
|
|
time = time1 - time
|
|
print("✅", file_name, ":", time)
|
|
|
|
|
|
|
|
def convert_append_data_ro_es(_source, item, fields, ref_key, ref_key_advance) :
|
|
|
|
subject_unitys = (
|
|
_source["subject_unitys"]
|
|
if "subject_unitys" in _source
|
|
else []
|
|
)
|
|
conflict_detections = (
|
|
_source["conflict_detections"]
|
|
if "conflict_detections" in _source
|
|
else []
|
|
)
|
|
conflict_type_detections = (
|
|
_source["conflict_type_detections"]
|
|
if "conflict_type_detections" in _source
|
|
else []
|
|
)
|
|
conflict_relation_identifications = (
|
|
_source["conflict_relation_identifications"]
|
|
if "conflict_relation_identifications" in _source
|
|
else []
|
|
)
|
|
conflict_evaluations = (
|
|
_source["conflict_evaluations"]
|
|
if "conflict_evaluations" in _source
|
|
else []
|
|
)
|
|
conflict_finals = (
|
|
_source["conflict_finals"]
|
|
if "conflict_finals" in _source
|
|
else []
|
|
)
|
|
|
|
subject_unity = {}
|
|
conflict_detection = {}
|
|
conflict_type_detection = {}
|
|
conflict_relation_identification = {}
|
|
conflict_evaluation = {}
|
|
conflict_final = {}
|
|
|
|
data = {}
|
|
if (
|
|
"subject_unity" in item
|
|
and item["subject_unity"]
|
|
and "subject_unity" in fields
|
|
):
|
|
main_type = ""
|
|
description = ""
|
|
reason = ""
|
|
ref_key_adv = (
|
|
ref_key_advance["subject_unity"]
|
|
if "subject_unity" in ref_key_advance
|
|
else ref_key
|
|
)
|
|
|
|
ss = item["subject_unity"]
|
|
if "main_type" in ss:
|
|
main_type = ss["main_type"]
|
|
elif "has_subject_unity" in ss:
|
|
main_type = ss["has_subject_unity"]
|
|
|
|
if "required_assumptions" in ss:
|
|
description = ss["required_assumptions"]
|
|
elif "description" in ss:
|
|
description = ss["description"]
|
|
|
|
if (
|
|
"matching_rules" in ss
|
|
and len(ss["matching_rules"])
|
|
and "reasoning" in ss["matching_rules"][0]
|
|
):
|
|
reason = ss["matching_rules"][0]["reasoning"]
|
|
elif "reason" in ss:
|
|
reason = ss["reason"]
|
|
cd = {
|
|
"ref_key": ref_key_adv,
|
|
"main_type": main_type,
|
|
"description": description,
|
|
"reason": reason,
|
|
}
|
|
|
|
subject_unitys = clear_array_object_by(subject_unitys, ref_key_adv)
|
|
|
|
subject_unitys.append(cd)
|
|
data["subject_unitys"] = subject_unitys
|
|
|
|
if (
|
|
"conflict_detection" in item
|
|
and item["conflict_detection"]
|
|
and "conflict_detection" in fields
|
|
):
|
|
description = ""
|
|
ref_key_adv = (
|
|
ref_key_advance["conflict_detection"]
|
|
if "conflict_detection" in ref_key_advance
|
|
else ref_key
|
|
)
|
|
|
|
ss = item["conflict_detection"]
|
|
if "has_conflict" in ss:
|
|
has_conflict = ss["has_conflict"]
|
|
|
|
if "explanation_of_conflict" in ss:
|
|
description = ss["explanation_of_conflict"]
|
|
elif "description" in ss:
|
|
description = ss["description"]
|
|
|
|
cd = {
|
|
"ref_key": ref_key_adv,
|
|
"has_conflict": has_conflict,
|
|
"description": description,
|
|
}
|
|
|
|
conflict_detections = clear_array_object_by(
|
|
conflict_detections, ref_key_adv
|
|
)
|
|
|
|
conflict_detections.append(cd)
|
|
data["conflict_detections"] = conflict_detections
|
|
|
|
if (
|
|
"conflict_type_detection" in item
|
|
and item["conflict_type_detection"]
|
|
and "conflict_type_detection" in fields
|
|
):
|
|
main_type = ""
|
|
description = ""
|
|
ref_key_adv = (
|
|
ref_key_advance["conflict_type_detection"]
|
|
if "conflict_type_detection" in ref_key_advance
|
|
else ref_key
|
|
)
|
|
|
|
ss = item["conflict_type_detection"]
|
|
if "main_type" in ss:
|
|
main_type = ss["main_type"]
|
|
elif "conflict_type" in ss:
|
|
main_type = ss["conflict_type"]
|
|
|
|
if "description" in ss:
|
|
description = ss["description"]
|
|
elif "explanation_of_type" in ss:
|
|
description = ss["explanation_of_type"]
|
|
cd = {
|
|
"ref_key": ref_key_adv,
|
|
"main_type": main_type,
|
|
"description": description,
|
|
}
|
|
|
|
conflict_type_detections = clear_array_object_by(
|
|
conflict_type_detections, ref_key_adv
|
|
)
|
|
|
|
conflict_type_detections.append(cd)
|
|
data["conflict_type_detections"] = conflict_type_detections
|
|
|
|
if (
|
|
"relation_identification" in item
|
|
and item["relation_identification"]
|
|
and "relation_identification" in fields
|
|
):
|
|
main_type = ""
|
|
description = ""
|
|
ref_key_adv = (
|
|
ref_key_advance["relation_identification"]
|
|
if "relation_identification" in ref_key_advance
|
|
else ref_key
|
|
)
|
|
|
|
ss = item["relation_identification"]
|
|
if "main_type" in ss:
|
|
main_type = ss["main_type"]
|
|
elif "relation_type" in ss:
|
|
main_type = ss["relation_type"]
|
|
|
|
if "description" in ss:
|
|
description = ss["description"]
|
|
elif "reasoning" in ss:
|
|
description = ss["reasoning"]
|
|
|
|
cd = {
|
|
"ref_key": ref_key_adv,
|
|
"main_type": main_type,
|
|
"description": description,
|
|
}
|
|
|
|
conflict_relation_identifications = clear_array_object_by(
|
|
conflict_relation_identifications, ref_key_adv
|
|
)
|
|
conflict_relation_identifications.append(cd)
|
|
data["conflict_relation_identifications"] = (
|
|
conflict_relation_identifications
|
|
)
|
|
|
|
# #--- در خروجی تغییر اسم داده شد conflict_relation_type = conflict_relation_identification
|
|
if (
|
|
"conflict_relation_type" in item
|
|
and item["conflict_relation_type"]
|
|
and "conflict_relation_type" in fields
|
|
):
|
|
main_type = ""
|
|
description = ""
|
|
ref_key_adv = (
|
|
ref_key_advance["conflict_relation_type"]
|
|
if "conflict_relation_type" in ref_key_advance
|
|
else ref_key
|
|
)
|
|
|
|
ss = item["conflict_relation_type"]
|
|
if "main_type" in ss:
|
|
main_type = ss["main_type"]
|
|
elif "relation_type" in ss:
|
|
main_type = ss["relation_type"]
|
|
|
|
if "description" in ss:
|
|
description = ss["description"]
|
|
elif "reasoning" in ss:
|
|
description = ss["reasoning"]
|
|
|
|
cd = {
|
|
"ref_key": ref_key_adv,
|
|
"main_type": main_type,
|
|
"description": description,
|
|
}
|
|
|
|
conflict_relation_identifications = clear_array_object_by(
|
|
conflict_relation_identifications, ref_key_adv
|
|
)
|
|
conflict_relation_identifications.append(cd)
|
|
data["conflict_relation_identifications"] = (
|
|
conflict_relation_identifications
|
|
)
|
|
|
|
if (
|
|
"conflict_evaluation" in item
|
|
and item["conflict_evaluation"]
|
|
and "conflict_evaluation" in fields
|
|
):
|
|
ref_key_adv = (
|
|
ref_key_advance["conflict_evaluation"]
|
|
if "conflict_evaluation" in ref_key_advance
|
|
else ref_key
|
|
)
|
|
|
|
item["conflict_evaluation"]["ref_key"] = ref_key_adv
|
|
|
|
conflict_evaluations = clear_array_object_by(
|
|
conflict_evaluations, ref_key_adv
|
|
)
|
|
conflict_evaluations.append(item["conflict_evaluation"])
|
|
data["conflict_evaluations"] = conflict_evaluations
|
|
elif "llm_output" in item and item["llm_output"] and "llm_output" in fields:
|
|
main_type = ""
|
|
description = ""
|
|
validates = []
|
|
ref_key_adv = (
|
|
ref_key_advance["conflict_evaluation"]
|
|
if "conflict_evaluation" in ref_key_advance
|
|
else ref_key
|
|
)
|
|
|
|
ss = item["llm_output"]
|
|
if "main_type" in ss:
|
|
main_type = ss["main_type"]
|
|
elif "valid_relation_type" in ss:
|
|
main_type = ss["valid_relation_type"]
|
|
|
|
if "description" in ss:
|
|
description = ss["description"]
|
|
elif "comments" in ss and len(ss["comments"]):
|
|
description = ss["comments"][0]
|
|
|
|
if "validates" in ss:
|
|
validates = ss["validates"]
|
|
else:
|
|
validates = get_evaluation_data(ss)
|
|
|
|
cd = {
|
|
"ref_key": ref_key_adv,
|
|
"validates": validates,
|
|
"main_type": main_type,
|
|
"description": description,
|
|
}
|
|
|
|
conflict_evaluations = clear_array_object_by(
|
|
conflict_evaluations, ref_key_adv
|
|
)
|
|
conflict_evaluations.append(cd)
|
|
data["conflict_evaluations"] = conflict_evaluations
|
|
|
|
if (
|
|
"conflict_final" in item
|
|
and item["conflict_final"]
|
|
and "conflict_final" in fields
|
|
):
|
|
ref_key_adv = (
|
|
ref_key_advance["conflict_final"]
|
|
if "conflict_final" in ref_key_advance
|
|
else ref_key
|
|
)
|
|
|
|
item["conflict_final"]["ref_key"] = ref_key_adv
|
|
|
|
conflict_finals = clear_array_object_by(conflict_finals, ref_key_adv)
|
|
conflict_finals.append(item["conflict_final"])
|
|
data["conflict_finals"] = conflict_finals
|
|
|
|
return data
|
|
|
|
|
|
def append_conflict_to_elastic(
|
|
elastic,
|
|
input_path,
|
|
out_put_index_name,
|
|
ref_key,
|
|
fields=[
|
|
"subject_unity",
|
|
"conflict_detection",
|
|
"relation_identification",
|
|
"conflict_evaluation",
|
|
"conflict_final",
|
|
"llm_output",
|
|
],
|
|
ref_key_advance={},
|
|
import_if_not_found=False,
|
|
):
|
|
list_files = []
|
|
if os.path.isdir(input_path):
|
|
list_files = [
|
|
os.path.join(input_path, f)
|
|
for f in os.listdir(input_path)
|
|
if os.path.isfile(os.path.join(input_path, f))
|
|
]
|
|
elif os.path.isfile(input_path):
|
|
list_files.append(input_path)
|
|
|
|
for file in list_files:
|
|
|
|
data_all = []
|
|
try:
|
|
data_all = DataHelper.load_orjson(file)
|
|
except Exception as e:
|
|
print("********** error read data file json ", file, str(e))
|
|
continue
|
|
|
|
print("@" * 5, "file start...", file)
|
|
list_notfound = []
|
|
bulk_data = []
|
|
bulk_data_import = []
|
|
failed_data = []
|
|
total_count = 0
|
|
|
|
for item in data_all:
|
|
|
|
_id = item["id"]
|
|
|
|
# if _id == "qs2489164_v2-9.qs2365626_v2-7":
|
|
# print("qs2489164_v2-9.qs2365626_v2-7")
|
|
|
|
import_state = False
|
|
_source = {}
|
|
res = None
|
|
try:
|
|
res = elastic.get_document(out_put_index_name, _id)
|
|
_source = res["_source"]
|
|
except:
|
|
list_notfound.append(_id)
|
|
if not import_if_not_found:
|
|
continue
|
|
|
|
import_state = True
|
|
version_key = item["version_key"]
|
|
|
|
previous_info = item["previous_info"]
|
|
qanon_title1 = previous_info["qanon_title"]
|
|
qanon_id1 = previous_info["qanon_id"]
|
|
|
|
next_info = item["next_info"]
|
|
qanon_title2 = next_info["qanon_title"]
|
|
qanon_id2 = next_info["qanon_id"]
|
|
|
|
title_type1 = getTitleType(qanon_title1)
|
|
title_type2 = getTitleType(qanon_title2)
|
|
conflict_tags = []
|
|
if qanon_id1 == qanon_id2:
|
|
conflict_tags.append("هم قانون")
|
|
|
|
previous_info["title_type"] = title_type1
|
|
next_info["title_type"] = title_type2
|
|
|
|
_source = {
|
|
"previous_info": previous_info,
|
|
"next_info": next_info,
|
|
"conflict_tags": conflict_tags,
|
|
"version_key": version_key,
|
|
"subject_unitys": [],
|
|
"conflict_detections": [],
|
|
"conflict_type_detections": [],
|
|
"conflict_relation_identifications": [],
|
|
"conflict_evaluations": [],
|
|
"conflict_finals": [],
|
|
}
|
|
res = {"_id": id, "_source": _source}
|
|
|
|
# print("id start...", _id)
|
|
data = convert_append_data_ro_es(_source, item, fields, ref_key, ref_key_advance)
|
|
|
|
if import_state:
|
|
data["id"] = _id
|
|
data["previous_info"] = _source["previous_info"]
|
|
data["next_info"] = _source["next_info"]
|
|
data["conflict_tags"] = _source["conflict_tags"]
|
|
data["version_key"] = _source["version_key"]
|
|
bulk_data_import.append({"_id": _id, "_source": data})
|
|
elif data:
|
|
bulk_data.append({"_id": _id, "_source": data})
|
|
|
|
if len(bulk_data_import) > 200:
|
|
total_count += len(bulk_data_import)
|
|
print("=!" * 10, " import to_elastic , total=" + str(total_count))
|
|
elastic.importBulkList(bulk_data_import, out_put_index_name)
|
|
bulk_data_import = []
|
|
|
|
if len(bulk_data) > 200:
|
|
total_count += len(bulk_data)
|
|
print("=" * 10, "to_elastic , total=" + str(total_count))
|
|
elastic.updateBulkList(bulk_data, out_put_index_name)
|
|
bulk_data = []
|
|
# break
|
|
|
|
if len(bulk_data_import) > 0:
|
|
total_count += len(bulk_data_import)
|
|
print("=!" * 10, " import to_elastic , total=" + str(total_count))
|
|
elastic.importBulkList(bulk_data_import, out_put_index_name)
|
|
bulk_data_import = []
|
|
|
|
if len(bulk_data) > 0:
|
|
total_count += len(bulk_data)
|
|
print("=" * 10, "to_elastic , total=" + str(total_count))
|
|
elastic.updateBulkList(bulk_data, out_put_index_name)
|
|
bulk_data = []
|
|
|
|
|
|
|
|
async def async_append_conflict_to_elastic(
|
|
host, password,
|
|
input_path,
|
|
out_put_index_name,
|
|
ref_key,
|
|
fields=[
|
|
"subject_unity",
|
|
"conflict_detection",
|
|
"relation_identification",
|
|
"conflict_evaluation",
|
|
"conflict_final",
|
|
"llm_output",
|
|
],
|
|
ref_key_advance={},
|
|
import_if_not_found=False,
|
|
):
|
|
list_files = []
|
|
if os.path.isdir(input_path):
|
|
list_files = [
|
|
os.path.join(input_path, f)
|
|
for f in os.listdir(input_path)
|
|
if os.path.isfile(os.path.join(input_path, f))
|
|
]
|
|
elif os.path.isfile(input_path):
|
|
list_files.append(input_path)
|
|
|
|
|
|
elastic = ElasticHelperAsync( es_url=host, es_pass=password, es_user="elastic")
|
|
|
|
for file in list_files:
|
|
|
|
data_all = []
|
|
try:
|
|
data_all = DataHelper.load_orjson(file)
|
|
except Exception as e:
|
|
print("********** error read data file json ", file, str(e))
|
|
continue
|
|
|
|
print("@" * 5, "file start...", file)
|
|
list_notfound = []
|
|
bulk_data = []
|
|
bulk_data_import = []
|
|
failed_data = []
|
|
total_count = 0
|
|
|
|
for item in data_all:
|
|
_id = item["id"]
|
|
# if _id == "qs2489164_v2-9.qs2365626_v2-7":
|
|
# print("qs2489164_v2-9.qs2365626_v2-7")
|
|
|
|
import_state = False
|
|
_source = {}
|
|
res = None
|
|
try:
|
|
res = await elastic.get_document(out_put_index_name, _id)
|
|
_source = res["_source"]
|
|
except Exception as e:
|
|
print( " Exception ", str(e))
|
|
list_notfound.append(_id)
|
|
if not import_if_not_found:
|
|
continue
|
|
|
|
import_state = True
|
|
version_key = item["version_key"]
|
|
|
|
previous_info = item["previous_info"]
|
|
qanon_title1 = previous_info["qanon_title"]
|
|
qanon_id1 = previous_info["qanon_id"]
|
|
|
|
next_info = item["next_info"]
|
|
qanon_title2 = next_info["qanon_title"]
|
|
qanon_id2 = next_info["qanon_id"]
|
|
|
|
title_type1 = getTitleType(qanon_title1)
|
|
title_type2 = getTitleType(qanon_title2)
|
|
conflict_tags = []
|
|
if qanon_id1 == qanon_id2:
|
|
conflict_tags.append("هم قانون")
|
|
|
|
previous_info["title_type"] = title_type1
|
|
next_info["title_type"] = title_type2
|
|
|
|
_source = {
|
|
"previous_info": previous_info,
|
|
"next_info": next_info,
|
|
"conflict_tags": conflict_tags,
|
|
"version_key": version_key,
|
|
"subject_unitys": [],
|
|
"conflict_detections": [],
|
|
"conflict_type_detections": [],
|
|
"conflict_relation_identifications": [],
|
|
"conflict_evaluations": [],
|
|
"conflict_finals": [],
|
|
}
|
|
res = {"_id": id, "_source": _source}
|
|
|
|
# print("id start...", _id)
|
|
data = convert_append_data_ro_es(_source, item, fields, ref_key, ref_key_advance)
|
|
|
|
if import_state:
|
|
data["id"] = _id
|
|
data["previous_info"] = _source["previous_info"]
|
|
data["next_info"] = _source["next_info"]
|
|
data["conflict_tags"] = _source["conflict_tags"]
|
|
data["version_key"] = _source["version_key"]
|
|
bulk_data_import.append({"_id": _id, "_source": data})
|
|
elif data:
|
|
bulk_data.append({"_id": _id, "_source": data})
|
|
|
|
if len(bulk_data_import) > 200:
|
|
total_count += len(bulk_data_import)
|
|
print("=!" * 10, " import to_elastic , total=" + str(total_count))
|
|
await elastic.importBulkList(bulk_data_import, out_put_index_name)
|
|
bulk_data_import = []
|
|
|
|
if len(bulk_data) > 200:
|
|
total_count += len(bulk_data)
|
|
print("=" * 10, "to_elastic , total=" + str(total_count))
|
|
await elastic.updateBulkList(bulk_data, out_put_index_name)
|
|
bulk_data = []
|
|
# break
|
|
|
|
if len(bulk_data_import) > 0:
|
|
total_count += len(bulk_data_import)
|
|
print("=!" * 10, " import to_elastic , total=" + str(total_count))
|
|
await elastic.importBulkList(bulk_data_import, out_put_index_name)
|
|
bulk_data_import = []
|
|
|
|
if len(bulk_data) > 0:
|
|
total_count += len(bulk_data)
|
|
print("=" * 10, "to_elastic , total=" + str(total_count))
|
|
await elastic.updateBulkList(bulk_data, out_put_index_name)
|
|
bulk_data = []
|
|
|
|
await elastic.es.close()
|
|
|
|
|
|
def clear_array_object_by(array, ref_key):
|
|
array = [c for c in array if c["ref_key"] != ref_key]
|
|
|
|
# تغییر کلید و حذف اضافات قبلی
|
|
# array = [c for c in array if (c["ref_key"] != "gemma4-2" )]
|
|
|
|
return array
|
|
|
|
|
|
def get_evaluation_data(data):
|
|
result = []
|
|
if data["is_subject_unity_assessment_correct"]:
|
|
result.append("تایید وجود وحدت موضوعی")
|
|
else:
|
|
result.append("رد وجود وحدت موضوعی")
|
|
if data["is_conflict_detection_correct"]:
|
|
result.append("تایید تشخیص مخالفت")
|
|
else:
|
|
result.append("رد تشخیص مخالفت")
|
|
|
|
if data["is_conflict_type_detection_correct"]:
|
|
result.append("تایید تشخیص نوع مخالفت")
|
|
else:
|
|
result.append("رد تشخیص نوع مخالفت")
|
|
|
|
if data["is_relation_type_detection_correct"]:
|
|
result.append("تایید تشخیص نوع تعارض")
|
|
else:
|
|
result.append("رد تشخیص نوع تعارض")
|
|
return result
|
|
|
|
|
|
## -----------------------------------------------------------------------
|
|
## -----------------------------------------------------------------------
|
|
def genrate_excel_data(
|
|
elastic,
|
|
file_ouput,
|
|
index_name_o="mj_qa_conflict",
|
|
key="subject_unitys",
|
|
key_value="qwen3-6",
|
|
base_url="http://192.168.23.160",
|
|
):
|
|
|
|
body = {
|
|
"query": {
|
|
"bool": {
|
|
"must": [
|
|
{
|
|
"nested": {
|
|
"path": key,
|
|
"query": {"term": {key + ".ref_key": {"value": key_value}}},
|
|
}
|
|
}
|
|
]
|
|
}
|
|
},
|
|
"sort": [
|
|
{"conflict_rank.subject_unity_bayes": "desc"},
|
|
{"conflict_rank.conflict_detection_bayes": "desc"},
|
|
{"conflict_rank.relation_identification_bayes": "desc"},
|
|
]
|
|
}
|
|
|
|
print(body)
|
|
columns = [
|
|
"ردیف",
|
|
"شناسه",
|
|
"لینک داخلی",
|
|
"لینک",
|
|
|
|
"موضوع/تعداد",
|
|
"موضوع/میانگین",
|
|
"موضوع/امتیاز",
|
|
|
|
"وحدت موضوع خبره",
|
|
"توضیح خبره",
|
|
|
|
"تخالف/تعداد",
|
|
"تخالف/میانگین",
|
|
"تخالف/امتیاز",
|
|
|
|
"تخالف خبره",
|
|
"توضیح خبره",
|
|
|
|
"نوع رابطه/تعداد",
|
|
"نوع رابطه/میانگین",
|
|
"نوع رابطه/امتیاز",
|
|
"نوع رابطه خبره",
|
|
"توضیح خبره",
|
|
]
|
|
# Create DataFrame with column names
|
|
|
|
try:
|
|
list_ = elastic.es_iterate_all_documents(index_name_o, body)
|
|
except Exception as e:
|
|
print(str(e))
|
|
return
|
|
|
|
data = list(list_)
|
|
total = len(data)
|
|
count = 0
|
|
total_count = 0
|
|
print(total)
|
|
data_out = []
|
|
|
|
for mentry in data:
|
|
count += 1
|
|
entry = mentry["source"]
|
|
_id = entry.get("id", "")
|
|
print(_id)
|
|
|
|
next_info = entry.get("next_info", {})
|
|
previous_info = entry.get("previous_info", {})
|
|
|
|
# subject_unitys = entry.get("subject_unitys", {})
|
|
# conflict_detections = entry.get("conflict_detections", {})
|
|
# # conflict_type_detections = entry.get("conflict_type_detections", {})
|
|
# conflict_relation_identifications = entry.get(
|
|
# "conflict_relation_identifications", {}
|
|
# )
|
|
# conflict_evaluations = entry.get("conflict_evaluations", {})
|
|
# # conflict_finals = entry.get("conflict_finals", {})
|
|
conflict_rank = entry.get("conflict_rank", {})
|
|
|
|
link1 = f'=HYPERLINK("{base_url}/refine/conflict/{_id}", "لینک داخلی")'
|
|
link2 = f'=HYPERLINK("https://majles.tavasi.ir/refine/conflict/{_id}", "لینک")'
|
|
|
|
|
|
item = [
|
|
str(count),
|
|
_id,
|
|
link1,
|
|
link2,
|
|
conflict_rank["subject_unity_count"],
|
|
conflict_rank["subject_unity_avg"],
|
|
conflict_rank["subject_unity_bayes"],
|
|
"",
|
|
"",
|
|
conflict_rank["conflict_detection_count"],
|
|
conflict_rank["conflict_detection_avg"],
|
|
conflict_rank["conflict_detection_bayes"],
|
|
"",
|
|
"",
|
|
conflict_rank["relation_identification_count"],
|
|
conflict_rank["relation_identification_avg"],
|
|
conflict_rank["relation_identification_bayes"],
|
|
"",
|
|
""
|
|
]
|
|
print(item)
|
|
|
|
data_out.append(item)
|
|
|
|
df = pd.DataFrame(data_out, columns=columns)
|
|
# Save to Excel
|
|
df.to_excel(file_ouput, index=False)
|
|
|
|
|
|
def getRate(main_type):
|
|
if main_type == "yes":
|
|
return 1
|
|
elif main_type == "yes_under_assumptions":
|
|
return 0.7
|
|
elif main_type == "no":
|
|
return 0
|
|
elif main_type == True:
|
|
return 1
|
|
else:
|
|
return 0
|
|
|
|
|
|
## -----------------------------------------------------------------------
|
|
# .\.env\Scripts\activate
|
|
# python -m app.majles.majles_conflict_works_async_v3
|
|
|
|
if __name__ == "__main__":
|
|
load_dotenv(dotenv_path="config.env")
|
|
|
|
host = os.getenv("HOST_160")
|
|
password = os.getenv("HOST_160_PASS")
|
|
|
|
# host = os.getenv("HOST_112")
|
|
# password = os.getenv("HOST_112_PASS")
|
|
|
|
print(".... host ......", host)
|
|
|
|
path_base_map = "G:/Project2/tavasi_160/src/repo/_other/"
|
|
## ----------------------------------------------------
|
|
## ----------------------------------------------------
|
|
# elastic = ElasticHelper(es_url=host,es_pass= password,es_user= "elastic", path_mappings=path_base_map)
|
|
|
|
# elastic.backupIndexToZipfile("G:/Project2/111/server160", "mj_qa_context", "mj_qa_context")
|
|
# elastic.backupIndexToZipfile("G:/Project2/111/server160", "mj_qa_rule", "mj_qa_rule")
|
|
# elastic.backupIndexToZipfile("G:/Project2/102_gpu/v3_conflict", "mj_qa_conflict", "mj_qa_conflict")
|
|
|
|
# elastic.createIndex( 'mj_qa_conflict', 'majles', 'mj_qa_conflict', path_base_map+ 'majles/'+'mj_qa_conflict.json' )
|
|
# elastic.restorFileToElastic2('G:/Project2/111/server160/mj_qa_conflict.zip', 'mj_qa_conflict', 'majles', False)
|
|
|
|
|
|
# map_file = "G:/Project2/tavasi_160/src/repo/_other/majles/mj_qa_conflict.json"
|
|
# elastic.createIndex(index_name="mj_qa_conflict",map_name="mj_qa_conflict",mapping_file_path=map_file)
|
|
# elastic.es.close()
|
|
|
|
## ----------------------------------------------------
|
|
## ----------------------------------------------------
|
|
# #--- درج اطلاعات پایه 285 هزارتایی نسخه 3
|
|
# #--- مواردی که گما4 وحدت موضوعی تشخیص داده است
|
|
# #--- v3_base_285k
|
|
## ----------------------------------------------------
|
|
# input_path = r"G:\Project2\102_gpu\v3_conflict\285k_group_v3"
|
|
# file_jumps = r"G:\Project2\102_gpu\v3_conflict\285k_group_v3_jump_files.json"
|
|
|
|
##---- not async ------
|
|
# elastic = ElasticHelper(es_url=host,es_pass= password,es_user= "elastic", path_mappings=path_base_map)
|
|
# ## import_conflict_to_elastic(elastic, input_path, "mj_qa_conflict", file_jumps)
|
|
|
|
# asyncio.run(
|
|
# async_pack_files_to_elastic(
|
|
# input_path, "mj_qa_conflict", file_jumps, host, password
|
|
# )
|
|
# )
|
|
|
|
|
|
|
|
## -----------------------------------------------------------------------
|
|
## -----------------------------------------------------------------------
|
|
## ---- 103 --> 102 مورد تست
|
|
## -----------------------------------------------------------------------
|
|
## -----------------------------------------------------------------------
|
|
|
|
# elastic = ElasticHelper(es_url=host,es_pass= password,es_user= "elastic", path_mappings=path_base_map)
|
|
# fields=[
|
|
# "subject_unity",
|
|
# "conflict_detection",
|
|
# "conflict_relation_type",
|
|
# "conflict_evaluation"
|
|
# ]
|
|
# append_conflict_to_elastic( elastic,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\100_sample_285k\gemma4_103\start.json",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="gemma4_103",
|
|
# fields=fields,
|
|
# )
|
|
|
|
# append_conflict_to_elastic( elastic,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\100_sample_285k\DeepSeek_32B_103\start.json",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="deepseek_103",
|
|
# fields=fields
|
|
# )
|
|
|
|
# append_conflict_to_elastic( elastic,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\100_sample_285k\oss120b_103\start.json",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="oss120_103",
|
|
# fields=fields
|
|
# )
|
|
|
|
# append_conflict_to_elastic( elastic,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\100_sample_285k\Qwen3_5_122B_103\start.json",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="qwen3_5_103",
|
|
# fields=fields
|
|
# )
|
|
|
|
# append_conflict_to_elastic( elastic,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\100_sample_285k\Qwen3_6_35B_103\start.json",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="qwen3_6_103",
|
|
# fields=fields
|
|
# )
|
|
|
|
# append_conflict_to_elastic( elastic,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\100_sample_285k\Qwen3_80B_103\start.json",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="qwen3_103",
|
|
# fields=fields
|
|
# )
|
|
|
|
# append_conflict_to_elastic( elastic,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\100_sample_285k\Qwopus4_6_27B_103\start.json",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="qwopus4_6_103",
|
|
# fields=fields
|
|
# )
|
|
|
|
|
|
# append_conflict_to_elastic( elastic,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\100_sample_285k\yourmind_103\start.json",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="yourmind_103",
|
|
# fields=fields
|
|
# )
|
|
|
|
|
|
|
|
|
|
|
|
## ----------------------------------------------------
|
|
## ----------------------------------------------------
|
|
# #--- افزودن ارزیابی 17 هزارتایی جدید با مدلهای مختلف
|
|
## -----------------------------------------------------------------------
|
|
|
|
# asyncio.run(
|
|
# async_append_conflict_to_elastic( host, password,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\17K_ev_gemma4\finall_data_finall_gemma4.json",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="gemma4",
|
|
# fields=["conflict_evaluation"])
|
|
# )
|
|
|
|
# asyncio.run(
|
|
# async_append_conflict_to_elastic( host, password,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\17K_ev_oss120b\finall_data_finall_oss120b.json",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="oss120",
|
|
# fields=["conflict_evaluation"])
|
|
# )
|
|
|
|
|
|
# asyncio.run(
|
|
# async_append_conflict_to_elastic( host, password,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\17K_ev_DeepSeek\finall_data_finall_DeepSeek.json",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="deepseek",
|
|
# fields=["conflict_evaluation"])
|
|
|
|
# )
|
|
|
|
|
|
# asyncio.run(
|
|
# async_append_conflict_to_elastic( host, password,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\17k_ev_Qwen3_5_122B\finall_data_finall_Qwen3_5_122B.json",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="qwen3_5_122B",
|
|
# fields=["conflict_evaluation"])
|
|
# )
|
|
|
|
|
|
# asyncio.run(
|
|
# async_append_conflict_to_elastic( host, password,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\17k_ev_Qwopus4_6\finall_data_finall_Qwopus4_6.json",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="qwopus4_6",
|
|
# fields=["conflict_evaluation"])
|
|
|
|
# )
|
|
|
|
|
|
# asyncio.run(
|
|
# async_append_conflict_to_elastic( host, password,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\17k_ev_Qwen3_80B\finall_data_finall_Qwen3_80B.json",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="qwen3_80B",
|
|
# fields=["conflict_evaluation"])
|
|
|
|
# )
|
|
|
|
|
|
## ----------------------------------------------------
|
|
# #--- تعداد 285هزار گروه با 331هزار جفتی که قبلا با گما4 وحدت موضوع داشتند
|
|
# #--- با دو مدل دیگر کانفلیکت و وحدت موضوع بررسی شد
|
|
## ----------------------------------------------------
|
|
|
|
# asyncio.run(
|
|
# async_append_conflict_to_elastic( host, password,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\285k_conflict_gemma4\finall_data",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="gemma4",
|
|
# fields=["conflict_detection", "conflict_relation_type"])
|
|
# )
|
|
|
|
|
|
# asyncio.run(
|
|
# async_append_conflict_to_elastic( host, password,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\285k_conflict_oss120\finall_data",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="oss120",
|
|
# fields=["conflict_detection", "conflict_relation_type"])
|
|
# )
|
|
|
|
# asyncio.run(
|
|
# async_append_conflict_to_elastic( host, password,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\285k_subject_yourmind\finall_data",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="yourmind",
|
|
# fields=["subject_unity"])
|
|
# )
|
|
|
|
# asyncio.run(
|
|
# async_append_conflict_to_elastic( host, password,
|
|
# input_path=r"G:\Project2\102_gpu\v3_conflict\285k_subject_oss120\finall_data",
|
|
# out_put_index_name="mj_qa_conflict",
|
|
# ref_key="oss120",
|
|
# fields=["subject_unity"])
|
|
# )
|
|
|
|
|
|
# elastic = ElasticHelper(es_url=host,es_pass= password,es_user= "elastic", path_mappings=path_base_map)
|
|
# file_ouput = r"G:\Project2\102_gpu\v3_conflict\v3_103_pair.xlsx"
|
|
# genrate_excel_data(elastic, file_ouput, "mj_qa_conflict", "subject_unitys", "gemma4_103", "http://192.168.23.160" )
|
|
|
|
|
|
## -----------------------------------------------------------------------
|
|
# .\.env\Scripts\activate
|
|
# python -m app.majles.majles_conflict_works_v3
|