First Version
This commit is contained in:
@@ -0,0 +1,301 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "6e0b19d0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# this file is loading the articles and the value measures JSON file \n",
|
||||
"# for each article and value measure it will send to LLM for an analysis\n",
|
||||
"# user can select which list of measures to be used for the analysis (orginal or opposite)\n",
|
||||
"# the analysis and th model input are saved into a JSN file"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "dd37ecab",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"MAX_NUMBER_OF_DOCUMENTS = 300"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "8edafda4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Loading Data and Measurs"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "639f75cf",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Loading JSON data"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "1a2f9775",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"with open('Step1_output_list_of_processed_measures.json', encoding=\"utf8\") as f:\n",
|
||||
" list_of_processed_measures = json.load(f)\n",
|
||||
" \n",
|
||||
"list_of_original_measures = [l['output_original'] for l in list_of_processed_measures]\n",
|
||||
"list_of_opsitive_measures = [l['output_opposite'] for l in list_of_processed_measures]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1c94123f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Analyzer class and implementation"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "b6025d8f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import joblib\n",
|
||||
"import time\n",
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"# positive\n",
|
||||
"PERSONALITY = \"\"\"\n",
|
||||
"شما یک تحلیلگر علمی و بیطرف هستید که در حوزه تحلیل محتوای کیفی تخصص دارید. وظیفه شما سنجش میزان رعایت یک معیار مشخص در یک متن علمی است.\n",
|
||||
"\n",
|
||||
"شما بر اساس یک «معیار رفتاری یا محتوایی» که به شما داده میشود و یک «متن علمی»، بررسی میکنید که تا چه حد این متن با معیار مذکور هماهنگ است. ارزیابی شما بدون تعصب، منصفانه، دقیق و تحلیلی است.\n",
|
||||
"\n",
|
||||
"شما در تحلیل خود:\n",
|
||||
"- ابتدا اجزای متن علمی را با مؤلفههای معیار تطبیق میدهید.\n",
|
||||
"- در صورت وجود تطابق یا عدم تطابق، به صورت شفاف و با مثال یا دلایل علمی تحلیل میکنید.\n",
|
||||
"- از قضاوت ارزشی پرهیز میکنید و لحن تحلیلی و عینی دارید.\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"INTRO_QUARY = \"\"\"\n",
|
||||
"اکنون لطفاً متن زیر را بر اساس معیار ارائهشده ارزیابی کن. تحلیل خود را با ساختاری علمی، دقیق و بیطرف بنویس.\n",
|
||||
"معیار مورد سنجش:\n",
|
||||
"\n",
|
||||
"\"\"\"\n",
|
||||
" \n",
|
||||
"QUESTION = \"\"\"\n",
|
||||
"\n",
|
||||
"شما در تحلیل خود:\n",
|
||||
"- ابتدا اجزای متن علمی را با مؤلفههای معیار تطبیق میدهید.\n",
|
||||
"- در صورت وجود تطابق یا عدم تطابق، به صورت شفاف و با مثال یا دلایل علمی تحلیل میکنید.\n",
|
||||
"- از قضاوت ارزشی پرهیز میکنید و لحن تحلیلی و عینی دارید.\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"from LLMInterface import LLMInterface\n",
|
||||
"class ValueAnalyser(LLMInterface):\n",
|
||||
" def __init__(self, list_of_measures, personality=PERSONALITY, number_of_jobs=64, number_of_meaures_to_valuate=64):\n",
|
||||
" super().__init__(personality=personality)\n",
|
||||
" self.number_of_jobs = number_of_jobs\n",
|
||||
"\n",
|
||||
" self.list_of_measures = list_of_measures\n",
|
||||
" self.list_of_measures = self.list_of_measures[:number_of_meaures_to_valuate]\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" def convert_to_message(self, measure, query):\n",
|
||||
" intro_text = 'حالا به متن توجه کن: '\n",
|
||||
" return INTRO_QUARY +'\\n'+ measure +'\\n'+ intro_text +'\\n'+ query +'\\n'+ QUESTION\n",
|
||||
"\n",
|
||||
" def get_text_analysis(self, text):\n",
|
||||
" def func(i):\n",
|
||||
" measure = self.list_of_measures[i]\n",
|
||||
" query = self.convert_to_message(measure, text)\n",
|
||||
" output = self.get_reponse(query)\n",
|
||||
" return (measure, query, output, -1.)\n",
|
||||
"\n",
|
||||
" with joblib.parallel_backend(\"threading\"):\n",
|
||||
" output_analysis = [joblib.Parallel(n_jobs=self.number_of_jobs)(joblib.delayed(func)(i) for i in range(len(self.list_of_measures)))]\n",
|
||||
" return output_analysis"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "afbf2718",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Resuming from paper index 294 (0‑based)\n",
|
||||
"Processed 295/300 – بررسی تهدیدها و شناسایی فرصتهای محیطی صنعت لوازم خانگی در شرایط تحریم اقتصادی، در جهت تبیین مدل اقتصاد مقاومتی (با رویکرد مدیریت استراتژیک)\n",
|
||||
"Processed 296/300 – ارائه مدل مدیریت تکنولوژی در شرایط اقتصاد مقاومتی در صنایع با تکنولوژی پیچیده؛ مطالعه موردی: صنعت مخابرات\n",
|
||||
"Processed 297/300 – بررسی فرصت های ناشی از کلان روند های اقتصاد دیجیتال به منظور تدوین الگوی توسعه صنعتی ایران در راستای سیاست های کلی اقتصاد مقاومتی\n",
|
||||
"Processed 298/300 – نقش و کارکرد اداره امور مالیاتی در راستای اصلاح نظام درآمدی دولت با تأکید بر افزایش سهم درآمدهای مالیاتی مطرح در سیاستهای کلی اقتصاد مقاومتی\n",
|
||||
"Processed 299/300 – بررسی عدالت اقتصادی از منظر قرآن(در راستای تحقق اقتصاد مقاومتی)\n",
|
||||
"Processed 300/300 – نقش دولت در مدیریت واردات در راستای تحقق سیاستهای کلی اقتصاد مقاومتی\n",
|
||||
"Processing complete.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"import time\n",
|
||||
"from typing import List, Dict, Any\n",
|
||||
"\n",
|
||||
"analyzer = ValueAnalyser(list_of_measures=list_of_original_measures)\n",
|
||||
"\n",
|
||||
"# Load the source data\n",
|
||||
"with open('Results.json', encoding='utf8') as f:\n",
|
||||
" data = json.load(f)\n",
|
||||
"\n",
|
||||
"TOTAL_PAPERS = len(data['results'])\n",
|
||||
"\n",
|
||||
"CHECKPOINT_FILE = 'checkpoint.txt'\n",
|
||||
"OUTPUT_FILE = 'step2-original.json'\n",
|
||||
"\n",
|
||||
"def load_checkpoint() -> int:\n",
|
||||
" \"\"\"Return the last successfully processed index (0‑based), or 0 if none.\"\"\"\n",
|
||||
" try:\n",
|
||||
" with open(CHECKPOINT_FILE, 'r') as f:\n",
|
||||
" return int(f.read().strip())\n",
|
||||
" except FileNotFoundError:\n",
|
||||
" return 0\n",
|
||||
"\n",
|
||||
"def save_checkpoint(index: int):\n",
|
||||
" \"\"\"Save the last completed index.\"\"\"\n",
|
||||
" with open(CHECKPOINT_FILE, 'w') as f:\n",
|
||||
" f.write(str(index))\n",
|
||||
"\n",
|
||||
"def load_existing_results() -> List[Dict[str, Any]]:\n",
|
||||
" \"\"\"Load previously saved results, or return empty list if file is missing/corrupt.\"\"\"\n",
|
||||
" try:\n",
|
||||
" with open(OUTPUT_FILE, 'r', encoding='utf8') as f:\n",
|
||||
" return json.load(f)\n",
|
||||
" except (FileNotFoundError, json.JSONDecodeError):\n",
|
||||
" return []\n",
|
||||
"\n",
|
||||
"def save_results(results: List[Dict[str, Any]]):\n",
|
||||
" \"\"\"Overwrite the output file with the full list of results.\"\"\"\n",
|
||||
" with open(OUTPUT_FILE, 'w', encoding='utf8') as f:\n",
|
||||
" json.dump(results, f, ensure_ascii=False, indent=2)\n",
|
||||
"\n",
|
||||
"# Resume from last checkpoint\n",
|
||||
"start_index = load_checkpoint()\n",
|
||||
"all_results = load_existing_results()\n",
|
||||
"\n",
|
||||
"# If you want to process a fixed range (e.g., 10 to 20), adjust start_index accordingly\n",
|
||||
"START = 0\n",
|
||||
"END = TOTAL_PAPERS # or 20, or any limit\n",
|
||||
"\n",
|
||||
"if start_index < START:\n",
|
||||
" start_index = START\n",
|
||||
"\n",
|
||||
"print(f\"Resuming from paper index {start_index} (0‑based)\")\n",
|
||||
"\n",
|
||||
"# Process each remaining paper\n",
|
||||
"for i in range(start_index, END):\n",
|
||||
" article = data['results'][i]\n",
|
||||
" retries = 3\n",
|
||||
" success = False\n",
|
||||
"\n",
|
||||
" for attempt in range(retries):\n",
|
||||
" try:\n",
|
||||
" # Simulate network call – your actual analysis may involve HTTP requests\n",
|
||||
" res = analyzer.get_text_analysis('\\n'.join(article))\n",
|
||||
" \n",
|
||||
" result_entry = {\n",
|
||||
" \"title\": article['title'],\n",
|
||||
" \"year\": article[\"year\"],\n",
|
||||
" \"grade\": article[\"grade\"],\n",
|
||||
" \"authors\": article[\"authors\"],\n",
|
||||
" \"universities\": article[\"universities\"],\n",
|
||||
" 'link': article['link'],\n",
|
||||
" 'abstract': article['abstract'],\n",
|
||||
" \"response\": res\n",
|
||||
" }\n",
|
||||
" \n",
|
||||
" all_results.append(result_entry)\n",
|
||||
" save_results(all_results) # save after each success\n",
|
||||
" save_checkpoint(i + 1) # next index to start from\n",
|
||||
" print(f\"Processed {i+1}/{END} – {article['title']}\")\n",
|
||||
" success = True\n",
|
||||
" break # exit retry loop\n",
|
||||
" \n",
|
||||
" except Exception as e:\n",
|
||||
" print(f\"Error on paper {i} (attempt {attempt+1}/{retries}): {e}\")\n",
|
||||
" if attempt < retries - 1:\n",
|
||||
" time.sleep(5) # wait before retry\n",
|
||||
" else:\n",
|
||||
" print(f\"Failed to process paper {i} after {retries} attempts. Skipping.\")\n",
|
||||
" # Optionally, save checkpoint as i+1 to skip this paper entirely\n",
|
||||
" save_checkpoint(i + 1)\n",
|
||||
" \n",
|
||||
" if not success:\n",
|
||||
" # Log the failure but continue with next paper\n",
|
||||
" continue\n",
|
||||
" \n",
|
||||
" # Small delay between papers to avoid overwhelming the server\n",
|
||||
" time.sleep(1)\n",
|
||||
"\n",
|
||||
"print(\"Processing complete.\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "baad4652-cc0b-4f0b-8e89-c1ec8bc95b3d",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"300\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(TOTAL_PAPERS)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f8764ea0-1301-4696-982d-e5f6120e959c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.13"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Reference in New Issue
Block a user