Update app_chatgpt.py
Browse files- app_chatgpt.py +37 -19
app_chatgpt.py
CHANGED
|
@@ -7,8 +7,35 @@ import pytesseract
|
|
| 7 |
import io
|
| 8 |
import os
|
| 9 |
import gradio as gr
|
|
|
|
|
|
|
| 10 |
|
| 11 |
openai.api_key = os.environ.get("Open_ai_key")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def extract_transactions(file):
|
| 14 |
"""Extract raw text content from various supported file formats."""
|
|
@@ -46,51 +73,41 @@ def reconcile_statements_openai(erp_file, external_file):
|
|
| 46 |
yield "β³ Processing your request...", ""
|
| 47 |
|
| 48 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
erp_data = extract_transactions(erp_file)
|
| 50 |
external_data = extract_transactions(external_file)
|
| 51 |
|
| 52 |
-
|
| 53 |
prompt = f"""
|
| 54 |
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
|
| 55 |
-
|
| 56 |
Bank or Vendor statement.
|
| 57 |
-
|
| 58 |
The goal is to identify which transactions match across both data sets, and which transactions are unmatched or potentially erroneous.
|
| 59 |
-
|
| 60 |
Each dataset contains transaction entries with **Date**, **Amount**, and **Description**.
|
| 61 |
-
|
| 62 |
ERP descriptions may include prefixes like "Vendor Payment - ", while external descriptions are simpler.
|
| 63 |
Please attempt to normalize and fuzzy-match transactions by:
|
| 64 |
- Ignoring common prefixes/suffixes
|
| 65 |
- Allowing for small amount rounding differences (Β±$0.01β$1)
|
| 66 |
- Matching based on partial vendor or keyword overlaps
|
| 67 |
.
|
| 68 |
-
|
| 69 |
---
|
| 70 |
-
|
| 71 |
Please follow this format in your response:
|
| 72 |
-
|
| 73 |
1. π **Introduction**
|
| 74 |
Briefly explain what reconciliation means and how you'll approach it.
|
| 75 |
-
|
| 76 |
2. β
**Matched Transactions**
|
| 77 |
Make a table to show side by side comparison of matched transactions
|
| 78 |
-
|
| 79 |
3. π¦ **Unmatched Bank Transactions**
|
| 80 |
List any transactions found in
|
| 81 |
a. External file but not in the ERP file OR
|
| 82 |
b. ERP file but Not in External file
|
| 83 |
-
|
| 84 |
4. π§Ύ **Summary & Suggested Next Steps**
|
| 85 |
Explain what the discrepancies might mean and what the user should do next.
|
| 86 |
-
|
| 87 |
---
|
| 88 |
-
|
| 89 |
Here is the ERP data:
|
| 90 |
{erp_data}
|
| 91 |
-
|
| 92 |
---
|
| 93 |
-
|
| 94 |
Here is the External (Bank or Vendor) data::
|
| 95 |
{external_data}
|
| 96 |
"""
|
|
@@ -98,7 +115,8 @@ Here is the External (Bank or Vendor) data::
|
|
| 98 |
response = openai.ChatCompletion.create(
|
| 99 |
model="gpt-4",
|
| 100 |
messages=[
|
| 101 |
-
{"role": "system",
|
|
|
|
| 102 |
{"role": "user", "content": prompt}
|
| 103 |
],
|
| 104 |
temperature=0.2,
|
|
@@ -117,6 +135,7 @@ Here is the External (Bank or Vendor) data::
|
|
| 117 |
except Exception as e:
|
| 118 |
yield "β Error occurred", f"<h3>Error</h3><pre>{e}</pre>"
|
| 119 |
|
|
|
|
| 120 |
# Gradio UI
|
| 121 |
with gr.Blocks(css="""
|
| 122 |
#company-logo {
|
|
@@ -138,9 +157,8 @@ with gr.Blocks(css="""
|
|
| 138 |
)
|
| 139 |
support_email_display = gr.Markdown("π¬ Or email us at: `[email protected]` (copy & paste)")
|
| 140 |
|
| 141 |
-
#support_btn = gr.Button("π§ Contact Support: [email protected]", link="mailto:[email protected]?subject=Support%20Request&body=Hi%20Beiing%20Human%20Team%2C%0A%0AI%20have%20a%20question%20about%20the%20reconciliation%20tool.%20Here%20are%20the%20details...")
|
| 142 |
|
| 143 |
-
|
| 144 |
status = gr.Markdown()
|
| 145 |
result = gr.HTML()
|
| 146 |
|
|
@@ -150,4 +168,4 @@ with gr.Blocks(css="""
|
|
| 150 |
outputs=[status, result]
|
| 151 |
)
|
| 152 |
|
| 153 |
-
iface.launch()
|
|
|
|
| 7 |
import io
|
| 8 |
import os
|
| 9 |
import gradio as gr
|
| 10 |
+
import boto3
|
| 11 |
+
import uuid
|
| 12 |
|
| 13 |
openai.api_key = os.environ.get("Open_ai_key")
|
| 14 |
+
bucket_name = os.environ.get("Bucket_name")
|
| 15 |
+
|
| 16 |
+
session = boto3.Session(
|
| 17 |
+
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
|
| 18 |
+
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY")
|
| 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 |
+
# Append UUID to filenames
|
| 27 |
+
erp_filename = f"{os.path.splitext(os.path.basename(erp_file_path))[0]}_{session_id}{os.path.splitext(erp_file_path)[1]}"
|
| 28 |
+
external_filename = f"{os.path.splitext(os.path.basename(external_file_path))[0]}_{session_id}{os.path.splitext(external_file_path)[1]}"
|
| 29 |
+
|
| 30 |
+
# Upload files
|
| 31 |
+
s3.upload_file(erp_file_path, bucket_name, erp_filename)
|
| 32 |
+
s3.upload_file(external_file_path, bucket_name, external_filename)
|
| 33 |
+
|
| 34 |
+
return {
|
| 35 |
+
"erp_s3_key": erp_filename,
|
| 36 |
+
"external_s3_key": external_filename
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
|
| 40 |
def extract_transactions(file):
|
| 41 |
"""Extract raw text content from various supported file formats."""
|
|
|
|
| 73 |
yield "β³ Processing your request...", ""
|
| 74 |
|
| 75 |
try:
|
| 76 |
+
s3_info = upload_files_to_s3(
|
| 77 |
+
erp_file_path=erp_file,
|
| 78 |
+
external_file_path=external_file,
|
| 79 |
+
bucket_name=bucket_name
|
| 80 |
+
)
|
| 81 |
erp_data = extract_transactions(erp_file)
|
| 82 |
external_data = extract_transactions(external_file)
|
| 83 |
|
|
|
|
| 84 |
prompt = f"""
|
| 85 |
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
|
|
|
|
| 86 |
Bank or Vendor statement.
|
|
|
|
| 87 |
The goal is to identify which transactions match across both data sets, and which transactions are unmatched or potentially erroneous.
|
|
|
|
| 88 |
Each dataset contains transaction entries with **Date**, **Amount**, and **Description**.
|
|
|
|
| 89 |
ERP descriptions may include prefixes like "Vendor Payment - ", while external descriptions are simpler.
|
| 90 |
Please attempt to normalize and fuzzy-match transactions by:
|
| 91 |
- Ignoring common prefixes/suffixes
|
| 92 |
- Allowing for small amount rounding differences (Β±$0.01β$1)
|
| 93 |
- Matching based on partial vendor or keyword overlaps
|
| 94 |
.
|
|
|
|
| 95 |
---
|
|
|
|
| 96 |
Please follow this format in your response:
|
|
|
|
| 97 |
1. π **Introduction**
|
| 98 |
Briefly explain what reconciliation means and how you'll approach it.
|
|
|
|
| 99 |
2. β
**Matched Transactions**
|
| 100 |
Make a table to show side by side comparison of matched transactions
|
|
|
|
| 101 |
3. π¦ **Unmatched Bank Transactions**
|
| 102 |
List any transactions found in
|
| 103 |
a. External file but not in the ERP file OR
|
| 104 |
b. ERP file but Not in External file
|
|
|
|
| 105 |
4. π§Ύ **Summary & Suggested Next Steps**
|
| 106 |
Explain what the discrepancies might mean and what the user should do next.
|
|
|
|
| 107 |
---
|
|
|
|
| 108 |
Here is the ERP data:
|
| 109 |
{erp_data}
|
|
|
|
| 110 |
---
|
|
|
|
| 111 |
Here is the External (Bank or Vendor) data::
|
| 112 |
{external_data}
|
| 113 |
"""
|
|
|
|
| 115 |
response = openai.ChatCompletion.create(
|
| 116 |
model="gpt-4",
|
| 117 |
messages=[
|
| 118 |
+
{"role": "system",
|
| 119 |
+
"content": "You are a financial analyst who specializes in reconciling financial data."},
|
| 120 |
{"role": "user", "content": prompt}
|
| 121 |
],
|
| 122 |
temperature=0.2,
|
|
|
|
| 135 |
except Exception as e:
|
| 136 |
yield "β Error occurred", f"<h3>Error</h3><pre>{e}</pre>"
|
| 137 |
|
| 138 |
+
|
| 139 |
# Gradio UI
|
| 140 |
with gr.Blocks(css="""
|
| 141 |
#company-logo {
|
|
|
|
| 157 |
)
|
| 158 |
support_email_display = gr.Markdown("π¬ Or email us at: `[email protected]` (copy & paste)")
|
| 159 |
|
| 160 |
+
# support_btn = gr.Button("π§ Contact Support: [email protected]", link="mailto:[email protected]?subject=Support%20Request&body=Hi%20Beiing%20Human%20Team%2C%0A%0AI%20have%20a%20question%20about%20the%20reconciliation%20tool.%20Here%20are%20the%20details...")
|
| 161 |
|
|
|
|
| 162 |
status = gr.Markdown()
|
| 163 |
result = gr.HTML()
|
| 164 |
|
|
|
|
| 168 |
outputs=[status, result]
|
| 169 |
)
|
| 170 |
|
| 171 |
+
iface.launch()
|