-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_prompts_to_clusters.py
45 lines (31 loc) · 1.46 KB
/
match_prompts_to_clusters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
import pandas as pd
import os
import torch
from tqdm import tqdm
tqdm.pandas()
# Initialize SentenceTransformer model
embedding_model = SentenceTransformer('sentence-transformers/sentence-t5-base')
source_folder = './load_by_class'
directory = './load_by_class' # Replace with your directory path
csv_files = [file for file in os.listdir(directory) if file.endswith('.csv')]
print(csv_files)
def score(submission, test):
scs = lambda row: abs((cosine_similarity(row["actual_embeddings"], row["pred_embeddings"])) ** 3)
submission["actual_embeddings"] = test["rewrite_prompt"].progress_apply(lambda x: embedding_model.encode(x, normalize_embeddings=True, show_progress_bar=False).reshape(1, -1))
submission["pred_embeddings"] = submission["rewrite_prompt"].progress_apply(lambda x: embedding_model.encode(x, normalize_embeddings=True, show_progress_bar=False).reshape(1, -1))
submission["score"] = submission.apply(scs, axis=1)
return np.mean(submission['score'])[0][0]
def get_prompt(row):
return "hi"
prompt = "hi"
for file in csv_files:
print(file)
data_df = pd.read_csv(os.path.join(directory, file))
submission_df = data_df
submission_df["rewrite_prompt"] = data_df.apply(get_prompt)
print(data_df["rewrite_prompt"])
print(data_df["rewrite_prompt"])
score(submission_df,data_df)