Spaces:
Sleeping
Sleeping
File size: 5,112 Bytes
f76cc7d 7008554 0427a5f 7008554 8b1095d 7008554 991ebc9 7008554 9380975 7008554 9380975 7008554 24d1ca2 7008554 17e511c ae84343 24d1ca2 7008554 24d1ca2 7008554 24d1ca2 7008554 24d1ca2 7008554 | 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 | 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]
# )
with gr.Blocks(css="""
#company-logo {
width: 25%;
margin: auto;
display: block;
}
""") as iface:
gr.HTML('<a href="https://beiinghuman.com" target="_blank"><img id="company-logo" src="/file=static/logo_Icon.png" alt="Beiing Human Logo"></a>')
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) |