Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,103 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π§ IPAD β Inverse Prompt for AI Detection
|
| 2 |
+
|
| 3 |
+
> **Fine-tuned Phi-3-Medium-128k-Instruct with LoRA using [LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory)**
|
| 4 |
+
> Author: [@bellafc](https://huggingface.co/bellafc)
|
| 5 |
+
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
## π Overview
|
| 9 |
+
|
| 10 |
+
Large Language Models (LLMs) have achieved human-level fluency in text generation, making it increasingly difficult to distinguish between human- and AI-authored content.
|
| 11 |
+
**IPAD (Inverse Prompt for AI Detection)** introduces a two-stage detection framework:
|
| 12 |
+
|
| 13 |
+
1. **Prompt Inverter** β predicts the underlying prompts that could have generated an input text.
|
| 14 |
+
2. **Distinguisher** β evaluates the alignment between the text and its predicted prompts to determine whether it was AI-generated.
|
| 15 |
+
|
| 16 |
+
All **Prompt Inverter**, **Distinguisher (RC)** and **Distinguisher (PTCV)** are **LoRA-fine-tuned versions** of
|
| 17 |
+
[`microsoft/Phi-3-medium-128k-instruct`](https://huggingface.co/microsoft/Phi-3-medium-128k-instruct),
|
| 18 |
+
trained using [LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory) for **robust AI text detection** under diverse and adversarial conditions.
|
| 19 |
+
|
| 20 |
+
- π§© **Distinguisher (RC)** β optimized for regular, unstructured text inputs (baseline detection).
|
| 21 |
+
- π¬ **Distinguisher (PTCV)** β specialized for *structured, compositional, or OOD* data, exhibiting enhanced robustness.
|
| 22 |
+
|
| 23 |
+
---
|
| 24 |
+
|
| 25 |
+
## βοΈ Model Details
|
| 26 |
+
|
| 27 |
+
| Property | Description |
|
| 28 |
+
|-----------|-------------|
|
| 29 |
+
| **Base model** | [`microsoft/Phi-3-medium-128k-instruct`](https://huggingface.co/microsoft/Phi-3-medium-128k-instruct) |
|
| 30 |
+
| **Architecture** | Decoder-only Transformer |
|
| 31 |
+
| **Fine-tuning** | LoRA (rank-8, Ξ±=16, dropout=0.05) |
|
| 32 |
+
| **Context length** | 128k tokens |
|
| 33 |
+
| **Framework** | [LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory) |
|
| 34 |
+
| **Task** | AI Text Detection (Discriminator) |
|
| 35 |
+
| **Language** | English |
|
| 36 |
+
| **License** | Apache 2.0 |
|
| 37 |
+
| **Author** | [@bellafc](https://huggingface.co/bellafc) |
|
| 38 |
+
|
| 39 |
+
---
|
| 40 |
+
|
| 41 |
+
## π Quick Usage
|
| 42 |
+
|
| 43 |
+
### π§© Prompt Inverter
|
| 44 |
+
|
| 45 |
+
```python
|
| 46 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 47 |
+
from peft import PeftModel
|
| 48 |
+
|
| 49 |
+
base_model = "microsoft/Phi-3-medium-128k-instruct"
|
| 50 |
+
lora_model = "bellafc/IPAD/Distinguisher_PTCV" # or Distinguisher_RC
|
| 51 |
+
|
| 52 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model)
|
| 53 |
+
model = AutoModelForCausalLM.from_pretrained(base_model, torch_dtype="auto", device_map="auto")
|
| 54 |
+
model = PeftModel.from_pretrained(model, lora_model)
|
| 55 |
+
|
| 56 |
+
# For RC, text should in this format: "Can LLM generate the input text {text to-be detected} through the prompt {prompt generated by Prompt Inverter (PI)}?"
|
| 57 |
+
# For PTCV, text should in this format: "Text2 is generated by LLM, determine whether text1 is also generated by LLM with a similar prompt. Text1: {text to-be detected}. Text2: {Regenerated text}"
|
| 58 |
+
|
| 59 |
+
text = "Text2 is generated by LLM, determine whether text1 is also generated by LLM with a similar prompt. Text1: ... . Text2: ... ."
|
| 60 |
+
gen = model.generate(
|
| 61 |
+
**inputs,
|
| 62 |
+
max_new_tokens=10,
|
| 63 |
+
output_scores=True,
|
| 64 |
+
return_dict_in_generate=True
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
generated_text = tokenizer.decode(gen.sequences[0], skip_special_tokens=True)
|
| 68 |
+
probs = softmax(gen.scores[0], dim=-1)
|
| 69 |
+
yes_token_id = tokenizer(" yes", add_special_tokens=False).input_ids[0]
|
| 70 |
+
print("Generated:", generated_text)
|
| 71 |
+
print(f"P('yes') = {probs[0, yes_token_id].item():.4f}")
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
### π§© Distinguishers
|
| 75 |
+
|
| 76 |
+
```python
|
| 77 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 78 |
+
from peft import PeftModel
|
| 79 |
+
|
| 80 |
+
base_model = "microsoft/Phi-3-medium-128k-instruct"
|
| 81 |
+
lora_model = "bellafc/IPAD/Distinguisher_PTCV" # or Distinguisher_RC
|
| 82 |
+
|
| 83 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model)
|
| 84 |
+
model = AutoModelForCausalLM.from_pretrained(base_model, torch_dtype="auto", device_map="auto")
|
| 85 |
+
model = PeftModel.from_pretrained(model, lora_model)
|
| 86 |
+
|
| 87 |
+
# For RC, text should in this format: "Can LLM generate the input text {text to-be detected} through the prompt {prompt generated by Prompt Inverter (PI)}?"
|
| 88 |
+
# For PTCV, text should in this format: "Text2 is generated by LLM, determine whether text1 is also generated by LLM with a similar prompt. Text1: {text to-be detected}. Text2: {Regenerated text}"
|
| 89 |
+
|
| 90 |
+
text = "Text2 is generated by LLM, determine whether text1 is also generated by LLM with a similar prompt. Text1: ... . Text2: ... ."
|
| 91 |
+
gen = model.generate(
|
| 92 |
+
**inputs,
|
| 93 |
+
max_new_tokens=10,
|
| 94 |
+
output_scores=True,
|
| 95 |
+
return_dict_in_generate=True
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
generated_text = tokenizer.decode(gen.sequences[0], skip_special_tokens=True)
|
| 99 |
+
probs = softmax(gen.scores[0], dim=-1)
|
| 100 |
+
yes_token_id = tokenizer(" yes", add_special_tokens=False).input_ids[0]
|
| 101 |
+
print("Generated:", generated_text)
|
| 102 |
+
print(f"P('yes') = {probs[0, yes_token_id].item():.4f}")
|
| 103 |
+
```
|