palexis3 commited on
Commit
00277e2
1 Parent(s): 5edc2d7

File Upload API (#10)

Browse files

- Added post api method to support file upload (360f86b30ddf202f9c0847be405aa57fedd3048b)
- Added file_upload file (88e4f9d2a6ac940c49fe603946605726893e8da1)
- Update fastapi post method to match current pattern (8231f9c63cd7759e0f3af006b39367327230c832)
- Add missing file (e5fb4c51758a29da9236629ddd85db33608a369a)
- Removed unnecessary directory (065050e8610115b44e59ab90e380b7a0c5903c84)
- Updated with main branch (5f1aad0a8257c197e34bcf91451761965899acbf)

Files changed (5) hide show
  1. .dockerignore +2 -1
  2. .gitignore +2 -1
  3. app/api/routers/file_upload.py +40 -0
  4. app/schema/index.py +10 -0
  5. main.py +8 -0
.dockerignore CHANGED
@@ -1,4 +1,5 @@
1
  __pycache__
2
  storage
3
  data/*
4
- venv
 
 
1
  __pycache__
2
  storage
3
  data/*
4
+ venv
5
+ myvenv
.gitignore CHANGED
@@ -2,4 +2,5 @@ __pycache__
2
  storage
3
  .env
4
  data/*
5
- venv
 
 
2
  storage
3
  .env
4
  data/*
5
+ venv
6
+ myvenv
app/api/routers/file_upload.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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}"}
app/schema/index.py CHANGED
@@ -53,6 +53,15 @@ class Transaction(TransactionResponse):
53
  user: User
54
 
55
 
 
 
 
 
 
 
 
 
 
56
  class IncomeStatementCreate(PydanticBaseModel):
57
  user_id: int
58
  date_from: datetime
@@ -65,3 +74,4 @@ class IncomeStatementResponse(PydanticBaseModel):
65
  date_to: datetime
66
  income: dict
67
  expenses: dict
 
 
53
  user: User
54
 
55
 
56
+ <<<<<<< HEAD
57
+ class FileUploadCreate(PydanticBaseModel):
58
+ source: str
59
+ date: datetime
60
+ category: str
61
+ name_description: str
62
+ amount: float
63
+ type: str
64
+ =======
65
  class IncomeStatementCreate(PydanticBaseModel):
66
  user_id: int
67
  date_from: datetime
 
74
  date_to: datetime
75
  income: dict
76
  expenses: dict
77
+ >>>>>>> main
main.py CHANGED
@@ -8,7 +8,11 @@ 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.api.routers.income_statement import income_statement_router
 
12
  from app.settings import init_settings
13
  from fastapi.staticfiles import StaticFiles
14
  from alembic.config import Config
@@ -54,7 +58,11 @@ def init_app(init_db: bool = True) -> FastAPI:
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(income_statement_router)
 
58
 
59
  return app
60
 
 
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
+ <<<<<<< HEAD
12
+ from app.api.routers.file_upload import file_upload_router
13
+ =======
14
  from app.api.routers.income_statement import income_statement_router
15
+ >>>>>>> main
16
  from app.settings import init_settings
17
  from fastapi.staticfiles import StaticFiles
18
  from alembic.config import Config
 
58
  app.include_router(chat_router, prefix="/api/chat")
59
  app.include_router(user_router)
60
  app.include_router(transaction_router)
61
+ <<<<<<< HEAD
62
+ app.include_router(file_upload_router)
63
+ =======
64
  app.include_router(income_statement_router)
65
+ >>>>>>> main
66
 
67
  return app
68