-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentai.py
98 lines (75 loc) · 3.37 KB
/
sentai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import pandas as pd
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow import keras
import tensorflow_hub as hub
import os
import json
from tkinter import *
import sys
from tkinter import filedialog
#File Dialogs and Inputs
f = str(os.path.join(sys.path[0], "config.json"))
if os.path.isfile(f) and os.access(f, os.R_OK):
print("File exists and is readable")
with open('config.json', 'r') as b:
config = json.load(b)
dataPath = config['key1']
modelPath = config['key2']
txtcol = config['key3']
labelcol = config['key4']
else:
print("Either file is missing or is not readable, creating file...")
root = Tk()
root.withdraw()
dataPath = filedialog.askopenfilename(initialdir="/", title="Select your CSV file",
filetypes=(("CSV files", "*.csv*"), ("all files", "*.*")))
modelPath = filedialog.askdirectory()
txtcol = input('Text Column Name in CSV file (Case Sensitive): ')
labelcol = input('Label Column Name in CSV file (Case Sensitive) (Only Binary is supported, labels must be 0 or 1): ')
root.destroy()
config = {"key1": dataPath, "key2": modelPath, "key3": txtcol, "key4": labelcol}
with open('config.json', 'w') as t:
json.dump(config, t)
jsonini = input("Do you want to recreate a json file? y or n: ")
if jsonini == "y":
root = Tk()
root.withdraw()
dataPath = filedialog.askopenfilename(initialdir="/", title="Select your CSV file",
filetypes=(("CSV files", "*.csv*"), ("all files", "*.*")))
modelPath = filedialog.askdirectory()
txtcol = input('Text Column Name in CSV file (Case Sensitive): ')
labelcol = input(
'Label Column Name in CSV file (Case Sensitive) (Only Binary is supported, labels must be 0 or 1): ')
root.destroy()
config = {"key1": dataPath, "key2": modelPath, "key3": txtcol, "key4": labelcol}
with open('config.json', 'w') as t:
json.dump(config, t)
#USE Model and proprocessing with Pandas Dataframe from Amit Chaudhary (START)
df = pd.read_csv(dataPath)
x_train, x_test, y_train, y_test = train_test_split(df[txtcol],
df[labelcol],
test_size=0.3,
stratify=df[labelcol],
random_state=42)
x = tf.keras.layers.Input(shape=[], dtype=tf.string)
y = hub.KerasLayer('https://tfhub.dev/google/universal-sentence-encoder/4',
trainable=True)(x)
z = tf.keras.layers.Dense(1, activation='sigmoid')(y)
model = tf.keras.models.Model(x, z)
model.summary()
learning_rate_fn = tf.keras.optimizers.schedules.PolynomialDecay(
initial_learning_rate=0.001,
end_learning_rate=0.0001,
decay_steps=10000,
power=0.5)
opt = keras.optimizers.Adam(learning_rate=learning_rate_fn)
model.compile(optimizer=opt,
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit(x_train,
y_train,
epochs=2,
validation_data=(x_test, y_test))
#USE Model and proprocessing with Pandas Dataframe from Amit Chaudhary (END)
model.save(str(modelPath) + '/my_model.h5')