Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,118 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
| 5 |
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
sr = 48000
|
| 9 |
-
a4_freq, tones_from_a4 = 440, 12 * (octave - 4) + (note - 9)
|
| 10 |
-
frequency = a4_freq * 2 ** (tones_from_a4 / 12)
|
| 11 |
-
duration = int(duration)
|
| 12 |
-
audio = np.linspace(0, duration, duration * sr)
|
| 13 |
-
audio = (20000 * np.sin(audio * (2 * np.pi * frequency))).astype(np.int16)
|
| 14 |
-
return (sr, audio)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openpyxl
|
| 3 |
+
import PyPDF2
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import pytesseract # Replaced EasyOCR
|
| 7 |
+
import io
|
| 8 |
+
import os
|
| 9 |
+
from huggingface_hub import InferenceClient
|
| 10 |
|
| 11 |
+
# Access the Hugging Face token from the environment variable
|
| 12 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 13 |
|
| 14 |
+
def reconcile_statements(erp_file, bank_file):
|
| 15 |
+
yield "โณ Processing your request... Please wait.", ""
|
| 16 |
|
| 17 |
+
# your existing code block...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
try:
|
| 20 |
+
# File parsing...
|
| 21 |
+
# Extract ERP statement
|
| 22 |
+
erp_statement = ""
|
| 23 |
+
erp_filename = erp_file.name
|
| 24 |
|
| 25 |
+
if erp_filename.endswith((".xlsx", ".xls")):
|
| 26 |
+
workbook = openpyxl.load_workbook(erp_filename)
|
| 27 |
+
sheet = workbook.active
|
| 28 |
+
for row in sheet.iter_rows():
|
| 29 |
+
for cell in row:
|
| 30 |
+
erp_statement += str(cell.value) + "\t"
|
| 31 |
+
erp_statement += "\n"
|
| 32 |
+
elif erp_filename.endswith(".pdf"):
|
| 33 |
+
pdf_reader = PyPDF2.PdfReader(erp_filename)
|
| 34 |
+
for page in pdf_reader.pages:
|
| 35 |
+
erp_statement += page.extract_text() or ""
|
| 36 |
+
elif erp_filename.endswith((".jpg", ".jpeg", ".png")):
|
| 37 |
+
image = Image.open(io.BytesIO(erp_file.read()))
|
| 38 |
+
erp_statement = pytesseract.image_to_string(image) # Tesseract OCR
|
| 39 |
+
elif erp_filename.endswith(".csv"):
|
| 40 |
+
df = pd.read_csv(erp_filename)
|
| 41 |
+
erp_statement = df.to_string()
|
| 42 |
+
else:
|
| 43 |
+
raise ValueError("Unsupported ERP file format.")
|
| 44 |
+
|
| 45 |
+
# Extract bank statement (similar logic as above)
|
| 46 |
+
bank_statement = ""
|
| 47 |
+
bank_filename = bank_file.name
|
| 48 |
+
|
| 49 |
+
if bank_filename.endswith((".xlsx", ".xls")):
|
| 50 |
+
workbook = openpyxl.load_workbook(bank_filename)
|
| 51 |
+
sheet = workbook.active
|
| 52 |
+
for row in sheet.iter_rows():
|
| 53 |
+
for cell in row:
|
| 54 |
+
bank_statement += str(cell.value) + "\t"
|
| 55 |
+
bank_statement += "\n"
|
| 56 |
+
elif bank_filename.endswith(".pdf"):
|
| 57 |
+
pdf_reader = PyPDF2.PdfReader(bank_filename)
|
| 58 |
+
for page in pdf_reader.pages:
|
| 59 |
+
bank_statement += page.extract_text() or ""
|
| 60 |
+
elif bank_filename.endswith((".jpg", ".jpeg", ".png")):
|
| 61 |
+
image = Image.open(io.BytesIO(bank_file.read()))
|
| 62 |
+
bank_statement = pytesseract.image_to_string(image) # Tesseract OCR
|
| 63 |
+
elif bank_filename.endswith(".csv"):
|
| 64 |
+
df = pd.read_csv(bank_filename)
|
| 65 |
+
bank_statement = df.to_string()
|
| 66 |
+
else:
|
| 67 |
+
raise ValueError("Unsupported bank file format.")
|
| 68 |
+
|
| 69 |
+
# Hugging Face request...
|
| 70 |
+
prompt = f"Reconcile these statements:\nERP:\n{erp_statement}\nBank:\n{bank_statement}"
|
| 71 |
+
|
| 72 |
+
client = InferenceClient(provider="together", api_key=hf_token)
|
| 73 |
+
completion = client.chat.completions.create(
|
| 74 |
+
model="deepseek-ai/DeepSeek-R1",
|
| 75 |
+
messages=[{"role": "user", "content": prompt}],
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
if completion.choices:
|
| 79 |
+
reconciliation_results = completion.choices[0].message.get('content', '')
|
| 80 |
+
else:
|
| 81 |
+
reconciliation_results = "โ ๏ธ No response received from the model."
|
| 82 |
+
|
| 83 |
+
output = f"""
|
| 84 |
+
<div style="font-family: 'Segoe UI', ...">
|
| 85 |
+
<h2>๐ Reconciliation Results</h2>
|
| 86 |
+
<div style="...">
|
| 87 |
+
<pre>{reconciliation_results}</pre>
|
| 88 |
+
</div>
|
| 89 |
+
</div>
|
| 90 |
+
"""
|
| 91 |
+
yield "โ
Processing complete!", output
|
| 92 |
+
|
| 93 |
+
except Exception as e:
|
| 94 |
+
yield f"โ Error: {e}", f"<h1>Error</h1><p>{e}</p>"
|
| 95 |
+
|
| 96 |
+
with gr.Blocks(css="""
|
| 97 |
+
#company-logo {
|
| 98 |
+
width: 25%;
|
| 99 |
+
margin: auto;
|
| 100 |
+
display: block;
|
| 101 |
+
}
|
| 102 |
+
""") as iface:
|
| 103 |
+
gr.Image("logo_Icon.png", elem_id="company-logo", label="Beiing Human")
|
| 104 |
+
status_text = gr.Markdown("๐ Upload your files to begin reconciliation.")
|
| 105 |
+
with gr.Row():
|
| 106 |
+
erp_input = gr.File(label="๐ Upload ERP Statement", type="filepath")
|
| 107 |
+
bank_input = gr.File(label="๐ Upload Bank Statement", type="filepath")
|
| 108 |
+
submit_btn = gr.Button("๐ Start Reconciliation")
|
| 109 |
+
result_output = gr.HTML()
|
| 110 |
+
|
| 111 |
+
submit_btn.click(
|
| 112 |
+
fn=reconcile_statements,
|
| 113 |
+
inputs=[erp_input, bank_input],
|
| 114 |
+
outputs=[status_text, result_output]
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
if __name__ == "__main__":
|
| 118 |
+
iface.launch(debug=True)
|