Как я могу создать ИИ, генерирующий текст на основе тензорного потока?
Я работаю над «чат-ботом с искусственным интеллектом», который связывает входные данные пользователя с файлом json и возвращает «ответ», также заранее определенный. Но вопрос в том, что я хочу добавить функцию генерации текста, и я не знаю, как это сделать (в Python). Я пробовал раньше, но не работал с архитектурой руки.
Вы можете помочь мне? Заранее спасибо.
Вот код: «training.py»
import random
import json
import pickle
import numpy as np
import tensorflow as tf
import nltk
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
intents = json.loads(open('intents.json').read())
words = []
classes = []
documents = []
ignoreLetters = ['?', '!', '.', ',']
for intent in intents['intents']:
for pattern in intent['patterns']:
wordList = nltk.word_tokenize(pattern)
words.extend(wordList)
documents.append((wordList, intent['tag']))
if intent['tag'] not in classes:
classes.append(intent['tag'])
words = [lemmatizer.lemmatize(word) for word in words if word not in ignoreLetters]
words = sorted(set(words))
classes = sorted(set(classes))
pickle.dump(words, open('words.pkl', 'wb'))
pickle.dump(classes, open('classes.pkl', 'wb'))
training = []
outputEmpty = [0] * len(classes)
for document in documents:
bag = []
wordPatterns = document[0]
wordPatterns = [lemmatizer.lemmatize(word.lower()) for word in wordPatterns]
for word in words:
bag.append(1) if word in wordPatterns else bag.append(0)
outputRow = list(outputEmpty)
outputRow[classes.index(document[1])] = 1
training.append(bag + outputRow)
random.shuffle(training)
training = np.array(training)
trainX = training[:, :len(words)]
trainY = training[:, len(words):]
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(256, input_shape=(len(trainX[0]),), activation = 'relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(128, activation = 'relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(len(trainY[0]), activation='softmax'))
sgd = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
hist = model.fit(trainX, trainY, epochs=2000, batch_size=5, verbose=1)
model.save('chatbotmodel.h5', hist)
print('Done')
чат-бот.py:
import random
import json
import pickle
import numpy as np
import time
import pyjokes
import nltk
from nltk.stem import WordNetLemmatizer
from tensorflow.keras.models import load_model
lemmatizer = WordNetLemmatizer()
intents = json.loads(open('intents.json').read())
words = pickle.load(open('words.pkl', 'rb'))
classes = pickle.load(open('classes.pkl', 'rb'))
model = load_model('chatbotmodel.h5')
def clean_up_sentence(sentence):
sentence_words = nltk.word_tokenize(sentence)
sentence_words = [lemmatizer.lemmatize(word) for word in sentence_words]
return sentence_words
def bag_of_words(sentence):
sentence_words = clean_up_sentence(sentence)
bag = [0] *len(words)
for w in sentence_words:
for i, word in enumerate(words):
if word == w:
bag[i] = 1
return np.array(bag)
def predict_class(sentence):
bow = bag_of_words(sentence)
res = model.predict(np.array([bow]))[0]
ERROR_TRESHOLD = 0.15
results = [[i, r] for i, r in enumerate(res) if r > ERROR_TRESHOLD]
results.sort(key=lambda x: x[1], reverse=True)
return_list = []
for r in results:
return_list.append({'intent': classes[r[0]], 'probability': str(r[1])})
return return_list
def get_response(intents_list, intents_json):
tag = intents_list[0]['intent']
list_of_intents = intents_json['intents']
for i in list_of_intents:
if i['tag'] == tag:
result = random.choice(i['responses'])
break
return result
def analyze_result():
if res[0] == '/':
if res == '/HOUR':
print(time.asctime())
elif res == '/JOKE':
print("Of course! Here's one for you! :")
print(pyjokes.get_joke(language="en",category="all"))
elif res == '/REPEAT':
res = old_res
print(res)
else:
old_res = res
print(res)
print('Say hi to Chat-RPI!')
while True:
message = input("")
ints = predict_class(message)
res = get_response(ints, intents)
analyze_result()
намерения.json:
{
"intents": [
{
"tag": "greetings",
"patterns": [
"hello",
"hey",
"hi",
"good day",
"Greetings",
"what's up?",
"how is it going?"
],
"responses": [
"Hello!",
"Hey!",
"What can I do for you?",
"Hey dude"
]
},
{
"tag": "goodbye",
"patterns": [
"cya",
"See you later",
"Goodbye",
"I am leaving",
"Have a good day",
"bye",
"see ya"
],
"responses": [
"Oh, okay!",
"Bye my dude!",
"Beep Boop Boop Beep?",
"see ya",
"Goodbye!",
"Sad to see you go :/"
]
},
{
"tag": "age",
"patterns": [
"How old are you?",
"Your age?",
"What's your age",
"age?",
"How many years do you have?",
"Are you inmortal?",
"age"
],
"responses": [
"I don't know!",
"!, It's a secret!"
]
},
{
"tag": "name",
"patterns": [
"What is your name?",
"What's your name",
"what is your name?",
"name",
"name?",
"How are you called",
"Which is your name?",
"Who are you?",
"Who's there?"
],
"responses": [
"My name is Chat-RPI!",
"Really? I'm Chat-RPI",
"Chat-RPI",
"My friends call me Chat-RPI"
]
},
{
"tag": "like_anime",
"patterns": [
"do you like anime?",
"Do you like manga",
"manga is nice",
"do you love anime ",
"Did you watch hetalia?",
"manga?",
"anime?"
],
"responses": [
"Yes!",
"Of course!",
"Umm... Yes, except furries",
"Hell Yes!"
]
},
{
"tag": "bestanime",
"patterns": [
"What manga/anime is the best?",
"Which manga do you prefer?",
"What manga is your favourite?",
"What anime do you like?",
"Which is your favourite manga?",
"your favourite manga?",
"your favourite anime?"
],
"responses": [
"Hetalia!",
"I love history, so I love hetalia!",
"Hetalia, but I prefer the manga",
"It's a secret, but I also like Kimetsu No Yaiba!"
]
},
{
"tag": "hour",
"patterns": [
"What hour it is?",
"What's the hour??",
"What hour is right now?",
"What hour is?",
"The hour?",
"In what moment of the day are we?",
"At which hour we are?"
],
"responses": [
"/HOUR"
]
},
{
"tag": "joke",
"patterns": [
"Tell me a joke",
"Can you tell me a joke?",
"Tell a joke",
"Say a joke, please.",
"Do you know any joke?",
"I want a joke",
"Any jokes?",
"Do you have another more?",
"Tell another"
],
"responses": [
"/JOKE"
]
},
{
"tag": "repeat_action",
"patterns": [
"Try again",
"Regenerate",
"Another time",
"Repeat",
"Can you do it another time?",
"Restart",
"Again"
],
"responses": [
"/REPEAT"
]
}
]
}
Как видите, это довольно просто, но ему удается работать на Raspberry Pi.
Я пытался использовать библиотеку под названием Transformer/s, которая использовала созданную сообществом версию Chat-GPT 2, но на Arm это не сработало.
Я рассчитываю на решение своей проблемы.