Files
AiDataParser/task/mn_voice/summerize_make_data.py
2026-11-28 16:36:19 +00:00

165 lines
5.7 KiB
Python
Executable File

import os
import json
from collections import defaultdict
import orjson
from pathlib import Path
def load_orjson(path: str | Path):
path = Path(path)
with path.open("rb") as f: # باید باینری باز بشه برای orjson
return orjson.loads(f.read())
def save_orjson(path, data):
with open(path, "wb") as f:
f.write(
orjson.dumps(data, option=orjson.OPT_INDENT_2 | orjson.OPT_NON_STR_KEYS)
)
S_PROPMT = """شما یک متخصص ویرایش و خلاصه‌سازی متن فارسی هستید. وظیفه شما ایجاد یک خلاصه‌ی دقیق، روان و مختصر است به گونه‌ای که **لحن و سبک نویسنده اصلی حفظ شود**. دستورالعمل‌ها:
1. متن را با دقت بخوانید و تمام نکات کلیدی، مثال‌ها، استدلال‌ها و جزئیات مهم را شناسایی کنید.
2. خلاصه باید تمام اطلاعات ضروری را منتقل کند بدون اینکه نکات مهم حذف شوند.
3. جملات را ساده کنید، اما **سبک و صدا و لحن نویسنده اصلی را حفظ کنید**.
4. از نظر لحن، به سبک متن وفادار باشید؛ ممکن است غیررسمی، شاعرانه یا تحلیلی باشد، بسته به متن.
5. بررسی نهایی: مطمئن شوید همه بخش‌های مهم متن در خلاصه هستند و متن روان و قابل فهم است.
فرمت خروجی: متن ساده و روان با حفظ لحن نویسنده."""
old1 = """You are an expert Persian language editor and summarization specialist. Your task is to create a concise, accurate, and coherent summary of the Persian text provided. Follow these instructions carefully:
Step 1: Read and Understand
Carefully read the entire text.
Identify all key points, arguments, examples, and facts.
Note any technical terms, nuanced ideas, or critical details.
Step 2: Structure the Summary
Maintain the original meaning; simplify sentences without altering facts.
Step 3: Condense the Text
Retain all essential information; do not omit critical points.
Step 4: Style and Tone
Use a clear, professional, and neutral tone.
Avoid personal opinions, interpretations, or additional commentary.
Ensure terminology and details from the original text are preserved.
Step 5: Final Check
Verify that all sections of the original text are represented.
Ensure the summary is coherent, concise, and readable.
Output Format: plain fluent text."""
def make_data1(all_data, system_prompt):
api_batch = []
for i, (k, v) in enumerate(all_data.items()):
# i_token = count_tokens(system_prompt=system_prompt, user_prompt=item["content"])
api_batch.append(
{
"id": k,
"system_prompt": system_prompt,
"user_prompt": f"Text to Summarize: {v}",
}
)
# print(f"i {i}")
# if i == 3:
# break
print(f"len {len(api_batch)}")
return api_batch
def make_data2(all_data, system_prompt):
api_batch = []
for i, (k, v) in enumerate(all_data.items()):
# i_token = count_tokens(system_prompt=system_prompt, user_prompt=item["content"])
api_batch.append(
{
"id": k,
"system_prompt": system_prompt,
"user_prompt": f"Text to Summarize: {v}",
}
)
# print(f"i {i}")
# if i == 3:
# break
print(f"len {len(api_batch)}")
def make_data3(all_data, system_prompt):
api_batch = []
for i, (k, v) in enumerate(all_data.items()):
# i_token = count_tokens(system_prompt=system_prompt, user_prompt=item["content"])
api_batch.append(
{
"id": k,
"system_prompt": system_prompt,
"user_prompt": f"Text to Summarize: {v}",
}
)
# print(f"i {i}")
# if i == 3:
# break
print(f"len {len(api_batch)}")
return api_batch
#####################################################################################
# input_data = load_orjson('/home1/ava3/init_mahdi/project/aiDataParser/task/mn_voice/step_emlaie_gemma/finall_data_emlaei_mn_voice.json')
# print(f'input_data {len(input_data)}')
# o_data = make_data1(input_data, system_prompt=S_PROPMT)
# print(f'o_data {len(o_data)}')
# save_orjson(
# data=o_data,
# path="/home1/ava3/init_mahdi/project/aiDataParser/task/mn_voice/p1_summerize.json"
# )
#####################################################################################
# input_data = load_orjson(
# "/home1/ava3/init_mahdi/project/mn_voice/data/mn_voice_index.json"
# )
# print(f"input_data {len(input_data)}")
# groups = defaultdict(list)
# for item in input_data:
# # base_id = item.get("base_id")
# # if not base_id:
# # if "_" in item["id"]:
# base_id = item["id"].split("_")[0]
# groups[base_id].append(
# {"content": item["content"], "child_order": item["child_order"]}
# )
# print(f"groups {len(groups)}")
# f_data = {}
# for k, v in groups.items():
# # مرتب‌سازی بر اساس child_order
# sorted_items = sorted(v, key=lambda x: int(x["child_order"]))
# # گرفتن فقط content ها
# contents = [item["content"] for item in sorted_items]
# f_data[k] = "\n\n".join(contents)
# print(f"input_data {len(f_data)}")
# o_data = make_data2(f_data, system_prompt=S_PROPMT)
# print(f"o_data {len(o_data)}")
# save_orjson(
# data=o_data,
# path="/home1/ava3/init_mahdi/project/aiDataParser/task/mn_voice/p2_summerize.json",
# )
# python3 -m aiDataParser.task.mn_voice.summerize_make_data