camphong24032002 commited on
Commit
d176aff
·
1 Parent(s): 903d71a
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from sentence_transformers import SentenceTransformer, util
3
+ from flask import Flask, render_template, request, jsonify
4
+ from nltk.corpus import stopwords
5
+ stop = stopwords.words('english')
6
+
7
+
8
+ def text_preprocessing(text):
9
+ # make all words with lower letters
10
+ text = text.lower()
11
+ # getting rid of any punctution
12
+ # text = text.replace('http\S+|www.\S+|@|%|:|,|', '', case=False)
13
+ # spliting each sentence to words to apply previous funtions on them
14
+ word_tokens = text.split(' ')
15
+ keywords = [item for item in word_tokens if item not in stop]
16
+ # assemble words of each sentence again and assign them in new column
17
+
18
+ return ' '.join(keywords)
19
+
20
+ def df_to_text(df):
21
+ text = []
22
+ for data in df:
23
+ text.append(f"Product ID: {data["ProductID"]} \n \
24
+ Product Name: {data["ProductName"]} \n \
25
+ Brand: {data["ProductBrand"]} \n \
26
+ Gender: {data["Gender"]} \n \
27
+ Price (INR): {data["Price (INR)"]} \n \
28
+ Description: {data["Description"]} \n \
29
+ Primary color: {data["PrimaryColor"]}")
30
+ return '\n\n'.join(text)
31
+
32
+
33
+ df = pd.read_csv("data/dataset.csv").reset_index(drop=True)
34
+ embedding_df = pd.read_csv("data/embedding.csv", header=None)
35
+ docs = embedding_df.values
36
+ text = input("Your search: ")
37
+ # text = "a white shirt for men"
38
+
39
+ model = SentenceTransformer("bert-base-nli-mean-tokens")
40
+
41
+
42
+ app = Flask(__name__)
43
+
44
+
45
+ @app.route("/")
46
+ def index():
47
+ return render_template("chat.html")
48
+
49
+
50
+ @app.route("/chat", methods=["POST"])
51
+ def chat():
52
+ data = request.get_json()
53
+ msg = data.get("msg")
54
+ try:
55
+ output_df = get_chat_response(msg)
56
+ output_text = df_to_text(output_df)
57
+ return jsonify({"response": True, "message": output_text})
58
+ except Exception as e:
59
+ print(e)
60
+ error_message = f'Error: {str(e)}'
61
+ return jsonify({"message": error_message, "response": False})
62
+
63
+
64
+ def get_chat_response(text):
65
+ query_vector = model.encode(text_preprocessing(text)).astype(float)
66
+ results = util.pytorch_cos_sim(query_vector, docs)
67
+ top_n = 3
68
+ sort_idx = results.argsort(descending=True, axis=1)[0][:top_n]
69
+ return df.iloc[sort_idx]
70
+
71
+
72
+ if __name__ == "__main__":
73
+ app.run(debug=True)
data/dataset.csv ADDED
The diff for this file is too large to render. See raw diff
 
data/embedding.csv ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ flask
2
+ torch==2.0.0
3
+ sentence_transformers
4
+ nltk
static/css/style.css ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ * {
2
+ box-sizing: border-box;
3
+ }
4
+
5
+ body {
6
+ background-color: #edeff2;
7
+ font-family: "Calibri", "Roboto", sans-serif;
8
+ }
9
+
10
+ .chat_window {
11
+ position: absolute;
12
+ width: calc(100% - 20px);
13
+ max-width: 800px;
14
+ height: 500px;
15
+ border-radius: 10px;
16
+ background-color: #fff;
17
+ left: 50%;
18
+ top: 50%;
19
+ transform: translateX(-50%) translateY(-50%);
20
+ box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
21
+ background-color: #f8f8f8;
22
+ overflow: hidden;
23
+ }
24
+
25
+ .top_menu {
26
+ background-color: #fff;
27
+ width: 100%;
28
+ padding: 20px 0 15px;
29
+ box-shadow: 0 1px 30px rgba(0, 0, 0, 0.1);
30
+ }
31
+ .top_menu .buttons {
32
+ margin: 3px 0 0 20px;
33
+ position: absolute;
34
+ }
35
+ .top_menu .buttons .button {
36
+ width: 16px;
37
+ height: 16px;
38
+ border-radius: 50%;
39
+ display: inline-block;
40
+ margin-right: 10px;
41
+ position: relative;
42
+ }
43
+ .top_menu .buttons .button.close {
44
+ background-color: #f5886e;
45
+ }
46
+ .top_menu .buttons .button.minimize {
47
+ background-color: #fdbf68;
48
+ }
49
+ .top_menu .buttons .button.maximize {
50
+ background-color: #a3d063;
51
+ }
52
+ .top_menu .title {
53
+ text-align: center;
54
+ color: #bcbdc0;
55
+ font-size: 20px;
56
+ }
57
+
58
+ .messages {
59
+ position: relative;
60
+ list-style: none;
61
+ padding: 20px 10px 0 10px;
62
+ margin: 0;
63
+ height: 347px;
64
+ overflow: scroll;
65
+ }
66
+ .messages .message {
67
+ clear: both;
68
+ overflow: hidden;
69
+ margin-bottom: 20px;
70
+ transition: all 0.5s linear;
71
+ opacity: 0;
72
+ }
73
+ .messages .message.left .avatar {
74
+ background-color: #f5886e;
75
+ float: left;
76
+ }
77
+ .messages .message.left .text_wrapper {
78
+ background-color: #ffe6cb;
79
+ margin-left: 20px;
80
+ }
81
+ .messages .message.left .text_wrapper::after, .messages .message.left .text_wrapper::before {
82
+ right: 100%;
83
+ border-right-color: #ffe6cb;
84
+ }
85
+ .messages .message.left .text {
86
+ color: #c48843;
87
+ }
88
+ .messages .message.right .avatar {
89
+ background-color: #fdbf68;
90
+ float: right;
91
+ }
92
+ .messages .message.right .text_wrapper {
93
+ background-color: #c7eafc;
94
+ margin-right: 20px;
95
+ float: right;
96
+ }
97
+ .messages .message.right .text_wrapper::after, .messages .message.right .text_wrapper::before {
98
+ left: 100%;
99
+ border-left-color: #c7eafc;
100
+ }
101
+ .messages .message.right .text {
102
+ color: #45829b;
103
+ }
104
+ .messages .message.appeared {
105
+ opacity: 1;
106
+ }
107
+ .messages .message .avatar {
108
+ width: 60px;
109
+ height: 60px;
110
+ border-radius: 50%;
111
+ display: inline-block;
112
+ }
113
+ .messages .message .text_wrapper {
114
+ display: inline-block;
115
+ padding: 20px;
116
+ border-radius: 6px;
117
+ width: calc(100% - 85px);
118
+ min-width: 100px;
119
+ position: relative;
120
+ }
121
+ .messages .message .text_wrapper::after, .messages .message .text_wrapper:before {
122
+ top: 18px;
123
+ border: solid transparent;
124
+ content: " ";
125
+ height: 0;
126
+ width: 0;
127
+ position: absolute;
128
+ pointer-events: none;
129
+ }
130
+ .messages .message .text_wrapper::after {
131
+ border-width: 13px;
132
+ margin-top: 0px;
133
+ }
134
+ .messages .message .text_wrapper::before {
135
+ border-width: 15px;
136
+ margin-top: -2px;
137
+ }
138
+ .messages .message .text_wrapper .text {
139
+ font-size: 18px;
140
+ font-weight: 300;
141
+ }
142
+
143
+ .bottom_wrapper {
144
+ position: relative;
145
+ width: 100%;
146
+ background-color: #fff;
147
+ padding: 20px 20px;
148
+ position: absolute;
149
+ bottom: 0;
150
+ }
151
+ .bottom_wrapper .message_input_wrapper {
152
+ display: inline-block;
153
+ height: 50px;
154
+ border-radius: 25px;
155
+ border: 1px solid #bcbdc0;
156
+ width: calc(100% - 160px);
157
+ position: relative;
158
+ padding: 0 20px;
159
+ }
160
+ .bottom_wrapper .message_input_wrapper .message_input {
161
+ border: none;
162
+ height: 100%;
163
+ box-sizing: border-box;
164
+ width: calc(100% - 40px);
165
+ position: absolute;
166
+ outline-width: 0;
167
+ color: gray;
168
+ }
169
+ .bottom_wrapper .send_message {
170
+ width: 140px;
171
+ height: 50px;
172
+ display: inline-block;
173
+ border-radius: 50px;
174
+ background-color: #a3d063;
175
+ border: 2px solid #a3d063;
176
+ color: #fff;
177
+ cursor: pointer;
178
+ transition: all 0.2s linear;
179
+ text-align: center;
180
+ float: right;
181
+ }
182
+ .bottom_wrapper .send_message:hover {
183
+ color: #a3d063;
184
+ background-color: #fff;
185
+ }
186
+ .bottom_wrapper .send_message .text {
187
+ font-size: 18px;
188
+ font-weight: 300;
189
+ display: inline-block;
190
+ line-height: 48px;
191
+ }
192
+
193
+ .message_template {
194
+ display: none;
195
+ }
196
+
static/js/script.js ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (function () {
2
+ let Message;
3
+ Message = function (arg) {
4
+ this.text = arg.text, this.message_side = arg.message_side;
5
+ this.draw = function (_this) {
6
+ return function () {
7
+ let $message;
8
+ $message = $($('.message_template').clone().html());
9
+ $message.addClass(_this.message_side).find('.text').html(_this.text);
10
+ $('.messages').append($message);
11
+ return setTimeout(function () {
12
+ return $message.addClass('appeared');
13
+ }, 0);
14
+ };
15
+ }(this);
16
+ return this;
17
+ };
18
+
19
+ let get_response = async(msg) => {
20
+ const response = await fetch("http://127.0.0.1:5000/chat", {
21
+ method: 'POST',
22
+ headers: {
23
+ 'Content-Type': 'application/json',
24
+ },
25
+ body: JSON.stringify({ msg: msg })
26
+
27
+
28
+ });
29
+ let json = await response.json();
30
+ let message = json.message;
31
+ return message.toString();
32
+
33
+ };
34
+ var Response;
35
+ Response = async function(arg) {
36
+ this.msg = arg.msg;
37
+ sendMessage(this.msg, 'right');
38
+ bot_res = await get_response(this.msg);
39
+ sendMessage(bot_res, 'left');
40
+ };
41
+
42
+ let sendMessage;
43
+ sendMessage = function (text, side) {
44
+ var $messages, message;
45
+ if (text === '') {
46
+ return;
47
+ }
48
+ $('.message_input').val('');
49
+ $messages = $('.messages');
50
+ let message_side;
51
+ message_side = message_side === 'left' ? 'right' : 'left';
52
+ message = new Message({
53
+ text: text,
54
+ message_side: side
55
+ });
56
+ message.draw();
57
+ return $messages.animate({ scrollTop: $messages.prop('scrollHeight') }, 300);
58
+ };
59
+ $(function () {
60
+ var getMessageText;
61
+ message_side = 'right';
62
+ getMessageText = function () {
63
+ var $message_input;
64
+ $message_input = $('.message_input');
65
+ return $message_input.val();
66
+ };
67
+
68
+ let msg;
69
+ $('.send_message').click(function (e) {
70
+ msg = getMessageText();
71
+ return Response({"msg": msg});
72
+ });
73
+ $('.message_input').keyup(function (e) {
74
+ if (e.which === 13) {
75
+ msg = getMessageText();
76
+ return Response({"msg": msg});
77
+ }
78
+ });
79
+ sendMessage('Hi there', 'left');
80
+ });
81
+ }.call(this));
templates/chat.html ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
7
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
8
+ <title>Autochat Bot</title>
9
+ <link href="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.6.5/flowbite.min.css" rel="stylesheet" />
10
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.6.5/flowbite.min.js"></script>
11
+ <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
12
+ <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
13
+ <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
14
+ <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
15
+ <script src="{{ url_for('static', filename='js/script.js') }}"></script>
16
+ </head>
17
+
18
+ <body>
19
+
20
+ <!------ Include the above in your HEAD tag ---------->
21
+ <div class="chat_window">
22
+ <div class="top_menu">
23
+ <div class="buttons">
24
+ <div class="button close"></div>
25
+ <div class="button minimize"></div>
26
+ <div class="button maximize"></div>
27
+ </div>
28
+ <div class="title">Chat</div>
29
+ </div>
30
+ <ul class="messages"></ul>
31
+ <div class="bottom_wrapper clearfix">
32
+ <div class="message_input_wrapper">
33
+ <input class="message_input" placeholder="Type your message here..." />
34
+ </div>
35
+ <div class="send_message">
36
+ <div class="icon"></div>
37
+ <div class="text">Send</div>
38
+ </div>
39
+ </div>
40
+ </div>
41
+ <div class="message_template">
42
+ <li class="message">
43
+ <div class="avatar"></div>
44
+ <div class="text_wrapper">
45
+ <div class="text"></div>
46
+ </div>
47
+ </li>
48
+ </div>
49
+
50
+ </body>
51
+
52
+ </html>