-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
36 lines (31 loc) · 1.01 KB
/
Copy pathapp.py
File metadata and controls
36 lines (31 loc) · 1.01 KB
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
from flask import Flask, render_template, request, jsonify
import mysql.connector
app = Flask(__name__)
db = mysql.connector.connect(
host="localhost",
user="root",
password="root",
database="jey"
)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/get_answer')
def get_answer():
question = request.args.get('question')
try:
cursor = db.cursor()
query = "SELECT answer FROM chatbot WHERE question LIKE %s"
cursor.execute(query, ('%' + question + '%',))
result = cursor.fetchone()
if result!=None:
return jsonify({"answer": "🤖 : " +result[0]})
else:
return jsonify({"answer": "🤖 : I don't know what you are talking about."})
except Exception as e:
return jsonify({"answer": "🤖 : I encountered an error while fetching the answer."})
finally:
if cursor:
cursor.close()
if __name__ == '__main__':
app.run(debug=True)