|
6 | 6 | import re
|
7 | 7 | import logging
|
8 | 8 | import sys
|
| 9 | +import pandas as pd |
9 | 10 |
|
10 | 11 | from collections import deque
|
11 | 12 |
|
@@ -56,6 +57,33 @@ def allowed_file(filename):
|
56 | 57 | return True
|
57 | 58 |
|
58 | 59 |
|
| 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 | + |
59 | 87 | async def get_code(user_prompt, user_openai_key=None, model="gpt-3.5-turbo"):
|
60 | 88 |
|
61 | 89 | prompt = f"""First, here is a history of what I asked you to do earlier.
|
@@ -253,8 +281,10 @@ def upload_file():
|
253 | 281 | if file.filename == '':
|
254 | 282 | return jsonify({'error': 'No selected file'}), 400
|
255 | 283 | 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 |
258 | 288 | else:
|
259 | 289 | return jsonify({'error': 'File type not allowed'}), 400
|
260 | 290 |
|
|
0 commit comments