Create code/peft/peft_example.py
Browse files- code/peft/peft_example.py +27 -0
code/peft/peft_example.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForQuestionAnswering, pipeline
|
2 |
+
from peft import PeftModel, PeftConfig
|
3 |
+
|
4 |
+
config = PeftConfig.from_pretrained("MohamedShakhsak/bert-qa-squad2_V1")
|
5 |
+
base_model = AutoModelForQuestionAnswering.from_pretrained(config.base_model_name_or_path)
|
6 |
+
model = PeftModel.from_pretrained(base_model, "MohamedShakhsak/bert-qa-squad2_V1")
|
7 |
+
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
9 |
+
qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer)
|
10 |
+
|
11 |
+
|
12 |
+
qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer)
|
13 |
+
|
14 |
+
examples = [
|
15 |
+
{
|
16 |
+
"question": "What is the capital of France?",
|
17 |
+
"context": "Paris is the capital and most populous city of France."
|
18 |
+
},
|
19 |
+
{
|
20 |
+
"question": "When was the iPhone first released?",
|
21 |
+
"context": "The first iPhone was released by Apple Inc. on June 29, 2007."
|
22 |
+
}
|
23 |
+
]
|
24 |
+
|
25 |
+
for example in examples:
|
26 |
+
answer = qa_pipeline(example)
|
27 |
+
print(f"Q: {example['question']}\nA: {answer}\n")
|