Skip to content

Commit 7a82c76

Browse files
committed
fix up-/download by using a fixed workspace name. this is fine as long as we do not implement multi-user-support. For this, things are prepared, though.
1 parent e01f465 commit 7a82c76

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

gpt_code_ui/kernel_program/kernel_manager.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import pathlib
1010
import threading
1111
import time
12-
import atexit
1312
import traceback
1413

1514
from time import sleep
@@ -201,6 +200,7 @@ def start_kernel(id: str):
201200
"pdfminer>=20191125,<20191200",
202201
"pdfplumber>=0.9,<0.10",
203202
"matplotlib>=3.7,<3.8",
203+
"openpyxl>=3.1.2,<4",
204204
]
205205
subprocess.run([kernel_python_executable, '-m', 'pip', 'install'] + default_packages)
206206

@@ -244,9 +244,14 @@ def start_kernel(id: str):
244244

245245

246246
if __name__ == "__main__":
247-
kc, kernel_dir = start_kernel(id='ffa907c8-1908-49e3-974b-c0c27cbac6e4') # This is just a random but fixed ID for now - to be prepared for later multi-kernel scenarios
247+
try:
248+
kernel_id = sys.argv[1]
249+
except IndexError as e:
250+
logger.exception('Missing kernel ID command line parameter', e)
251+
else:
252+
kc, kernel_dir = start_kernel(id=kernel_id)
248253

249-
# make sure the dir with the virtualenv will be deleted after kernel termination
250-
atexit.register(lambda: shutil.rmtree(kernel_dir, ignore_errors=True))
254+
# make sure the dir with the virtualenv will be deleted after kernel termination
255+
atexit.register(lambda: shutil.rmtree(kernel_dir, ignore_errors=True))
251256

252-
start_snakemq(kc)
257+
start_snakemq(kc)

gpt_code_ui/kernel_program/main.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,18 @@
4747
app = Flask(__name__)
4848
CORS(app)
4949

50+
5051
def start_kernel_manager():
5152
global kernel_manager_process
5253

5354
kernel_manager_script_path = os.path.join(
5455
pathlib.Path(__file__).parent.resolve(), "kernel_manager.py"
5556
)
56-
kernel_manager_process = subprocess.Popen(
57-
[sys.executable, kernel_manager_script_path]
58-
)
57+
kernel_manager_process = subprocess.Popen([
58+
sys.executable,
59+
kernel_manager_script_path,
60+
'workspace', # This will be used as part of the folder name for the workspace and to create the venv inside. Can be anything, but using 'workspace' makes file up-/download very simple
61+
])
5962

6063
# Write PID as <pid>.pid to config.KERNEL_PID_DIR
6164
os.makedirs(config.KERNEL_PID_DIR, exist_ok=True)

gpt_code_ui/webapp/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
else:
3434
raise ValueError(f'Invalid OPENAI_API_TYPE: {openai.api_type}')
3535

36-
UPLOAD_FOLDER = 'workspace/'
36+
UPLOAD_FOLDER = 'kernel.workspace/'
3737
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
3838

3939

@@ -119,6 +119,7 @@ async def get_code(user_prompt, user_openai_key=None, model="gpt-3.5-turbo"):
119119
For data visualization, you can use
120120
'matplotlib', # matplotlib==3.7.1
121121
Be sure to generate charts with matplotlib. If you need geographical charts, use geopandas with the geopandas.datasets module.
122+
If an additional package is required, you can add the corresponding "!pip install PACKAGE" call to the beginning of the code.
122123
If the user has just uploaded a file, focus on the file that was most recently uploaded (and optionally all previously uploaded files)
123124
124125
Teacher mode: if the code modifies or produces a file, at the end of the code block insert a print statement that prints a link to it as HTML string: <a href='/download?file=INSERT_FILENAME_HERE'>Download file</a>. Replace INSERT_FILENAME_HERE with the actual filename."""
@@ -237,7 +238,7 @@ def download_file():
237238
file = request.args.get('file')
238239
# from `workspace/` send the file
239240
# make sure to set required headers to make it download the file
240-
return send_from_directory(os.path.join(os.getcwd(), 'workspace'), file, as_attachment=True)
241+
return send_from_directory(os.path.join(os.getcwd(), 'kernel.workspace'), file, as_attachment=True)
241242

242243

243244
@app.route('/inject-context', methods=['POST'])

0 commit comments

Comments
 (0)