Update app_chatgpt.py
Browse files- app_chatgpt.py +30 -13
app_chatgpt.py
CHANGED
|
@@ -19,24 +19,39 @@ session = boto3.Session(
|
|
| 19 |
)
|
| 20 |
|
| 21 |
|
| 22 |
-
def upload_files_to_s3(erp_file_path, external_file_path, bucket_name):
|
|
|
|
| 23 |
s3 = session.client("s3")
|
|
|
|
| 24 |
session_id = str(uuid.uuid4())
|
| 25 |
|
| 26 |
-
#
|
| 27 |
erp_filename = f"{os.path.splitext(os.path.basename(erp_file_path))[0]}_{session_id}{os.path.splitext(erp_file_path)[1]}"
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
|
|
|
| 30 |
erp_s3_key = f"ERP Statements/{erp_filename}"
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
# Upload files
|
| 34 |
s3.upload_file(erp_file_path, bucket_name, erp_s3_key)
|
| 35 |
-
s3.upload_file(external_file_path, bucket_name,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
return {
|
| 38 |
-
"erp_s3_key":
|
| 39 |
-
"
|
|
|
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
|
|
@@ -76,11 +91,6 @@ def reconcile_statements_openai(erp_file, external_file):
|
|
| 76 |
yield "⏳ Processing your request...", ""
|
| 77 |
|
| 78 |
try:
|
| 79 |
-
s3_info = upload_files_to_s3(
|
| 80 |
-
erp_file_path=erp_file,
|
| 81 |
-
external_file_path=external_file,
|
| 82 |
-
bucket_name=bucket_name
|
| 83 |
-
)
|
| 84 |
erp_data = extract_transactions(erp_file)
|
| 85 |
external_data = extract_transactions(external_file)
|
| 86 |
|
|
@@ -135,6 +145,13 @@ Here is the External (Bank or Vendor) data::
|
|
| 135 |
"""
|
| 136 |
yield "✅ Done!", html
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
except Exception as e:
|
| 139 |
yield "❌ Error occurred", f"<h3>Error</h3><pre>{e}</pre>"
|
| 140 |
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
|
| 22 |
+
def upload_files_to_s3(erp_file_path, external_file_path, content, bucket_name):
|
| 23 |
+
session = boto3.Session() # If using env vars (no manual creds needed)
|
| 24 |
s3 = session.client("s3")
|
| 25 |
+
|
| 26 |
session_id = str(uuid.uuid4())
|
| 27 |
|
| 28 |
+
# Create filenames with UUID
|
| 29 |
erp_filename = f"{os.path.splitext(os.path.basename(erp_file_path))[0]}_{session_id}{os.path.splitext(erp_file_path)[1]}"
|
| 30 |
+
bank_filename = f"{os.path.splitext(os.path.basename(external_file_path))[0]}_{session_id}{os.path.splitext(external_file_path)[1]}"
|
| 31 |
+
txt_filename = f"{session_id}.txt"
|
| 32 |
+
|
| 33 |
+
# Write content to a local .txt file
|
| 34 |
+
with open(txt_filename, "w", encoding="utf-8") as file:
|
| 35 |
+
file.write(content)
|
| 36 |
|
| 37 |
+
# Define S3 keys (organized into folders)
|
| 38 |
erp_s3_key = f"ERP Statements/{erp_filename}"
|
| 39 |
+
bank_s3_key = f"Bank Statements/{bank_filename}"
|
| 40 |
+
txt_s3_key = f"Reconciliation Results/{txt_filename}"
|
| 41 |
|
| 42 |
+
# Upload all 3 files to S3
|
| 43 |
s3.upload_file(erp_file_path, bucket_name, erp_s3_key)
|
| 44 |
+
s3.upload_file(external_file_path, bucket_name, bank_s3_key)
|
| 45 |
+
s3.upload_file(txt_filename, bucket_name, txt_s3_key)
|
| 46 |
+
|
| 47 |
+
# Optionally clean up the local .txt file
|
| 48 |
+
os.remove(txt_filename)
|
| 49 |
|
| 50 |
return {
|
| 51 |
+
"erp_s3_key": erp_s3_key,
|
| 52 |
+
"bank_s3_key": bank_s3_key,
|
| 53 |
+
"result_s3_key": txt_s3_key,
|
| 54 |
+
"session_id": session_id
|
| 55 |
}
|
| 56 |
|
| 57 |
|
|
|
|
| 91 |
yield "⏳ Processing your request...", ""
|
| 92 |
|
| 93 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
erp_data = extract_transactions(erp_file)
|
| 95 |
external_data = extract_transactions(external_file)
|
| 96 |
|
|
|
|
| 145 |
"""
|
| 146 |
yield "✅ Done!", html
|
| 147 |
|
| 148 |
+
s3_info = upload_files_to_s3(
|
| 149 |
+
erp_file_path=erp_file,
|
| 150 |
+
external_file_path=external_file,
|
| 151 |
+
content=html,
|
| 152 |
+
bucket_name=bucket_name
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
except Exception as e:
|
| 156 |
yield "❌ Error occurred", f"<h3>Error</h3><pre>{e}</pre>"
|
| 157 |
|