-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
89 lines (65 loc) · 2.75 KB
/
server.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
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
87
88
89
from flask import Flask, render_template, request
import os
from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader, download_loader
import docx2txt
os.environ["OPENAI_API_KEY"] = 'your key here'
app = Flask(__name__)
# Define the TwitterTweetReader using the download_loader function
TwitterTweetReader = download_loader("TwitterTweetReader")
# Define a function to load the Twitter data using the TwitterTweetReader
def load_twitter_data(handles):
# Replace 'YOUR_TOKEN' with your actual Twitter API bearer token
loader = TwitterTweetReader(bearer_token="your key here")
documents = loader.load_data(twitterhandles=handles)
index = GPTSimpleVectorIndex(documents)
return index
def load_folder():
documents = SimpleDirectoryReader('data').load_data()
index = GPTSimpleVectorIndex(documents)
return index
# Define the home page route
@app.route('/')
def home():
return render_template('home.html')
# Define the results page route
@app.route('/results', methods=['POST'])
def results():
# Get the list of Twitter handles from the form input
handles = request.form.getlist('handles')
# Get the search query from the form input
query = request.form['query']
# Load the Twitter data using the load_twitter_data function
index = load_twitter_data(handles)
# Perform the query on the index and extract the data
results = index.query(query)
# Render the results.html template with the search results
return render_template('results.html', results=results)
# Define the folder results page route
@app.route('/folder_results', methods=['POST'])
def folder_results():
# Get the list of Twitter handles from the form input
# Get the search query from the form input
query = request.form['folder_query']
# Load the Twitter data using the load_twitter_data function
index = load_folder()
# Perform the query on the index and extract the data
results = index.query(query)
# Render the results.html template with the search results
return render_template('folder_results.html', results=results)
## Define an upload route for the /data/ folder to upload files
@app.route('/upload', methods=['POST'])
def upload():
# Get the file from the form input
file = request.files['file']
# Save the file to the data folder
file.save(os.path.join('data', file.filename))
index = load_folder()
## Run the query on the uploaded file
# Get the search query from the form input
query = request.form['folder_query']
## Perform the query on the index and extract the data
results = index.query(query)
# Redirect to the folder_results page
return render_template('folder_results.html', results=results)
if __name__ == '__main__':
app.run(debug=True)