dazzleun-7 commited on
Commit
ee537c7
·
verified ·
1 Parent(s): 1f5d6fb

Delete ready.py

Browse files
Files changed (1) hide show
  1. ready.py +0 -143
ready.py DELETED
@@ -1,143 +0,0 @@
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
- # 멜론 데이터 불러오기
122
-
123
- melon_data = pd.read_csv('./melon_data.csv')
124
- melon_emotions = pd.read_csv('./melon_emotions_final.csv')
125
- melon_emotions = pd.merge(melon_emotions, melon_data, left_on='Title', right_on='title', how='inner')
126
- melon_emotions = melon_emotions[['singer', 'Title', 'genre','Emotions']]
127
- melon_emotions = melon_emotions.drop_duplicates(subset='Title', keep='first')
128
- melon_emotions['Emotions'] = melon_emotions['Emotions'].apply(lambda x: ast.literal_eval(x))
129
-
130
- emotions = melon_emotions['Emotions'].to_list()
131
-
132
- #gradio
133
- !pip install --upgrade gradio
134
- import numpy as np
135
- import pandas as pd
136
- import requests
137
- from PIL import Image
138
- import torch
139
- from transformers import AutoProcessor, AutoModelForZeroShotImageClassification, pipeline
140
- import gradio as gr
141
- import openai
142
- from sklearn.metrics.pairwise import cosine_similarity
143
- import ast