long1111 commited on
Commit
bf7e46f
1 Parent(s): 7c83e88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py CHANGED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import print_function
2
+ from config import *
3
+
4
+ import tiktoken
5
+ import pinecone
6
+ import uuid
7
+ import sys
8
+ import logging
9
+
10
+ from flask import Flask, jsonify
11
+ from flask_cors import CORS, cross_origin
12
+ from flask import request
13
+
14
+ from handle_file import handle_file
15
+ from answer_question import get_answer_from_files
16
+
17
+ logging.basicConfig(
18
+ level=logging.INFO,
19
+ format="%(asctime)s [%(levelname)s] %(message)s",
20
+ handlers=[
21
+ logging.FileHandler("debug.log"),
22
+ logging.StreamHandler(sys.stdout)
23
+ ]
24
+ )
25
+
26
+ logging.basicConfig(
27
+ level=logging.INFO,
28
+ format="%(asctime)s [%(levelname)s] %(message)s",
29
+ handlers=[
30
+ logging.FileHandler("debug.log"),
31
+ logging.StreamHandler(sys.stdout)
32
+ ]
33
+ )
34
+
35
+ def load_pinecone_index() -> pinecone.Index:
36
+ """
37
+ Load index from Pinecone, raise error if the index can't be found.
38
+ """
39
+ pinecone.init(
40
+ api_key=PINECONE_API_KEY,
41
+ environment=PINECONE_ENV,
42
+ )
43
+ index_name = PINECONE_INDEX
44
+ if not index_name in pinecone.list_indexes():
45
+ print(pinecone.list_indexes())
46
+ raise KeyError(f"Index '{index_name}' does not exist.")
47
+ index = pinecone.Index(index_name)
48
+
49
+ return index
50
+
51
+ def create_app():
52
+ pinecone_index = load_pinecone_index()
53
+ tokenizer = tiktoken.get_encoding("gpt2")
54
+ session_id = str(uuid.uuid4().hex)
55
+ app = Flask(__name__)
56
+ app.pinecone_index = pinecone_index
57
+ app.tokenizer = tokenizer
58
+ app.session_id = session_id
59
+ # log session id
60
+ logging.info(f"session_id: {session_id}")
61
+ app.config["file_text_dict"] = {}
62
+ CORS(app, supports_credentials=True)
63
+
64
+ return app
65
+
66
+ app = create_app()
67
+
68
+ @app.route(f"/process_file", methods=["POST"])
69
+ @cross_origin(supports_credentials=True)
70
+ def process_file():
71
+ try:
72
+ file = request.files['file']
73
+ logging.info(str(file))
74
+ handle_file(
75
+ file, app.session_id, app.pinecone_index, app.tokenizer)
76
+ return jsonify({"success": True})
77
+ except Exception as e:
78
+ logging.error(str(e))
79
+ return jsonify({"success": False})
80
+
81
+ @app.route(f"/answer_question", methods=["POST"])
82
+ @cross_origin(supports_credentials=True)
83
+ def answer_question():
84
+ try:
85
+ params = request.get_json()
86
+ question = params["question"]
87
+
88
+ answer_question_response = get_answer_from_files(
89
+ question, app.session_id, app.pinecone_index)
90
+ return answer_question_response
91
+ except Exception as e:
92
+ return str(e)
93
+
94
+ @app.route("/healthcheck", methods=["GET"])
95
+ @cross_origin(supports_credentials=True)
96
+ def healthcheck():
97
+ return "OK"
98
+
99
+ if __name__ == "__main__":
100
+ app.run(debug=True, port=SERVER_PORT, threaded=True)