-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathapp.py
41 lines (28 loc) · 1.02 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
import os
import sys
from uuid import uuid4
from chat import ask_question
from flask import Flask, Response, jsonify, request
from flask_cors import CORS
app = Flask(__name__, static_folder="../frontend/build", static_url_path="/")
CORS(app)
@app.route("/")
def api_index():
return app.send_static_file("index.html")
@app.route("/api/chat", methods=["POST"])
def api_chat():
request_json = request.get_json()
question = request_json.get("question")
if question is None:
return jsonify({"msg": "Missing question from request JSON"}), 400
session_id = request.args.get("session_id", str(uuid4()))
return Response(ask_question(question, session_id), mimetype="text/event-stream")
@app.cli.command()
def create_index():
"""Create or re-create the Elasticsearch index."""
basedir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(f"{basedir}/../")
from data import index_data
index_data.main()
if __name__ == "__main__":
app.run(host="0.0.0.0", port=4000, debug=False)