-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
51 lines (44 loc) · 1.7 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from flask import Flask
from flask import render_template,jsonify,request
import requests
from engine import *
import random
import responses
app = Flask(__name__)
app.secret_key = '12345'
@app.route('/')
def hello_world():
return render_template('home.html')
get_random_response = lambda intent:random.choice(intent_response_dict[intent])
@app.route('/chat',methods=["POST"])
def chat():
try:
user_message = request.form["text"]
response = requests.get("http://localhost:5000/parse",params={"q":user_message})
response = response.json()
entities = response.get("entities")
topresponse = response["intent"]
intent = topresponse.get("name")
print("Intent {}, Entities {}".format(intent,entities))
if intent == "best_phone":
response_text = responses.getBestPhone(entities).title()
elif intent == "camera_phone":
response_text = responses.getBestCameraPhone(entities).title()
elif intent == "ram_internal":
response_text = "Ram and internal intent was called"
elif intent == "battery_phone":
response_text = responses.getBatterPhone().title()
elif intent == "user_id":
file=open("user.txt","w")
file.write(entities[0]["value"])
file.close()
response_text = "Hello Prashant!"
else:
response_text = get_random_response(intent)
return jsonify({"status":"success","response":response_text})
except Exception as e:
print(e)
return jsonify({"status":"success","response":"Sorry I am not trained to do that yet..."})
app.config["DEBUG"] = True
if __name__ == "__main__":
app.run(port=8080)