File size: 4,293 Bytes
57b7c34
b22506c
 
 
a9402fb
b22506c
57b7c34
b22506c
 
57b7c34
b22506c
 
0427a5f
0b04e53
4fa7dc2
b22506c
b2ff95a
b22506c
b2ff95a
 
d02fb12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b1095d
b2ff95a
d02fb12
991ebc9
b22506c
 
 
 
 
b2ff95a
b22506c
 
 
 
b2464dd
437e9bc
b2ff95a
 
 
 
 
 
 
 
b22506c
 
b2ff95a
 
5b6d1c4
 
 
 
 
 
 
 
b22506c
 
 
 
 
 
 
 
 
0b04e53
b22506c
 
 
 
 
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
import gradio as gr
import openpyxl
import PyPDF2
import pandas as pd
from PIL import Image
import pytesseract  # Replaced EasyOCR
import io
import os
from huggingface_hub import InferenceClient

# Access the Hugging Face token from the environment variable
hf_token = os.environ.get("HF_TOKEN")

def reconcile_statements(erp_file, bank_file):
    yield "⏳ Processing your request... Please wait.", ""

    # your existing code block...

    try:
        # File parsing...
        # Extract ERP statement
        erp_statement = ""
        erp_filename = erp_file.name

        if erp_filename.endswith((".xlsx", ".xls")):
            workbook = openpyxl.load_workbook(erp_filename)
            sheet = workbook.active
            for row in sheet.iter_rows():
                for cell in row:
                    erp_statement += str(cell.value) + "\t"
                erp_statement += "\n"
        elif erp_filename.endswith(".pdf"):
            pdf_reader = PyPDF2.PdfReader(erp_filename)
            for page in pdf_reader.pages:
                erp_statement += page.extract_text() or ""
        elif erp_filename.endswith((".jpg", ".jpeg", ".png")):
            image = Image.open(io.BytesIO(erp_file.read()))
            erp_statement = pytesseract.image_to_string(image)  # Tesseract OCR
        elif erp_filename.endswith(".csv"):
            df = pd.read_csv(erp_filename)
            erp_statement = df.to_string()
        else:
            raise ValueError("Unsupported ERP file format.")

        # Extract bank statement (similar logic as above)
        bank_statement = ""
        bank_filename = bank_file.name

        if bank_filename.endswith((".xlsx", ".xls")):
            workbook = openpyxl.load_workbook(bank_filename)
            sheet = workbook.active
            for row in sheet.iter_rows():
                for cell in row:
                    bank_statement += str(cell.value) + "\t"
                bank_statement += "\n"
        elif bank_filename.endswith(".pdf"):
            pdf_reader = PyPDF2.PdfReader(bank_filename)
            for page in pdf_reader.pages:
                bank_statement += page.extract_text() or ""
        elif bank_filename.endswith((".jpg", ".jpeg", ".png")):
            image = Image.open(io.BytesIO(bank_file.read()))
            bank_statement = pytesseract.image_to_string(image)  # Tesseract OCR
        elif bank_filename.endswith(".csv"):
            df = pd.read_csv(bank_filename)
            bank_statement = df.to_string()
        else:
            raise ValueError("Unsupported bank file format.")

        # Hugging Face request...
        prompt = f"Reconcile these statements:\nERP:\n{erp_statement}\nBank:\n{bank_statement}"

        client = InferenceClient(provider="together", api_key=hf_token)
        completion = client.chat.completions.create(
            model="deepseek-ai/DeepSeek-R1",
            messages=[{"role": "user", "content": prompt}],
        )

        if completion.choices:
            reconciliation_results = completion.choices[0].message.get('content', '')
        else:
            reconciliation_results = "⚠️ No response received from the model."

        output = f"""
        <div style="font-family: 'Segoe UI', ...">
            <h2>πŸ” Reconciliation Results</h2>
            <div style="...">
                <pre>{reconciliation_results}</pre>
            </div>
        </div>
        """
        yield "βœ… Processing complete!", output

    except Exception as e:
        yield f"❌ Error: {e}", f"<h1>Error</h1><p>{e}</p>"

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")
    status_text = gr.Markdown("πŸ‘‹ Upload your files to begin reconciliation.")
    with gr.Row():
        erp_input = gr.File(label="πŸ“‚ Upload ERP Statement", type="filepath")
        bank_input = gr.File(label="πŸ“‚ Upload Bank Statement", type="filepath")
    submit_btn = gr.Button("πŸ”„ Start Reconciliation")
    result_output = gr.HTML()

    submit_btn.click(
        fn=reconcile_statements,
        inputs=[erp_input, bank_input],
        outputs=[status_text, result_output]
    )
    
if __name__ == "__main__":
    iface.launch(debug=True)