-
Notifications
You must be signed in to change notification settings - Fork 0
/
app copy.py
94 lines (76 loc) · 3.2 KB
/
app copy.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
90
91
92
93
94
from flask import Flask, render_template, request, session
from flask_socketio import SocketIO, emit
import utils
import openai
import os
from werkzeug.utils import secure_filename
from io import BytesIO
import base64
import eventlet
app = Flask(__name__)
app.secret_key = 'secret_key'
socketio = SocketIO(app, cors_allowed_origins="*")
# Log initialization
app.logger.info("App initialized")
model = "gpt-3.5-turbo-16k"
openai.organization = os.environ.get('OPENAI_ORG')
UPLOAD_FOLDER = os.getcwd() + '/temp_uploads'
if not os.path.isdir(UPLOAD_FOLDER):
os.mkdir(UPLOAD_FOLDER)
ALLOWED_EXTENSIONS = {'pdf'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('process_file')
def handle_file(data):
try:
app.logger.info("File processing started")
file_data = base64.b64decode(data['file'])
file_name = data['filename']
if not file_data or not allowed_file(file_name):
emit('file_response', {'error': 'Invalid file.'})
return
file = BytesIO(file_data)
# save to temporary folder
filename = secure_filename(file_name)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
with open(filepath, 'wb') as f:
f.write(file.read())
# Removing all the processing code for now for debugging #
#loader = utils.DocumentLoader(filepath)
#document = loader.load_document()
#processing = utils.OpenAIInterface(model)
#overview = processing.generate_overview(document)
#responses = processing.generate_response(document)
#stylized_output = processing.stylize_output(overview, responses, filename)
# Delete the file after processing
os.remove(filepath)
# Save the stylized_output in the session
session['stylized_output'] = filename
session['filename_for_download'] = filename + ".html"
emit('file_response', {'content': filename, 'filename': filename + ".html"})
app.logger.info("File processing complete")
except Exception as e:
emit('file_response', {'error': f"Server error: {str(e)}"})
app.logger.error(f"Error during file processing: {str(e)}")
@socketio.on('download_file')
def handle_download(data):
try:
app.logger.info("File download initiated")
buffer = BytesIO()
stylized_output = session.get('stylized_output', "")
filename_for_download = session.get('filename_for_download', "output.html")
buffer.write(stylized_output.encode('utf-8'))
buffer.seek(0)
# Convert buffer to base64 and send to client
base64_data = base64.b64encode(buffer.getvalue()).decode('utf-8')
emit('download_response', {'file_data': base64_data, 'filename': filename_for_download})
app.logger.info("File download complete")
except Exception as e:
emit('download_response', {'error': f"Server error: {str(e)}"})
app.logger.error(f"Error during file download: {str(e)}")
if __name__ == "__main__":
socketio.run(app, debug=True)