|
import nltk |
|
nltk.download('punkt') |
|
|
|
import nltk |
|
from nltk.stem.lancaster import LancasterStemmer |
|
import numpy as np |
|
import tflearn |
|
import tensorflow |
|
import random |
|
import json |
|
import pandas as pd |
|
import pickle |
|
import gradio as gr |
|
|
|
stemmer = LancasterStemmer() |
|
|
|
with open("intents.json") as file: |
|
data = json.load(file) |
|
|
|
with open("data.pickle", "rb") as f: |
|
words, labels, training, output = pickle.load(f) |
|
|
|
net = tflearn.input_data(shape=[None, len(training[0])]) |
|
net = tflearn.fully_connected(net, 8) |
|
net = tflearn.fully_connected(net, 8) |
|
net = tflearn.fully_connected(net, len(output[0]), activation="softmax") |
|
net = tflearn.regression(net) |
|
|
|
model = tflearn.DNN(net) |
|
model.load("MentalHealthChatBotmodel.tflearn") |
|
|
|
|
|
|
|
def chat(message, history): |
|
history = history or [] |
|
message = message.lower() |
|
results = model.predict([bag_of_words(message, words)]) |
|
results_index = np.argmax(results) |
|
tag = labels[results_index] |
|
|
|
for tg in data["intents"]: |
|
if tg['tag'] == tag: |
|
responses = tg['responses'] |
|
|
|
|
|
response = random.choice(responses) |
|
|
|
history.append((message, response)) |
|
return history, history |
|
|
|
chatbot = gr.Chatbot().style(color_map=("green", "pink")) |
|
demo = gr.Interface( |
|
chat, |
|
["text", "state"], |
|
[chatbot, "state"], |
|
allow_flagging="never", |
|
) |
|
if __name__ == "__main__": |
|
demo.launch() |