Repository navigation
Expand file tree
/
Copy pathindex-ckiplab.py
More file actions
68 lines (43 loc) · 1.84 KB
/
Copy pathindex-ckiplab.py
File metadata and controls
68 lines (43 loc) · 1.84 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from transformers import BertTokenizer, BertModel
import torch
import torch.nn.functional as F
# 選擇使用的模型
model_name = 'ckiplab/bert-base-chinese'
# 載入 tokenizer 和 model
tokenizer = BertTokenizer.from_pretrained(model_name)
model = BertModel.from_pretrained(model_name)
def get_embeddings(text):
inputs = tokenizer(text, max_length=512, return_tensors='pt')
with torch.no_grad():
outputs = model(**inputs)
embeddings = outputs.last_hidden_state
first_token_embedding = embeddings[0][0]
normalized_first_token_embedding = F.normalize(first_token_embedding, p=2, dim=0)
return normalized_first_token_embedding.tolist()
# ----
from datasets import load_dataset
import requests
import json
from supabase import create_client, Client
import time
import os
print(model_name)
dataset = load_dataset("MediaTek-Research/TCEval-v2", "drcd")
supabase_url = 'https://imcpayinnpcetclzvdfu.supabase.co'
supabase_api_key = ''
supabase: Client = create_client(supabase_url, supabase_api_key)
supabase.table('questions').delete().eq('model', model_name).execute()
supabase.table('paragraphs').delete().eq('model', model_name).execute()
# -----
current_paragraph = ''
current_paragraph_id = 0
for i in range(0, 3493):
print(i)
data = dataset["test"][i]
if current_paragraph != data["paragraph"]:
embedding = get_embeddings(data["paragraph"])
response = supabase.table('paragraphs').upsert({"content": data["paragraph"], "embedding": embedding, "model": model_name }).execute()
current_paragraph = data["paragraph"]
current_paragraph_id = response.data[0]["id"]
q_embedding = get_embeddings(data["question"])
supabase.table('questions').upsert({"dataset_id": i, "content": data["question"], "embedding": q_embedding, "model": model_name, "paragraph_id": current_paragraph_id }).execute()