-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.py
34 lines (26 loc) · 1.08 KB
/
chatbot.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
from flask import Flask, render_template, request, jsonify
from qamodel import ScienceChatBot
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
"""
Main endpoint for user interaction.
Methods:
- GET: Renders the HTML template for the chatbot interface.
- POST: Processes user input, predicts answers, and returns the responses in JSON format.
Returns:
- GET: HTML template.
- POST: JSON response with predicted answers.
"""
SEPERATOR = '\n'
if request.method == 'POST':
# Converting the string input into a list of strings (questions)
user_message = str(request.json['user_message'])
user_message = user_message.split(SEPERATOR)
# Instantiating the ScienceChatBot class to predict the answers
chatbot = ScienceChatBot()
answers = chatbot.predict_answer(user_message)
# Returning json response to be rendered in the chatbot
chatbot_response = SEPERATOR.join(answers)
return jsonify({'message': chatbot_response})
return render_template('index.html')