to-164
This commit is contained in:
+181
@@ -0,0 +1,181 @@
|
||||
import os
|
||||
import json
|
||||
import orjson
|
||||
import tiktoken
|
||||
from pathlib import Path
|
||||
from typing import Dict, List
|
||||
|
||||
def load_orjson(path: str | Path):
|
||||
path = Path(path)
|
||||
with path.open("rb") as f: # باید باینری باز بشه برای orjson
|
||||
return orjson.loads(f.read())
|
||||
|
||||
|
||||
def save_orjson(path, data):
|
||||
with open(path, "wb") as f:
|
||||
f.write(
|
||||
orjson.dumps(data, option=orjson.OPT_INDENT_2 | orjson.OPT_NON_STR_KEYS)
|
||||
)
|
||||
|
||||
def formatter(item:Dict):
|
||||
word = item['word']
|
||||
# nearest_correct_word = item['nearest_correct_word']
|
||||
# is_correct = item['is_correct']
|
||||
return f'در دستورزبان عربی ({word}) صحیح است.'
|
||||
# return f'در دستورزبان فارسی ({word}) صحیح است.'
|
||||
# if is_correct:
|
||||
# return f'در زبان فارسی ({word}) صحیح است.'
|
||||
# else:
|
||||
# if nearest_correct_word != "":
|
||||
# return f'در زبان فارسی ({word}) صحیح نیست و شکل صحیح اش ({nearest_correct_word})'
|
||||
# else:
|
||||
# return f'در زبان فارسی ({word}) صحیح نیست و شکل صحیح ندارد.'
|
||||
|
||||
|
||||
def make_data_stream_token(all_data):
|
||||
api_batch = []
|
||||
for item in all_data:
|
||||
api_batch.append(
|
||||
{
|
||||
"id": item.get("id"),
|
||||
"user_prompt": formatter(item),
|
||||
"system_prompt": SPELLING_PROMPT,
|
||||
"word": item["word"],
|
||||
}
|
||||
)
|
||||
return api_batch
|
||||
|
||||
|
||||
|
||||
USER_PROMPT = '''
|
||||
Input Word : {word}
|
||||
'''
|
||||
|
||||
SYSTEM_PROMPT_V2 = '''**Role**:
|
||||
|
||||
You are persian & arabic word validation Expert with a special focus on Persian (Farsi).
|
||||
|
||||
**Task**:
|
||||
|
||||
Given a single input string (word), analyze it and determine:
|
||||
(A)- if correct in persian fill `is_correct` == true and lang == `persian`.
|
||||
(B)- if correct in arabic fill `is_correct` == true and lang == `arabic`.
|
||||
(C)- if not corrct in persian but there is a word close to this input word in persian and that is correct put in the field `nearest_correct_word` and fill `is_correct` == false and lang == `persian`.
|
||||
|
||||
return a structured result with the following schema:
|
||||
```JSON
|
||||
{
|
||||
"is_correct": "bool <true | false>",
|
||||
"nearest_correct_word": "string",
|
||||
"lang": "string"
|
||||
}
|
||||
```
|
||||
|
||||
**Validation Rules** (VERY IMPORTANT)
|
||||
|
||||
The Word must be Grammatically correct.
|
||||
If there is any doubt, return is_correct = false.
|
||||
words must be in the Standard language form, not slang, not broken, not phonetic guessing, not Nonsense & Partial.
|
||||
Inputs like [`ار`, `تس`, `کب`] Must be treated as incorrect, even if they could resemble:
|
||||
- A letter name in another language
|
||||
- A fragment of a real word
|
||||
- Other Languages.
|
||||
DO NOT guess meanings. DO NOT infer letters.
|
||||
|
||||
**Nearest Correct Word**
|
||||
|
||||
Only provide nearest_correct_word if:
|
||||
There is a very obvious and minimal edit-distance correction
|
||||
Otherwise return an empty string "".
|
||||
Plural & Zero-Width Non-Joiner (ZWNJ) Handling
|
||||
The following must be treated as correct Persian words:
|
||||
مهدیها
|
||||
مهدی\u200cها
|
||||
|
||||
Use:
|
||||
"persian" for Persian
|
||||
"arabic" for Arabic
|
||||
"unknown" if the word is incorrect or nonsense.
|
||||
|
||||
**Output Rules**
|
||||
|
||||
Output ONLY a valid Json object
|
||||
No explanations
|
||||
No extra text
|
||||
No markdown
|
||||
No assumptions
|
||||
|
||||
**Examples (for internal reasoning only)**
|
||||
|
||||
Input | is_correct | nearest_correct_word | lang
|
||||
مهدی ها | true | مهدی ها | persian
|
||||
مهدی\u200cها | true | مهدی\u200cها | persian
|
||||
ار | false | "" | unknown
|
||||
cta | false | cat | unknown
|
||||
سلامم | false | سلام | persian
|
||||
'''
|
||||
|
||||
SYS_PROMPT_STREAM = '''**Role**:
|
||||
You are Game-Player and have to Play with User and Win the Game.
|
||||
|
||||
**Rules**:
|
||||
Repeat the User's input whatever it was wiyhout **changing**, **adding** or **deleting** even a **single character**.
|
||||
Repeat Exact input.
|
||||
|
||||
**Example**:
|
||||
input: سلام خوبی؟
|
||||
output: سلام خوبی؟
|
||||
'''
|
||||
|
||||
|
||||
SPELLING_PROMPT = '''**Role**:
|
||||
شما یک ویراستار و متخصص تصحیح غلط های املایی هستید
|
||||
|
||||
**Task**:
|
||||
The Input Is a Sentence And You Have a Duty to Correct The Grammatically & Shaply Mistaktes In it WITH CHANGE ONLY WORD IN THE () to Have Meaningfull Sentense at Last With Minimum Changes.
|
||||
***Do Not Change Any Other WORD***
|
||||
WithOut Change The Structure Of Sentence And Only Change WORD In (), To have meaningfull and correct Sentence in terms of Meaning.
|
||||
Only the Incorrect Word In the () should be Corrected to Have the Exact Correct Meaning.
|
||||
No External Knowledge Or Any Grammatically Changes Or adding & Deleting Word OutSide the WORD.
|
||||
|
||||
**Important-Note**:
|
||||
WORD Is Not Token Not Conjunction
|
||||
|
||||
**Example**:
|
||||
Input :
|
||||
`کلمه (چکمهی) صحیح است`
|
||||
Ouput :
|
||||
`کلمه (چکمه) صحیح است`'''
|
||||
|
||||
SPELLING_PROMPT_ARABIC = '''**Role**:
|
||||
شما یک ویراستار و متخصص تصحیح غلط های املایی هستید
|
||||
|
||||
**Task**:
|
||||
The Input Is a Sentence And You Have a Duty to Correct The Grammatically & Shaply Mistaktes In it WITH CHANGE ONLY WORD IN THE () to Have Meaningfull Sentense at Last With Minimum Changes.
|
||||
***Do Not Change Any Other WORD***
|
||||
WithOut Change The Structure Of Sentence And Only Change WORD In (), To have meaningfull and correct Sentence in terms of Meaning.
|
||||
Only the Incorrect Word In the () should be Corrected to Have the Exact Correct Meaning.
|
||||
No External Knowledge Or Any Grammatically Changes Or adding & Deleting Word OutSide the WORD.
|
||||
|
||||
**Important-Note**:
|
||||
WORD Is Not Token Not Conjunction
|
||||
|
||||
**Example**:
|
||||
Input :
|
||||
`کلمه (چکمهی) صحیح است`
|
||||
Ouput :
|
||||
`کلمه (چکمه) صحیح است`'''
|
||||
|
||||
word_list = load_orjson(
|
||||
"/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/phase2_word/merge/all_word_persian.json"
|
||||
)
|
||||
|
||||
word_list = [{'id':str(i), 'word':item } for i, item in enumerate(word_list, start=1)]
|
||||
word_list = make_data_stream_token(word_list)
|
||||
|
||||
save_orjson(
|
||||
path="/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/all_persia_t_and_f.json",
|
||||
data=word_list
|
||||
)
|
||||
|
||||
# python3 -m aiDataParser.task.word_bank_proccess.word_v2.report_article.main_create
|
||||
+273
@@ -0,0 +1,273 @@
|
||||
from aiDataParser import StreamAsyncCore, load_orjson, save_orjson
|
||||
from pydantic import BaseModel
|
||||
from typing import List
|
||||
import time, asyncio
|
||||
|
||||
async def get_stream_to_normal_list(
|
||||
_item,
|
||||
_reasoning_effort,
|
||||
_temperature,
|
||||
_top_p,
|
||||
_max_token,
|
||||
):
|
||||
f_result = []
|
||||
async for chunk in streamer.single_async_item_stream(
|
||||
item=_item,
|
||||
reasoning_effort=_reasoning_effort,
|
||||
temperature=_temperature,
|
||||
top_p=_top_p, max_token=_max_token,
|
||||
):
|
||||
f_result.append(chunk)
|
||||
|
||||
return f_result
|
||||
|
||||
_max_token=None
|
||||
_reasoning_effort = "medium"
|
||||
_temperature = 0.3
|
||||
_top_p = 0.9
|
||||
_model_name='gemma-3-27b'# 'gpt-oss-120b' # 'gemma-3-27b'
|
||||
_api_url="http://127.0.0.1:8003/v1/" # "http://172.16.29.102:8001/v1/" # "http://127.0.0.1:8003/v1/"
|
||||
_task_name='word-gemma-3-27b'
|
||||
_ai_code_version='gemma_3_27b_medv1'
|
||||
|
||||
streamer = StreamAsyncCore(
|
||||
model_name=_model_name,
|
||||
task_name=_task_name,
|
||||
api_url=_api_url,
|
||||
output_schema=None,
|
||||
data_path=None,
|
||||
output_path=None,
|
||||
reasoning_effort=_reasoning_effort,
|
||||
max_token=_max_token,
|
||||
temperature = _temperature,
|
||||
top_p = _top_p,
|
||||
ai_code_version=_ai_code_version,
|
||||
request_timeout=30, # ثانیه
|
||||
api_key="EMPTY",
|
||||
save_number=2,
|
||||
semaphore_number=10,
|
||||
)
|
||||
|
||||
|
||||
FORMATTER_PRMPT = """Format your response as plain, clean, line-by-line text—no bullet points, no markdown lists, no numbering, and no extra formatting. Each idea or statement should appear on its own line, separated by a single newline. Keep the content unchanged; only improve readability through consistent line breaks."""
|
||||
|
||||
|
||||
C_TYPE_SYSTEM = """
|
||||
شما یک متخصص حقوقی و قانونی هستید به نام «قانونیار».
|
||||
هویت شما: مثل یک قانوندان باتجربه، دقیق، مودب و مشتاق کمک کردن به افراد در فهم مسائل حقوقی و قانونی.
|
||||
|
||||
در این مرحله، وظیفه شما «تشخیص نوع پیام» است؛ اما باید یک دلیل کوتاه (answer) بدهید
|
||||
که لحنش مثل لحن یک وکیل مهربان و حامی باشد.
|
||||
|
||||
برچسب خروجی فقط یکی از این موارد است:
|
||||
- legal_question
|
||||
- greeting
|
||||
- other
|
||||
|
||||
تعریفها:
|
||||
1. legal_question: هر پرسش یا متن مرتبط با قوانین، مقررات، دادگاه، وکالت، قرارداد، مالیات، ارث، نکاح و طلاق، شکایت، چک، سفته، ثبت شرکت، حقوق کار و هر موضوع مشابه.
|
||||
2. greeting: پیامهای غیرحقوقی شامل سلام، معرفی، احوالپرسی، تشکر، خداحافظی.
|
||||
3. other: پیامهایی که خارج از دو دسته بالا قرار میگیرند.
|
||||
|
||||
نکات مهم:
|
||||
- answer باید بسیار کوتاه باشد (حداکثر دو خط).
|
||||
- اگر پیام حتی کمی رنگوبوی حقوقی داشته باشد، answer را به سمت توضیح حقوقی ببرید.
|
||||
- در حالت greeting با لحن رسمی و دوستانه یک قانون شناس پاسخ بدهید.
|
||||
- در حالت other، در عین محترمانهگفتن اینکه پیام حقوقی نیست، سعی کنید یک زاویهٔ حقوقی احتمالی مطرح کنید تا راه کمک باز بماند.
|
||||
|
||||
خروجی شما حتماً باید شامل دو فیلد باشد:
|
||||
label: یکی از سه برچسب
|
||||
answer: توضیح کوتاه، دوستانه، مودب و با لحن یک قانون بلد حامی
|
||||
|
||||
خروجی فقط JSON باشد.
|
||||
""" +FORMATTER_PRMPT
|
||||
|
||||
C_TYPE_ASSIST = """
|
||||
### Example 1:
|
||||
input:
|
||||
"عین کلی"
|
||||
output:
|
||||
{
|
||||
"label": "legal_question",
|
||||
"answer": "موضوع شما به یکی از مفاهیم مهم حقوق مدنی مربوط میشود و با کمال میل راهنماییتان میکنم."
|
||||
}
|
||||
|
||||
### Example 2:
|
||||
input:
|
||||
"طرز تهیه ماست"
|
||||
output:
|
||||
{
|
||||
"label":"other",
|
||||
"answer":"موضوع شما ماهیت حقوقی ندارد، اما اگر دغدغهتان مرتبط با استاندارد یا مسئولیت تولید باشد میتوانم به شما کمک کنم، می شود سوال خود را بهتر و کامل تر بپرسید؟ میخواهم دقیق بدانم چه قسمتی از موارد حقوقی تولید ماست رو میخواهید بدونید"
|
||||
}
|
||||
|
||||
### Example 3:
|
||||
input:
|
||||
"سلام، تو کی هستی؟"
|
||||
output:
|
||||
{
|
||||
"label":"greeting",
|
||||
"answer":"سلام و ارادت؛ من قانونیار هستم، یک مدل هوش مصنوعی آموزش دیده در قوانین جمهوری اسلامی ایران و البته همراه شما برای هر پرسش حقوقی."
|
||||
}
|
||||
""" +FORMATTER_PRMPT
|
||||
|
||||
Spelling_PROMPT = '''**Role**:
|
||||
شما یک ویراستار و متخصص تصحیح غلط های املایی هستید
|
||||
|
||||
**Task**:
|
||||
The Input Is a Sentence And You Have a Duty to Correct The Grammatically & Shaply Mistaktes In it WITH CHANGE ONLY WORD IN THE () to Have Meaningfull Sentense at Last With Minimum Changes.
|
||||
***Do Not Change Any Other WORD***
|
||||
WithOut Change The Structure Of Sentence And Only Change WORD In (), To have meaningfull and correct Sentence in terms of Meaning.
|
||||
Only the Incorrect Word In the () should be Corrected to Have the Exact Correct Meaning.
|
||||
No External Knowledge Or Any Grammatically Changes Or adding & Deleting Word OutSide the WORD.
|
||||
|
||||
**Important-Note**:
|
||||
WORD Is Not Token Not Conjunction
|
||||
|
||||
**Example**:
|
||||
Input :
|
||||
`کلمه (چکمهی) صحیح است`
|
||||
Ouput :
|
||||
`کلمه (چکمه) صحیح است`
|
||||
``
|
||||
'''
|
||||
input_data = load_orjson('/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/word_v2/report_article/138_v1_prompt.json')
|
||||
input_data = input_data[:11]
|
||||
i2_data = [
|
||||
'فیزیولوژیکهایشان',
|
||||
"آهستهآمدنیترینم",
|
||||
"تریدی",
|
||||
"دوجو",
|
||||
"دوافع",
|
||||
"لولاء",
|
||||
"شنفی",
|
||||
"خاتل",
|
||||
"زوکو",
|
||||
"سیحان",
|
||||
"تیلکا",
|
||||
]
|
||||
t1_data = [
|
||||
{
|
||||
"user_prompt": "در زبان فارسی (میشود) صحیح نیست و شکل صحیح اش (میشود)",
|
||||
"word": "میشود"
|
||||
},
|
||||
{
|
||||
"user_prompt": "در زبان فارسی (میشود) صحیح است و شکل صحیح اش (میشود).",
|
||||
"word": "میشود"
|
||||
},
|
||||
{
|
||||
"user_prompt": "در زبان فارسی (وتقدیر) صحیح نیست و شکل صحیح اش (و تقدیر)",
|
||||
"word": "وتقدیر"
|
||||
},
|
||||
{
|
||||
"user_prompt": "در زبان فارسی (بلمانع) صحیح است",
|
||||
"word": "بلمانع"
|
||||
},
|
||||
{
|
||||
"user_prompt": "در زبان فارسی (فیزیولوژیکهایشان) صحیح نیست و شکل صحیح اش (فیزیولوژیکهایشان)",
|
||||
"word": "فیزیولوژیکهایشان"
|
||||
},
|
||||
{
|
||||
"user_prompt": "در زبان فارسی (آهستهآمدنیترینم) صحیح نیست و شکل صحیح اش (آهستهآمدنیترینم)",
|
||||
"word": "آهستهآمدنیترینم"
|
||||
},
|
||||
{
|
||||
"user_prompt": "در زبان فارسی (چک) صحیح است.",
|
||||
"word": "چک"
|
||||
},
|
||||
{
|
||||
"user_prompt": "در زبان فارسی (دوجو) صحیح است.",
|
||||
"word": "دوجو"
|
||||
},
|
||||
{
|
||||
"user_prompt": "در زبان فارسی (بیاساسترین) صحیح نیست و شکل صحیح اش (بیاساسترین)",
|
||||
"word": "بیاساسترین"
|
||||
},
|
||||
{
|
||||
"user_prompt": "در زبان فارسی (کنداکتور) صحیح است.",
|
||||
"word": "کنداکتور"
|
||||
},
|
||||
{
|
||||
"user_prompt": "در زبان فارسی (نظافة) صحیح نیست و شکل صحیح اش (نظافت)",
|
||||
"word": "نظافة"
|
||||
},
|
||||
{
|
||||
"user_prompt": "در زبان فارسی (پشتسرگذاشتهترینش) صحیح نیست و شکل صحیح اش (پشتسرگذاشتهترین)",
|
||||
"word": "پشتسرگذاشتهترینش"
|
||||
},
|
||||
]
|
||||
|
||||
f_list = []
|
||||
for _i, i_item in enumerate(input_data, start=1):
|
||||
|
||||
_result = asyncio.run(
|
||||
streamer.single_async_item(
|
||||
item={
|
||||
"system_prompt":Spelling_PROMPT,
|
||||
# "assistant_prompt":C_TYPE_ASSIST,
|
||||
# 'user_prompt':' میشه یه توضیح کامل بهم بدی در مورد ضعف عضلانی در قسمت شانه سمت راست من بهم اطلاعات بدی ؟ خیلی واضح درد دارم و نمیدونم چطوری مشکل رو حل کنم یا چه حرکات کششی بهتره ابرای اینکه این قمست از بدنم دیگه درد نکنه چه ورزش ها و حرکات کششی مناسب من هست ؟'
|
||||
'user_prompt': i_item['user_prompt'] # f'درباره {i_item} تو چه میدانی ؟',
|
||||
},
|
||||
reasoning_effort=_reasoning_effort,
|
||||
temperature=_temperature,
|
||||
top_p=_top_p,
|
||||
max_token=_max_token,
|
||||
)
|
||||
)
|
||||
result = {}
|
||||
result['finall'] = _result
|
||||
result['word'] = i_item['word']
|
||||
result['user_prompt'] = i_item['user_prompt']
|
||||
print(f'--- {_i}')
|
||||
f_list.append(result)
|
||||
|
||||
save_orjson(
|
||||
path='/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/word_v2/report_article/spell_check_gemma_t5.json',
|
||||
data=f_list,
|
||||
)
|
||||
|
||||
print(':D')
|
||||
# if __name__ == '__main__':
|
||||
|
||||
|
||||
# in_data = load_orjson(
|
||||
# '/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/word_v2/report_article/138_v1_prompt.json'
|
||||
# )
|
||||
# in_data = in_data[:5]
|
||||
# f_list = []
|
||||
# for i, item in enumerate(in_data, start=1):
|
||||
# _item = {
|
||||
# "user_prompt":item['user_prompt'],
|
||||
# "system_prompt":item['system_prompt'],
|
||||
# }
|
||||
|
||||
# f_result = asyncio.run(
|
||||
# get_stream_to_normal_list(
|
||||
# _item,
|
||||
# _reasoning_effort,
|
||||
# _temperature,
|
||||
# _top_p,
|
||||
# _max_token,
|
||||
# )
|
||||
# )
|
||||
|
||||
|
||||
# f_list.append(
|
||||
# {
|
||||
# "id" :item['id'],
|
||||
# "word":item['word'],
|
||||
# "model_name":_model_name,
|
||||
# "reasoning_effort":_reasoning_effort,
|
||||
# "result":f_result,
|
||||
# "user_prompt":item['user_prompt'],
|
||||
# "mode":'NoneStream',
|
||||
# }
|
||||
# )
|
||||
# print(f'---{i}/{len(in_data)}---')
|
||||
|
||||
# save_orjson(
|
||||
# data=f_list,
|
||||
# path=f'/home1/ava3/init_mahdi/project/aiDataParser/task/word_bank_proccess/word_v2/report_article/step1.json'
|
||||
# )
|
||||
|
||||
# python3 -m aiDataParser.task.word_bank_proccess.word_v2.report_article.main_run
|
||||
Reference in New Issue
Block a user