瀏覽代碼

Update 'chatbot/app.py'

SadhulaSaiKumar 1 年之前
父節點
當前提交
074f6cdbfb
共有 1 個檔案被更改,包括 51 行新增120 行删除
  1. 51
    120
      chatbot/app.py

+ 51
- 120
chatbot/app.py 查看文件

@@ -1,121 +1,52 @@
1
-import requests
2
-from flask import Flask, request, jsonify,render_template
3
-from flask_cors import CORS
4
-
5
-app = Flask(__name__)
6
-
7
-
8
-
9
-CORS(app)
10
-
11
-# import nltk
12
-# nltk.download('popular')
13
-# from nltk.stem import WordNetLemmatizer
14
-# lemmatizer = WordNetLemmatizer()
15
-# import pickle
16
-# import numpy as np
17
-
18
-# from keras.models import load_model
19
-# model = load_model('model.h5')
20
-# import json
21
-# import random
22
-# intents = json.loads(open('data.json').read())
23
-# words = pickle.load(open('texts.pkl','rb'))
24
-# classes = pickle.load(open('labels.pkl','rb'))
25
-
26
-# def clean_up_sentence(sentence):
27
-#     # tokenize the pattern - split words into array
28
-#     sentence_words = nltk.word_tokenize(sentence)
29
-#     # stem each word - create short form for word
30
-#     sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words]
31
-#     return sentence_words
32
-
33
-# # return bag of words array: 0 or 1 for each word in the bag that exists in the sentence
34
-
35
-# def bow(sentence, words, show_details=True):
36
-#     # tokenize the pattern
37
-#     sentence_words = clean_up_sentence(sentence)
38
-#     # bag of words - matrix of N words, vocabulary matrix
39
-#     bag = [0]*len(words)  
40
-#     for s in sentence_words:
41
-#         for i,w in enumerate(words):
42
-#             if w == s: 
43
-#                 # assign 1 if current word is in the vocabulary position
44
-#                 bag[i] = 1
45
-#                 if show_details:
46
-#                     print ("found in bag: %s" % w)
47
-#     return(np.array(bag))
48
-
49
-# def predict_class(sentence, model):
50
-#     # filter out predictions below a threshold
51
-#     p = bow(sentence, words,show_details=False)
52
-#     res = model.predict(np.array([p]))[0]
53
-#     ERROR_THRESHOLD = 0.25
54
-#     results = [[i,r] for i,r in enumerate(res) if r>ERROR_THRESHOLD]
55
-#     # sort by strength of probability
56
-#     results.sort(key=lambda x: x[1], reverse=True)
57
-#     return_list = []
58
-#     for r in results:
59
-#         return_list.append({"intent": classes[r[0]], "probability": str(r[1])})
60
-#     return return_list
61
-
62
-# def getResponse(ints, intents_json):
63
-#     tag = ints[0]['intent']
64
-#     list_of_intents = intents_json['intents']
65
-#     for i in list_of_intents:
66
-#         if(i['tag']== tag):
67
-#             result = random.choice(i['responses'])
68
-#             break
69
-#     return result
70
-
71
-# def chatbot_response(msg):
72
-#     ints = predict_class(msg, model)
73
-#     res = getResponse(ints, intents)
74
-#     return res
75
-
76
-
77
-
78
-app.static_folder = 'static'
79
-
80
-
81
-
82
-# Define the Rasa server URL
83
-rasa_server_url = "http://localhost:5005/webhooks/rest/webhook"
84
-
85
-@app.route("/")
86
-def home():
87
-    return render_template("index.html")
88
-
89
-
90
-@app.route('/webhook', methods=['POST','GET'])
91
-def webhook():
92
-    message = request.json['message']
93
-   # message =request.args.get('msg')
94
-
95
-    # Send the message to the Rasa server
96
-    rasa_response = requests.post(rasa_server_url, json={"message": message}).json()
97
- 
98
-    # for d in rasa_response:
99
-    #     a=d["text"]
100
-
101
-    # Return the Rasa response as a JSON object
102
-    print(rasa_response)
103
-    if len(rasa_response)==0:
104
-        return jsonify([
105
-    {
106
-        "recipient_id": "default",
107
-        "text": "I'm sorry, I didn't understand that. Can you please rephrase?"
108
-    }])
109
-    else:
110
-        return jsonify(rasa_response)
111
-
112
-
113
-
114
-@app.route("/get")
115
-def get_bot_response():
116
-    userText = request.args.get('msg')
117
-    return chatbot_response(userText)
118
-
119
-
120
-if __name__ == '__main__':
1
+import requests
2
+from flask import Flask, request, jsonify,render_template
3
+from flask_cors import CORS
4
+import time
5
+import pandas as pd
6
+app = Flask(__name__)
7
+
8
+CORS(app)
9
+
10
+app.static_folder = 'static'
11
+
12
+# Define the Rasa server URL
13
+rasa_server_url = "http://localhost:5005/webhooks/rest/webhook"
14
+
15
+@app.route("/")
16
+def home():
17
+    return render_template("index.html")
18
+
19
+
20
+@app.route('/webhook', methods=['POST','GET'])
21
+def webhook():
22
+    user_ip = request.remote_addr 
23
+    print("user ip is :",user_ip)
24
+    message = request.json['message']
25
+   
26
+   
27
+    #recipient_id = request.json['recipient_id']
28
+
29
+    # Send the message to the Rasa server
30
+    rasa_response = requests.post(rasa_server_url, json={"message": message,"sender":user_ip}).json()
31
+ 
32
+    # Return the Rasa response as a JSON object
33
+    print(rasa_response)
34
+    if len(rasa_response)==0:
35
+        return jsonify([
36
+    {
37
+        "recipient_id": "default",
38
+        "text": "I'm sorry, I didn't understand that. Can you please rephrase?"
39
+    }])
40
+
41
+    else:
42
+        return jsonify(rasa_response)
43
+
44
+
45
+@app.route("/get")
46
+def get_bot_response():
47
+    userText = request.args.get('msg')
48
+    return chatbot_response(userText)
49
+
50
+
51
+if __name__ == '__main__':
121 52
     app.run(host='0.0.0.0',port=5020)

Loading…
取消
儲存