File size: 2,078 Bytes
90653e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# import os
# from datetime import datetime

# from flask import Flask, redirect, render_template, request, url_for
# from model import initialize_index

# app = Flask(__name__)
# index = None

# @app.route("/")
# def home():
#     return render_template('chat.html')

# @app.route("/get")
# def get_bot_response():
#     query_text = request.args.get("msg", None)
#     if query_text is None:
#         return "Invalid input"
#     response = index.query(query_text)
#     return str(response), 200

# if __name__ == "__main__":
#     index = initialize_index("index.json")
#     app.run(debug=True)
import os
from datetime import datetime

import openai
from flask import Flask, redirect, render_template, request, session, url_for
from model import initialize_index

app = Flask(__name__)
app.secret_key = os.environ.get('SECRET_KEY', 'my-secret-key')
index = None

# Set up OpenAI authentication
# openai.api_key = os.environ.get('OPENAI_API_KEY')

@app.route("/")
def home():
    if 'access_token' not in session:
        return render_template('auth.html')
    global index
    try: 
        index = initialize_index("index.json")
    except:
        return render_template("auth.html", error="Invalid Token Key")
    return render_template('chat.html')


@app.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "POST":
        username = request.form.get("username")
        access_token = request.form.get("access_token")
        openai.api_key = access_token
        session['access_token'] = access_token
        return redirect(url_for('home'))
    return render_template('auth.html')

@app.route("/logout")
def logout():
    session.pop('access_token', None)
    return redirect(url_for('home'))

@app.route("/chat")
def chat():
    query_text = request.args.get("msg", None)
    if query_text is None:
        return "Invalid input"

    if 'access_token' not in session:
        return redirect(url_for('login'))

    response = index.query(query_text)
    return str(response), 200


if __name__ == "__main__":
    app.run(debug=True)