File size: 2,091 Bytes
bfd0858
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e52d66
 
bfd0858
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e52d66
bfd0858
3e52d66
bfd0858
 
 
 
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
82
83
84
85
86
import os
import sys
import time
import pickle
import openai
import configparser
from flask import Flask, render_template, request, redirect, url_for
dir_path = os.path.abspath(os.getcwd())

src_path = dir_path + "/src"
sys.path.append(src_path)

COMPLETIONS_MODEL = "gpt-3.5-turbo"
EMBEDDING_MODEL = "text-embedding-ada-002"
config_dir = dir_path + "/src/utils"
config = configparser.ConfigParser()
config.read(os.path.join(config_dir, 'gpt_local_config.cfg'))
# openai.api_key = config.get('token', 'GPT_TOKEN')
openai.api_key = os.environ.get("GPT_TOKEN")

import embedding_qa as emq

# Specify the path to your pickle file
pickle_file_path = 'caNano_embedding_pack_5_14.pickle'

# Load the pickle file
with open(pickle_file_path, 'rb') as file:
    loaded_data = pickle.load(file)

document_df = loaded_data['df']
document_embedding = loaded_data['embedding']

COMPLETIONS_API_PARAMS = {
    # We use temperature of 0.0 because it gives the
    # most predictable, factual answer.
    "temperature": 0.0,
    "max_tokens": 4000,
    "model": "gpt-3.5-turbo"
}

app = Flask("caNanoWiki_AI")

# Set the passcode for authentication
PASSCODE_auth = ""

# Define a variable to track if the user is authenticated
authenticated = False
last_activity_time = 0

# Timeout duration in seconds
timeout_duration = 5 * 60

# Session Length
session_duration = 30 * 60


@app.template_filter('nl2br')
def nl2br_filter(s):
    return s.replace('\n', '<br>')


@app.route('/', methods=['GET', 'POST'])
def index():
    user_input = ""
    processed_input = None
    if request.method == 'POST':
        user_input = request.form['user_input']

        processed_input, chosen_sec_idxes = emq.answer_query_with_context(
            user_input,
            document_df,
            document_embedding
        )

        return render_template(
            'index.html',
            processed_input=processed_input,
            source_sections=chosen_sec_idxes,
            user_input=user_input)

    return render_template('index.html')


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)