Upload folder using huggingface_hub
Browse files- .ipynb_checkpoints/README-checkpoint.md +0 -0
- README.md +146 -0
.ipynb_checkpoints/README-checkpoint.md
ADDED
|
File without changes
|
README.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- fa
|
| 5 |
+
- en
|
| 6 |
+
library_name: transformers
|
| 7 |
+
tags:
|
| 8 |
+
- llama
|
| 9 |
+
- persian
|
| 10 |
+
- farsi
|
| 11 |
+
- question-answering
|
| 12 |
+
- scientific-qa
|
| 13 |
+
- text-generation
|
| 14 |
+
- instruction-following
|
| 15 |
+
- LoRA
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
# PersianSciQA-LLaMA-13B
|
| 19 |
+
## A Context-Adherent Question Answering Model for Persian Scientific Texts
|
| 20 |
+
|
| 21 |
+
This model is a fine-tuned version of `ViraIntelligentDataMining/PersianLLaMA-13B`, specifically re-aligned to perform reliable, context-bound Question Answering on Persian scientific documents. Its key feature is its ability to **mitigate hallucination** by refusing to answer when the context does not contain the required information. This work was developed by Safora Jolfaei.
|
| 22 |
+
|
| 23 |
+
این مدل یک نسخه فاین-تیون شده از `ViraIntelligentDataMining/PersianLLaMA-13B` است که به طور ویژه برای پاسخگویی به سوالات بر اساس متن (Question Answering) در حوزه متون علمی فارسی تنظیم شده است. ویژگی اصلی این مدل، **کاهش توهم (Hallucination)** از طریق خودداری از پاسخگویی در مواقعی است که اطلاعات لازم در متن زمینه وجود ندارد. این مدل توسط صفورا جلفائی توسعه داده شده است.
|
| 24 |
+
|
| 25 |
+
## Model Description
|
| 26 |
+
|
| 27 |
+
This model is the result of a two-stage fine-tuning process designed to correct "task-model misalignment."
|
| 28 |
+
|
| 29 |
+
1. **Initial Fine-tuning (`safora/PersianSciQA-LoRA`)**: The base model was first fine-tuned to identify salient information in scientific abstracts, creating an effective "relevance detector" (`safora/PersianSciQA-LoRA`). However, this initial model suffered from hallucination in a RAG pipeline.
|
| 30 |
+
2. **Corrective Fine-tuning (This Model)**: Using a "Teacher/Editor" methodology, the `safora/PersianSciQA-LoRA` adapter was further fine-tuned on a new, high-quality, context-bound instruction dataset. This process "edited" the model's behavior, explicitly teaching it to adhere strictly to the provided context and to output `CANNOT_ANSWER` when the information is absent.
|
| 31 |
+
|
| 32 |
+
This process makes the model a reliable tool for applications requiring high-fidelity, grounded generation.
|
| 33 |
+
|
| 34 |
+
## Intended Use & How to Use
|
| 35 |
+
|
| 36 |
+
This model is intended for generative question answering where the answer must be derived solely from a given context. It follows a specific instruction format.
|
| 37 |
+
|
| 38 |
+
**Installation:**
|
| 39 |
+
```bash
|
| 40 |
+
pip install transformers torch sentencepiece accelerate
|
| 41 |
+
|
| 42 |
+
Usage Example:
|
| 43 |
+
|
| 44 |
+
import torch
|
| 45 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 46 |
+
|
| 47 |
+
model_id = "safora/PersianSciQA-LLaMA-13B"
|
| 48 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 49 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 50 |
+
model_id,
|
| 51 |
+
torch_dtype=torch.bfloat16,
|
| 52 |
+
device_map="auto"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
def get_response(context, question):
|
| 56 |
+
prompt = f"""<s>[INST] با توجه به متن زیر:
|
| 57 |
+
{context}
|
| 58 |
+
|
| 59 |
+
به این سوال پاسخ بده:
|
| 60 |
+
{question} [/INST]"""
|
| 61 |
+
|
| 62 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 63 |
+
outputs = model.generate(**inputs, max_new_tokens=256, pad_token_id=tokenizer.eos_token_id)
|
| 64 |
+
|
| 65 |
+
response = tokenizer.decode(outputs[0, inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 66 |
+
return response
|
| 67 |
+
|
| 68 |
+
# --- Example 1: Answer is in the context ---
|
| 69 |
+
context1 = "در این پژوهش، یک الگوریتم جدید برای بهینهسازی مصرف انرژی در شبکههای حسگر بیسیم ارائه شده است که منجر به افزایش ۳۰ درصدی طول عمر شبکه میشود."
|
| 70 |
+
question1 = "الگوریتم جدید چه تاثیری بر شبکه دارد؟"
|
| 71 |
+
|
| 72 |
+
print(f"Question 1: {question1}")
|
| 73 |
+
print(f"Answer 1: {get_response(context1, question1)}\n")
|
| 74 |
+
# Expected Output: این الگوریتم باعث افزایش ۳۰ درصدی طول عمر شبکه میشود.
|
| 75 |
+
|
| 76 |
+
# --- Example 2: Answer is NOT in the context ---
|
| 77 |
+
context2 = "این مقاله به بررسی تاثیر ورزش بر سلامت روان در نوجوانان میپردازد."
|
| 78 |
+
question2 = "هزینه انجام این تحقیق چقدر بوده است؟"
|
| 79 |
+
|
| 80 |
+
print(f"Question 2: {question2}")
|
| 81 |
+
print(f"Answer 2: {get_response(context2, question2)}")
|
| 82 |
+
# Expected Output: CANNOT_ANSWER
|
| 83 |
+
|
| 84 |
+
Limitations and Bias
|
| 85 |
+
The primary feature of this model is its designed limitation:
|
| 86 |
+
|
| 87 |
+
Refusal to Answer: The model was explicitly trained to output the literal string CANNOT_ANSWER when the provided context does not contain the necessary information to answer a question. This is not an error, but the intended behavior to prevent factual invention.
|
| 88 |
+
|
| 89 |
+
Domain Specificity: The model's expertise is in the domain of scientific and academic Persian text. Its performance on other domains (e.g., conversational or literary text) may be suboptimal.
|
| 90 |
+
|
| 91 |
+
Bias: As the model is based on PersianLLaMA-13B, it may inherit any biases present in the original pre-training data.
|
| 92 |
+
|
| 93 |
+
Fine-tuning Details
|
| 94 |
+
The corrective fine-tuning was performed using LoRA on a single NVIDIA A100 GPU.
|
| 95 |
+
|
| 96 |
+
Base Model: ViraIntelligentDataMining/PersianLLaMA-13B merged with the initial adapter safora/PersianSciQA-LoRA.
|
| 97 |
+
|
| 98 |
+
Dataset: safora/PersianSciQA-Extractive (8,232 instruction-answer pairs).
|
| 99 |
+
|
| 100 |
+
LoRA Rank (r): 16
|
| 101 |
+
|
| 102 |
+
LoRA Alpha (alpha): 32
|
| 103 |
+
|
| 104 |
+
Learning Rate: 5e-6 with a cosine scheduler
|
| 105 |
+
|
| 106 |
+
Epochs: 3
|
| 107 |
+
|
| 108 |
+
Effective Batch Size: 8
|
| 109 |
+
|
| 110 |
+
Precision: bfloat16
|
| 111 |
+
|
| 112 |
+
Citation
|
| 113 |
+
If you use this model or dataset in your research, please consider citing the following works.
|
| 114 |
+
|
| 115 |
+
This Model:
|
| 116 |
+
|
| 117 |
+
@misc{jolfaei2024persiansciqa,
|
| 118 |
+
author = {Jolfaei, Safora},
|
| 119 |
+
title = {PersianSciQA-LLaMA-13B: A Context-Adherent QA Model for Persian Scientific Texts},
|
| 120 |
+
year = {2024},
|
| 121 |
+
publisher = {Hugging Face},
|
| 122 |
+
journal = {Hugging Face repository},
|
| 123 |
+
howpublished = {\url{[https://huggingface.co/safora/PersianSciQA-LLaMA-13B](https://huggingface.co/safora/PersianSciQA-LLaMA-13B)}}
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
Dataset:
|
| 127 |
+
|
| 128 |
+
@misc{jolfaei2024persiansciqa_extractive,
|
| 129 |
+
author = {Jolfaei, Safora},
|
| 130 |
+
title = {PersianSciQA-Extractive: A Context-Bound Instruction Dataset for Persian Scientific QA},
|
| 131 |
+
year = {2024},
|
| 132 |
+
publisher = {Hugging Face},
|
| 133 |
+
journal = {Hugging Face repository},
|
| 134 |
+
howpublished = {\url{[https://huggingface.co/datasets/safora/PersianSciQA-Extractive](https://huggingface.co/datasets/safora/PersianSciQA-Extractive)}}
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
Base Model:
|
| 138 |
+
|
| 139 |
+
@misc{persianllama,
|
| 140 |
+
author = {Vira Intelligent Data Mining},
|
| 141 |
+
title = {PersianLLaMA-13B},
|
| 142 |
+
year = {2023},
|
| 143 |
+
publisher = {Hugging Face},
|
| 144 |
+
journal = {Hugging Face repository},
|
| 145 |
+
howpublished = {\url{[https://huggingface.co/ViraIntelligentDataMining/PersianLLaMA-13B](https://huggingface.co/ViraIntelligentDataMining/PersianLLaMA-13B)}}
|
| 146 |
+
}
|