Yhhxhfh commited on
Commit
6895902
1 Parent(s): 40c33c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -2,8 +2,8 @@ import uvicorn
2
  import nltk
3
  nltk.download('punkt')
4
  nltk.download('wordnet')
5
- nltk.download('omw-1.4')
6
  nltk.download('punkt_tab')
 
7
  nltk.download('averaged_perceptron_tagger')
8
  from nltk.stem import WordNetLemmatizer
9
  from nltk.corpus import wordnet
@@ -16,7 +16,6 @@ import random
16
  import asyncio
17
  import concurrent.futures
18
  import multiprocessing
19
- import io
20
 
21
  import numpy as np
22
  from tensorflow.keras import Sequential
@@ -57,11 +56,16 @@ async def train_and_save_model():
57
  documents = []
58
  ignore_words = ['?', '!']
59
 
60
- if not r.exists('intents'):
 
 
 
61
  intents = {"intents": []}
62
- r.set('intents', json.dumps(intents))
63
- else:
64
- intents = json.loads(r.get('intents'))
 
 
65
 
66
  print("Loading user questions from Redis...")
67
  if not r.exists('user_questions_loaded'):
@@ -79,7 +83,7 @@ async def train_and_save_model():
79
  classes.append("unknown")
80
  r.set('user_questions_loaded', 1)
81
 
82
- print("Processing intents from Redis...")
83
  for intent in intents['intents']:
84
  for pattern in intent['patterns']:
85
  w = nltk.word_tokenize(pattern)
@@ -130,9 +134,8 @@ async def train_and_save_model():
130
  train_y = np.array([row[1] for row in training])
131
 
132
  print("Loading or creating model...")
133
- if r.exists('chatbot_model'):
134
- with io.BytesIO(r.get('chatbot_model')) as f:
135
- model = load_model(f)
136
  else:
137
  input_layer = Input(shape=(len(train_x[0]),))
138
  layer1 = Dense(128, activation='relu')(input_layer)
@@ -147,15 +150,13 @@ async def train_and_save_model():
147
  model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
148
 
149
  print("Training the model...")
150
- model.fit(train_x, train_y, epochs=1, batch_size=len(train_x), verbose=0, callbacks=[TqdmCallback(verbose=2)])
151
 
152
  print("Saving data to Redis...")
153
  r.set('words', pickle.dumps(words))
154
  r.set('classes', pickle.dumps(classes))
155
 
156
- with io.BytesIO() as f:
157
- save_model(model, f)
158
- r.set('chatbot_model', f.getvalue())
159
 
160
  print("Data and model saved. Re-training...")
161
 
@@ -182,8 +183,7 @@ class ChatMessage(BaseModel):
182
  async def chat(message: ChatMessage):
183
  words = pickle.loads(r.get('words'))
184
  classes = pickle.loads(r.get('classes'))
185
- with io.BytesIO(r.get('chatbot_model')) as f:
186
- model = load_model(f)
187
 
188
  sentence_words = nltk.word_tokenize(message.message)
189
  sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words]
@@ -211,7 +211,7 @@ async def tag_question(question: str, tag: str):
211
  r.set(f"tag:{question}", tag)
212
  return {"message": "Tag saved"}
213
 
214
- html_code = """
215
  <!DOCTYPE html>
216
  <html>
217
  <head>
 
2
  import nltk
3
  nltk.download('punkt')
4
  nltk.download('wordnet')
 
5
  nltk.download('punkt_tab')
6
+ nltk.download('omw-1.4')
7
  nltk.download('averaged_perceptron_tagger')
8
  from nltk.stem import WordNetLemmatizer
9
  from nltk.corpus import wordnet
 
16
  import asyncio
17
  import concurrent.futures
18
  import multiprocessing
 
19
 
20
  import numpy as np
21
  from tensorflow.keras import Sequential
 
56
  documents = []
57
  ignore_words = ['?', '!']
58
 
59
+ try:
60
+ with open('intents.json') as file:
61
+ intents = json.load(file)
62
+ except FileNotFoundError:
63
  intents = {"intents": []}
64
+ with open('intents.json', 'w') as file:
65
+ json.dump(intents, file, indent=4)
66
+ print("intents.json created. Please populate it with training data.")
67
+ await asyncio.sleep(60)
68
+ continue
69
 
70
  print("Loading user questions from Redis...")
71
  if not r.exists('user_questions_loaded'):
 
83
  classes.append("unknown")
84
  r.set('user_questions_loaded', 1)
85
 
86
+ print("Processing intents from intents.json...")
87
  for intent in intents['intents']:
88
  for pattern in intent['patterns']:
89
  w = nltk.word_tokenize(pattern)
 
134
  train_y = np.array([row[1] for row in training])
135
 
136
  print("Loading or creating model...")
137
+ if os.path.exists('chatbot_model.h5'):
138
+ model = load_model('chatbot_model.h5')
 
139
  else:
140
  input_layer = Input(shape=(len(train_x[0]),))
141
  layer1 = Dense(128, activation='relu')(input_layer)
 
150
  model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
151
 
152
  print("Training the model...")
153
+ model.fit(train_x, train_y, epochs=1, batch_size=len(train_x), verbose=0, callbacks=[TqdmCallback(verbose=2)])
154
 
155
  print("Saving data to Redis...")
156
  r.set('words', pickle.dumps(words))
157
  r.set('classes', pickle.dumps(classes))
158
 
159
+ save_model(model, 'chatbot_model.h5')
 
 
160
 
161
  print("Data and model saved. Re-training...")
162
 
 
183
  async def chat(message: ChatMessage):
184
  words = pickle.loads(r.get('words'))
185
  classes = pickle.loads(r.get('classes'))
186
+ model = load_model('chatbot_model.h5')
 
187
 
188
  sentence_words = nltk.word_tokenize(message.message)
189
  sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words]
 
211
  r.set(f"tag:{question}", tag)
212
  return {"message": "Tag saved"}
213
 
214
+ html_code = """
215
  <!DOCTYPE html>
216
  <html>
217
  <head>