ManfredAabye
commited on
Commit
•
a114c8b
1
Parent(s):
fdb1eec
First test software GPU or CUDA
Browse files- main_CUDA.py +158 -0
- main_GPU.py +157 -0
main_CUDA.py
ADDED
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import sqlite3
|
4 |
+
import torch
|
5 |
+
from datasets import Dataset, DatasetDict
|
6 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments
|
7 |
+
|
8 |
+
SUPPORTED_FILE_TYPES = ['.sh', '.bat', '.ps1', '.cs', '.c', '.cpp', '.h', '.cmake', '.py', '.git', '.sql', '.csv', '.sqlite', '.lsl']
|
9 |
+
|
10 |
+
def extrahiere_parameter(file_path):
|
11 |
+
try:
|
12 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
13 |
+
lines = file.readlines()
|
14 |
+
anzahl_zeilen = len(lines)
|
15 |
+
anzahl_zeichen = sum(len(line) for line in lines)
|
16 |
+
long_text_mode = anzahl_zeilen > 1000
|
17 |
+
dimensionalität = 1 # Beispielwert, kann angepasst werden
|
18 |
+
return {
|
19 |
+
"text": file_path,
|
20 |
+
"anzahl_zeilen": anzahl_zeilen,
|
21 |
+
"anzahl_zeichen": anzahl_zeichen,
|
22 |
+
"long_text_mode": long_text_mode,
|
23 |
+
"dimensionalität": dimensionalität
|
24 |
+
}
|
25 |
+
except UnicodeDecodeError as e:
|
26 |
+
print(f"Fehler beim Lesen der Datei {file_path}: {e}")
|
27 |
+
return None
|
28 |
+
except Exception as e:
|
29 |
+
print(f"Allgemeiner Fehler beim Lesen der Datei {file_path}: {e}")
|
30 |
+
return None
|
31 |
+
|
32 |
+
def durchsuchen_und_extrahieren(root_dir, db_pfad):
|
33 |
+
try:
|
34 |
+
with sqlite3.connect(db_pfad) as conn:
|
35 |
+
cursor = conn.cursor()
|
36 |
+
cursor.execute('''CREATE TABLE IF NOT EXISTS dateiparameter
|
37 |
+
(id INTEGER PRIMARY KEY,
|
38 |
+
dateipfad TEXT,
|
39 |
+
anzahl_zeilen INTEGER,
|
40 |
+
anzahl_zeichen INTEGER,
|
41 |
+
long_text_mode BOOLEAN,
|
42 |
+
dimensionalität INTEGER)''')
|
43 |
+
|
44 |
+
for subdir, _, files in os.walk(root_dir):
|
45 |
+
for file in files:
|
46 |
+
if any(file.endswith(ext) for ext in SUPPORTED_FILE_TYPES):
|
47 |
+
file_path = os.path.join(subdir, file)
|
48 |
+
parameter = extrahiere_parameter(file_path)
|
49 |
+
if parameter:
|
50 |
+
cursor.execute('''INSERT INTO dateiparameter (dateipfad, anzahl_zeilen, anzahl_zeichen, long_text_mode, dimensionalität)
|
51 |
+
VALUES (?, ?, ?, ?, ?)''', (file_path, parameter["anzahl_zeilen"], parameter["anzahl_zeichen"], parameter["long_text_mode"], parameter["dimensionalität"]))
|
52 |
+
conn.commit()
|
53 |
+
print("Parameter erfolgreich extrahiert und in der Datenbank gespeichert.")
|
54 |
+
except sqlite3.Error as e:
|
55 |
+
print(f"SQLite Fehler: {e}")
|
56 |
+
except Exception as e:
|
57 |
+
print(f"Allgemeiner Fehler: {e}")
|
58 |
+
|
59 |
+
def extrahiere_parameter_aus_db(db_pfad):
|
60 |
+
try:
|
61 |
+
with sqlite3.connect(db_pfad) as conn:
|
62 |
+
cursor = conn.cursor()
|
63 |
+
cursor.execute("SELECT * FROM dateiparameter")
|
64 |
+
daten = cursor.fetchall()
|
65 |
+
return daten
|
66 |
+
except sqlite3.Error as e:
|
67 |
+
print(f"SQLite Fehler: {e}")
|
68 |
+
return None
|
69 |
+
except Exception as e:
|
70 |
+
print(f"Allgemeiner Fehler: {e}")
|
71 |
+
return None
|
72 |
+
|
73 |
+
def konvertiere_zu_hf_dataset(daten):
|
74 |
+
dataset_dict = {
|
75 |
+
"text": [],
|
76 |
+
"anzahl_zeilen": [],
|
77 |
+
"anzahl_zeichen": [],
|
78 |
+
"long_text_mode": [],
|
79 |
+
"dimensionalität": []
|
80 |
+
}
|
81 |
+
|
82 |
+
for eintrag in daten:
|
83 |
+
dataset_dict["text"].append(eintrag[1]) # 'text' entspricht 'dateipfad'
|
84 |
+
dataset_dict["anzahl_zeilen"].append(eintrag[2])
|
85 |
+
dataset_dict["anzahl_zeichen"].append(eintrag[3])
|
86 |
+
dataset_dict["long_text_mode"].append(eintrag[4])
|
87 |
+
dataset_dict["dimensionalität"].append(eintrag[5])
|
88 |
+
|
89 |
+
return Dataset.from_dict(dataset_dict)
|
90 |
+
|
91 |
+
def trainiere_und_speichere_modell(hf_dataset, output_model_dir):
|
92 |
+
try:
|
93 |
+
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased", use_fast=True)
|
94 |
+
|
95 |
+
def tokenize_function(examples):
|
96 |
+
return tokenizer(examples["text"], padding="max_length", truncation=True)
|
97 |
+
|
98 |
+
tokenized_datasets = hf_dataset.map(tokenize_function, batched=True)
|
99 |
+
|
100 |
+
# Beispielhaftes Hinzufügen von Dummy-Labels für das Training
|
101 |
+
tokenized_datasets = tokenized_datasets.map(lambda examples: {"label": [0] * len(examples["text"])}, batched=True) # Dummy labels as int
|
102 |
+
|
103 |
+
# Aufteilen des Datensatzes in Training und Test
|
104 |
+
train_test_split = tokenized_datasets.train_test_split(test_size=0.2)
|
105 |
+
train_dataset = train_test_split["train"]
|
106 |
+
eval_dataset = train_test_split["test"]
|
107 |
+
|
108 |
+
num_labels = len(set(train_dataset["label"]))
|
109 |
+
|
110 |
+
model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=num_labels)
|
111 |
+
|
112 |
+
training_args = TrainingArguments(
|
113 |
+
output_dir=output_model_dir,
|
114 |
+
evaluation_strategy="epoch", # Aktualisiert nach der Deprecation-Warnung
|
115 |
+
per_device_train_batch_size=8,
|
116 |
+
per_device_eval_batch_size=8,
|
117 |
+
num_train_epochs=3,
|
118 |
+
weight_decay=0.01,
|
119 |
+
)
|
120 |
+
|
121 |
+
trainer = Trainer(
|
122 |
+
model=model,
|
123 |
+
args=training_args,
|
124 |
+
train_dataset=train_dataset,
|
125 |
+
eval_dataset=eval_dataset,
|
126 |
+
)
|
127 |
+
|
128 |
+
trainer.train()
|
129 |
+
model.save_pretrained(output_model_dir)
|
130 |
+
tokenizer.save_pretrained(output_model_dir)
|
131 |
+
|
132 |
+
print(f"Das Modell wurde erfolgreich in {output_model_dir} gespeichert.")
|
133 |
+
print("You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.")
|
134 |
+
|
135 |
+
except Exception as e:
|
136 |
+
print(f"Fehler beim Trainieren und Speichern des Modells: {e}")
|
137 |
+
|
138 |
+
if __name__ == "__main__":
|
139 |
+
# Verzeichnispfad als Argument übergeben, falls vorhanden
|
140 |
+
if len(sys.argv) > 1:
|
141 |
+
directory_path = sys.argv[1]
|
142 |
+
else:
|
143 |
+
directory_path = '.' # Standardverzeichnis, falls kein Argument übergeben wurde
|
144 |
+
|
145 |
+
db_name = os.path.basename(os.path.normpath(directory_path)) + '.db'
|
146 |
+
|
147 |
+
durchsuchen_und_extrahieren(directory_path, db_name)
|
148 |
+
|
149 |
+
daten = extrahiere_parameter_aus_db(db_name)
|
150 |
+
if daten:
|
151 |
+
hf_dataset = konvertiere_zu_hf_dataset(daten)
|
152 |
+
|
153 |
+
output_model = os.path.basename(os.path.normpath(directory_path)) + '_model' # Verzeichnisname Modell
|
154 |
+
output_model_dir = os.path.join(os.path.dirname(db_name), output_model)
|
155 |
+
|
156 |
+
trainiere_und_speichere_modell(hf_dataset, output_model_dir)
|
157 |
+
else:
|
158 |
+
print("Keine Daten gefunden, um ein HF-Dataset zu erstellen.")
|
main_GPU.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import sqlite3
|
4 |
+
from datasets import Dataset, DatasetDict
|
5 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments
|
6 |
+
|
7 |
+
SUPPORTED_FILE_TYPES = ['.sh', '.bat', '.ps1', '.cs', '.c', '.cpp', '.h', '.cmake', '.py', '.git', '.sql', '.csv', '.sqlite', '.lsl']
|
8 |
+
|
9 |
+
def extrahiere_parameter(file_path):
|
10 |
+
try:
|
11 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
12 |
+
lines = file.readlines()
|
13 |
+
anzahl_zeilen = len(lines)
|
14 |
+
anzahl_zeichen = sum(len(line) for line in lines)
|
15 |
+
long_text_mode = anzahl_zeilen > 1000
|
16 |
+
dimensionalität = 1 # Beispielwert, kann angepasst werden
|
17 |
+
return {
|
18 |
+
"text": file_path,
|
19 |
+
"anzahl_zeilen": anzahl_zeilen,
|
20 |
+
"anzahl_zeichen": anzahl_zeichen,
|
21 |
+
"long_text_mode": long_text_mode,
|
22 |
+
"dimensionalität": dimensionalität
|
23 |
+
}
|
24 |
+
except UnicodeDecodeError as e:
|
25 |
+
print(f"Fehler beim Lesen der Datei {file_path}: {e}")
|
26 |
+
return None
|
27 |
+
except Exception as e:
|
28 |
+
print(f"Allgemeiner Fehler beim Lesen der Datei {file_path}: {e}")
|
29 |
+
return None
|
30 |
+
|
31 |
+
def durchsuchen_und_extrahieren(root_dir, db_pfad):
|
32 |
+
try:
|
33 |
+
with sqlite3.connect(db_pfad) as conn:
|
34 |
+
cursor = conn.cursor()
|
35 |
+
cursor.execute('''CREATE TABLE IF NOT EXISTS dateiparameter
|
36 |
+
(id INTEGER PRIMARY KEY,
|
37 |
+
dateipfad TEXT,
|
38 |
+
anzahl_zeilen INTEGER,
|
39 |
+
anzahl_zeichen INTEGER,
|
40 |
+
long_text_mode BOOLEAN,
|
41 |
+
dimensionalität INTEGER)''')
|
42 |
+
|
43 |
+
for subdir, _, files in os.walk(root_dir):
|
44 |
+
for file in files:
|
45 |
+
if any(file.endswith(ext) for ext in SUPPORTED_FILE_TYPES):
|
46 |
+
file_path = os.path.join(subdir, file)
|
47 |
+
parameter = extrahiere_parameter(file_path)
|
48 |
+
if parameter:
|
49 |
+
cursor.execute('''INSERT INTO dateiparameter (dateipfad, anzahl_zeilen, anzahl_zeichen, long_text_mode, dimensionalität)
|
50 |
+
VALUES (?, ?, ?, ?, ?)''', (file_path, parameter["anzahl_zeilen"], parameter["anzahl_zeichen"], parameter["long_text_mode"], parameter["dimensionalität"]))
|
51 |
+
conn.commit()
|
52 |
+
print("Parameter erfolgreich extrahiert und in der Datenbank gespeichert.")
|
53 |
+
except sqlite3.Error as e:
|
54 |
+
print(f"SQLite Fehler: {e}")
|
55 |
+
except Exception as e:
|
56 |
+
print(f"Allgemeiner Fehler: {e}")
|
57 |
+
|
58 |
+
def extrahiere_parameter_aus_db(db_pfad):
|
59 |
+
try:
|
60 |
+
with sqlite3.connect(db_pfad) as conn:
|
61 |
+
cursor = conn.cursor()
|
62 |
+
cursor.execute("SELECT * FROM dateiparameter")
|
63 |
+
daten = cursor.fetchall()
|
64 |
+
return daten
|
65 |
+
except sqlite3.Error as e:
|
66 |
+
print(f"SQLite Fehler: {e}")
|
67 |
+
return None
|
68 |
+
except Exception as e:
|
69 |
+
print(f"Allgemeiner Fehler: {e}")
|
70 |
+
return None
|
71 |
+
|
72 |
+
def konvertiere_zu_hf_dataset(daten):
|
73 |
+
dataset_dict = {
|
74 |
+
"text": [],
|
75 |
+
"anzahl_zeilen": [],
|
76 |
+
"anzahl_zeichen": [],
|
77 |
+
"long_text_mode": [],
|
78 |
+
"dimensionalität": []
|
79 |
+
}
|
80 |
+
|
81 |
+
for eintrag in daten:
|
82 |
+
dataset_dict["text"].append(eintrag[1]) # 'text' entspricht 'dateipfad'
|
83 |
+
dataset_dict["anzahl_zeilen"].append(eintrag[2])
|
84 |
+
dataset_dict["anzahl_zeichen"].append(eintrag[3])
|
85 |
+
dataset_dict["long_text_mode"].append(eintrag[4])
|
86 |
+
dataset_dict["dimensionalität"].append(eintrag[5])
|
87 |
+
|
88 |
+
return Dataset.from_dict(dataset_dict)
|
89 |
+
|
90 |
+
def trainiere_und_speichere_modell(hf_dataset, output_model_dir):
|
91 |
+
try:
|
92 |
+
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased", use_fast=True)
|
93 |
+
|
94 |
+
def tokenize_function(examples):
|
95 |
+
return tokenizer(examples["text"], padding="max_length", truncation=True)
|
96 |
+
|
97 |
+
tokenized_datasets = hf_dataset.map(tokenize_function, batched=True)
|
98 |
+
|
99 |
+
# Beispielhaftes Hinzufügen von Dummy-Labels für das Training
|
100 |
+
tokenized_datasets = tokenized_datasets.map(lambda examples: {"label": [0.0] * len(examples["text"])}, batched=True) # Dummy labels as float
|
101 |
+
|
102 |
+
# Aufteilen des Datensatzes in Training und Test
|
103 |
+
train_test_split = tokenized_datasets.train_test_split(test_size=0.2)
|
104 |
+
train_dataset = train_test_split["train"]
|
105 |
+
eval_dataset = train_test_split["test"]
|
106 |
+
|
107 |
+
num_labels = len(set(train_dataset["label"]))
|
108 |
+
|
109 |
+
model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=num_labels)
|
110 |
+
|
111 |
+
training_args = TrainingArguments(
|
112 |
+
output_dir=output_model_dir,
|
113 |
+
evaluation_strategy="epoch", # Aktualisiert nach der Deprecation-Warnung
|
114 |
+
per_device_train_batch_size=8,
|
115 |
+
per_device_eval_batch_size=8,
|
116 |
+
num_train_epochs=3,
|
117 |
+
weight_decay=0.01,
|
118 |
+
)
|
119 |
+
|
120 |
+
trainer = Trainer(
|
121 |
+
model=model,
|
122 |
+
args=training_args,
|
123 |
+
train_dataset=train_dataset,
|
124 |
+
eval_dataset=eval_dataset,
|
125 |
+
)
|
126 |
+
|
127 |
+
trainer.train()
|
128 |
+
model.save_pretrained(output_model_dir)
|
129 |
+
tokenizer.save_pretrained(output_model_dir)
|
130 |
+
|
131 |
+
print(f"Das Modell wurde erfolgreich in {output_model_dir} gespeichert.")
|
132 |
+
print("You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.")
|
133 |
+
|
134 |
+
except Exception as e:
|
135 |
+
print(f"Fehler beim Trainieren und Speichern des Modells: {e}")
|
136 |
+
|
137 |
+
if __name__ == "__main__":
|
138 |
+
# Verzeichnispfad als Argument übergeben, falls vorhanden
|
139 |
+
if len(sys.argv) > 1:
|
140 |
+
directory_path = sys.argv[1]
|
141 |
+
else:
|
142 |
+
directory_path = '.' # Standardverzeichnis, falls kein Argument übergeben wurde
|
143 |
+
|
144 |
+
db_name = os.path.basename(os.path.normpath(directory_path)) + '.db'
|
145 |
+
|
146 |
+
durchsuchen_und_extrahieren(directory_path, db_name)
|
147 |
+
|
148 |
+
daten = extrahiere_parameter_aus_db(db_name)
|
149 |
+
if daten:
|
150 |
+
hf_dataset = konvertiere_zu_hf_dataset(daten)
|
151 |
+
|
152 |
+
output_model = os.path.basename(os.path.normpath(directory_path)) + '_model' # Verzeichnisname Modell
|
153 |
+
output_model_dir = os.path.join(os.path.dirname(db_name), output_model)
|
154 |
+
|
155 |
+
trainiere_und_speichere_modell(hf_dataset, output_model_dir)
|
156 |
+
else:
|
157 |
+
print("Keine Daten gefunden, um ein HF-Dataset zu erstellen.")
|