Delete main_GPU_V002.py
Browse files- main_GPU_V002.py +0 -170
main_GPU_V002.py
DELETED
|
@@ -1,170 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import sys
|
| 3 |
-
import sqlite3
|
| 4 |
-
from datasets import Dataset
|
| 5 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TFAutoModelForSequenceClassification, 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 |
-
# PyTorch Modell
|
| 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 |
-
# TensorFlow Modell
|
| 133 |
-
tf_model = TFAutoModelForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=num_labels)
|
| 134 |
-
tf_model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
|
| 135 |
-
|
| 136 |
-
# Dummy-Daten für das Speichern im TensorFlow-Format
|
| 137 |
-
import tensorflow as tf
|
| 138 |
-
dummy_input = tf.constant(tokenizer("This is a dummy input", return_tensors="tf")["input_ids"])
|
| 139 |
-
|
| 140 |
-
# Speichern des TensorFlow-Modells
|
| 141 |
-
tf_model(dummy_input) # Modell einmal aufrufen, um es zu "bauen"
|
| 142 |
-
tf_model.save_pretrained(output_model_dir)
|
| 143 |
-
|
| 144 |
-
print(f"Das Modell wurde erfolgreich in {output_model_dir} gespeichert.")
|
| 145 |
-
|
| 146 |
-
except Exception as e:
|
| 147 |
-
print(f"Fehler beim Trainieren und Speichern des Modells: {e}")
|
| 148 |
-
|
| 149 |
-
if __name__ == "__main__":
|
| 150 |
-
# Verzeichnispfad als Argument übergeben, falls vorhanden
|
| 151 |
-
if len(sys.argv) > 1:
|
| 152 |
-
directory_path = sys.argv[1]
|
| 153 |
-
else:
|
| 154 |
-
directory_path = '.' # Standardverzeichnis, falls kein Argument übergeben wurde
|
| 155 |
-
|
| 156 |
-
db_name = os.path.basename(os.path.normpath(directory_path)) + '.db'
|
| 157 |
-
|
| 158 |
-
durchsuchen_und_extrahieren(directory_path, db_name)
|
| 159 |
-
|
| 160 |
-
daten = extrahiere_parameter_aus_db(db_name)
|
| 161 |
-
if daten:
|
| 162 |
-
hf_dataset = konvertiere_zu_hf_dataset(daten)
|
| 163 |
-
|
| 164 |
-
output_model = os.path.basename(os.path.normpath(directory_path)) + '_model' # Verzeichnisname Modell
|
| 165 |
-
output_model_dir = os.path.join(os.path.dirname(db_name), output_model)
|
| 166 |
-
|
| 167 |
-
trainiere_und_speichere_modell(hf_dataset, output_model_dir)
|
| 168 |
-
else:
|
| 169 |
-
print("Keine Daten gefunden, um ein HF-Dataset zu erstellen.")
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|