Upload 2 files
Browse files- intents.json +34 -0
- main.py +131 -0
intents.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{"intents":[
|
| 2 |
+
{"tag": "greeting",
|
| 3 |
+
"pattern": ["oi","como está?","olá", "tem alguem ai?", "bom dia","tudo bem?"],
|
| 4 |
+
"responses": ["Olá","estou bem, obrigado por ter perguntado ;)","Bom ver você de novo!"],
|
| 5 |
+
"context_set": ""
|
| 6 |
+
},
|
| 7 |
+
{"tag":"goodbye",
|
| 8 |
+
"pattern":["tchau","até mais","até logo","tenha um bom dia"],
|
| 9 |
+
"responses": ["triste por te ver indo :_(", "até mais!", "tchau!"],
|
| 10 |
+
"context_set": ""
|
| 11 |
+
},
|
| 12 |
+
{"tag":"age",
|
| 13 |
+
"pattern":["quantos anos você tem?" ],
|
| 14 |
+
"responses":["tenho 1 dia de vida apenas"],
|
| 15 |
+
"context_set":""
|
| 16 |
+
},
|
| 17 |
+
{"tag": "name",
|
| 18 |
+
"pattern": ["quem é você?", "qual é o seu nome?","como se chama?"],
|
| 19 |
+
"responses":["Meu nome é Eneias","Eu sou seu pai luke!", "Sou você do futuro!"],
|
| 20 |
+
"context_set":""
|
| 21 |
+
},
|
| 22 |
+
{"tag": "venda",
|
| 23 |
+
"pattern": ["Quero contratar seus serviços","Quais são os seus planos?"],
|
| 24 |
+
"responses":["Temos planos que se encaixam perfeitamente para você. Aqui estão eles: ","Temos os seguintes planos: "],
|
| 25 |
+
"context_set":""
|
| 26 |
+
},
|
| 27 |
+
{"tag": "default",
|
| 28 |
+
"pattern": ["", " "," "],
|
| 29 |
+
"responses":["Desculpa, não entendi :("],
|
| 30 |
+
"context_set":""
|
| 31 |
+
}
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
}
|
main.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import nltk
|
| 2 |
+
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from keras.models import Sequential, load_model
|
| 5 |
+
from keras.layers import Dense
|
| 6 |
+
import random
|
| 7 |
+
import json
|
| 8 |
+
import pickle
|
| 9 |
+
|
| 10 |
+
###Deu certo###
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
nltk.download('punkt')
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
stemmer = nltk.stem.lancaster.LancasterStemmer()
|
| 17 |
+
|
| 18 |
+
# Carregando a base de dados
|
| 19 |
+
with open("/home/mrdev/Ollama_testes/tutorial_IA/intents.json") as file:
|
| 20 |
+
data = json.load(file)
|
| 21 |
+
|
| 22 |
+
# Separando as informações
|
| 23 |
+
try:
|
| 24 |
+
with open("/home/mrdev/Ollama_testes/tutorial_IA/data.pickle", "rb") as f:
|
| 25 |
+
words, labels, training, output = pickle.load(f)
|
| 26 |
+
except:
|
| 27 |
+
words = []
|
| 28 |
+
labels = []
|
| 29 |
+
docs_y = []
|
| 30 |
+
docs_x = []
|
| 31 |
+
|
| 32 |
+
# Loop para armazenar as informações de forma separada
|
| 33 |
+
for intent in data["intents"]:
|
| 34 |
+
for pattern in intent["pattern"]:
|
| 35 |
+
wrds = nltk.word_tokenize(pattern)
|
| 36 |
+
words.extend(wrds)
|
| 37 |
+
docs_x.append(wrds)
|
| 38 |
+
docs_y.append(intent["tag"])
|
| 39 |
+
|
| 40 |
+
if intent["tag"] not in labels:
|
| 41 |
+
labels.append(intent["tag"])
|
| 42 |
+
|
| 43 |
+
# Organizando/pre-processamento
|
| 44 |
+
words = [stemmer.stem(w.lower()) for w in words if w != "?"]
|
| 45 |
+
words = sorted(list(set(words)))
|
| 46 |
+
|
| 47 |
+
labels = sorted(labels)
|
| 48 |
+
|
| 49 |
+
# Iniciando a tokenização e treinamento
|
| 50 |
+
training = []
|
| 51 |
+
output = []
|
| 52 |
+
|
| 53 |
+
out_empty = [0 for _ in range(len(labels))]
|
| 54 |
+
|
| 55 |
+
for x, doc in enumerate(docs_x):
|
| 56 |
+
bag = []
|
| 57 |
+
|
| 58 |
+
wrds = [stemmer.stem(w) for w in doc]
|
| 59 |
+
|
| 60 |
+
for w in words:
|
| 61 |
+
if w in wrds:
|
| 62 |
+
bag.append(1)
|
| 63 |
+
else:
|
| 64 |
+
bag.append(0)
|
| 65 |
+
output_row = out_empty[:]
|
| 66 |
+
output_row[labels.index(docs_y[x])] = 1
|
| 67 |
+
|
| 68 |
+
training.append(bag)
|
| 69 |
+
output.append(output_row)
|
| 70 |
+
|
| 71 |
+
training = np.array(training)
|
| 72 |
+
output = np.array(output)
|
| 73 |
+
|
| 74 |
+
with open("/home/mrdev/Ollama_testes/tutorial_IA/data.pickle", "wb") as f:
|
| 75 |
+
pickle.dump((words, labels, training, output), f)
|
| 76 |
+
|
| 77 |
+
#print(data["intents"])
|
| 78 |
+
|
| 79 |
+
# Iniciando a criação do modelo
|
| 80 |
+
tf.keras.backend.clear_session() # Limpa a sessão do TensorFlow
|
| 81 |
+
model = Sequential()
|
| 82 |
+
model.add(Dense(8, input_shape=(len(training[0]),), activation='relu'))
|
| 83 |
+
model.add(Dense(8, activation='relu'))
|
| 84 |
+
model.add(Dense(len(output[0]), activation='softmax'))
|
| 85 |
+
|
| 86 |
+
# Compilando o modelo
|
| 87 |
+
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
| 88 |
+
|
| 89 |
+
# Tentando carregar o modelo treinado
|
| 90 |
+
try:
|
| 91 |
+
model = load_model("/home/mrdev/Ollama_testes/tutorial_IA/model_keras.h5")
|
| 92 |
+
except:
|
| 93 |
+
# Treinando o modelo
|
| 94 |
+
model.fit(training, output, epochs=1000, batch_size=8, verbose=1)
|
| 95 |
+
# Salvando o modelo treinado
|
| 96 |
+
model.save("/home/mrdev/Ollama_testes/tutorial_IA/model_keras.h5")
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def bag_of_words(s, words):
|
| 100 |
+
bag = [0 for _ in range(len(words))]
|
| 101 |
+
s_words = nltk.word_tokenize(s)
|
| 102 |
+
s_words = [stemmer.stem(word.lower()) for word in s_words]
|
| 103 |
+
|
| 104 |
+
for se in s_words:
|
| 105 |
+
for i, w in enumerate(words):
|
| 106 |
+
if w == se:
|
| 107 |
+
bag[i] = 1
|
| 108 |
+
return np.array(bag)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# Iniciando o chat em tempo real
|
| 112 |
+
def chat():
|
| 113 |
+
print("__start__")
|
| 114 |
+
while True:
|
| 115 |
+
inp = input("Você: ")
|
| 116 |
+
if inp.lower() == "sair":
|
| 117 |
+
print("Saindo")
|
| 118 |
+
break
|
| 119 |
+
else:
|
| 120 |
+
results = model.predict(np.array([bag_of_words(inp, words)]))
|
| 121 |
+
results_index = np.argmax(results)
|
| 122 |
+
tag = labels[results_index]
|
| 123 |
+
|
| 124 |
+
for tg in data["intents"]:
|
| 125 |
+
if tg["tag"] == tag:
|
| 126 |
+
responses = tg["responses"]
|
| 127 |
+
|
| 128 |
+
print(random.choice(responses))
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
chat()
|