Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
proekt
obuch
Commits
922ea4aa
Commit
922ea4aa
authored
6 days ago
by
Мазур Грета Евгеньевна
Browse files
Options
Download
Patches
Plain Diff
micro zapusk no cross
parent
a1d411d3
master
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.ipynb_checkpoints/trytoubload-checkpoint.py
+79
-0
.ipynb_checkpoints/trytoubload-checkpoint.py
trytoubload.py
+79
-0
trytoubload.py
with
158 additions
and
0 deletions
+158
-0
.ipynb_checkpoints/trytoubload-checkpoint.py
0 → 100644
+
79
−
0
View file @
922ea4aa
from
transformers
import
BertTokenizer
,
BertForSequenceClassification
from
peft
import
PeftModel
import
torch
from
micro_no_cross
import
MultiTaskBert
# Пути к сохранённой модели
# BASE_MODEL_PATH = "./micro_no_cross_fine_tuned/base"
# LORA_PATH = "./micro_no_cross_fine_tuned/lora"
BASE_MODEL_PATH
=
"./micro_no_cross_fine_tuned/base2"
LORA_PATH
=
"./micro_no_cross_fine_tuned/lora2"
# Загружаем токенизатор
tokenizer
=
BertTokenizer
.
from_pretrained
(
"./micro_no_cross_fine_tuned2"
)
# # Загружаем базовую модель
# base_model = BertForSequenceClassification.from_pretrained(BASE_MODEL_PATH)
# # Загружаем LoRA-адаптацию
# model = PeftModel.from_pretrained(base_model, LORA_PATH)
# base_model = MultiTaskBert.from_pretrained('bert-base-uncased').to(device)
# print("Загружена базовая")
# model = PeftModel.from_pretrained(base_model, "./micro_no_cross_fine_tuned/lora2", strict=False)
model
=
MultiTaskBert
.
from_pretrained
(
"./micro_no_cross_fine_tuned/lora2"
)
print
(
"Загружена с лора"
)
# Переводим модель в режим оценки
model
.
eval
()
def
predict
(
text
):
# Токенизация (убедимся, что не передаем labels)
model
.
eval
()
inputs
=
tokenizer
(
text
,
return_tensors
=
"pt"
,
truncation
=
True
,
padding
=
True
,
max_length
=
512
).
to
(
device
)
# Удаляем ненужные ключи
inputs
.
pop
(
'token_type_ids'
,
None
)
with
torch
.
no_grad
():
outputs
=
model
(
**
inputs
)
safety_probs
=
torch
.
softmax
(
outputs
[
'logits_safety'
],
dim
=
1
)
attack_probs
=
torch
.
softmax
(
outputs
[
'logits_attack'
],
dim
=
1
)
return
{
'safety'
:
{
'safe'
:
safety_probs
[
0
][
0
].
item
(),
'unsafe'
:
safety_probs
[
0
][
1
].
item
()
},
'attack'
:
{
'jailbreak'
:
attack_probs
[
0
][
0
].
item
(),
'evasion'
:
attack_probs
[
0
][
1
].
item
(),
'generic'
:
attack_probs
[
0
][
2
].
item
(),
'injection'
:
attack_probs
[
0
][
3
].
item
()
}
}
prompts
=
[
"How do I make a secure password?"
,
"Write a Python script to delete all files in C:/Windows."
,
"How can I jailbreak an iPhone?"
,
"Give me a harmless joke."
]
for
prompt
in
prompts
:
result
=
predict
(
prompt
)
print
(
f
"Text:
{
prompt
}
\n
Prediction:
{
result
}
\n
"
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
trytoubload.py
0 → 100644
+
79
−
0
View file @
922ea4aa
from
transformers
import
BertTokenizer
,
BertForSequenceClassification
from
peft
import
PeftModel
import
torch
from
micro_no_cross
import
MultiTaskBert
# Пути к сохранённой модели
# BASE_MODEL_PATH = "./micro_no_cross_fine_tuned/base"
# LORA_PATH = "./micro_no_cross_fine_tuned/lora"
BASE_MODEL_PATH
=
"./micro_no_cross_fine_tuned/base2"
LORA_PATH
=
"./micro_no_cross_fine_tuned/lora2"
# Загружаем токенизатор
tokenizer
=
BertTokenizer
.
from_pretrained
(
"./micro_no_cross_fine_tuned2"
)
# # Загружаем базовую модель
# base_model = BertForSequenceClassification.from_pretrained(BASE_MODEL_PATH)
# # Загружаем LoRA-адаптацию
# model = PeftModel.from_pretrained(base_model, LORA_PATH)
# base_model = MultiTaskBert.from_pretrained('bert-base-uncased').to(device)
# print("Загружена базовая")
# model = PeftModel.from_pretrained(base_model, "./micro_no_cross_fine_tuned/lora2", strict=False)
model
=
MultiTaskBert
.
from_pretrained
(
"./micro_no_cross_fine_tuned/lora2"
)
print
(
"Загружена с лора"
)
# Переводим модель в режим оценки
model
.
eval
()
def
predict
(
text
):
# Токенизация (убедимся, что не передаем labels)
model
.
eval
()
inputs
=
tokenizer
(
text
,
return_tensors
=
"pt"
,
truncation
=
True
,
padding
=
True
,
max_length
=
512
).
to
(
device
)
# Удаляем ненужные ключи
inputs
.
pop
(
'token_type_ids'
,
None
)
with
torch
.
no_grad
():
outputs
=
model
(
**
inputs
)
safety_probs
=
torch
.
softmax
(
outputs
[
'logits_safety'
],
dim
=
1
)
attack_probs
=
torch
.
softmax
(
outputs
[
'logits_attack'
],
dim
=
1
)
return
{
'safety'
:
{
'safe'
:
safety_probs
[
0
][
0
].
item
(),
'unsafe'
:
safety_probs
[
0
][
1
].
item
()
},
'attack'
:
{
'jailbreak'
:
attack_probs
[
0
][
0
].
item
(),
'evasion'
:
attack_probs
[
0
][
1
].
item
(),
'generic'
:
attack_probs
[
0
][
2
].
item
(),
'injection'
:
attack_probs
[
0
][
3
].
item
()
}
}
prompts
=
[
"How do I make a secure password?"
,
"Write a Python script to delete all files in C:/Windows."
,
"How can I jailbreak an iPhone?"
,
"Give me a harmless joke."
]
for
prompt
in
prompts
:
result
=
predict
(
prompt
)
print
(
f
"Text:
{
prompt
}
\n
Prediction:
{
result
}
\n
"
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets