Mattdoc99 commited on
Commit
6cf8a5c
·
1 Parent(s): d80e34e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, render_template
2
+ from flask_ngrok import run_with_ngrok
3
+ from langchain.chains import llm
4
+ from ast import Index
5
+ from gpt_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
6
+ from langchain import OpenAI
7
+ import sys
8
+ import os
9
+
10
+ app = Flask(__name__)
11
+ run_with_ngrok(app)
12
+
13
+ os.environ["OPENAI_API_KEY"] = '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'
14
+
15
+ def construct_index(directory_path):
16
+ max_input_size = 4096
17
+ num_outputs = 256
18
+ max_chunk_overlap = 20
19
+ chunk_size_limit = 600
20
+
21
+ llm_predictor = LLMPredictor(llm=OpenAI(temperature=0, model_name="text-davinci-003", max_tokens=num_outputs))
22
+ prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
23
+
24
+ documents = SimpleDirectoryReader(directory_path).load_data()
25
+
26
+ index = GPTSimpleVectorIndex(
27
+ documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper, verbose=True
28
+
29
+ )
30
+
31
+ index.save_to_disk('index.json')
32
+
33
+ return index
34
+
35
+ book_path = input("Enter the path to the directory containing your documents: ")
36
+ index = construct_index(book_path)
37
+
38
+ # Initialize the history list
39
+ history = []
40
+
41
+ @app.route('/', methods=['GET', 'POST'])
42
+ def chatbot():
43
+ if request.method == 'POST':
44
+ query = request.form['query']
45
+ best_answer = index.query(query, response_mode="compact", verbose=True)
46
+
47
+ # Add the current query and response to the history list
48
+ history.append((query, best_answer))
49
+
50
+ # Reverse the order of the history list so that the most recent queries are at the top
51
+ history.reverse()
52
+
53
+ return render_template('index.html', answer=best_answer, history=history)
54
+
55
+ # Reverse the order of the history list so that the most recent queries are at the top
56
+ history.reverse()
57
+
58
+ return render_template('index.html', history=history)
59
+
60
+ if __name__ == '__main__':
61
+ app.run()