Spaces:
Build error
Build error
Upload 2 files
Browse files- database.py +175 -0
- t5.py +13 -0
database.py
ADDED
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datasets import load_dataset
|
2 |
+
import pandas as pd
|
3 |
+
import torch
|
4 |
+
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
|
5 |
+
from transformers import BertTokenizer, BertForQuestionAnswering, BertConfig,AutoModelForCausalLM
|
6 |
+
from pymongo import MongoClient
|
7 |
+
import torchtext
|
8 |
+
torchtext.disable_torchtext_deprecation_warning()
|
9 |
+
from torchtext.data import get_tokenizer
|
10 |
+
from yeni_tokenize import TokenizerProcessor
|
11 |
+
|
12 |
+
|
13 |
+
class Database:
|
14 |
+
|
15 |
+
# MongoDB connection settings
|
16 |
+
|
17 |
+
def get_mongodb(database_name='yeniDatabase', collection_name='test', host='localhost', port=27017):
|
18 |
+
"""
|
19 |
+
MongoDB connection and collection selection
|
20 |
+
"""
|
21 |
+
client = MongoClient(f'mongodb://{host}:{port}/')
|
22 |
+
db = client[database_name]
|
23 |
+
collection = db[collection_name]
|
24 |
+
return collection
|
25 |
+
|
26 |
+
@staticmethod
|
27 |
+
def get_mongodb():
|
28 |
+
# MongoDB bağlantı bilgilerini döndürecek şekilde tanımlanmalıdır.
|
29 |
+
return 'mongodb://localhost:27017/', 'yeniDatabase', 'train'
|
30 |
+
|
31 |
+
@staticmethod
|
32 |
+
def get_input_texts():
|
33 |
+
# MongoDB bağlantı bilgilerini alma
|
34 |
+
mongo_url, db_name, collection_name = Database.get_mongodb()
|
35 |
+
# MongoDB'ye bağlanma
|
36 |
+
client = MongoClient(mongo_url)
|
37 |
+
db = client[db_name]
|
38 |
+
collection = db[collection_name]
|
39 |
+
# Sorguyu tanımlama
|
40 |
+
query = {"Prompt": {"$exists": True}}
|
41 |
+
# Sorguyu çalıştırma ve dökümanları çekme
|
42 |
+
cursor = collection.find(query, {"Prompt": 1, "_id": 0})
|
43 |
+
# Cursor'ı döküman listesine dönüştürme
|
44 |
+
input_texts_from_db = [doc['Prompt'] for doc in cursor]
|
45 |
+
# Input text'leri döndürme
|
46 |
+
# Düz metin listesine dönüştürme
|
47 |
+
return input_texts_from_db
|
48 |
+
input_text= get_input_texts()
|
49 |
+
print("metinler yazılıyor:")
|
50 |
+
for text in input_text:
|
51 |
+
print(text)
|
52 |
+
|
53 |
+
|
54 |
+
@staticmethod
|
55 |
+
def get_output_texts():
|
56 |
+
# MongoDB bağlantı bilgilerini alma
|
57 |
+
mongo_url, db_name, collection_name = Database.get_mongodb()
|
58 |
+
# MongoDB'ye bağlanma
|
59 |
+
client = MongoClient(mongo_url)
|
60 |
+
db = client[db_name]
|
61 |
+
collection = db[collection_name]
|
62 |
+
# Sorguyu tanımlama
|
63 |
+
query = {"Response": {"$exists": True}}
|
64 |
+
# Sorguyu çalıştırma ve dökümanları çekme
|
65 |
+
cursor = collection.find(query, {"Response": 1, "_id": 0})
|
66 |
+
# Cursor'ı döküman listesine dönüştürme
|
67 |
+
output_texts_from_db = [doc['Response'] for doc in cursor]
|
68 |
+
#output metin listesine çevirme
|
69 |
+
return output_texts_from_db
|
70 |
+
|
71 |
+
@staticmethod
|
72 |
+
def get_average_prompt_token_length():
|
73 |
+
# MongoDB bağlantı bilgilerini alma
|
74 |
+
mongo_url, db_name, collection_name = Database.get_mongodb()
|
75 |
+
# MongoDB'ye bağlanma
|
76 |
+
client = MongoClient(mongo_url)
|
77 |
+
db = client[db_name]
|
78 |
+
collection = db[collection_name]
|
79 |
+
# Tüm dökümanları çekme ve 'prompt_token_length' alanını alma
|
80 |
+
docs = collection.find({}, {'Prompt_token_length': 1})
|
81 |
+
# 'prompt_token_length' değerlerini toplama ve sayma
|
82 |
+
total_length = 0
|
83 |
+
count = 0
|
84 |
+
for doc in docs:
|
85 |
+
if 'Prompt_token_length' in doc:
|
86 |
+
total_length += doc['Prompt_token_length']
|
87 |
+
count += 1
|
88 |
+
# Ortalama hesaplama
|
89 |
+
average_length = total_length / count if count > 0 else 0
|
90 |
+
return int(average_length)
|
91 |
+
|
92 |
+
|
93 |
+
# Tokenizer ve Modeli yükleme
|
94 |
+
"""
|
95 |
+
class TokenizerProcessor:
|
96 |
+
def __init__(self, tokenizer_name='bert-base-uncased'):
|
97 |
+
self.tokenizer = BertTokenizer.from_pretrained(tokenizer_name)
|
98 |
+
|
99 |
+
def tokenize_and_encode(self, input_texts, output_texts, max_length=100):
|
100 |
+
encoded = self.tokenizer.batch_encode_plus(
|
101 |
+
text_pair=list(zip(input_texts, output_texts)),
|
102 |
+
padding='max_length',
|
103 |
+
truncation=True,
|
104 |
+
max_length=max_length,
|
105 |
+
return_attention_mask=True,
|
106 |
+
return_tensors='pt'
|
107 |
+
)
|
108 |
+
return encoded
|
109 |
+
|
110 |
+
paraphrase = tokenizer.encode_plus(sequence_0, sequence_2, return_tensors="pt")
|
111 |
+
not_paraphrase = tokenizer.encode_plus(sequence_0, sequence_1, return_tensors="pt")
|
112 |
+
|
113 |
+
paraphrase_classification_logits = model(**paraphrase)[0]
|
114 |
+
not_paraphrase_classification_logits = model(**not_paraphrase)[0]
|
115 |
+
def custom_padding(self, input_ids_list, max_length=100, pad_token_id=0):
|
116 |
+
padded_inputs = []
|
117 |
+
for ids in input_ids_list:
|
118 |
+
if len(ids) < max_length:
|
119 |
+
padded_ids = ids + [pad_token_id] * (max_length - len(ids))
|
120 |
+
else:
|
121 |
+
padded_ids = ids[:max_length]
|
122 |
+
padded_inputs.append(padded_ids)
|
123 |
+
return padded_inputs
|
124 |
+
|
125 |
+
def pad_and_truncate_pairs(self, input_texts, output_texts, max_length=100):
|
126 |
+
|
127 |
+
#input ve output verilerinin uzunluğunu eşitleme
|
128 |
+
inputs = self.tokenizer(input_texts, padding=False, truncation=False, return_tensors=None)
|
129 |
+
outputs = self.tokenizer(output_texts, padding=False, truncation=False, return_tensors=None)
|
130 |
+
|
131 |
+
input_ids = self.custom_padding(inputs['input_ids'], max_length, self.tokenizer.pad_token_id)
|
132 |
+
output_ids = self.custom_padding(outputs['input_ids'], max_length, self.tokenizer.pad_token_id)
|
133 |
+
|
134 |
+
input_ids_tensor = torch.tensor(input_ids)
|
135 |
+
output_ids_tensor = torch.tensor(output_ids)
|
136 |
+
|
137 |
+
input_attention_mask = (input_ids_tensor != self.tokenizer.pad_token_id).long()
|
138 |
+
output_attention_mask = (output_ids_tensor != self.tokenizer.pad_token_id).long()
|
139 |
+
|
140 |
+
return {
|
141 |
+
'input_ids': input_ids_tensor,
|
142 |
+
'input_attention_mask': input_attention_mask,
|
143 |
+
'output_ids': output_ids_tensor,
|
144 |
+
'output_attention_mask': output_attention_mask
|
145 |
+
}
|
146 |
+
|
147 |
+
"""
|
148 |
+
#cümleleri teker teker input ve output verilerinden çekmem gerekiyor
|
149 |
+
#def tokenize_and_pad_sequences(sequence_1,sequence2,)
|
150 |
+
|
151 |
+
|
152 |
+
"""class DataPipeline:
|
153 |
+
def __init__(self, tokenizer_name='bert-base-uncased', max_length=100):
|
154 |
+
self.tokenizer_processor = TokenizerProcessor(tokenizer_name)
|
155 |
+
self.max_length = max_length
|
156 |
+
|
157 |
+
def prepare_data(self):
|
158 |
+
input_texts = Database.get_input_texts()
|
159 |
+
output_texts = Database.get_output_texts()
|
160 |
+
encoded_data = self.tokenizer_processor.pad_and_truncate_pairs(input_texts, output_texts, self.max_length)
|
161 |
+
return encoded_data
|
162 |
+
|
163 |
+
def tokenize_texts(self, texts):
|
164 |
+
return [self.tokenize(text) for text in texts]
|
165 |
+
|
166 |
+
def encode_texts(self, texts):
|
167 |
+
return [self.encode(text, self.max_length) for text in texts]
|
168 |
+
|
169 |
+
# Example Usage
|
170 |
+
if __name__ == "__main__":
|
171 |
+
data_pipeline = DataPipeline()
|
172 |
+
encoded_data = data_pipeline.prepare_data()
|
173 |
+
print(encoded_data)
|
174 |
+
"""
|
175 |
+
|
t5.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# pip install accelerate
|
3 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
4 |
+
|
5 |
+
tokenizer = T5Tokenizer.from_pretrained("dbmdz/bert-base-turkish-cased")
|
6 |
+
model = T5ForConditionalGeneration.from_pretrained("dbmdz/bert-base-turkish-cased", device_map="auto")
|
7 |
+
|
8 |
+
|
9 |
+
input_text = "Başlık: Dijital Pazarlama, Alt başlıklar: dijital pazarlama nasıl yapılır?, dijital pazarlama ve gelişimi, dijital pazarlamanın iş hayatındaki yeri , anahtar kelimeler : pazarlama, ticaret, dijtalleşme, müşteri, ürün "
|
10 |
+
input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to("cuda")
|
11 |
+
|
12 |
+
outputs = model.generate(input_ids)
|
13 |
+
print(tokenizer.decode(outputs[0]))
|