datasciencedojo commited on
Commit
0eba60f
·
1 Parent(s): 29d905a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import nltk
2
+ nltk.download('punkt')
3
+
4
+ import nltk
5
+ from nltk.stem.lancaster import LancasterStemmer
6
+ import numpy as np
7
+ import tflearn
8
+ import tensorflow
9
+ import random
10
+ import json
11
+ import pandas as pd
12
+ import pickle
13
+ import gradio as gr
14
+
15
+ stemmer = LancasterStemmer()
16
+
17
+ with open("intents.json") as file:
18
+ data = json.load(file)
19
+
20
+ with open("data.pickle", "rb") as f:
21
+ words, labels, training, output = pickle.load(f)
22
+
23
+ net = tflearn.input_data(shape=[None, len(training[0])])
24
+ net = tflearn.fully_connected(net, 8)
25
+ net = tflearn.fully_connected(net, 8)
26
+ net = tflearn.fully_connected(net, len(output[0]), activation="softmax")
27
+ net = tflearn.regression(net)
28
+
29
+ model = tflearn.DNN(net)
30
+ model.load("MentalHealthChatBotmodel.tflearn")
31
+ # print('model loaded successfully')
32
+
33
+
34
+ def chat(message, history):
35
+ history = history or []
36
+ message = message.lower()
37
+ results = model.predict([bag_of_words(message, words)])
38
+ results_index = np.argmax(results)
39
+ tag = labels[results_index]
40
+
41
+ for tg in data["intents"]:
42
+ if tg['tag'] == tag:
43
+ responses = tg['responses']
44
+
45
+ # print(random.choice(responses))
46
+ response = random.choice(responses)
47
+
48
+ history.append((message, response))
49
+ return history, history
50
+
51
+ chatbot = gr.Chatbot().style(color_map=("green", "pink"))
52
+ demo = gr.Interface(
53
+ chat,
54
+ ["text", "state"],
55
+ [chatbot, "state"],
56
+ allow_flagging="never",
57
+ )
58
+ if __name__ == "__main__":
59
+ demo.launch()