Spaces:
Sleeping
Sleeping
Create ready.py
Browse files
ready.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#ํ์ ํจํค์ง ์ค์น
|
2 |
+
!pip install mxnet
|
3 |
+
!pip install gluonnlp==0.8.0
|
4 |
+
!pip install tqdm pandas
|
5 |
+
!pip install sentencepiece
|
6 |
+
!pip install transformers
|
7 |
+
!pip install torch
|
8 |
+
!pip install numpy==1.23.1
|
9 |
+
|
10 |
+
#KoBERT ๊นํ๋ธ์์ ๋ถ๋ฌ์ค๊ธฐ
|
11 |
+
!pip install 'git+https://github.com/SKTBrain/KoBERT.git#egg=kobert_tokenizer&subdirectory=kobert_hf'
|
12 |
+
|
13 |
+
!pip install langchain==0.0.125 chromadb==0.3.14 pypdf==3.7.0 tiktoken==0.3.3
|
14 |
+
!pip install openai==0.28
|
15 |
+
!pip install gradio transformers torch opencv-python-headless
|
16 |
+
|
17 |
+
# import torch
|
18 |
+
from torch import nn
|
19 |
+
import torch.nn.functional as F
|
20 |
+
import torch.optim as optim
|
21 |
+
from torch.utils.data import Dataset, DataLoader
|
22 |
+
import gluonnlp as nlp
|
23 |
+
import numpy as np
|
24 |
+
from tqdm import tqdm, tqdm_notebook
|
25 |
+
import pandas as pd
|
26 |
+
|
27 |
+
# Hugging Face๋ฅผ ํตํ ๋ชจ๋ธ ๋ฐ ํ ํฌ๋์ด์ Import
|
28 |
+
from kobert_tokenizer import KoBERTTokenizer
|
29 |
+
from transformers import BertModel
|
30 |
+
|
31 |
+
from transformers import AdamW
|
32 |
+
from transformers.optimization import get_cosine_schedule_with_warmup
|
33 |
+
|
34 |
+
n_devices = torch.cuda.device_count()
|
35 |
+
print(n_devices)
|
36 |
+
|
37 |
+
for i in range(n_devices):
|
38 |
+
print(torch.cuda.get_device_name(i))
|
39 |
+
|
40 |
+
if torch.cuda.is_available():
|
41 |
+
device = torch.device("cuda")
|
42 |
+
print('There are %d GPU(s) available.' % torch.cuda.device_count())
|
43 |
+
print('We will use the GPU:', torch.cuda.get_device_name(0))
|
44 |
+
else:
|
45 |
+
device = torch.device("cpu")
|
46 |
+
print('No GPU available, using the CPU instead.')
|
47 |
+
|
48 |
+
|
49 |
+
# Kobert_softmax
|
50 |
+
class BERTClassifier(nn.Module):
|
51 |
+
def __init__(self,
|
52 |
+
bert,
|
53 |
+
hidden_size=768,
|
54 |
+
num_classes=6,
|
55 |
+
dr_rate=None,
|
56 |
+
params=None):
|
57 |
+
super(BERTClassifier, self).__init__()
|
58 |
+
self.bert = bert
|
59 |
+
self.dr_rate = dr_rate
|
60 |
+
self.softmax = nn.Softmax(dim=1) # Softmax๋ก ๋ณ๊ฒฝ
|
61 |
+
self.classifier = nn.Sequential(
|
62 |
+
nn.Dropout(p=0.5),
|
63 |
+
nn.Linear(in_features=hidden_size, out_features=512),
|
64 |
+
nn.Linear(in_features=512, out_features=num_classes),
|
65 |
+
)
|
66 |
+
|
67 |
+
# ์ ๊ทํ ๋ ์ด์ด ์ถ๊ฐ (Layer Normalization)
|
68 |
+
self.layer_norm = nn.LayerNorm(768)
|
69 |
+
|
70 |
+
# ๋๋กญ์์
|
71 |
+
self.dropout = nn.Dropout(p=dr_rate)
|
72 |
+
|
73 |
+
def gen_attention_mask(self, token_ids, valid_length):
|
74 |
+
attention_mask = torch.zeros_like(token_ids)
|
75 |
+
for i, v in enumerate(valid_length):
|
76 |
+
attention_mask[i][:v] = 1
|
77 |
+
return attention_mask.float()
|
78 |
+
|
79 |
+
def forward(self, token_ids, valid_length, segment_ids):
|
80 |
+
attention_mask = self.gen_attention_mask(token_ids, valid_length)
|
81 |
+
_, pooler = self.bert(input_ids=token_ids, token_type_ids=segment_ids.long(), attention_mask=attention_mask.float().to(token_ids.device))
|
82 |
+
|
83 |
+
pooled_output = self.dropout(pooler)
|
84 |
+
normalized_output = self.layer_norm(pooled_output)
|
85 |
+
out = self.classifier(normalized_output)
|
86 |
+
|
87 |
+
# LayerNorm ์ ์ฉ
|
88 |
+
pooler = self.layer_norm(pooler)
|
89 |
+
|
90 |
+
if self.dr_rate:
|
91 |
+
pooler = self.dropout(pooler)
|
92 |
+
|
93 |
+
logits = self.classifier(pooler) # ๋ถ๋ฅ๋ฅผ ์ํ ๋ก์ง ๊ฐ ๊ณ์ฐ
|
94 |
+
probabilities = self.softmax(logits) # Softmax๋ก ๊ฐ ํด๋์ค์ ํ๋ฅ ๊ณ์ฐ
|
95 |
+
return probabilities # ๊ฐ ํด๋์ค์ ๋ํ ํ๋ฅ ๋ฐํ
|
96 |
+
|
97 |
+
#์ ์ํ ๋ชจ๋ธ ๋ถ๋ฌ์ค๊ธฐ
|
98 |
+
model = BERTClassifier(bertmodel,dr_rate=0.4).to(device)
|
99 |
+
#model = BERTClassifier(bertmodel, dr_rate=0.5).to('cpu')
|
100 |
+
|
101 |
+
# Prepare optimizer and schedule (linear warmup and decay)
|
102 |
+
no_decay = ['bias', 'LayerNorm.weight']
|
103 |
+
optimizer_grouped_parameters = [
|
104 |
+
{'params': [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01},
|
105 |
+
{'params': [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
|
106 |
+
]
|
107 |
+
optimizer = AdamW(optimizer_grouped_parameters, lr=learning_rate)
|
108 |
+
loss_fn = nn.CrossEntropyLoss()
|
109 |
+
t_total = len(train_dataloader) * num_epochs
|
110 |
+
warmup_step = int(t_total * warmup_ratio)
|
111 |
+
scheduler = get_cosine_schedule_with_warmup(optimizer, num_warmup_steps=warmup_step, num_training_steps=t_total)
|
112 |
+
def calc_accuracy(X,Y):
|
113 |
+
max_vals, max_indices = torch.max(X, 1)
|
114 |
+
train_acc = (max_indices == Y).sum().data.cpu().numpy()/max_indices.size()[0]
|
115 |
+
return train_acc
|
116 |
+
train_dataloader
|
117 |
+
|
118 |
+
model = torch.load('./model_weights_softmax(model).pth')
|
119 |
+
model.eval()
|
120 |
+
|
121 |
+
#gradio
|
122 |
+
!pip install --upgrade gradio
|
123 |
+
import numpy as np
|
124 |
+
import pandas as pd
|
125 |
+
import requests
|
126 |
+
from PIL import Image
|
127 |
+
import torch
|
128 |
+
from transformers import AutoProcessor, AutoModelForZeroShotImageClassification, pipeline
|
129 |
+
import gradio as gr
|
130 |
+
import openai
|
131 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
132 |
+
import ast
|