File size: 10,912 Bytes
994363d
 
6eb96c7
994363d
ac5688e
 
b12a659
 
 
 
 
 
 
 
 
 
 
 
994363d
b12a659
ac5688e
b12a659
 
 
ac5688e
 
b12a659
 
 
9c97640
b12a659
 
 
 
83b2b3a
b12a659
 
 
 
ac5688e
b12a659
 
83b881c
b12a659
 
 
 
 
 
 
 
9c97640
83b2b3a
b12a659
 
9c97640
b12a659
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac5688e
b12a659
 
 
ac5688e
994363d
0d231e5
994363d
b12a659
 
994363d
0d231e5
b12a659
 
 
994363d
 
0d231e5
b12a659
 
 
 
0d231e5
994363d
b12a659
 
 
994363d
 
0d231e5
5970cfe
b12a659
 
 
 
 
 
 
8213e12
b12a659
 
 
 
 
8213e12
994363d
 
b12a659
 
994363d
b12a659
 
d44d9fb
 
994363d
d44d9fb
 
994363d
 
6fd83e3
 
 
 
 
 
994363d
 
 
 
ad49761
 
16f1089
ad49761
d44d9fb
 
ad49761
994363d
 
 
 
 
0d231e5
d44d9fb
994363d
 
b12a659
 
 
994363d
9e9f019
994363d
ac5688e
 
994363d
 
d7b47f2
994363d
 
b12a659
 
994363d
b12a659
994363d
 
 
 
 
 
 
b12a659
 
 
 
8213e12
 
 
b12a659
8213e12
994363d
0d231e5
b12a659
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83b2b3a
994363d
b12a659
 
 
8213e12
994363d
ac5688e
8213e12
 
83ee96d
d44d9fb
0d231e5
994363d
8213e12
 
0d231e5
994363d
0d231e5
994363d
 
8213e12
994363d
 
 
d44d9fb
8213e12
994363d
8213e12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import openai
import pandas as pd
import os
import gradio as gr
import boto3
import uuid
import logging
import sys
from datetime import datetime

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(sys.stdout),  # This will show in HF Spaces logs
        logging.FileHandler('app.log', mode='a')  # Also save to file
    ]
)

logger = logging.getLogger(__name__)

# Log application startup
logger.info("πŸš€ Starting ERP Reconciliation Tool")
logger.info(f"πŸ“… Application started at: {datetime.now()}")


try:
    openai.api_key = os.environ.get("Open_ai_key")
    bucket_name = os.environ.get("Bucket_name")

    if not openai.api_key:
        logger.warning("⚠️ OpenAI API key not found in environment variables")
    else:
        logger.info("βœ… OpenAI API key loaded successfully")

    if not bucket_name:
        logger.warning("⚠️ S3 bucket name not found in environment variables")
    else:
        logger.info(f"βœ… S3 bucket configured: {bucket_name}")

except Exception as e:
    logger.error(f"❌ Error loading environment variables: {e}")

try:
    session = boto3.Session(
        aws_access_key_id=os.getenv("access_key"),
        aws_secret_access_key=os.getenv("secret_access_key")
    )
    logger.info("βœ… AWS session created successfully")
except Exception as e:
    logger.error(f"❌ Error creating AWS session: {e}")


def upload_files_to_s3(erp_file_path, external_file_path, content, bucket_name):
    logger.info(f"πŸ“€ Starting S3 upload process")

    try:
        s3 = session.client("s3")
        session_id = str(uuid.uuid4())

        logger.info(f"πŸ†” Generated session ID: {session_id}")

        base_erp = os.path.splitext(os.path.basename(erp_file_path))[0]
        base_bank = os.path.splitext(os.path.basename(external_file_path))[0]

        # Add session ID to filenames
        erp_filename = f"{base_erp}_{session_id}.csv"
        bank_filename = f"{base_bank}_{session_id}.csv"
        txt_filename = f"{session_id}.txt"

        logger.info(f"πŸ“„ Generated filenames - ERP: {erp_filename}, Bank: {bank_filename}, Result: {txt_filename}")

        # Write reconciliation output to local file
        with open(txt_filename, "w", encoding="utf-8") as file:
            file.write(content)
        logger.info(f"βœ… Reconciliation result written to local file: {txt_filename}")

        # === Separate folders by type ===
        erp_s3_key = f"ERP Statements/{erp_filename}"
        bank_s3_key = f"Bank Statements/{bank_filename}"
        txt_s3_key = f"Reconciliation Results/{txt_filename}"

        # === Combined folder for each run ===
        combined_prefix = f"Combined Files/{session_id}/"
        erp_combined_key = combined_prefix + erp_filename
        bank_combined_key = combined_prefix + bank_filename
        txt_combined_key = combined_prefix + txt_filename

        # βœ… Upload to type-based folders
        logger.info("πŸ“€ Uploading files to type-based S3 folders...")
        s3.upload_file(erp_file_path, bucket_name, erp_s3_key)
        logger.info(f"βœ… Uploaded ERP file to: {erp_s3_key}")

        s3.upload_file(external_file_path, bucket_name, bank_s3_key)
        logger.info(f"βœ… Uploaded Bank file to: {bank_s3_key}")

        s3.upload_file(txt_filename, bucket_name, txt_s3_key)
        logger.info(f"βœ… Uploaded result file to: {txt_s3_key}")

        # βœ… Upload to combined folder
        logger.info("πŸ“€ Uploading files to combined S3 folder...")
        s3.upload_file(erp_file_path, bucket_name, erp_combined_key)
        s3.upload_file(external_file_path, bucket_name, bank_combined_key)
        s3.upload_file(txt_filename, bucket_name, txt_combined_key)
        logger.info(f"βœ… All files uploaded to combined folder: {combined_prefix}")

        # Clean up local file
        os.remove(txt_filename)
        logger.info(f"πŸ—‘οΈ Cleaned up local file: {txt_filename}")

        upload_result = {
            "erp_s3_key": erp_s3_key,
            "bank_s3_key": bank_s3_key,
            "result_s3_key": txt_s3_key,
            "combined_keys": [erp_combined_key, bank_combined_key, txt_combined_key],
            "session_id": session_id
        }

        logger.info(f"βœ… S3 upload process completed successfully for session: {session_id}")
        return upload_result

    except Exception as e:
        logger.error(f"❌ Error in S3 upload process: {e}")
        raise e


# βœ… Extract transactions from CSV only
def extract_transactions(file):
    logger.info(f"πŸ“Š Processing file: {file.name}")

    filename = file.name.lower()
    if not filename.endswith(".csv"):
        error_msg = f"Unsupported file format: {filename}. Please upload a CSV file only."
        logger.error(f"❌ {error_msg}")
        raise ValueError(error_msg)

    try:
        df = pd.read_csv(file.name)
        logger.info(f"βœ… Successfully read CSV file: {file.name}")
        logger.info(f"πŸ“ˆ File contains {len(df)} rows and {len(df.columns)} columns")
        logger.info(f"🏷️ Column names: {list(df.columns)}")

        return df.to_string(index=False)
    except Exception as e:
        error_msg = f"Error processing {file.name}: {e}"
        logger.error(f"❌ {error_msg}")
        raise ValueError(error_msg)


# βœ… Reconcile the statements using OpenAI
def reconcile_statements_openai(erp_file, external_file):
    session_start = datetime.now()
    logger.info(f"πŸ” Starting reconciliation process at: {session_start}")

    # Validate inputs
    if not erp_file or not external_file:
        error_msg = "Both ERP and External files are required"
        logger.error(f"❌ {error_msg}")
        yield "❌ Error: Missing files", f"<h3>Error</h3><pre>{error_msg}</pre>", None
        return

    logger.info(f"πŸ“ ERP file: {erp_file.name if erp_file else 'None'}")
    logger.info(f"🏦 External file: {external_file.name if external_file else 'None'}")

    yield "⏳ Processing your request...", "", None

    try:
        # Extract data from files
        logger.info("πŸ“Š Extracting data from ERP file...")
        erp_data = extract_transactions(erp_file)

        logger.info("πŸ“Š Extracting data from External file...")
        external_data = extract_transactions(external_file)

        prompt = f"""
You are a financial analyst specializing in account reconciliations. Your task is to compare two data sets: one from an ERP system and the other from a 
Bank or Vendor statement.
The goal is to identify which transactions match across both data sets, and which transactions are unmatched or potentially erroneous.
Each dataset contains transaction entries with **Date**, **Amount**, and **Description**.
ERP descriptions may include prefixes like "Vendor Payment - ", while external descriptions are simpler.
Please attempt to normalize and fuzzy-match transactions by:
- Ignoring common prefixes/suffixes
- Allowing for small amount rounding differences (Β±$0.01–$1)
- Matching based on partial vendor or keyword overlaps
.
---
Please follow this format in your response:
1. πŸ“˜ **Introduction**  
   Briefly explain what reconciliation means and how you'll approach it.
2. βœ… **Matched Transactions**  
   Make a table to show side by side comparison of matched transactions
3. 🏦 **Unmatched Transactions**  
   List any transactions found in 
   a. External file but not in the ERP file OR 
   b. ERP file but Not in External file
4. 🧾 **Summary & Suggested Next Steps**  
   Explain what the discrepancies might mean and what the user should do next.
---
Here is the ERP data:
{erp_data}
---
Here is the External (Bank or Vendor) data:
{external_data}
"""

        logger.info("πŸ€– Sending request to OpenAI...")
        logger.info(f"πŸ“ Prompt length: {len(prompt)} characters")

        response = openai.ChatCompletion.create(
            model="gpt-5",
            messages=[
                {"role": "system",
                 "content": "You are a financial analyst who specializes in reconciling financial data."},
                {"role": "user", "content": prompt}
            ],
            temperature=1,
        )

        logger.info("βœ… Received response from OpenAI")

        content = response.choices[0].message['content']
        logger.info(f"πŸ“„ Response length: {len(content)} characters")

        html = f"""
        <div style="font-family: 'Segoe UI', sans-serif; line-height: 1.5;">
            <h2>πŸ” Reconciliation Report</h2>
            <pre>{content}</pre>
        </div>
        """

        # Calculate processing time
        processing_time = (datetime.now() - session_start).total_seconds()
        logger.info(f"⏱️ Processing completed in {processing_time:.2f} seconds")
        download_filename = f"reconciliation_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
        with open(download_filename, "w", encoding="utf-8") as f:
            f.write(content)

        yield "βœ… Done!", html, download_filename

        # βœ… Upload the files + result to S3
        try:
            logger.info("πŸ“€ Starting S3 upload...")
            upload_result = upload_files_to_s3(
                erp_file_path=erp_file.name,
                external_file_path=external_file.name,
                content=html,
                bucket_name=bucket_name
            )
            logger.info(f"βœ… Files uploaded successfully with session ID: {upload_result['session_id']}")
        except Exception as upload_error:
            logger.error(f"❌ S3 upload failed: {upload_error}")
            # Don't fail the entire process if S3 upload fails
            logger.info("⚠️ Continuing despite S3 upload failure")

        logger.info(f"πŸŽ‰ Reconciliation process completed successfully!")

    except Exception as e:
        error_msg = f"Error during reconciliation: {e}"
        logger.error(f"❌ {error_msg}")
        logger.error(f"πŸ› Full error details: {str(e)}")
        yield "❌ Error occurred", f"<h3>Error</h3><pre>{error_msg}</pre>", None


# βœ… Gradio UI
with gr.Blocks(css="""#company-logo { width: 25%; margin: auto; display: block; }""") as iface:
    gr.Image("logo_Icon.png", elem_id="company-logo", label="Beiing Human")
    gr.Markdown("## πŸ“Š ERP vs Bank/Vendor Reconciliation Tool")

    with gr.Row():
        erp_file = gr.File(label="πŸ“ Upload ERP Statement (CSV only)", type="filepath", file_types=[".csv"])
        external_file = gr.File(label="🏦 Upload Bank or Vendor Statement (CSV only)", type="filepath", file_types=[".csv"])

    btn = gr.Button("πŸ” Reconcile")

    status = gr.Markdown()
    result = gr.HTML()
    download = gr.File(label="⬇️ Download Reconciliation Report", type="filepath")

    btn.click(
        fn=reconcile_statements_openai,
        inputs=[erp_file, external_file],
        outputs=[status, result, download]
    )
iface.launch()