Spaces:
Sleeping
Sleeping
Push experimentation code for file upload fix
Browse files- .DS_Store +0 -0
- app/.DS_Store +0 -0
- app/categorization/file_processing.py +18 -13
- app/settings.py +1 -0
- app/transactions_rag/transactions_2024.csv +1 -1
.DS_Store
ADDED
|
Binary file (8.2 kB). View file
|
|
|
app/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
app/categorization/file_processing.py
CHANGED
|
@@ -41,7 +41,7 @@ async def process_file(file_path: str) -> Dict[str, Union[str, pd.DataFrame]]:
|
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
# Return an error indicator and exception info
|
| 44 |
-
logging.
|
| 45 |
print(f"ERROR processing file {file_name}: {e}")
|
| 46 |
result["error"] = str(e)
|
| 47 |
|
|
@@ -58,17 +58,21 @@ def standardize_csv_file(file_path: str) -> pd.DataFrame:
|
|
| 58 |
Returns:
|
| 59 |
pd.DataFrame: Prepared transaction data.
|
| 60 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
tx_list.columns = tx_list.columns.str.lower().str.strip()
|
| 65 |
-
|
| 66 |
-
# Standardize dates to YYYY/MM/DD format
|
| 67 |
-
tx_list["date"] = pd.to_datetime(tx_list["date"]).dt.strftime("%Y/%m/%d")
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
return tx_list
|
| 74 |
|
|
@@ -108,19 +112,20 @@ async def save_results(db: AsyncSession, results: List) -> None:
|
|
| 108 |
txn_list_to_save = [TransactionCreate(**row.to_dict(), user_id=1) for _, row in tx_list.iterrows()]
|
| 109 |
await Transaction.bulk_create(db, txn_list_to_save)
|
| 110 |
|
| 111 |
-
new_ref_data = tx_list[["
|
| 112 |
if os.path.exists(CATEGORY_REFERENCE_OUTPUT_FILE):
|
| 113 |
# If it exists, add master file to interim results
|
| 114 |
-
old_ref_data = pd.read_csv(CATEGORY_REFERENCE_OUTPUT_FILE, names=["
|
| 115 |
new_ref_data = pd.concat([old_ref_data, new_ref_data], ignore_index=True)
|
| 116 |
|
| 117 |
# Drop duplicates, sort, and write to create new Master File
|
| 118 |
-
new_ref_data.drop_duplicates(subset=["
|
| 119 |
CATEGORY_REFERENCE_OUTPUT_FILE, mode="w", index=False, header=True
|
| 120 |
)
|
| 121 |
|
| 122 |
# Summarize results
|
| 123 |
print(f"\nProcessed {len(results)} files: {len(ok_files)} successful, {len(ko_files)} with errors\n")
|
|
|
|
| 124 |
if len(ko_files):
|
| 125 |
print(f"Errors in the following files:")
|
| 126 |
for message in error_messages:
|
|
|
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
# Return an error indicator and exception info
|
| 44 |
+
logging.debug(logging.ERROR, f"| File: {file_name} | Unexpected Error: {e}")
|
| 45 |
print(f"ERROR processing file {file_name}: {e}")
|
| 46 |
result["error"] = str(e)
|
| 47 |
|
|
|
|
| 58 |
Returns:
|
| 59 |
pd.DataFrame: Prepared transaction data.
|
| 60 |
"""
|
| 61 |
+
try:
|
| 62 |
+
tx_list = pd.read_csv(file_path, index_col=False)
|
| 63 |
+
tx_list.attrs["file_name"] = file_path
|
| 64 |
+
tx_list.columns = tx_list.columns.str.lower().str.strip()
|
| 65 |
|
| 66 |
+
# Standardize dates to YYYY/MM/DD format
|
| 67 |
+
tx_list["date"] = pd.to_datetime(tx_list["date"]).dt.strftime("%Y/%m/%d")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
# Add source and reindex to desired tx format; category column is new and therefore empty
|
| 70 |
+
tx_list.loc[:, "source"] = os.path.basename(file_path)
|
| 71 |
+
tx_list = tx_list.reindex(columns=["transaction_date", "type", "category", "name_description", "amount"])
|
| 72 |
+
|
| 73 |
+
except Exception as e:
|
| 74 |
+
# Return an error indicator and exception info
|
| 75 |
+
logging.debug("standardize_csv_file Error: {e}")
|
| 76 |
|
| 77 |
return tx_list
|
| 78 |
|
|
|
|
| 112 |
txn_list_to_save = [TransactionCreate(**row.to_dict(), user_id=1) for _, row in tx_list.iterrows()]
|
| 113 |
await Transaction.bulk_create(db, txn_list_to_save)
|
| 114 |
|
| 115 |
+
new_ref_data = tx_list[["name_description", "category"]]
|
| 116 |
if os.path.exists(CATEGORY_REFERENCE_OUTPUT_FILE):
|
| 117 |
# If it exists, add master file to interim results
|
| 118 |
+
old_ref_data = pd.read_csv(CATEGORY_REFERENCE_OUTPUT_FILE, names=["name_description", "category"], header=0)
|
| 119 |
new_ref_data = pd.concat([old_ref_data, new_ref_data], ignore_index=True)
|
| 120 |
|
| 121 |
# Drop duplicates, sort, and write to create new Master File
|
| 122 |
+
new_ref_data.drop_duplicates(subset=["name_description"]).sort_values(by=["name_description"]).to_csv(
|
| 123 |
CATEGORY_REFERENCE_OUTPUT_FILE, mode="w", index=False, header=True
|
| 124 |
)
|
| 125 |
|
| 126 |
# Summarize results
|
| 127 |
print(f"\nProcessed {len(results)} files: {len(ok_files)} successful, {len(ko_files)} with errors\n")
|
| 128 |
+
logging.debug(f"\nProcessed {len(results)} files: {len(ok_files)} successful, {len(ko_files)} with errors\n")
|
| 129 |
if len(ko_files):
|
| 130 |
print(f"Errors in the following files:")
|
| 131 |
for message in error_messages:
|
app/settings.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
from typing import Dict
|
|
|
|
| 3 |
from llama_index.core.settings import Settings
|
| 4 |
|
| 5 |
|
|
|
|
| 1 |
import os
|
| 2 |
from typing import Dict
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
from llama_index.core.settings import Settings
|
| 5 |
|
| 6 |
|
app/transactions_rag/transactions_2024.csv
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
|
| 2 |
2023-12-30,Comcast Internet,Expense,9.96
|
| 3 |
2023-12-30,Lemonade Home Insurance,Expense,17.53
|
| 4 |
2023-12-30,Monthly Appartment Rent,Expense,2000.0
|
|
|
|
| 1 |
+
transaction_date,name_description,type,amount
|
| 2 |
2023-12-30,Comcast Internet,Expense,9.96
|
| 3 |
2023-12-30,Lemonade Home Insurance,Expense,17.53
|
| 4 |
2023-12-30,Monthly Appartment Rent,Expense,2000.0
|