This commit is contained in:
hsafaei
2026-07-01 18:28:21 +03:30
parent 1d01b849c3
commit f40ec45b7e
2 changed files with 771 additions and 209 deletions
+422
View File
@@ -0,0 +1,422 @@
import numbers
import os
from dotenv import load_dotenv
from elasticsearch import Elasticsearch, helpers
import json
import re
import zipfile
import pandas as pd
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
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 clear_array_object_by(array, version_key):
array = [c for c in array if c["version_key"] != version_key]
return array
def compute_votes(list_main, conflict_type):
vote = 0
count = 0
for su in list_main :
if not "ref_key" in su :
continue
ref_key = su["ref_key"]
if "has_conflict" in su :
main_type = su["has_conflict"]
main_type = "true" if main_type is True else "false"
else :
main_type = su["main_type"]
if not ref_key in dict_version_weight[conflict_type] :
continue
version_weight = dict_version_weight[conflict_type][ref_key]
if main_type in dict_main_type_weight :
weight = dict_main_type_weight[main_type]
vote += weight * version_weight
count += 1
return vote, count
def merge_unrepeat_by_ref_key(list1, list2) :
for item in list2 :
if not "main_type" in item or not "ref_key" in item :
continue
ref_key = item["ref_key"]
find = [i for i in list1 if i["ref_key"] == ref_key]
if not find :
list1.append(item)
return list1
## -----------------------------------------------------------------------
def compute_avg_all(elastic, index_name="mj_qa_conflict", body={}):
try:
list_ = elastic.es_iterate_all_documents(index_name, body)
except Exception as e:
save_error(0, e)
data = list(list_)
total = len(data)
percent_prev = 0
count = 0
bulk_data = []
total_count = 0
total_vote_su = 0
total_count_su = 0
total_vote_cd = 0
total_count_cd = 0
total_vote_rt = 0
total_count_rt = 0
for mentry in data:
count += 1
entry = mentry["source"]
_id = entry.get("id", "")
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", [])
percent = int((count / total) * 100)
if percent != percent_prev:
print("compute_avg_all -> %.2f %%" % percent)
percent_prev = percent
vote, count = compute_votes(subject_unitys, "subject_unitys")
total_vote_su += vote
total_count_su += count
vote, count = compute_votes(conflict_detections, "conflict_detections")
total_vote_cd += vote
total_count_cd += count
relation_identifications = conflict_relation_identifications
relation_identifications = merge_unrepeat_by_ref_key(relation_identifications, conflict_evaluations)
vote, count = compute_votes(relation_identifications, "relation_identifications")
total_vote_rt += vote
total_count_rt += count
C_avg_su = total_vote_su/ total_count_su if total_count_su > 0 else 0
C_avg_cd = total_vote_cd/ total_count_cd if total_count_cd > 0 else 0
C_avg_rt = total_vote_rt/ total_count_rt if total_count_rt > 0 else 0
print( "-" * 50 )
print( "-" * 5, "subject_unity total_vote, total_count, avge : ", total_vote_su, total_count_su, C_avg_su )
print( "-" * 10 )
print( "-" * 5, "conflict_detections total_vote, total_count, avge : ", total_vote_cd, total_count_cd, C_avg_cd )
print( "-" * 10 )
print( "-" * 5, "conflict_relation_identifications total_vote, total_count, avge : ", total_vote_rt, total_count_rt, C_avg_rt )
print( "-" * 50 )
return C_avg_su, C_avg_cd, C_avg_rt
def re_compute_conflict_ranks(elastic, index_name, C_avg_su, C_avg_cd, C_avg_rt, m_count= 3, body= {}):
try:
list_ = elastic.es_iterate_all_documents(index_name, body)
except Exception as e:
save_error(0, e)
data = list(list_)
total = len(data)
print( "-" * 50)
print( " total count --> ", total)
print( "-" * 50)
percent_prev = 0
count = 0
bulk_data = []
total_count = 0
for mentry in data:
count += 1
entry = mentry["source"]
_id = entry.get("id", "")
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_ranks = entry.get("conflict_ranks", [])
percent = int((count / total) * 100)
if percent != percent_prev:
print("re_compute_conflict_ranks -> %.2f %%" % percent, total)
percent_prev = percent
vote_su, count_su = compute_votes(subject_unitys, "subject_unitys")
avg_su = vote_su / count_su if count_su > 0 else 0
if count_su == 0 :
bayes_su = 0
else:
bayes_su = (count_su * avg_su + m_count * C_avg_su) / (count_su + m_count)
vote_cd, count_cd = compute_votes(conflict_detections, "conflict_detections")
avg_cd = vote_cd / count_cd if count_cd > 0 else 0
if count_cd == 0 :
bayes_cd = 0
else:
bayes_cd = (count_cd * avg_cd + m_count * C_avg_cd) / (count_cd + m_count)
relation_identifications = conflict_relation_identifications
relation_identifications = merge_unrepeat_by_ref_key(relation_identifications, conflict_evaluations)
vote_rt, count_rt = compute_votes(relation_identifications, "relation_identifications")
avg_rt = vote_rt / count_rt if count_rt > 0 else 0
if count_rt == 0 :
bayes_rt = 0
else:
bayes_rt = (count_rt * avg_rt + m_count * C_avg_rt) / (count_rt + m_count)
conflict_rank = {
"subject_unity_count": count_su,
"subject_unity_avg": round(avg_su * 100, 3),
"subject_unity_bayes": round(bayes_su * 100, 3),
"conflict_detection_count": count_cd,
"conflict_detection_avg": round(avg_cd * 100, 3),
"conflict_detection_bayes": round(bayes_cd * 100, 3),
"relation_identification_count": count_rt,
"relation_identification_avg": round(avg_rt * 100, 3),
"relation_identification_bayes": round(bayes_rt * 100, 3)
}
data = {
"conflict_rank": conflict_rank
}
bulk_data.append({"_id": _id, "_source": data})
if len(bulk_data) > 100:
total_count += len(bulk_data)
# print("=" * 10, "to_elastic , total=" + str(total_count))
elastic.updateBulkList(bulk_data, index_name)
bulk_data = []
if len(bulk_data) > 0:
total_count += len(bulk_data)
# print("=" * 10, "to_elastic , total=" + str(total_count))
elastic.updateBulkList(bulk_data, index_name)
bulk_data = []
dict_main_type_weight={
"yes" : 1,
"no" : -1,
"yes_under_assumptions" : 1,
"true" : 1,
"false" : -1,
"تعارض مستقر": 1,
"مطلق مقدم، مقید موخر": 1,
"مقید مقدم، مطلق موخر": 1,
"تکرار حکم": 1,
"بدون تعارض": -1,
# "تایید وجود وحدت موضوعی": 0.5,
# "تایید تشخیص نوع مخالفت": 0.5,
# "تایید تشخیص نوع تعارض": 0.5,
# "تایید تشخیص مخالفت": 0.5,
# "رد تشخیص مخالفت": -0.5,
# "رد تشخیص نوع تعارض": -0.5,
# "رد تشخیص نوع مخالفت": -0.5,
# "رد وجود وحدت موضوعی": -0.5,
}
dict_version_weight = {
"subject_unitys" : {
"oss120": 0.8,
"gemma4": 1,
"oss120-v2": 1,
"gemma4-v3": 1.5,
"Magistral-Small-p3": 1,
"DeepSeek-R1-32B-p3": 1,
"Qwen3-80B-p3": 1.2,
"Qwen3.6-35B-p3": 1.2,
"gemma-4-p3": 1.5,
"gpt-oss-120b-p3": 1,
"gpt-oss-20b-p3": 0.8,
"openyourmind": 1,
"qwen3-6": 1,
"gemma4-p2": 1,
"oss120-p2": 1,
"qwen3-6-p2": 1.2,
"yourmind-p3": 1
},
"conflict_detections" : {
"oss120": 0.8,
"gemma4": 1,
"oss120-v2": 1,
"gemma4-v3": 1.2,
"Magistral-Small-p3": 1.2,
"DeepSeek-R1-32B-p3": 1,
"Qwen3-80B-p3": 1.4,
"Qwen3.6-35B-p3": 1.4,
"gemma-4-p3": 1.2,
"gpt-oss-120b-p3": 1,
"gpt-oss-20b-p3": 0.8,
"openyourmind": 1.2,
"qwen3-6": 1,
"gemma4-p2": 1,
"oss120-p2": 1,
"qwen3-6-p2": 1,
"yourmind-p3": 1.2
},
"relation_identifications" : {
"oss120": 0.5,
"gemma4": 1,
"oss120-v2": 0.8,
"gemma4-v3": 1,
"Magistral-Small-p3": 1,
"DeepSeek-R1-32B-p3": 1,
"Qwen3-80B-p3": 1.2,
"Qwen3.6-35B-p3": 1.2,
"gemma-4-p3": 1,
"gpt-oss-120b-p3": 1,
"gpt-oss-20b-p3": 0.5,
"openyourmind": 1,
"qwen3-6": 1,
"gemma4-p2": 1,
"oss120-p2": 1,
"qwen3-6-p2": 1,
"yourmind-p3": 1
}
}
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
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")
# host = os.getenv("HOST_H102")
# password = os.getenv("HOST_H102_PASS")
# print( "---------------->>>>>>>>>>>>>>>", host )
# host=os.getenv('HOST_113')
# password=os.getenv('HOST_113_PASS')
print(".... majles_codes_update_steps ......", host)
path_base_map = "G:/Project2/tavasi_160/src/repo/_other/"
elastic = ElasticHelper(host, password, "elastic", path_base_map)
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
version_keys_v3 = [
"su-gemma4_cd-ri-urmind_e-qwen3.6_285k_group_v3",
]
## -----------------------------------------------------------------------
body={
"query":{
"terms": {
"version_key": version_keys_v3
}
}
}
C_avg_su, C_avg_cd, C_avg_rt = compute_avg_all(elastic, "mj_qa_conflict", body)
# #--------------------------------------------------
# #--------------------------------------------------
C_avg_su = 1
C_avg_cd = -1.0766904925102267
C_avg_rt = 0.3042815398297548
# C_avg_rt = 0.30
body={
"query": {
"bool": {
"must":[
{
"terms": {
"version_key": version_keys_v3
}
}
],
"must_not": [
{
"exists": {
"field": "conflict_rank.subject_unity_count"
}
}
]
}
}
}
# re_compute_conflict_ranks(elastic, "mj_qa_conflict", C_avg_su, C_avg_cd, C_avg_rt, 3, body)
# re_compute_conflict_ranks(elastic, "mj_qa_conflict", C_avg_su, C_avg_cd, C_avg_rt, 3, {})
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
# .\.env\Scripts\activate
# python -m app.majles.majles_conflict_ranks_v3
+210 -70
View File
@@ -42,27 +42,28 @@ def get_evaluation_data(data):
result.append("رد تشخیص نوع تعارض") result.append("رد تشخیص نوع تعارض")
return result return result
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
def import_conflict_to_elastic(input_path, out_put_index_name, file_jump_ids): def import_conflict_to_elastic(input_path, out_put_index_name, file_jump_ids):
jump_ids = [] jump_ids = []
if os.path.exists(file_jump_ids): if os.path.exists(file_jump_ids):
with open(file_jump_ids, "r") as f: with open(file_jump_ids, "r", encoding="utf-8") as f:
jump_ids = f.readlines() jump_ids = [line.strip() for line in f if line.strip()]
list_files = [] list_files = []
if os.path.isdir(input_path): 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))] 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): elif os.path.isfile(input_path):
list_files.append(input_path) list_files.append(input_path)
file_count = 0 file_count = 0
total_import = 0 total_import = 0
for file in list_files: for file in list_files:
@@ -78,7 +79,17 @@ def import_conflict_to_elastic(input_path, out_put_index_name, file_jump_ids):
continue continue
file_count += 1 file_count += 1
print("@"*5,"file start... file_count / count ", str(file_count), '/', len(data_all), file) print(
"@" * 5,
"file start... file_count / count ",
str(file_count),
"/",
len(list_files),
file,
)
if file in jump_ids:
continue
bulk_data = [] bulk_data = []
failed_data = [] failed_data = []
@@ -113,14 +124,12 @@ def import_conflict_to_elastic(input_path, out_put_index_name, file_jump_ids):
previous_info["title_type"] = title_type1 previous_info["title_type"] = title_type1
next_info["title_type"] = title_type2 next_info["title_type"] = title_type2
conflict_detection = item["conflict_detection"] conflict_detection = item["conflict_detection"]
# conflict_type_detection = item["conflict_type_detection"] # conflict_type_detection = item["conflict_type_detection"]
conflict_relation_identification = item["conflict_relation_type"] conflict_relation_identification = item["conflict_relation_type"]
conflict_evaluation = item["conflict_evaluation"] conflict_evaluation = item["conflict_evaluation"]
conflict_final = item["conflict_final"] conflict_final = item["conflict_final"]
subject_unity["ref_key"] = "gemma4" subject_unity["ref_key"] = "gemma4"
es_data = { es_data = {
@@ -128,7 +137,7 @@ def import_conflict_to_elastic(input_path, out_put_index_name, file_jump_ids):
"version_key": version_key, "version_key": version_key,
"previous_info": previous_info, "previous_info": previous_info,
"next_info": next_info, "next_info": next_info,
"subject_unitys": [subject_unity] "subject_unitys": [subject_unity],
} }
if conflict_detection: if conflict_detection:
conflict_detection["ref_key"] = "openyourmind" conflict_detection["ref_key"] = "openyourmind"
@@ -136,7 +145,9 @@ def import_conflict_to_elastic(input_path, out_put_index_name, file_jump_ids):
if conflict_relation_identification: if conflict_relation_identification:
conflict_relation_identification["ref_key"] = "openyourmind" conflict_relation_identification["ref_key"] = "openyourmind"
es_data["conflict_relation_identifications"] = [conflict_relation_identification] es_data["conflict_relation_identifications"] = [
conflict_relation_identification
]
if conflict_evaluation: if conflict_evaluation:
conflict_evaluation["ref_key"] = "qwen3-6" conflict_evaluation["ref_key"] = "qwen3-6"
@@ -149,22 +160,35 @@ def import_conflict_to_elastic(input_path, out_put_index_name, file_jump_ids):
if len(bulk_data) > 200: if len(bulk_data) > 200:
total_count += len(bulk_data) total_count += len(bulk_data)
print("=" * 10, "to_elastic , total=" + str(total_count) + " -- " + str(total_import)) print(
"=" * 10,
"to_elastic , total="
+ str(total_count)
+ " -- "
+ str(total_import),
)
elastic.importBulkList(bulk_data, out_put_index_name) elastic.importBulkList(bulk_data, out_put_index_name)
bulk_data = [] bulk_data = []
if len(bulk_data) > 0: if len(bulk_data) > 0:
total_count += len(bulk_data) total_count += len(bulk_data)
print("=" * 10, "to_elastic , total=" + str(total_count) + " -- " + str(total_import)) print(
"=" * 10,
"to_elastic , total="
+ str(total_count)
+ " -- "
+ str(total_import),
)
elastic.importBulkList(bulk_data, out_put_index_name) elastic.importBulkList(bulk_data, out_put_index_name)
bulk_data = [] bulk_data = []
total_import += total_count total_import += total_count
jump_ids.append(file)
with open(file_jump_ids, "w") as f: with open(file_jump_ids, "w") as f:
f.write("\n".join(jump_ids)) f.write("\n".join(jump_ids))
def append_conflict_to_elastic( def append_conflict_to_elastic(
elastic, elastic,
input_path, input_path,
@@ -179,11 +203,15 @@ def append_conflict_to_elastic(
"llm_output", "llm_output",
], ],
ref_key_advance={}, ref_key_advance={},
import_if_not_found = False import_if_not_found=False,
): ):
list_files = [] list_files = []
if os.path.isdir(input_path): 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))] 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): elif os.path.isfile(input_path):
list_files.append(input_path) list_files.append(input_path)
@@ -252,19 +280,40 @@ def append_conflict_to_elastic(
"conflict_evaluations": [], "conflict_evaluations": [],
"conflict_finals": [], "conflict_finals": [],
} }
res = { res = {"_id": id, "_source": _source}
"_id": id,
"_source": _source
}
# print("id start...", _id) # print("id start...", _id)
subject_unitys = res["_source"]["subject_unitys"] if "subject_unitys" in res["_source"] else [] subject_unitys = (
conflict_detections = res["_source"]["conflict_detections"] if "conflict_detections" in res["_source"] else [] res["_source"]["subject_unitys"]
conflict_type_detections = res["_source"]["conflict_type_detections"] if "conflict_type_detections" in res["_source"] else [] if "subject_unitys" in res["_source"]
conflict_relation_identifications = res["_source"]["conflict_relation_identifications"] if "conflict_relation_identifications" in res["_source"] else [] else []
conflict_evaluations = res["_source"]["conflict_evaluations"] if "conflict_evaluations" in res["_source"] else [] )
conflict_finals = res["_source"]["conflict_finals"] if "conflict_finals" in res["_source"] else [] conflict_detections = (
res["_source"]["conflict_detections"]
if "conflict_detections" in res["_source"]
else []
)
conflict_type_detections = (
res["_source"]["conflict_type_detections"]
if "conflict_type_detections" in res["_source"]
else []
)
conflict_relation_identifications = (
res["_source"]["conflict_relation_identifications"]
if "conflict_relation_identifications" in res["_source"]
else []
)
conflict_evaluations = (
res["_source"]["conflict_evaluations"]
if "conflict_evaluations" in res["_source"]
else []
)
conflict_finals = (
res["_source"]["conflict_finals"]
if "conflict_finals" in res["_source"]
else []
)
subject_unity = {} subject_unity = {}
conflict_detection = {} conflict_detection = {}
@@ -282,7 +331,11 @@ def append_conflict_to_elastic(
main_type = "" main_type = ""
description = "" description = ""
reason = "" reason = ""
ref_key_adv = ref_key_advance["subject_unity"] if "subject_unity" in ref_key_advance else ref_key ref_key_adv = (
ref_key_advance["subject_unity"]
if "subject_unity" in ref_key_advance
else ref_key
)
ss = item["subject_unity"] ss = item["subject_unity"]
if "main_type" in ss: if "main_type" in ss:
@@ -321,8 +374,11 @@ def append_conflict_to_elastic(
and "conflict_detection" in fields and "conflict_detection" in fields
): ):
description = "" description = ""
ref_key_adv = ref_key_advance["conflict_detection"] if "conflict_detection" in ref_key_advance else ref_key ref_key_adv = (
ref_key_advance["conflict_detection"]
if "conflict_detection" in ref_key_advance
else ref_key
)
ss = item["conflict_detection"] ss = item["conflict_detection"]
if "has_conflict" in ss: if "has_conflict" in ss:
@@ -339,7 +395,9 @@ def append_conflict_to_elastic(
"description": description, "description": description,
} }
conflict_detections = clear_array_object_by(conflict_detections, ref_key_adv) conflict_detections = clear_array_object_by(
conflict_detections, ref_key_adv
)
conflict_detections.append(cd) conflict_detections.append(cd)
data["conflict_detections"] = conflict_detections data["conflict_detections"] = conflict_detections
@@ -351,8 +409,11 @@ def append_conflict_to_elastic(
): ):
main_type = "" main_type = ""
description = "" description = ""
ref_key_adv = ref_key_advance["conflict_type_detection"] if "conflict_type_detection" in ref_key_advance else ref_key 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"] ss = item["conflict_type_detection"]
if "main_type" in ss: if "main_type" in ss:
@@ -370,7 +431,9 @@ def append_conflict_to_elastic(
"description": description, "description": description,
} }
conflict_type_detections = clear_array_object_by(conflict_type_detections, ref_key_adv) conflict_type_detections = clear_array_object_by(
conflict_type_detections, ref_key_adv
)
conflict_type_detections.append(cd) conflict_type_detections.append(cd)
data["conflict_type_detections"] = conflict_type_detections data["conflict_type_detections"] = conflict_type_detections
@@ -382,8 +445,11 @@ def append_conflict_to_elastic(
): ):
main_type = "" main_type = ""
description = "" description = ""
ref_key_adv = ref_key_advance["relation_identification"] if "relation_identification" in ref_key_advance else ref_key ref_key_adv = (
ref_key_advance["relation_identification"]
if "relation_identification" in ref_key_advance
else ref_key
)
ss = item["relation_identification"] ss = item["relation_identification"]
if "main_type" in ss: if "main_type" in ss:
@@ -402,7 +468,9 @@ def append_conflict_to_elastic(
"description": description, "description": description,
} }
conflict_relation_identifications = clear_array_object_by(conflict_relation_identifications, ref_key_adv) conflict_relation_identifications = clear_array_object_by(
conflict_relation_identifications, ref_key_adv
)
conflict_relation_identifications.append(cd) conflict_relation_identifications.append(cd)
data["conflict_relation_identifications"] = ( data["conflict_relation_identifications"] = (
conflict_relation_identifications conflict_relation_identifications
@@ -416,8 +484,11 @@ def append_conflict_to_elastic(
): ):
main_type = "" main_type = ""
description = "" description = ""
ref_key_adv = ref_key_advance["conflict_relation_type"] if "conflict_relation_type" in ref_key_advance else ref_key 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"] ss = item["conflict_relation_type"]
if "main_type" in ss: if "main_type" in ss:
@@ -436,7 +507,9 @@ def append_conflict_to_elastic(
"description": description, "description": description,
} }
conflict_relation_identifications = clear_array_object_by(conflict_relation_identifications, ref_key_adv) conflict_relation_identifications = clear_array_object_by(
conflict_relation_identifications, ref_key_adv
)
conflict_relation_identifications.append(cd) conflict_relation_identifications.append(cd)
data["conflict_relation_identifications"] = ( data["conflict_relation_identifications"] = (
conflict_relation_identifications conflict_relation_identifications
@@ -447,19 +520,28 @@ def append_conflict_to_elastic(
and item["conflict_evaluation"] and item["conflict_evaluation"]
and "conflict_evaluation" in fields and "conflict_evaluation" in fields
): ):
ref_key_adv = ref_key_advance["conflict_evaluation"] if "conflict_evaluation" in ref_key_advance else ref_key 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 item["conflict_evaluation"]["ref_key"] = ref_key_adv
conflict_evaluations = clear_array_object_by(conflict_evaluations, ref_key_adv) conflict_evaluations = clear_array_object_by(
conflict_evaluations, ref_key_adv
)
conflict_evaluations.append(item["conflict_evaluation"]) conflict_evaluations.append(item["conflict_evaluation"])
data["conflict_evaluations"] = conflict_evaluations data["conflict_evaluations"] = conflict_evaluations
elif "llm_output" in item and item["llm_output"] and "llm_output" in fields: elif "llm_output" in item and item["llm_output"] and "llm_output" in fields:
main_type = "" main_type = ""
description = "" description = ""
validates = [] validates = []
ref_key_adv = ref_key_advance["conflict_evaluation"] if "conflict_evaluation" in ref_key_advance else ref_key ref_key_adv = (
ref_key_advance["conflict_evaluation"]
if "conflict_evaluation" in ref_key_advance
else ref_key
)
ss = item["llm_output"] ss = item["llm_output"]
if "main_type" in ss: if "main_type" in ss:
@@ -484,7 +566,9 @@ def append_conflict_to_elastic(
"description": description, "description": description,
} }
conflict_evaluations = clear_array_object_by(conflict_evaluations, ref_key_adv) conflict_evaluations = clear_array_object_by(
conflict_evaluations, ref_key_adv
)
conflict_evaluations.append(cd) conflict_evaluations.append(cd)
data["conflict_evaluations"] = conflict_evaluations data["conflict_evaluations"] = conflict_evaluations
@@ -493,7 +577,11 @@ def append_conflict_to_elastic(
and item["conflict_final"] and item["conflict_final"]
and "conflict_final" in fields and "conflict_final" in fields
): ):
ref_key_adv = ref_key_advance["conflict_final"] if "conflict_final" in ref_key_advance else ref_key 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 item["conflict_final"]["ref_key"] = ref_key_adv
@@ -537,7 +625,6 @@ def append_conflict_to_elastic(
bulk_data = [] bulk_data = []
def clear_array_object_by(array, ref_key): 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"] != ref_key]
@@ -572,7 +659,14 @@ def get_evaluation_data(data):
## ----------------------------------------------------------------------- ## -----------------------------------------------------------------------
## ----------------------------------------------------------------------- ## -----------------------------------------------------------------------
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"): 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 = { body = {
"query": { "query": {
@@ -581,17 +675,13 @@ def genrate_excel_data(elastic, file_ouput, index_name_o="mj_qa_conflict", key="
{ {
"nested": { "nested": {
"path": key, "path": key,
"query": { "query": {"term": {key + ".ref_key": {"value": key_value}}},
"term": {key+".ref_key": {"value": key_value}}
}
} }
} }
] ]
} }
}, },
"sort":[ "sort": [{"previous_info.ts_date": "desc"}],
{"previous_info.ts_date": "desc"}
]
} }
print(body) print(body)
@@ -670,14 +760,14 @@ def genrate_excel_data(elastic, file_ouput, index_name_o="mj_qa_conflict", key="
subject_unitys = entry.get("subject_unitys", {}) subject_unitys = entry.get("subject_unitys", {})
conflict_detections = entry.get("conflict_detections", {}) conflict_detections = entry.get("conflict_detections", {})
conflict_type_detections = entry.get("conflict_type_detections", {}) conflict_type_detections = entry.get("conflict_type_detections", {})
conflict_relation_identifications = entry.get("conflict_relation_identifications", {}) conflict_relation_identifications = entry.get(
"conflict_relation_identifications", {}
)
conflict_evaluations = entry.get("conflict_evaluations", {}) conflict_evaluations = entry.get("conflict_evaluations", {})
conflict_finals = entry.get("conflict_finals", {}) conflict_finals = entry.get("conflict_finals", {})
link = f'=HYPERLINK("{base_url}/refine/conflict/{_id}", "لینک")' link = f'=HYPERLINK("{base_url}/refine/conflict/{_id}", "لینک")'
subject_rate = 0 subject_rate = 0
subject_oss120 = "" subject_oss120 = ""
subject_qwen3_6 = "" subject_qwen3_6 = ""
@@ -734,7 +824,6 @@ def genrate_excel_data(elastic, file_ouput, index_name_o="mj_qa_conflict", key="
conflict_openyourmind = str(su["has_conflict"]) conflict_openyourmind = str(su["has_conflict"])
conflict_rate += getRate(su["has_conflict"]) conflict_rate += getRate(su["has_conflict"])
relation_rate = 0 relation_rate = 0
relation_oss120 = "" relation_oss120 = ""
relation_qwen3_6 = "" relation_qwen3_6 = ""
@@ -763,7 +852,6 @@ def genrate_excel_data(elastic, file_ouput, index_name_o="mj_qa_conflict", key="
relation_openyourmind = su["main_type"] relation_openyourmind = su["main_type"]
relation_rate += getRate(su["main_type"]) relation_rate += getRate(su["main_type"])
evalution_rate = 0 evalution_rate = 0
evalution_oss120 = "" evalution_oss120 = ""
evalution_qwen3_6 = "" evalution_qwen3_6 = ""
@@ -792,23 +880,56 @@ def genrate_excel_data(elastic, file_ouput, index_name_o="mj_qa_conflict", key="
evalution_openyourmind = su["main_type"] evalution_openyourmind = su["main_type"]
evalution_rate += getRate(su["main_type"]) evalution_rate += getRate(su["main_type"])
item = [ item = [
str(count), _id, link , str(count),
"", "", subject_rate, subject_oss120, subject_qwen3_6, subject_gemma4_p2, subject_oss120_p2, subject_qwen3_6p2, subject_openyourmind, _id,
"", "", conflict_rate, conflict_oss120, conflict_qwen3_6, conflict_gemma4_p2, conflict_oss120_p2, conflict_qwen3_6p2, conflict_openyourmind, link,
"", "", relation_rate, relation_oss120, relation_qwen3_6, relation_gemma4_p2, relation_oss120_p2, relation_qwen3_6p2, relation_openyourmind, "",
"", "", evalution_rate, evalution_oss120, evalution_qwen3_6, evalution_gemma4_p2, evalution_oss120_p2, evalution_qwen3_6p2, evalution_openyourmind, "",
subject_rate,
subject_oss120,
subject_qwen3_6,
subject_gemma4_p2,
subject_oss120_p2,
subject_qwen3_6p2,
subject_openyourmind,
"",
"",
conflict_rate,
conflict_oss120,
conflict_qwen3_6,
conflict_gemma4_p2,
conflict_oss120_p2,
conflict_qwen3_6p2,
conflict_openyourmind,
"",
"",
relation_rate,
relation_oss120,
relation_qwen3_6,
relation_gemma4_p2,
relation_oss120_p2,
relation_qwen3_6p2,
relation_openyourmind,
"",
"",
evalution_rate,
evalution_oss120,
evalution_qwen3_6,
evalution_gemma4_p2,
evalution_oss120_p2,
evalution_qwen3_6p2,
evalution_openyourmind,
] ]
print(item) print(item)
data_out.append(item) data_out.append(item)
df = pd.DataFrame(data_out, columns=columns) df = pd.DataFrame(data_out, columns=columns)
# Save to Excel # Save to Excel
df.to_excel(file_ouput, index=False) df.to_excel(file_ouput, index=False)
def getRate(main_type): def getRate(main_type):
if main_type == "yes": if main_type == "yes":
return 1 return 1
@@ -821,6 +942,7 @@ def getRate(main_type):
else: else:
return 0 return 0
## ----------------------------------------------------------------------- ## -----------------------------------------------------------------------
@@ -840,11 +962,29 @@ elastic = ElasticHelper(host, password, "elastic", path_base_map)
## ---------------------------------------------------- ## ----------------------------------------------------
input_path = r"G:\Project2\102_gpu\v3_conflict\285k_group_v3" # input_path = r"G:\Project2\102_gpu\v3_conflict\285k_group_v3"
file_jump_ids = r"G:\Project2\102_gpu\v3_conflict\285k_group_v3_jump_ids.json" # file_jump_ids = r"G:\Project2\102_gpu\v3_conflict\285k_group_v3_jump_ids.json"
import_conflict_to_elastic(input_path, "mj_qa_conflict", file_jump_ids) # import_conflict_to_elastic(input_path, "mj_qa_conflict", file_jump_ids)
## -----------------------------------------------------------------------
append_conflict_to_elastic( elastic,
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"])
# append_conflict_to_elastic( elastic,
# input_path=r"G:\Project2\102_gpu\v3_conflict\17K_ev_gemma4\finall_data_finall_oss120.json",
# out_put_index_name="mj_qa_conflict",
# ref_key="oss120",
# fields=["conflict_evaluation"])
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
## -----------------------------------------------------------------------
## ----------------------------------------------------------------------- ## -----------------------------------------------------------------------
# .\.env\Scripts\activate # .\.env\Scripts\activate
# python -m app.majles.majles_conflict_works_v3 # python -m app.majles.majles_conflict_works_v3