{ "cells": [ { "cell_type": "code", "execution_count": 18, "id": "455e3ffe-b0f3-4c51-9e43-c0c13eb3add9", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from arabert.preprocess import ArabertPreprocessor" ] }, { "cell_type": "code", "execution_count": 19, "id": "2c1af2d3-1c84-42c6-8d4a-6f8312ab2038", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING:root:Model provided is not in the accepted model list. Preprocessor will default to a base Arabic preprocessor\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Done! Saved to /home/skiredj.abderrahman/khalil/sbert_training/third_training/benchmark/benchmark15_preprocessed_arabertv02.csv\n", " sentence1 \\\n", "0 شخص ما يحمل لوح التزلج ليلا على الرصيف . \n", "1 تتسابق النساء في سباق الدايتونا 500 . \n", "2 تمشي النساء جنبا إلى جنب . \n", "3 يقفز الرجل ذو القميص الأخضر عاليا على العشب . \n", "4 رجلان يجلسان على العشب ومعهما موز . \n", "\n", " sentence2 score \n", "0 رجل جالس بمفرده يقرأ على طاولة مستديرة ، خارج ... 0.8 \n", "1 يتسابق بعض الرجال ضمن مسابقة التزلج . 1.0 \n", "2 هناك فتيات يمشين متجاورات 2.6 \n", "3 يمشي الرجل ذو القميص الأبيض على العشب الطويل م... 2.2 \n", "4 ثلاثة رجال يتسكعون عند فرشة بيع الفاكهة . 1.4 \n" ] } ], "source": [ "#mteb/sts17-crosslingual-sts\n", "\n", "\n", "# Load CSV\n", "df = pd.read_csv('/home/skiredj.abderrahman/khalil/sbert_training/third_training/benchmark/benchmark15.csv')\n", "\n", "# Drop lang column\n", "df = df.drop(columns=['lang'], errors='ignore')\n", "\n", "# Initialize ArabertPreprocessor\n", "model_name = \"/home/skiredj.abderrahman/khalil/sbert_training/third_training/bert-base-arabertv02\"\n", "arabert_prep = ArabertPreprocessor(model_name=model_name)\n", "\n", "# Preprocess all text columns (adjust column names as needed)\n", "text_columns = df.select_dtypes(include='object').columns.tolist()\n", "\n", "for col in text_columns:\n", " df[col] = df[col].apply(lambda x: arabert_prep.preprocess(str(x)) if pd.notna(x) else x)\n", "\n", "# Save preprocessed CSV\n", "output_path = '/home/skiredj.abderrahman/khalil/sbert_training/third_training/benchmark/benchmark15_preprocessed_arabertv02.csv'\n", "df.to_csv(output_path, index=False)\n", "\n", "print(f\"Done! Saved to {output_path}\")\n", "print(df.head())" ] }, { "cell_type": "code", "execution_count": 22, "id": "fdfe7c48-5358-4cde-a81c-b3300634c766", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/skiredj.abderrahman/.conda/envs/sbert_khalil/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Loading CSV...\n", " 250 pairs loaded.\n", " sentence1 \\\n", "0 شخص ما يحمل لوح التزلج ليلا على الرصيف . \n", "1 تتسابق النساء في سباق الدايتونا 500 . \n", "2 تمشي النساء جنبا إلى جنب . \n", "3 يقفز الرجل ذو القميص الأخضر عاليا على العشب . \n", "4 رجلان يجلسان على العشب ومعهما موز . \n", "\n", " sentence2 score \n", "0 رجل جالس بمفرده يقرأ على طاولة مستديرة ، خارج ... 0.8 \n", "1 يتسابق بعض الرجال ضمن مسابقة التزلج . 1.0 \n", "2 هناك فتيات يمشين متجاورات 2.6 \n", "3 يمشي الرجل ذو القميص الأبيض على العشب الطويل م... 2.2 \n", "4 ثلاثة رجال يتسكعون عند فرشة بيع الفاكهة . 1.4 \n", "\n", "Loading SBERT model and encoding sentences...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Loading weights: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████| 199/199 [00:00<00:00, 856.85it/s, Materializing param=pooler.dense.weight]\n", "Batches: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 8/8 [00:00<00:00, 22.09it/s]\n", "Batches: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 8/8 [00:00<00:00, 156.77it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Computing cosine similarities...\n", "\n", "─── STS Correlation Results ───────────────────────────────\n", " Pearson r : 0.8219 (p=1.5414e-62)\n", " Spearman r : 0.8230 (p=7.4959e-63)\n", "\n", "─── Hit Rate & MRR ────────────────────────────────────────\n", " (Relevance threshold on gold score: >= 0.5)\n", " Hit Rate @ 1 : 0.5668 (123/217)\n", " Hit Rate @ 3 : 0.7189 (156/217)\n", " Hit Rate @ 5 : 0.7604 (165/217)\n", " MRR : 0.6610\n", "\n", "Results saved to: /home/skiredj.abderrahman/khalil/sbert_training/third_training/benchmark/benchmark15_preprocessed_arabertv02_benchmark_results.csv\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "from sentence_transformers import SentenceTransformer\n", "from sklearn.metrics.pairwise import cosine_similarity\n", "from scipy.stats import pearsonr, spearmanr\n", "\n", "# ─── CONFIG ───────────────────────────────────────────────────────────────────\n", "MODEL_PATH = \"/home/skiredj.abderrahman/khalil/sbert_training/third_training/output/arabert_20260326_1254/final\"\n", "CSV_PATH = \"/home/skiredj.abderrahman/khalil/sbert_training/third_training/benchmark/benchmark15_preprocessed_arabertv02.csv\"\n", "TOP_K = [1, 3, 5]\n", "SCORE_THRESHOLD = 0.5 # adjust if your score scale is not [0,1]\n", "# ──────────────────────────────────────────────────────────────────────────────\n", "\n", "# 1. Load CSV\n", "print(\"Loading CSV...\")\n", "df = pd.read_csv(CSV_PATH)\n", "if 'lang' in df.columns:\n", " df = df.drop(columns=['lang'])\n", "\n", "print(f\" {len(df)} pairs loaded.\")\n", "print(df.head())\n", "\n", "# 2. Encode\n", "print(\"\\nLoading SBERT model and encoding sentences...\")\n", "model = SentenceTransformer(MODEL_PATH)\n", "emb1 = model.encode(df['sentence1'].tolist(), batch_size=32, show_progress_bar=True, convert_to_numpy=True)\n", "emb2 = model.encode(df['sentence2'].tolist(), batch_size=32, show_progress_bar=True, convert_to_numpy=True)\n", "\n", "# 3. Cosine similarity per pair\n", "print(\"\\nComputing cosine similarities...\")\n", "cos_scores = np.array([\n", " cosine_similarity(emb1[i].reshape(1, -1), emb2[i].reshape(1, -1))[0][0]\n", " for i in range(len(df))\n", "])\n", "\n", "# 4. Pearson & Spearman\n", "gold_scores = df['score'].astype(float).values\n", "pearson_corr, p_val_p = pearsonr(cos_scores, gold_scores)\n", "spearman_corr, p_val_s = spearmanr(cos_scores, gold_scores)\n", "\n", "print(\"\\n─── STS Correlation Results ───────────────────────────────\")\n", "print(f\" Pearson r : {pearson_corr:.4f} (p={p_val_p:.4e})\")\n", "print(f\" Spearman r : {spearman_corr:.4f} (p={p_val_s:.4e})\")\n", "\n", "# 5. Hit Rate & MRR\n", "print(\"\\n─── Hit Rate & MRR ────────────────────────────────────────\")\n", "print(f\" (Relevance threshold on gold score: >= {SCORE_THRESHOLD})\")\n", "\n", "hit_counts = {k: 0 for k in TOP_K}\n", "reciprocal_ranks = []\n", "n_queries = 0\n", "\n", "for i in range(len(df)):\n", " if gold_scores[i] < SCORE_THRESHOLD:\n", " continue\n", "\n", " query_emb = emb1[i].reshape(1, -1)\n", " sims = cosine_similarity(query_emb, emb2)[0]\n", " ranked_idxs = np.argsort(sims)[::-1]\n", "\n", " rank = np.where(ranked_idxs == i)[0][0] + 1 # 1-based\n", "\n", " for k in TOP_K:\n", " if rank <= k:\n", " hit_counts[k] += 1\n", "\n", " reciprocal_ranks.append(1.0 / rank)\n", " n_queries += 1\n", "\n", "if n_queries == 0:\n", " print(\" No relevant pairs found — lower SCORE_THRESHOLD.\")\n", "else:\n", " for k in TOP_K:\n", " hr = hit_counts[k] / n_queries\n", " print(f\" Hit Rate @{k:2d} : {hr:.4f} ({hit_counts[k]}/{n_queries})\")\n", "\n", " mrr = np.mean(reciprocal_ranks)\n", " print(f\" MRR : {mrr:.4f}\")\n", "\n", "# 6. Save results\n", "df['predicted_cos_sim'] = cos_scores\n", "output_path = CSV_PATH.replace('.csv', '_benchmark_results.csv')\n", "df.to_csv(output_path, index=False)\n", "print(f\"\\nResults saved to: {output_path}\")" ] }, { "cell_type": "code", "execution_count": 23, "id": "0596225c-0dbe-4cbc-a97e-6f4be5e19cfa", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loading CSV...\n", " 250 pairs loaded.\n", " sentence1 \\\n", "0 شخص ما يحمل لوح التزلج ليلا على الرصيف. \n", "1 تتسابق النساء في سباق الدايتونا 500. \n", "2 تمشي النساء جنبا إلى جنب. \n", "3 يقفز الرجل ذو القميص الأخضر عاليا على العشب. \n", "4 رجلان يجلسان على العشب ومعهما موز. \n", "\n", " sentence2 score \n", "0 رجل جالس بمفرده يقرأ على طاولة مستديرة ، خارج ... 0.8 \n", "1 يتسابق بعض الرجال ضمن مسابقة التزلج. 1.0 \n", "2 هناك فتيات يمشين متجاورات 2.6 \n", "3 يمشي الرجل ذو القميص الأبيض على العشب الطويل م... 2.2 \n", "4 ثلاثة رجال يتسكعون عند فرشة بيع الفاكهة. 1.4 \n", "\n", "Loading multilingual-e5-base and encoding sentences...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.\n", "WARNING:huggingface_hub.utils._http:Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.\n", "Loading weights: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████| 199/199 [00:00<00:00, 743.94it/s, Materializing param=pooler.dense.weight]\n", "\u001b[1mXLMRobertaModel LOAD REPORT\u001b[0m from: intfloat/multilingual-e5-base\n", "Key | Status | | \n", "------------------------+------------+--+-\n", "embeddings.position_ids | UNEXPECTED | | \n", "\n", "\u001b[3mNotes:\n", "- UNEXPECTED\u001b[3m\t:can be ignored when loading from different task/architecture; not ok if you expect identical arch.\u001b[0m\n", "Batches: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 8/8 [00:00<00:00, 78.36it/s]\n", "Batches: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 8/8 [00:00<00:00, 123.73it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "Computing cosine similarities...\n", "\n", "─── STS Correlation Results ───────────────────────────────\n", " Pearson r : 0.7446 (p=1.9457e-45)\n", " Spearman r : 0.7450 (p=1.6963e-45)\n", "\n", "─── Hit Rate & MRR ────────────────────────────────────────\n", " (Relevance threshold on gold score: >= 0.5)\n", " Hit Rate @ 1 : 0.5991 (130/217)\n", " Hit Rate @ 3 : 0.7327 (159/217)\n", " Hit Rate @ 5 : 0.7696 (167/217)\n", " MRR : 0.6794\n", "\n", "Results saved to: /home/skiredj.abderrahman/khalil/sbert_training/third_training/benchmark/benchmark15_e5_benchmark_results.csv\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "from sentence_transformers import SentenceTransformer\n", "from sklearn.metrics.pairwise import cosine_similarity\n", "from scipy.stats import pearsonr, spearmanr\n", "\n", "# ─── CONFIG ───────────────────────────────────────────────────────────────────\n", "MODEL_NAME = \"intfloat/multilingual-e5-base\"\n", "CSV_PATH = \"/home/skiredj.abderrahman/khalil/sbert_training/third_training/benchmark/benchmark15.csv\"\n", "TOP_K = [1, 3, 5]\n", "SCORE_THRESHOLD = 0.5 # adjust to your score scale\n", "PREFIX = \"query: \" # symmetric STS — same prefix for both sides\n", "# ──────────────────────────────────────────────────────────────────────────────\n", "\n", "# 1. Load CSV\n", "print(\"Loading CSV...\")\n", "df = pd.read_csv(CSV_PATH)\n", "if 'lang' in df.columns:\n", " df = df.drop(columns=['lang'])\n", "\n", "print(f\" {len(df)} pairs loaded.\")\n", "print(df.head())\n", "\n", "# 2. Apply E5 prefix\n", "sentences1 = [PREFIX + str(s) for s in df['sentence1'].tolist()]\n", "sentences2 = [PREFIX + str(s) for s in df['sentence2'].tolist()]\n", "\n", "# 3. Encode\n", "print(\"\\nLoading multilingual-e5-base and encoding sentences...\")\n", "model = SentenceTransformer(MODEL_NAME)\n", "emb1 = model.encode(sentences1, batch_size=32, show_progress_bar=True, normalize_embeddings=True, convert_to_numpy=True)\n", "emb2 = model.encode(sentences2, batch_size=32, show_progress_bar=True, normalize_embeddings=True, convert_to_numpy=True)\n", "\n", "# 4. Cosine similarity per pair\n", "# Note: since embeddings are L2-normalized, cosine sim == dot product\n", "print(\"\\nComputing cosine similarities...\")\n", "cos_scores = np.array([\n", " cosine_similarity(emb1[i].reshape(1, -1), emb2[i].reshape(1, -1))[0][0]\n", " for i in range(len(df))\n", "])\n", "\n", "# 5. Pearson & Spearman\n", "gold_scores = df['score'].astype(float).values\n", "pearson_corr, p_val_p = pearsonr(cos_scores, gold_scores)\n", "spearman_corr, p_val_s = spearmanr(cos_scores, gold_scores)\n", "\n", "print(\"\\n─── STS Correlation Results ───────────────────────────────\")\n", "print(f\" Pearson r : {pearson_corr:.4f} (p={p_val_p:.4e})\")\n", "print(f\" Spearman r : {spearman_corr:.4f} (p={p_val_s:.4e})\")\n", "\n", "# 6. Hit Rate & MRR\n", "print(\"\\n─── Hit Rate & MRR ────────────────────────────────────────\")\n", "print(f\" (Relevance threshold on gold score: >= {SCORE_THRESHOLD})\")\n", "\n", "hit_counts = {k: 0 for k in TOP_K}\n", "reciprocal_ranks = []\n", "n_queries = 0\n", "\n", "for i in range(len(df)):\n", " if gold_scores[i] < SCORE_THRESHOLD:\n", " continue\n", "\n", " query_emb = emb1[i].reshape(1, -1)\n", " sims = cosine_similarity(query_emb, emb2)[0]\n", " ranked_idxs = np.argsort(sims)[::-1]\n", "\n", " rank = np.where(ranked_idxs == i)[0][0] + 1 # 1-based\n", "\n", " for k in TOP_K:\n", " if rank <= k:\n", " hit_counts[k] += 1\n", "\n", " reciprocal_ranks.append(1.0 / rank)\n", " n_queries += 1\n", "\n", "if n_queries == 0:\n", " print(\" No relevant pairs found — lower SCORE_THRESHOLD.\")\n", "else:\n", " for k in TOP_K:\n", " hr = hit_counts[k] / n_queries\n", " print(f\" Hit Rate @{k:2d} : {hr:.4f} ({hit_counts[k]}/{n_queries})\")\n", "\n", " mrr = np.mean(reciprocal_ranks)\n", " print(f\" MRR : {mrr:.4f}\")\n", "\n", "# 7. Save results\n", "df['predicted_cos_sim'] = cos_scores\n", "output_path = CSV_PATH.replace('.csv', '_e5_benchmark_results.csv')\n", "df.to_csv(output_path, index=False)\n", "print(f\"\\nResults saved to: {output_path}\")" ] }, { "cell_type": "code", "execution_count": 26, "id": "374c893e-c7ec-4ba8-9c3c-b23c79e56b8c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Successfully converted to /home/skiredj.abderrahman/khalil/sbert_training/third_training/benchmark/ArabicaQA_test.csv\n" ] } ], "source": [ "import json\n", "import csv\n", "\n", "# Load your json file\n", "with open('/home/skiredj.abderrahman/khalil/sbert_training/third_training/benchmark/ArabicaQA_test.json', 'r', encoding='utf-8') as f:\n", " json_data = json.load(f)\n", "\n", "output_file = '/home/skiredj.abderrahman/khalil/sbert_training/third_training/benchmark/ArabicaQA_test.csv'\n", "\n", "with open(output_file, 'w', newline='', encoding='utf-8-sig') as csvfile:\n", " writer = csv.writer(csvfile)\n", " # Writing the header\n", " writer.writerow(['query', 'passage'])\n", "\n", " # Navigating the SQuAD structure\n", " for entry in json_data.get('data', []):\n", " for paragraph in entry.get('paragraphs', []):\n", " # If you want the full context as the passage, use this:\n", " # context = paragraph.get('context', '') \n", " \n", " for qa in paragraph.get('qas', []):\n", " query = qa.get('question')\n", " \n", " # Extracting the first answer text as the passage\n", " answers = qa.get('answers', [])\n", " if answers:\n", " passage = answers[0].get('text')\n", " writer.writerow([query, passage])\n", "\n", "print(f\"Successfully converted to {output_file}\")" ] }, { "cell_type": "code", "execution_count": 28, "id": "1a8404ab-ab43-4123-9fda-7ea7f9bbb987", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | query | \n", "passage | \n", "
|---|---|---|
| 0 | \n", "من الذي يترأس المرصد الحضري لمدينة الرياض؟ | \n", "يترأس مجلس المرصد أمير المنطقة بالإضافة إلى أر... | \n", "
| 1 | \n", "ما هي الإنجازات البارزة التي حققها المرصد الحض... | \n", "في مارس 2017 حصل المرصد الحضري بمدينة الرياض ... | \n", "
| 2 | \n", "ما هو المرصد الحضري لمدينة الرياض؟ | \n", "هو مركز متخصص في وضع نظم مراقبة حضرية لرصد سير... | \n", "
| 3 | \n", "ما هو الهدف من المرصد الحضري لمدينة الرياض؟ | \n", "يهدف إلى فهم الوضع الحالي للمدينة ووضع تصور لل... | \n", "
| 4 | \n", "ما هي مهام المرصد الحضري لمدينة الرياض؟ | \n", "ومراقبة الإنجاز لتحقيق الأهداف واتخاذ القرارات. | \n", "
| ... | \n", "... | \n", "... | \n", "
| 13965 | \n", "ما هو الموقف الفلسفي؟\\n | \n", "هي وجهة نظر تحاول شرح أو تفسير مشكلة معينة في ... | \n", "
| 13966 | \n", "ما هو مصطلح \"نظرية\"؟\\n | \n", "هو بيان للغة الإنجليزية العامية وليس انعكاسًا... | \n", "
| 13967 | \n", "ما هو بنك دبي؟ | \n", "مصرف إسلامي مقره في دبي، الإمارات العربية المتحدة | \n", "
| 13968 | \n", "متي تأسس بنك دبي؟ | \n", "سبتمبر 2002 | \n", "
| 13969 | \n", "متي أستحوز عليه استحوذ عليه بنك الإمارات دبي ا... | \n", "1 ديسمبر 2012 | \n", "
13970 rows × 2 columns
\n", "| \n", " | query | \n", "passage | \n", "
|---|---|---|
| 0 | \n", "من الذي يترأس المرصد الحضري لمدينة الرياض؟ | \n", "يترأس مجلس المرصد أمير المنطقة بالإضافة إلى أربعة عشر عضوا، | \n", "
| 1 | \n", "ما هي الإنجازات البارزة التي حققها المرصد الحضري بمدينة الرياض؟ | \n", "في مارس 2017 حصل المرصد الحضري بمدينة الرياض على الشهادة الذهبية WCCD ISO 37120 من المجلس العالمي لبيانات المدن في كندا (WCCD) | \n", "
| 2 | \n", "ما هو المرصد الحضري لمدينة الرياض؟ | \n", "هو مركز متخصص في وضع نظم مراقبة حضرية لرصد سير عمليات التنمية الحضرية للمدينة، | \n", "
| 3 | \n", "ما هو الهدف من المرصد الحضري لمدينة الرياض؟ | \n", "يهدف إلى فهم الوضع الحالي للمدينة ووضع تصور للاتجاهات المستقبلية، | \n", "
| 4 | \n", "ما هي مهام المرصد الحضري لمدينة الرياض؟ | \n", "ومراقبة الإنجاز لتحقيق الأهداف واتخاذ القرارات. | \n", "
| ... | \n", "... | \n", "... | \n", "
| 13965 | \n", "ما هو الموقف الفلسفي؟\\n | \n", "هي وجهة نظر تحاول شرح أو تفسير مشكلة معينة في الفلسفة. | \n", "
| 13966 | \n", "ما هو مصطلح \"نظرية\"؟\\n | \n", "هو بيان للغة الإنجليزية العامية وليس انعكاسًا لمصطلح نظرية | \n", "
| 13967 | \n", "ما هو بنك دبي؟ | \n", "مصرف إسلامي مقره في دبي، الإمارات العربية المتحدة | \n", "
| 13968 | \n", "متي تأسس بنك دبي؟ | \n", "سبتمبر 2002 | \n", "
| 13969 | \n", "متي أستحوز عليه استحوذ عليه بنك الإمارات دبي الوطني.\\n؟ | \n", "1 ديسمبر 2012 | \n", "
13970 rows × 2 columns
\n", "