File size: 3,811 Bytes
5e848f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
477b776
 
 
5e848f1
477b776
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import itertools
import os

#%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf

from sklearn.preprocessing import LabelBinarizer, LabelEncoder
from sklearn.metrics import confusion_matrix

from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.preprocessing import text, sequence
from keras import utils
from tensorflow.keras.utils import to_categorical













from google.colab import auth
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials

# setup
gc = gspread.authorize(GoogleCredentials.get_application_default())

# read data and put it in a dataframe
gsheets = gc.open_by_url('https://docs.google.com/spreadsheets/d/15XNk8vY1pL6bzUo16AHWrx7Gws1Mz5JxOCGvkTnAczA/edit#gid=0')
sheets = gsheets.worksheet('Sheet1').get_all_values()
df = pd.DataFrame(sheets[1:], columns=sheets[0])













train_size = int(len(df) * 0.7)
train_posts = df['post'][:train_size]
train_tags = df['tags'][:train_size]

test_posts = df['post'][train_size:]
test_tags = df['tags'][train_size:]

max_words = 1000
tokenize = text.Tokenizer(num_words=max_words, char_level=False)
tokenize.fit_on_texts(train_posts) # only fit on train

x_train = tokenize.texts_to_matrix(train_posts)
x_test = tokenize.texts_to_matrix(test_posts)

encoder = LabelEncoder()
encoder.fit(train_tags)
y_train = encoder.transform(train_tags)
y_test = encoder.transform(test_tags)

num_classes = np.max(y_train) + 1
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

batch_size = 2 # Normally it should be 32 or 64, smaller better but slower.  Since sample size is too small, use small batch size can get better predicting result and still run very fast
epochs = 10 # was 2.  

# Build the model
model = Sequential()
model.add(Dense(512, input_shape=(max_words,)))
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(512))
model.add(Activation('sigmoid'))
model.add(Dropout(0.5))

model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))

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

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
              
history = model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_split=0.1)












predicted=[]
score=[]
questn=[]

print(f'Number of training {train_size} rows is loaded')

for i  in range(len(test_posts)):
  classifyTest(test_posts.iloc[i])

df = pd.DataFrame({'Question':questn, 'Predicted Answer':predicted, 'Score':score, 'Real Answer':test_tags})
ansIs=df['Predicted Answer']==df['Real Answer']
df = pd.DataFrame({'Question':questn, 'Predicted Answer':predicted, 'Score':score, 'Real Answer':test_tags, 'Answer is ':ansIs})














def classify(string):
  a=[string]

  #print(a)
  questn.append(string)
  x_test_new = tokenize.texts_to_matrix(a)

  # Here's how to generate a prediction on individual examples
  text_labels = encoder.classes_ 

  prediction = model.predict(np.array([x_test_new[0]]))
  predicted_label = text_labels[np.argmax(prediction)]
  #print(test_posts.iloc[0][:50], "...")
  #print('Actual label:' + test_tags.iloc[0])

  print(prediction[0][np.argmax(prediction)])
  #score.append(prediction[0][np.argmax(prediction)])

  print("Predicted label: " + predicted_label + "\n")
  #predicted.append(predicted_label)
  return predicted_label



import gradio as gr

def greet(name):
  return classify(name)

iface = gr.Interface(fn=greet, inputs="text", outputs="text")
iface.launch()