forked from Christian-Downs/AutoSAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (67 loc) · 2.31 KB
/
main.py
File metadata and controls
87 lines (67 loc) · 2.31 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
from flask import Flask, render_template, jsonify, request
from aiTester import caller
from fileNameParse import convert_text_to_json
from jsonToProject import jsonToProject
import fileZipOutput
from test_code import tester
import json
import shutil
import os
from werkzeug.serving import WSGIRequestHandler
from configparser import ConfigParser
# read config file with 'General' section
config = ConfigParser()
config.read('config.ini')
port = config.get('General', 'port', fallback='5000')
#Render webpage
app = Flask(__name__)
WSGIRequestHandler.timeout = 600 # 10 minutes
# Clear the log file on server restart
if os.path.exists("generation.log"):
open("generation.log", "w").close() # Open in "w" mode to overwrite with an empty file
@app.route('/')
def my_form():
return render_template('main.html')
#Send user input and execute code
@app.route('/', methods=['POST'])
def my_form_post():
output_folder = 'output'
if os.path.exists(output_folder):
shutil.rmtree(output_folder)
# Example Usage:
text = request.form.get('text')
print(request)
#Send ChatGPT user prompt and store output input.txt
caller(text)
file_path = 'input.txt'
with open(file_path, 'r') as file:
file_content = file.read()
file.close()
#conver input.txt to .json file
with open("Output.json", "w") as file:
json_data = convert_text_to_json(file_content)
file.write(json_data)
#convert .json to files
jsonToProject(str(json_data))
tester()
#convert files to zip and display back to user
fileZipOutput.folder_to_zip("output")
return fileZipOutput.send_to_user()
@app.route('/set_logging', methods=['POST'])
def set_log():
data = request.get_json()
if 'message' in data:
with open("generation.log", "a") as f: # Append mode
f.write(data['message'] + "\n") # Append a newline for separation
return jsonify({"status": "success"})
return jsonify({"status": "error"}), 400
@app.route('/update_logging')
def get_log():
try:
with open("generation.log", "r") as f:
message = f.read()
except FileNotFoundError:
message = "No log available."
return jsonify({"message": message})
if __name__ == '__main__':
app.run(debug=True, use_reloader=False, port=int(port))