Skip to content

Commit c1dc9f5

Browse files
committed
have a sneak preview into specific file types to be able to inject column informtion into the prompt if possible
1 parent b519dae commit c1dc9f5

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

gpt_code_ui/webapp/main.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import re
77
import logging
88
import sys
9+
import pandas as pd
910

1011
from collections import deque
1112

@@ -56,6 +57,33 @@ def allowed_file(filename):
5657
return True
5758

5859

60+
def inspect_file(filename: str) -> str:
61+
READER_MAP = {
62+
'.csv': pd.read_csv,
63+
'.tsv': pd.read_csv,
64+
'.xlsx': pd.read_excel,
65+
'.xls': pd.read_excel,
66+
'.xml': pd.read_xml,
67+
'.json': pd.read_json,
68+
'.hdf': pd.read_hdf,
69+
'.hdf5': pd.read_hdf,
70+
'.feather': pd.read_feather,
71+
'.parquet': pd.read_parquet,
72+
'.pkl': pd.read_pickle,
73+
'.sql': pd.read_sql,
74+
}
75+
76+
_, ext = os.path.splitext(filename)
77+
78+
try:
79+
df = READER_MAP[ext.lower()](filename)
80+
return f'The file contains the following columns: {", ".join(df.columns)}'
81+
except KeyError:
82+
return '' # unsupported file type
83+
except Exception:
84+
return '' # file reading failed. - Don't want to know why.
85+
86+
5987
async def get_code(user_prompt, user_openai_key=None, model="gpt-3.5-turbo"):
6088

6189
prompt = f"""First, here is a history of what I asked you to do earlier.
@@ -253,8 +281,10 @@ def upload_file():
253281
if file.filename == '':
254282
return jsonify({'error': 'No selected file'}), 400
255283
if file and allowed_file(file.filename):
256-
file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
257-
return jsonify({'message': 'File successfully uploaded'}), 200
284+
file_target = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
285+
file.save(file_target)
286+
file_info = inspect_file(file_target)
287+
return jsonify({'message': f'File {file.filename} uploaded successfully.\n{file_info}'}), 200
258288
else:
259289
return jsonify({'error': 'File type not allowed'}), 400
260290

0 commit comments

Comments
 (0)