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
8e3f3f8e
Commit
8e3f3f8e
authored
2 weeks ago
by
Мазур Грета Евгеньевна
Browse files
Options
Download
Patches
Plain Diff
zero-shot
parent
45698dd3
master
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.ipynb_checkpoints/goll-checkpoint.py
+32
-1
.ipynb_checkpoints/goll-checkpoint.py
goll.py
+32
-1
goll.py
with
64 additions
and
2 deletions
+64
-2
.ipynb_checkpoints/goll-checkpoint.py
+
32
−
1
View file @
8e3f3f8e
print
(
"hello"
)
\ No newline at end of file
from
transformers
import
pipeline
import
pandas
as
pd
from
sklearn.metrics
import
classification_report
# Загрузка данных
data
=
pd
.
read_csv
(
'all_dataset.csv'
)
# Загрузка модели Qwen-0.5B для zero-shot классификации
classifier
=
pipeline
(
"zero-shot-classification"
,
model
=
"Qwen/Qwen2.5-0.5B"
)
# Категории для классификации
candidate_labels
=
[
"safe"
,
"unsafe"
]
# Функция для zero-shot классификации всего датасета
def
zero_shot_classify_dataset
(
dataset
,
classifier
,
candidate_labels
):
predictions
=
[]
for
text
in
dataset
[
'prompt'
]:
result
=
classifier
(
text
,
candidate_labels
)
predicted_label
=
result
[
'labels'
][
0
]
# Выбираем наиболее вероятную категорию
predictions
.
append
(
predicted_label
)
return
predictions
# Применение zero-shot классификации к тестовому набору
test_data
=
data
.
sample
(
frac
=
0.2
,
random_state
=
42
)
# Примерно 20% данных для теста
test_predictions
=
zero_shot_classify_dataset
(
test_data
,
classifier
,
candidate_labels
)
# Добавление предсказаний в датасет
test_data
[
'zero_shot_prediction'
]
=
test_predictions
# Оценка метрик
print
(
"Zero-shot Classification Report:"
)
print
(
classification_report
(
test_data
[
'safety'
],
test_data
[
'zero_shot_prediction'
],
target_names
=
candidate_labels
))
\ No newline at end of file
This diff is collapsed.
Click to expand it.
goll.py
+
32
−
1
View file @
8e3f3f8e
print
(
"hello"
)
\ No newline at end of file
from
transformers
import
pipeline
import
pandas
as
pd
from
sklearn.metrics
import
classification_report
# Загрузка данных
data
=
pd
.
read_csv
(
'all_dataset.csv'
)
# Загрузка модели Qwen-0.5B для zero-shot классификации
classifier
=
pipeline
(
"zero-shot-classification"
,
model
=
"Qwen/Qwen2.5-0.5B"
)
# Категории для классификации
candidate_labels
=
[
"safe"
,
"unsafe"
]
# Функция для zero-shot классификации всего датасета
def
zero_shot_classify_dataset
(
dataset
,
classifier
,
candidate_labels
):
predictions
=
[]
for
text
in
dataset
[
'prompt'
]:
result
=
classifier
(
text
,
candidate_labels
)
predicted_label
=
result
[
'labels'
][
0
]
# Выбираем наиболее вероятную категорию
predictions
.
append
(
predicted_label
)
return
predictions
# Применение zero-shot классификации к тестовому набору
test_data
=
data
.
sample
(
frac
=
0.2
,
random_state
=
42
)
# Примерно 20% данных для теста
test_predictions
=
zero_shot_classify_dataset
(
test_data
,
classifier
,
candidate_labels
)
# Добавление предсказаний в датасет
test_data
[
'zero_shot_prediction'
]
=
test_predictions
# Оценка метрик
print
(
"Zero-shot Classification Report:"
)
print
(
classification_report
(
test_data
[
'safety'
],
test_data
[
'zero_shot_prediction'
],
target_names
=
candidate_labels
))
\ 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