Spaces:
Sleeping
Sleeping
Update fastapi post method to match current pattern
Browse files- app/api/routers/file_upload.py +27 -21
- main.py +2 -0
app/api/routers/file_upload.py
CHANGED
@@ -1,34 +1,40 @@
|
|
1 |
from typing import Annotated
|
2 |
-
from fastapi import
|
3 |
from app.categorization.file_processing import process_file, save_results
|
4 |
-
from app.
|
5 |
import asyncio
|
6 |
import os
|
|
|
7 |
|
8 |
-
|
9 |
|
10 |
-
@
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
try:
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
os.makedirs(file_upload_directory_path)
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
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 |
-
|
32 |
-
|
33 |
-
|
34 |
-
return {"message": f"Successfully uploaded {file.filename}"}
|
|
|
1 |
from typing import Annotated
|
2 |
+
from fastapi import APIRouter, UploadFile
|
3 |
from app.categorization.file_processing import process_file, save_results
|
4 |
+
from app.schema.index import FileUploadCreate
|
5 |
import asyncio
|
6 |
import os
|
7 |
+
import csv
|
8 |
|
9 |
+
file_upload_router = r = APIRouter(prefix="/api/v1/file_upload", tags=["file_upload"])
|
10 |
|
11 |
+
@r.post(
|
12 |
+
"/",
|
13 |
+
responses={
|
14 |
+
200: {"description": "File successfully uploaded"},
|
15 |
+
400: {"description": "Bad request"},
|
16 |
+
500: {"description": "Internal server error"},
|
17 |
+
},
|
18 |
+
)
|
19 |
+
async def create_file(input_file: UploadFile):
|
20 |
try:
|
21 |
+
# Create directory to store all uploaded .csv files
|
22 |
+
file_upload_directory_path = "data/tx_data/input"
|
23 |
+
if not os.path.exists(file_upload_directory_path):
|
24 |
+
os.makedirs(file_upload_directory_path)
|
|
|
25 |
|
26 |
+
# Write items of .csv filte to directory
|
27 |
+
with open(os.path.join(file_upload_directory_path, input_file.filename)) as output_file:
|
28 |
+
[output_file.write(" ".join(row)+'\n') for row in csv.reader(input_file)]
|
29 |
+
output_file.close()
|
30 |
|
31 |
+
# With the newly created file and it's path, process and save it for embedding
|
32 |
+
processed_file = process_file(os.path.realpath(input_file.filename))
|
33 |
+
result = await asyncio.gather(processed_file)
|
34 |
+
save_results(result)
|
35 |
|
36 |
except Exception:
|
37 |
return {"message": "There was an error uploading this file. Ensure you have a .csv file with the following columns:"
|
38 |
"\n source, date, type, category, description, amount"}
|
39 |
+
|
40 |
+
return {"message": f"Successfully uploaded {input_file.filename}"}
|
|
|
|
main.py
CHANGED
@@ -8,6 +8,7 @@ from fastapi.responses import RedirectResponse
|
|
8 |
from app.api.routers.chat import chat_router
|
9 |
from app.api.routers.user import user_router
|
10 |
from app.api.routers.transaction import transaction_router
|
|
|
11 |
from app.settings import init_settings
|
12 |
from fastapi.staticfiles import StaticFiles
|
13 |
from alembic.config import Config
|
@@ -53,6 +54,7 @@ def init_app(init_db: bool = True) -> FastAPI:
|
|
53 |
app.include_router(chat_router, prefix="/api/chat")
|
54 |
app.include_router(user_router)
|
55 |
app.include_router(transaction_router)
|
|
|
56 |
|
57 |
return app
|
58 |
|
|
|
8 |
from app.api.routers.chat import chat_router
|
9 |
from app.api.routers.user import user_router
|
10 |
from app.api.routers.transaction import transaction_router
|
11 |
+
from app.api.routers.file_upload import file_upload_router
|
12 |
from app.settings import init_settings
|
13 |
from fastapi.staticfiles import StaticFiles
|
14 |
from alembic.config import Config
|
|
|
54 |
app.include_router(chat_router, prefix="/api/chat")
|
55 |
app.include_router(user_router)
|
56 |
app.include_router(transaction_router)
|
57 |
+
app.include_router(file_upload_router)
|
58 |
|
59 |
return app
|
60 |
|