Spaces:
Sleeping
Sleeping
Added file_upload file
Browse files
app/api/routers/file_upload.py
CHANGED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Annotated
|
2 |
+
from fastapi import FastAPI, File, UploadFile
|
3 |
+
from app.categorization.file_processing import process_file, save_results
|
4 |
+
from app.categorization.config import CATEGORY_REFERENCE_OUTPUT_FILE
|
5 |
+
import asyncio
|
6 |
+
import os
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
@app.post("/v1/file_upload")
|
11 |
+
async def create_file(file: Annotated[bytes, File()]):
|
12 |
+
try:
|
13 |
+
with open(file.filename, 'wb') as f:
|
14 |
+
# Create directory to store all uploaded .csv files
|
15 |
+
file_upload_directory_path = "data/tx_data/input"
|
16 |
+
if not os.path.exists(file_upload_directory_path):
|
17 |
+
os.makedirs(file_upload_directory_path)
|
18 |
+
|
19 |
+
# Write items of .csv filte to directory
|
20 |
+
with open(os.path.join(file_upload_directory_path, file.filename)):
|
21 |
+
f.write(f.readlines)
|
22 |
+
|
23 |
+
# with the newly created file and it's path, process and save it for embedding
|
24 |
+
processed_file = process_file(os.path.realpath(file.filename))
|
25 |
+
result = await asyncio.gather(processed_file)
|
26 |
+
save_results(result)
|
27 |
+
|
28 |
+
except Exception:
|
29 |
+
return {"message": "There was an error uploading this file. Ensure you have a .csv file with the following columns:"
|
30 |
+
"\n source, date, type, category, description, amount"}
|
31 |
+
finally:
|
32 |
+
file.file.close()
|
33 |
+
|
34 |
+
return {"message": f"Successfully uploaded {file.filename}"}
|