-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
122 lines (101 loc) · 4.56 KB
/
main.py
File metadata and controls
122 lines (101 loc) · 4.56 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
Import os to access and save files. Import Flask to use its framework.
Import uuid to generate unique session IDs. Import dotenv to
use environment variables. Import 'creator' module.
"""
import glob
import os
import uuid
from flask import Flask, render_template, request, redirect, url_for, session, abort
from dotenv import load_dotenv
import helpers
# ---------------------------------------------------------------------------- #
# app setup #
# ---------------------------------------------------------------------------- #
load_dotenv()
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = './upload_folder'
UPLOAD_FOLDER = app.config['UPLOAD_FOLDER']
app.secret_key = os.getenv("FLASK_KEY")
if __name__ == '__main__':
app.run(debug=True, port=os.getenv("PORT", default=5000))
@app.route('/', methods=['GET', 'POST'])
def index():
"""Method for '/ route that handles both new
sessions and users navigating there from '/complete'"""
# tell the navbar what "Home" goes to
home_link = url_for('index')
if request.method == 'GET':
# Check if upload_folder exists, and if not, create it
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
# ask what page the user got here from
from_page = request.args.get('from_page')
# create a session ID
session_id = uuid.uuid4()
session['id'] = session_id
if from_page == 'complete':
# get the ID from the URL params
session_id = request.args.get('id')
# delete every summary and transcript with that id
summary_filelist = glob.glob(f'summary_{session_id}.json')
transcript_filelist = glob.glob(f'transcript_{session_id}.json')
helpers.delete_files(summary_filelist)
helpers.delete_files(transcript_filelist)
# delete the session ID
session.pop(session_id, None)
return render_template('index.html', home_link=home_link)
# ------ if they're hitting 'submit' ------ #
if request.method == 'POST':
print("post")
session_id = session['id']
# get the user's uploaded file
uploaded_file = request.files['file']
filename = uploaded_file.filename
file_path = os.path.join(
UPLOAD_FOLDER, filename)
# save the file in the upload folder
uploaded_file.save(file_path)
# kick off the functions to process the file
helpers.main(file_path, session_id)
# send them to '/complete'
return redirect(url_for('complete', home_link=home_link))
@app.route('/complete', methods=['GET', 'POST'])
def complete():
"""Handle displaying summary and transcript"""
session_id = session.get('id', None)
home_link = url_for('index', from_page='complete', id=session_id)
print(id)
if request.method == 'POST':
if request.form['text'] == "":
helpers.remake_summary(session_id)
short_summary = helpers.get_short_summary(session_id)
transcript = helpers.get_transcript(session_id)
prompt = helpers.get_prompt()
folder = './upload_folder'
for file in os.listdir(folder):
os.remove(os.path.join(folder, file))
return render_template('complete.html', prompt=prompt,
short_summary=short_summary,
transcript=transcript, home_link=home_link)
else:
user_prompt = request.form['text']
helpers.remake_with_new_prompt(user_prompt, session_id)
short_summary = helpers.get_short_summary(session_id)
transcript = helpers.get_transcript(session_id)
folder = './upload_folder'
for file in os.listdir(folder):
os.remove(os.path.join(folder, file))
return render_template('complete.html', prompt=user_prompt,
short_summary=short_summary,
transcript=transcript, home_link=home_link)
else:
short_summary = helpers.get_short_summary(session_id)
transcript = helpers.get_transcript(session_id)
prompt = helpers.get_prompt()
folder = './upload_folder'
for file in os.listdir(folder):
os.remove(os.path.join(folder, file))
return render_template('complete.html', prompt=prompt,
short_summary=short_summary,
transcript=transcript, home_link=home_link)