<jupyter_start><jupyter_text><jupyter_code>from keras.models import Sequential
from keras.layers import Conv1D, GlobalMaxPooling1D, Embedding, LSTM
from keras.layers.core import Dense, Dropout, Activation
from keras.preprocessing.text import Tokenizer
from keras import metrics, regularizers
from keras.preprocessing import sequence
import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelBinarizer
#Load cleaned dataset
from google.colab import drive
drive.mount("/content/gdrive")
data = pd.read_csv('/content/gdrive/My Drive/dataset/sample.csv', header = 0, names = ['Query', 'Description'],encoding="utf-8-sig")
#Split the dataset to Training and Test subsets (90/10)
train, test = train_test_split(data, test_size = 0.1, random_state = 17)

train_descs = train['Description'].astype("str")
train_labels = train['Query'].astype("str")
 
test_descs = test['Description'].astype("str")
test_labels = test['Query'].astype("str")
# Model Parameters
vocab_size = 1000

sequences_length = 1200

embedding_dimensionality = 64 
max_features = 2000 #equal to vocab_size

num_labels = len(train_labels.unique())
batch_size = 32
nb_epoch = 20

nof_filters = 200
kernel_size = 16

hidden_dims = 512
# Convert Texts to Numeric Vectors for Input
tokenizer = Tokenizer(num_words = vocab_size)
tokenizer.fit_on_texts(train_descs)

x_train = tokenizer.texts_to_sequences(train_descs)
x_test = tokenizer.texts_to_sequences(test_descs)

x_train = sequence.pad_sequences(x_train, maxlen = sequences_length, padding = 'post')
x_test = sequence.pad_sequences(x_test, maxlen = sequences_length, padding = 'post')

encoder = LabelBinarizer()
encoder.fit(train_labels)
y_train = encoder.transform(train_labels)
y_test = encoder.transform(test_labels)
model = Sequential()
model.add(Embedding(max_features, embedding_dimensionality, input_length = 1200))

model.add(Conv1D(nof_filters, kernel_size, padding='valid', activation='relu', strides = 1))
model.add(GlobalMaxPooling1D())

model.add(Dense(hidden_dims))
model.add(Dropout(0.3))
model.add(Activation('relu'))

model.add(Dense(num_labels))
model.add(Activation('softmax'))

model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', #'sgd', 'adam', 'RMSprop', 'Adagrad'
                   metrics = [metrics.categorical_accuracy])
history = model.fit(x_train, y_train,
                    batch_size = batch_size,
                    epochs = nb_epoch,
                    verbose = True,
                    validation_split = 0.01)
score = model.evaluate(x_test, y_test, batch_size = batch_size, verbose = True)
 
print('\nTest categorical_crossentropy:', score[0])
print('Categorical accuracy:', score[1])

# summarize history for accuracy
import matplotlib.pyplot as plt

plt.plot(history.history['categorical_accuracy'])
plt.plot(history.history['val_categorical_accuracy'])
plt.title('model accuracy')
plt.ylabel('classification accuracy')
plt.xlabel('epochs')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
model.save( '/content/gdrive/My Drive/dataset/model-cnn.h5' )
import tensorflow as tf

model = tf.keras.models.load_model('/content/gdrive/My Drive/dataset/model-cnn.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
open("/content/gdrive/My Drive/dataset/converted_model-cnn.tflite", "wb").write(tflite_model)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
text=input()
try:
    predict=model.predict(tokenizer.texts_to_sequences([text]))
    print(predict)
    print(encoder.inverse_transform(predict))
    print(predict.argmax(axis=-1))
except:
    print("Invalid Input")
import json

with open( '/content/gdrive/My Drive/dataset/dict.json' , 'w' ) as file:
    json.dump( tokenizer.word_index , file )

import tensorflow as tf
interpreter = tf.lite.Interpreter("/content/gdrive/My Drive/dataset/converted_model-cnn.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
output_details
input_details
tf.version
import tensorflow as tf
from google.colab import drive
drive.mount("/content/gdrive")
interpreter = tf.lite.Interpreter(model_path="/content/gdrive/My Drive/dataset/converted_model-cnn.tflite")
interpreter.allocate_tensors()

# Print input shape and type
inputs = interpreter.get_input_details()
print('{} input(s):'.format(len(inputs)))
for i in range(0, len(inputs)):
    print('{} {}'.format(inputs[i]['shape'], inputs[i]['dtype']))

# Print output shape and type
outputs = interpreter.get_output_details()
print('\n{} output(s):'.format(len(outputs)))
for i in range(0, len(outputs)):
    print('{} {}'.format(outputs[i]['shape'], outputs[i]['dtype']))<jupyter_output>Drive already mounted at /content/gdrive; to attempt to forcibly remount, call drive.mount("/content/gdrive", force_remount=True).
1 input(s):
[   1 1200] <class 'numpy.float32'>

1 output(s):
[ 1 25] <class 'numpy.float32'>
StarcoderdataJupyterStructuredCleanDedup