diff --git a/1_errors.txt b/1_errors.txt index e300531..556f910 100644 --- a/1_errors.txt +++ b/1_errors.txt @@ -37,4 +37,14 @@ err message : name 'out_put_index_name' is not defined err date : 2026-06-01 19:09:38.841180 +************************************************** + + id : 0 + filename : G:\Project2\dataParsers\app\majles\majles_conflict_ranks.py + function : compute_avg_all + err line no : 59 + err line : list_ = elastic.es_iterate_all_documents(out_put_index_name, body) + err message : name 'out_put_index_name' is not defined + err date : 2026-06-29 17:39:01.813037 + ************************************************** diff --git a/app/majles/import_context_rules_v2.py b/app/majles/import_context_rules_v2.py index 2b4a0b0..2f28cfa 100644 --- a/app/majles/import_context_rules_v2.py +++ b/app/majles/import_context_rules_v2.py @@ -316,7 +316,7 @@ def import_error_groups_json(elastic, file_json, file_import_ids, file_type="gro elastic.updateBulkList(bulk_data, index_name_o) bulk_data = [] - path_out = f"G:/Project2/102_gpu/v2/" + path_out = f"G:/Project2/102_gpu/v2_base/" DataHelper.save_orjson(path = path_out + "error1_delete_ids.json", data = delete_ids) DataHelper.save_orjson(path = path_out + "error1_new_ids.json", data = new_ids) DataHelper.save_orjson(path = path_out + "error1_map_prev_ids.json", data = map_prev_ids) @@ -431,7 +431,7 @@ def fill_error_context_json(elastic, file_json, file_import_ids, file_type="grou elastic.updateBulkList(bulk_data, index_name_o) bulk_data = [] - path_out = f"G:/Project2/102_gpu/v2/" + path_out = f"G:/Project2/102_gpu/v2_base/" DataHelper.save_orjson(path = path_out + "error1_delete_ids.json", data = delete_ids) DataHelper.save_orjson(path = path_out + "error1_new_ids.json", data = new_ids) DataHelper.save_orjson(path = path_out + "error1_map_prev_ids.json", data = map_prev_ids) @@ -940,9 +940,9 @@ if __name__ == "__main__": ### ---------------------------------------------- # data_key = "groups" - # log_file = open(f"G:/Project2/102_gpu/v2/log_{data_key}_1.txt", mode="w", encoding="utf8") - # file = f"G:/Project2/102_gpu/v2/mj_qa_chunks.json" - # file_import_ids = f"G:/Project2/102_gpu/v2/{data_key}_import_ids.txt" + # log_file = open(f"G:/Project2/102_gpu/v2_base/groups/log_{data_key}_1.txt", mode="w", encoding="utf8") + # file = f"G:/Project2/102_gpu/v2_base/groups/mj_qa_chunks.json" + # file_import_ids = f"G:/Project2/102_gpu/v2_base/groups/{data_key}_import_ids.txt" # extract_groups_of_json_files(elastic, file, file_import_ids, data_key, "v2") # log_file.close() @@ -957,9 +957,9 @@ if __name__ == "__main__": # # ### ---------------------------------------------- # data_key = "terms" # log_file = open( - # f"G:/Project2/102_gpu/log_{data_key}_1.txt", mode="w", encoding="utf8" + # f"G:/Project2/102_gpu/v2_base/terms/log_{data_key}_1.txt", mode="w", encoding="utf8" # ) - # filezip = f"G:/Project2/102_gpu/{data_key}.zip" + # filezip = f"G:/Project2/102_gpu/v2_base/terms/{data_key}.zip" # file_import_ids = f"G:/Project2/102_gpu/{data_key}_import_ids.txt" # extract_zip_files(elastic, filezip, file_import_ids, data_key, "v2") # log_file.close() @@ -972,9 +972,9 @@ if __name__ == "__main__": # لذا بعد از شناسایی و اصلاح موارد مذکور جدید اضافه شد و قبلی ها پاک شد # data_key = "groups" - # log_file = open(f"G:/Project2/102_gpu/v2/error_log_{data_key}_1.txt", mode="w", encoding="utf8") - # file = f"G:/Project2/102_gpu/v2/f1_valid_5099_mj_qa_chunked_ids_28_11_1404.json" - # file_import_ids = f"G:/Project2/102_gpu/v2/error1_{data_key}_import_ids.txt" + # log_file = open(f"G:/Project2/102_gpu/v2_base/groups/error_log_{data_key}_1.txt", mode="w", encoding="utf8") + # file = f"G:/Project2/102_gpu/v2_base/groups/f1_valid_5099_mj_qa_chunked_ids_28_11_1404.json" + # file_import_ids = f"G:/Project2/102_gpu/v2_base/groups/error1_{data_key}_import_ids.txt" # import_error_groups_json(elastic, file, file_import_ids, data_key, "v2") @@ -985,7 +985,7 @@ if __name__ == "__main__": # #--- حذف موارد خطای قبل - # file_json = f"G:/Project2/102_gpu/v2/error1_delete_ids.json" + # file_json = f"G:/Project2/102_gpu/v2_base/groups/error1_delete_ids.json" # data_json = DataHelper.load_orjson(file_json) # body={ # "query":{ @@ -998,10 +998,10 @@ if __name__ == "__main__": # ### ---------------------------------------------- # data_key = "rules" # log_file = open( - # f"G:/Project2/102_gpu/v2/log_{data_key}_1.txt", mode="w", encoding="utf8" + # f"G:/Project2/102_gpu/v2_base/rules/log_{data_key}_1.txt", mode="w", encoding="utf8" # ) - # file = f"G:/Project2/102_gpu/v2/rules_v2_433252.json" - # file_import_ids = f"G:/Project2/102_gpu/v2/{data_key}_import_ids.txt" + # file = f"G:/Project2/102_gpu/v2_base/rules/rules_v2_433252.json" + # file_import_ids = f"G:/Project2/102_gpu/v2_base/rules/{data_key}_import_ids.txt" # import_rules_of_json(elastic, file, "mj_qa_rule", "v2") # log_file.close() diff --git a/app/majles/import_context_rules_v3.py b/app/majles/import_context_rules_v3.py new file mode 100644 index 0000000..ad87df1 --- /dev/null +++ b/app/majles/import_context_rules_v3.py @@ -0,0 +1,204 @@ +# import mariadb +from dotenv import load_dotenv +import os +import datetime +import json +import uuid +import pandas as pd +import zipfile +import sys +import time +from tqdm import tqdm +from pydantic import BaseModel + +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 + + + +log_file = None + + +def import_contexts_of_json_files(elastic, input_file, file_jump_ids, version_key="base_v3", index_name_o = "mj_qa_context"): + + jump_ids = [] + if os.path.exists(file_jump_ids): + with open(file_jump_ids, "r") as f: + jump_ids = f.readlines() + + data_all = [] + try: + data_all = DataHelper.load_orjson(input_file) + except Exception as e: + print("********** error read data file json " , input_file , str(e)) + return + + total = len(data_all) + + print( "-" * 50) + print( " total count --> ", total) + print( "-" * 50) + percent_prev = 0 + count = 0 + bulk_data = [] + total_count = 0 + + + for item in data_all : + count += 1 + + _id = item["_id"] + _source = item["_source"] + + percent = int((count / total) * 100) + if percent != percent_prev: + print("import_contexts_of_json_files -> %.2f %%" % percent, total) + percent_prev = percent + + + _source["title_type"]= getTitleType(_source["qanon_title"]) + contexts = _source["contexts"] + if contexts : + context_new = [] + for ct in contexts: + context_new.append( + { + "context_type": ct["type"], + "title": ct["mention"], + "content": ct["resolved"] + } + ) + _source["context"] = context_new + + del _source["contexts"] + _source["version_key"] = version_key + + bulk_data.append({"_id": _id, "_source": _source}) + + if len(bulk_data) > 100: + total_count += len(bulk_data) + print("=" * 10, " to_elastic , total=" + str(total_count)) + elastic.importBulkList(bulk_data, index_name_o) + bulk_data = [] + + + if len(bulk_data) > 0: + total_count += len(bulk_data) + print("=" * 10, " to_elastic , total=" + str(total_count)) + elastic.importBulkList(bulk_data, index_name_o) + bulk_data = [] + + + + with open(file_jump_ids, "w") as f: + f.write("\n".join(jump_ids)) + + print("end success import_to_elastic") + + +def import_rules_of_json_files(elastic, input_file, file_jump_ids, version_key="base_v3", index_name_o = "mj_qa_rule"): + + jump_ids = [] + if os.path.exists(file_jump_ids): + with open(file_jump_ids, "r") as f: + jump_ids = f.readlines() + + data_all = [] + try: + data_all = DataHelper.load_orjson(input_file) + except Exception as e: + print("********** error read data file json " , input_file , str(e)) + return + + total = len(data_all) + + print( "-" * 50) + print( " total count --> ", total) + print( "-" * 50) + percent_prev = 0 + count = 0 + bulk_data = [] + total_count = 0 + + + for item in data_all : + count += 1 + + _id = item["_id"] + + if _id in jump_ids : + continue + + jump_ids.append(_id) + _source = item["_source"] + + percent = int((count / total) * 100) + if percent != percent_prev: + print("import_rules_of_json_files -> %.2f %%" % percent, total) + percent_prev = percent + + del _source["tcodes"] + + _source["version_key"] = version_key + + bulk_data.append({"_id": _id, "_source": _source}) + + if len(bulk_data) > 100: + total_count += len(bulk_data) + print("=" * 10, " to_elastic , total=" + str(total_count)) + elastic.importBulkList(bulk_data, index_name_o) + bulk_data = [] + + if len(bulk_data) > 0: + total_count += len(bulk_data) + print("=" * 10, " to_elastic , total=" + str(total_count)) + elastic.importBulkList(bulk_data, index_name_o) + bulk_data = [] + + + + with open(file_jump_ids, "w") as f: + f.write("\n".join(jump_ids)) + + print("end success import_to_elastic") + + + +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_H101") +# password = os.getenv("HOST_H101_PASS") + + +# path_base_map = ( +# r"G:/Project2/gateaway_py/elastic_backend/elastic_backend/app/schemas/mappings/" +# ) +elastic = ElasticHelper(es_url=host, es_pass=password, es_user="elastic") +text_normalize = TextNormalizer() + + + +# input_file = r"G:\Project2\102_gpu\v3_base\mj_qa_group_v3.json"; +# jump_ids_file = r"G:\Project2\102_gpu\v3_base\proccess_group_v3.json"; +# import_contexts_of_json_files(elastic, input_file, jump_ids_file ) + +input_file = r"G:\Project2\102_gpu\v3_base\mj_qa_rule_v3.json"; +jump_ids_file = r"G:\Project2\102_gpu\v3_base\proccess_rule_v3.json"; +import_rules_of_json_files(elastic, input_file, jump_ids_file ) + +## ----------------------------------------------------------------------- +# .\.env\Scripts\activate +# python -m app.majles.import_context_rules_v3 + diff --git a/app/majles/majles_conflict_ranks_v2.py b/app/majles/majles_conflict_ranks_v2.py new file mode 100644 index 0000000..a542570 --- /dev/null +++ b/app/majles/majles_conflict_ranks_v2.py @@ -0,0 +1,551 @@ +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, + "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, + "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, + "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_v2 = [ + "pair_gvc_e1_jv3_130k_chunk_v2_all_qavanin_part1", # 130826 + "oss120b-Qwen3.6.35B.eval_all_qavanin_prompt2", # 127078 + "su-gemma4_cd-ri-urmind_e-qwen3.6_p1p2_eval_oss_valid_sections_only", # 28094 + "oss120b_Qwen3.6.35B.eval_backup.gemma4.31B_all_qavanin_prompt2", # 5163 + "pair_gvc_e1_jv3_6152_chunk_v2", # 3071 + "pair_gvc_e1_jv3_2880_chunk_v2", # 2879 + "pair_gvc_e1_jv3_2501_chunk_v2", # 2501 + "pair_gvc_e1_jv3_all" # 1835 +] + +## ----------------------------------------------------------------------- + +body={ + "query":{ + "terms": { + "version_key": version_keys_v2 + } + } +} +# C_avg_su, C_avg_cd, C_avg_rt = compute_avg_all(elastic, "mj_qa_conflict", body) +# #-------------------------------------------------- +# #----- subject_unity total_vote, total_count, avge : 108125.20000029108 346281 0.3122469901620103 +# #----- conflict_detections total_vote, total_count, avge : 36015.799999964365 232220 0.15509344587014195 +# #----- conflict_relation_identifications total_vote, total_count, avge : 123440.40000029562 289375 0.4265758963293153 +# #-------------------------------------------------- +# #----- subject_unity total_vote, total_count, avge : 108125.20000028852 346281 0.31224699016200286 +# #----- conflict_detections total_vote, total_count, avge : 36015.79999992643 232220 0.15509344586997859 +# #----- conflict_relation_identifications total_vote, total_count, avge : 119328.4000002682 148263 0.8048427456632349 +# #-------------------------------------------------- +# #----- subject_unity total_vote, total_count, avge : 108125.20000027672 346281 0.3122469901619688 +# #----- conflict_detections total_vote, total_count, avge : 36015.799999949406 232220 0.15509344587007753 +# #----- conflict_relation_identifications total_vote, total_count, avge : 75817.70000002025 148263 0.5113730330562598 +# #-------------------------------------------------- +C_avg_su = 0.3122469901620103 +C_avg_cd = 0.15509344587014195 +C_avg_rt = 0.5113730330562598 +C_avg_rt = 0.30 + + +body={ + "query": { + "bool": { + "must":[ + { + "terms": { + "version_key": version_keys_v2 + } + } + ], + "must_not": [ + { + "exists": { + "field": "conflict_rank.subject_unity_count" + } + + } + ] + } + } +} + +body_100={ + "query": { + "nested": { + "path": "subject_unitys", + "query": { + "term": { + "subject_unitys.ref_key": { + "value": "Qwen3-80B-p3" + } + } + } + } + } +} + +body_7005={ + "query": { + "nested": { + "path": "conflict_evaluations", + "query": { + "term": { + "conflict_evaluations.ref_key": { + "value": "qwen3-6-v2" + } + } + } + } + } +} + +body_51000={ + "query": { + "bool": { + "must":[ + { + "nested": { + "path": "subject_unitys", + "query": { + "term": { + "subject_unitys.ref_key": { + "value": "gemma4-v3" + } + } + } + } + } + ], + "must_not": [ + { + "exists": { + "field": "conflict_rank.subject_unity_count" + } + + } + ] + } + } +} + + +body_test1={ + "query": { + "bool": { + "must": [ + { + "terms": { + "version_key": version_keys_v2 + } + }, + { + "term": { + "previous_info.tcode_mains": { + "value": "بیمه" + } + } + } + ], + "must_not": [ + { + "nested": { + "path": "subject_unitys", + "query": { + "term": { + "subject_unitys.ref_key": { + "value": "Qwen3-80B-p3" + } + } + } + } + }, + { + "nested": { + "path": "conflict_evaluations", + "query": { + "term": { + "conflict_evaluations.ref_key": { + "value": "qwen3-6-v2" + } + } + } + } + } + ] + } + } +} + + +# re_compute_conflict_ranks(elastic, "mj_qa_conflict", C_avg_su, C_avg_cd, C_avg_rt, 3, body_100) +# re_compute_conflict_ranks(elastic, "mj_qa_conflict", C_avg_su, C_avg_cd, C_avg_rt, 3, body_7005) +# re_compute_conflict_ranks(elastic, "mj_qa_conflict", C_avg_su, C_avg_cd, C_avg_rt, 3, body_51000) + +# 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, body_test1) +# 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 diff --git a/app/majles/majles_conflict_works.py b/app/majles/majles_conflict_works_v2.py similarity index 91% rename from app/majles/majles_conflict_works.py rename to app/majles/majles_conflict_works_v2.py index 6537dcf..c2948e7 100644 --- a/app/majles/majles_conflict_works.py +++ b/app/majles/majles_conflict_works_v2.py @@ -271,7 +271,7 @@ def append_conflict_to_elastic( 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(path): + elif os.path.isfile(input_path): list_files.append(input_path) for file in list_files : @@ -279,8 +279,8 @@ def append_conflict_to_elastic( data_all = [] try: data_all = DataHelper.load_orjson(file) - except: - print("********** error read data file json " , file) + except Exception as e: + print("********** error read data file json " , file , str(e)) continue print("@"*5,"file start...", file) @@ -920,14 +920,6 @@ password = os.getenv("HOST_160_PASS") # 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/" @@ -936,7 +928,7 @@ elastic = ElasticHelper(host, password, "elastic", path_base_map) ## ---------------------------------------------------- # import_conflict_to_elastic( -# path=r"G:\Project2\102_gpu\v3\133k_oss120_base\output_130k_all_qavanin_persian_translate_full.json", +# path=r"G:\Project2\102_gpu\v2_conflict\133k_oss120_base\output_130k_all_qavanin_persian_translate_full.json", # out_put_index_name="mj_qa_conflict") @@ -964,13 +956,13 @@ elastic = ElasticHelper(host, password, "elastic", path_base_map) # # #------------------------------تست 5 تایی----------------------------------------------------------------- # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\5_gemini_claude\claude-opus-4-6.json", +# input_path=r"G:\Project2\102_gpu\v2_conflict\5_gemini_claude\claude-opus-4-6.json", # out_put_index_name="mj_qa_conflict", # ref_key="claude-opus-4-6", # fields=["llm_output"]) # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\5_gemini_claude\gemini-3.1-pro-preview.json", +# input_path=r"G:\Project2\102_gpu\v2_conflict\5_gemini_claude\gemini-3.1-pro-preview.json", # out_put_index_name="mj_qa_conflict", # ref_key="gemini3-1", # fields=["llm_output"]) @@ -979,82 +971,85 @@ elastic = ElasticHelper(host, password, "elastic", path_base_map) # #-----100_qwen_3.6 -------------- # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\100_qwen_3.6\finall.json", +# input_path=r"G:\Project2\102_gpu\v2_conflict\100_qwen_3.6\finall.json", # out_put_index_name="mj_qa_conflict", # ref_key="qwen3-6", # fields=["subject_unity", "conflict_detection", "conflict_type_detection", "conflict_relation_identification", "conflict_evaluation", "conflict_final"]) # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\100_qwen_3.6\no_conflict_detection.json", +# input_path=r"G:\Project2\102_gpu\v2_conflict\100_qwen_3.6\no_conflict_detection.json", # out_put_index_name="mj_qa_conflict", # ref_key="qwen3-6", # fields=["subject_unity", "conflict_detection"]) # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\100_qwen_3.6\no_subject_unity.json", +# input_path=r"G:\Project2\102_gpu\v2_conflict\100_qwen_3.6\no_subject_unity.json", # out_put_index_name="mj_qa_conflict", # ref_key="qwen3-6", # fields=["subject_unity"]) +# # #----------------------------------------------------------------------------------------------- +# # #----------------------------------------------------------------------------------------------- +# # #----- تست بسته 100 تایی با مدلهای مختلف -------------- # # #-----100_qwen3_6-p2 -------------- # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\100_qwen_3.6-p2\t2.json", +# input_path=r"G:\Project2\102_gpu\v2_conflict\100_qwen_3.6-p2\t2.json", # out_put_index_name="mj_qa_conflict", # ref_key="qwen3-6-p2") # # #-----100_gemma4-p2 -------------- # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\100_gemma4-p2\t2.json", +# input_path=r"G:\Project2\102_gpu\v2_conflict\100_gemma4-p2\t2.json", # out_put_index_name="mj_qa_conflict", # ref_key="gemma4-p2") # # # #-----100_oss120-p2 -------------- # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\100_oss120-p2\t2.json", +# input_path=r"G:\Project2\102_gpu\v2_conflict\100_oss120-p2\t2.json", # out_put_index_name="mj_qa_conflict", # ref_key="oss120-p2") # # # #-----100_OpenYourMind -------------- # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\100_OpenYourMind\t2.json", +# input_path=r"G:\Project2\102_gpu\v2_conflict\100_OpenYourMind\t2.json", # out_put_index_name="mj_qa_conflict", # ref_key="openyourmind") - +# # #----------------------------------------------------------------------------------------------- # #------------------------------------تست 1 تایی با سرویس پولی ----------------------------------------------------------- # # #-----1 sample_data_Claude 4.6 Sonnet -------------- # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\sample_data_Claude 4.6 Sonnet\t2.json", +# input_path=r"G:\Project2\102_gpu\v2_conflict\sample_data_Claude 4.6 Sonnet\t2.json", # out_put_index_name="mj_qa_conflict", # ref_key="claude4.6") # # #-----1 sample_data_DeepSeek V4 Pro -------------- # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\sample_data_DeepSeek V4 Pro\t2.json", +# input_path=r"G:\Project2\102_gpu\v2_conflict\sample_data_DeepSeek V4 Pro\t2.json", # out_put_index_name="mj_qa_conflict", # ref_key="deepseekv4pro") # # # #-----1 sample_data_Gemini 3.1 Pro -------------- # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\sample_data_Gemini 3.1 Pro\t2.json", +# input_path=r"G:\Project2\102_gpu\v2_conflict\sample_data_Gemini 3.1 Pro\t2.json", # out_put_index_name="mj_qa_conflict", # ref_key="gemini3.1pro") # # # #-----1 sample_data_GPT-o4-mini-high -------------- # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\sample_data_GPT-o4-mini-high\t2.json", +# input_path=r"G:\Project2\102_gpu\v2_conflict\sample_data_GPT-o4-mini-high\t2.json", # out_put_index_name="mj_qa_conflict", # ref_key="gpt-o4-mini-high") # # # #-----1 sample_data_Kimi 2.5 -------------- # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\sample_data_Kimi 2.5\t2.json", +# input_path=r"G:\Project2\102_gpu\v2_conflict\sample_data_Kimi 2.5\t2.json", # out_put_index_name="mj_qa_conflict", # ref_key="kimi2.5") # # # #-----1 sample_data_Minimax M2 -------------- # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\sample_data_Minimax M2\t2.json", +# input_path=r"G:\Project2\102_gpu\v2_conflict\sample_data_Minimax M2\t2.json", # out_put_index_name="mj_qa_conflict", # ref_key="minimax-m2") @@ -1083,7 +1078,7 @@ elastic = ElasticHelper(host, password, "elastic", path_base_map) # } # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\all_qavanin_prompt2", +# input_path=r"G:\Project2\102_gpu\v2_conflict\all_qavanin_prompt2", # out_put_index_name="mj_qa_conflict", # ref_key="oss120-v2", # fields = fields, @@ -1115,7 +1110,7 @@ elastic = ElasticHelper(host, password, "elastic", path_base_map) # } # append_conflict_to_elastic( elastic, -# input_path=r"G:\Project2\102_gpu\v3\gemma_ym_ym_qwen_phase1_2_just_evalution_step_oss_120b", +# input_path=r"G:\Project2\102_gpu\v2_conflict\gemma_ym_ym_qwen_phase1_2_just_evalution_step_oss_120b", # out_put_index_name="mj_qa_conflict", # ref_key="oss120-v3", # fields = fields, @@ -1124,10 +1119,59 @@ elastic = ElasticHelper(host, password, "elastic", path_base_map) # ) + + + # # #---------------------------------------------------------------------------------------------- # # #---------------------------------------------------------------------------------------------- -# file_ouput = r"G:\Project2\102_gpu\v3\100_pair_2_tavasi_1.xlsx" +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v2_conflict\100_sample_better_prompt\DeepSeek-R1-Distill-Qwen-32B\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="DeepSeek-R1-32B-p3") + +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v2_conflict\100_sample_better_prompt\gemma-4-31B-it\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="gemma-4-p3") + +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v2_conflict\100_sample_better_prompt\gpt-oss-20b\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="gpt-oss-20b-p3") + +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v2_conflict\100_sample_better_prompt\gpt-oss-120b\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="gpt-oss-120b-p3") + +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v2_conflict\100_sample_better_prompt\Magistral-Small-2509\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="Magistral-Small-p3") + +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v2_conflict\100_sample_better_prompt\Qwen3-Next-80B-A3B-Thinking\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="Qwen3-80B-p3") + +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v2_conflict\100_sample_better_prompt\Qwen3.6-35B-A3B\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="Qwen3.6-35B-p3") + +# append_conflict_to_elastic( elastic, +# input_path=r"G:\Project2\102_gpu\v2_conflict\100_sample_better_prompt\Qwopus3.5-122B-A10B-Kimi-K2.6-destilled-abliterated-NVFP4\t2.json", +# out_put_index_name="mj_qa_conflict", +# ref_key="yourmind-p3") + + + +# # #---------------------------------------------------------------------------------------------- +# # #---------------------------------------------------------------------------------------------- +# # #---------------------------------------------------------------------------------------------- + +# file_ouput = r"G:\Project2\102_gpu\v2_conflict\100_pair_2_tavasi_1.xlsx" # # genrate_excel_data(elastic, file_ouput, "mj_qa_conflict", "subject_unitys", "qwen3-6", "http://192.168.23.160" ) # genrate_excel_data(elastic, file_ouput, "mj_qa_conflict", "subject_unitys", "qwen3-6", "https://majles.tavasi.ir" ) diff --git a/app/majles/majles_conflict_works_v3.py b/app/majles/majles_conflict_works_v3.py new file mode 100644 index 0000000..08b2b5f --- /dev/null +++ b/app/majles/majles_conflict_works_v3.py @@ -0,0 +1,850 @@ +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 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 import_conflict_to_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") as f: + jump_ids = f.readlines() + + + 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(data_all), file) + + bulk_data = [] + failed_data = [] + total_count = 0 + + for item in data_all: + + _id = item["id"] + + if _id in jump_ids : + continue + + 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" : + continue + + 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] + + 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 + + with open(file_jump_ids, "w") as f: + f.write("\n".join(jump_ids)) + + + +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) + 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) + + subject_unitys = res["_source"]["subject_unitys"] if "subject_unitys" 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 = {} + 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 + + 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 = [] + + + +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"] != "oss120-p2-v2" and c["ref_key"] != "gemma4-p2-v2" )] + + 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":[ + {"previous_info.ts_date": "desc"} + ] + } + + print(body) + columns = [ + "ردیف", + "شناسه", + "لینک", + "وحدت موضوع خبره", + "توضیح خبره", + "امتیاز ماشینی", + "وحدت موضوع oss120", + "وحدت موضوع qwen3-6", + "وحدت موضوع gemma4-p2", + "وحدت موضوع oss120-p2", + "وحدت موضوع qwen3-6-p2", + "وحدت موضوع openyourmind", + "تخالف حکمی خبره", + "توضیح خبره", + "امتیاز ماشینی", + "تخالف حکمی oss120", + "تخالف حکمی qwen3-6", + "تخالف حکمی gemma4-p2", + "تخالف حکمی oss120-p2", + "تخالف حکمی qwen3-6-p2", + "تخالف حکمی openyourmind", + "نوع رابطه خبره", + "توضیح خبره", + "امتیاز ماشینی", + "نوع رابطه oss120", + "نوع رابطه qwen3-6", + "نوع رابطه gemma4-p2", + "نوع رابطه oss120-p2", + "نوع رابطه qwen3-6-p2", + "نوع رابطه openyourmind", + "ارزیابی آخر خبره", + "توضیح خبره", + "امتیاز ماشینی", + "ارزیابی آخر oss120", + "ارزیابی آخر qwen3-6", + "ارزیابی آخر gemma4-p2", + "ارزیابی آخر oss120-p2", + "ارزیابی آخر qwen3-6-p2", + "ارزیابی آخر openyourmind", + ] + # 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_out = [ + # ["John", 25, "New York"], + # ["Sarah", 30, "London"], + # ["Mike", 28, "Toronto"], + # ["Emma", 35, "Sydney"], + # ] + + 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", {}) + + + link = f'=HYPERLINK("{base_url}/refine/conflict/{_id}", "لینک")' + + + subject_rate = 0 + subject_oss120 = "" + subject_qwen3_6 = "" + subject_gemma4_p2= "" + subject_oss120_p2= "" + subject_qwen3_6p2= "" + subject_openyourmind= "" + + for su in subject_unitys : + if su["ref_key"] == "oss120" : + subject_oss120 = su["main_type"] + # subject_rate += getRate(su["main_type"]) + if su["ref_key"] == "qwen3-6" : + subject_qwen3_6 = su["main_type"] + subject_rate += getRate(su["main_type"]) + if su["ref_key"] == "gemma4-p2" : + subject_gemma4_p2 = su["main_type"] + subject_rate += getRate(su["main_type"]) + if su["ref_key"] == "oss120-p2" : + subject_oss120_p2 = su["main_type"] + subject_rate += getRate(su["main_type"]) + if su["ref_key"] == "qwen3-6-p2" : + subject_qwen3_6p2 = su["main_type"] + subject_rate += getRate(su["main_type"]) + if su["ref_key"] == "openyourmind" : + subject_openyourmind = su["main_type"] + subject_rate += getRate(su["main_type"]) + + conflict_rate = 0 + conflict_oss120 = "" + conflict_qwen3_6 = "" + conflict_gemma4_p2= "" + conflict_oss120_p2= "" + conflict_qwen3_6p2= "" + conflict_openyourmind= "" + + for su in conflict_detections : + if su["ref_key"] == "oss120" : + conflict_oss120 = str(su["has_conflict"]) + # conflict_rate += getRate(su["has_conflict"]) + if su["ref_key"] == "qwen3-6" : + conflict_qwen3_6 = str(su["has_conflict"]) + conflict_rate += getRate(su["has_conflict"]) + if su["ref_key"] == "gemma4-p2" : + conflict_gemma4_p2 = str(su["has_conflict"]) + conflict_rate += getRate(su["has_conflict"]) + if su["ref_key"] == "oss120-p2" : + conflict_oss120_p2 = str(su["has_conflict"]) + conflict_rate += getRate(su["has_conflict"]) + if su["ref_key"] == "qwen3-6-p2" : + conflict_qwen3_6p2 = str(su["has_conflict"]) + conflict_rate += getRate(su["has_conflict"]) + if su["ref_key"] == "openyourmind" : + conflict_openyourmind = str(su["has_conflict"]) + conflict_rate += getRate(su["has_conflict"]) + + + relation_rate = 0 + relation_oss120 = "" + relation_qwen3_6 = "" + relation_gemma4_p2= "" + relation_oss120_p2= "" + relation_qwen3_6p2= "" + relation_openyourmind= "" + + for su in conflict_relation_identifications : + if su["ref_key"] == "oss120" : + relation_oss120 = su["main_type"] + # relation_rate += getRate(su["main_type"]) + if su["ref_key"] == "qwen3-6" : + relation_qwen3_6 = su["main_type"] + relation_rate += getRate(su["main_type"]) + if su["ref_key"] == "gemma4-p2" : + relation_gemma4_p2 = su["main_type"] + relation_rate += getRate(su["main_type"]) + if su["ref_key"] == "oss120-p2" : + relation_oss120_p2 = su["main_type"] + relation_rate += getRate(su["main_type"]) + if su["ref_key"] == "qwen3-6-p2" : + relation_qwen3_6p2 = su["main_type"] + relation_rate += getRate(su["main_type"]) + if su["ref_key"] == "openyourmind" : + relation_openyourmind = su["main_type"] + relation_rate += getRate(su["main_type"]) + + + evalution_rate = 0 + evalution_oss120 = "" + evalution_qwen3_6 = "" + evalution_gemma4_p2= "" + evalution_oss120_p2= "" + evalution_qwen3_6p2= "" + evalution_openyourmind= "" + + for su in conflict_evaluations : + if su["ref_key"] == "qwen3-6" : + evalution_oss120 = su["main_type"] + # evalution_rate += getRate(su["main_type"]) + if su["ref_key"] == "qwen3-6" : + evalution_qwen3_6 = su["main_type"] + evalution_rate += getRate(su["main_type"]) + if su["ref_key"] == "gemma4-p2" : + evalution_gemma4_p2 = su["main_type"] + evalution_rate += getRate(su["main_type"]) + if su["ref_key"] == "oss120-p2" : + evalution_oss120_p2 = su["main_type"] + evalution_rate += getRate(su["main_type"]) + if su["ref_key"] == "qwen3-6-p2" : + evalution_qwen3_6p2 = su["main_type"] + evalution_rate += getRate(su["main_type"]) + if su["ref_key"] == "openyourmind" : + evalution_openyourmind = su["main_type"] + evalution_rate += getRate(su["main_type"]) + + + item = [ + str(count), _id, link , + "", "", 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) + + + 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 + +## ----------------------------------------------------------------------- + + +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(host, password, "elastic", path_base_map) + + +## ---------------------------------------------------- +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" +import_conflict_to_elastic(input_path, "mj_qa_conflict", file_jump_ids) + + +## ----------------------------------------------------------------------- +# .\.env\Scripts\activate +# python -m app.majles.majles_conflict_works_v3 diff --git a/app/majles/majles_plans_import.py b/app/majles/majles_plans_import.py index 923313a..1ff1304 100644 --- a/app/majles/majles_plans_import.py +++ b/app/majles/majles_plans_import.py @@ -346,11 +346,11 @@ def import_v2_section_list(elastic, all_codes, folder_path, index_name="mj_plan" load_dotenv(dotenv_path='config.env') -host=os.getenv('HOST_160') -password=os.getenv('HOST_160_PASS') +# 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_112') +password=os.getenv('HOST_112_PASS') # host=os.getenv('HOST_113') # password=os.getenv('HOST_113_PASS')