Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,87 +3,34 @@ import pandas as pd
|
|
| 3 |
import gradio as gr
|
| 4 |
import tempfile
|
| 5 |
import re
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
toshiba_data_pattern = re.compile(r'(\d+)\s+(\d+)\s+(\w+)\s+(\d{4}-\d{2}-\d{2})\s+([\d.]+)\s+([\d.]+)\s+([\d.]+)\s+(\w+)\s+([\d.]+)')
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
bhel_data_pattern = re.compile(r'Sl No\s+Material Description\s+Unit\s+Quantity\s+Dely Qty\s+Dely Date\s+Unit Rate\s+Value', re.IGNORECASE)
|
| 15 |
-
bhel_data_line_pattern = re.compile(r'(\d+)\s+([A-Za-z0-9 ]+)\s+(\w+)\s+(\d+)\s+(\d+)\s+(\d{2}-\d{2}-\d{4})\s+([\d.]+)\s+([\d.]+)')
|
| 16 |
-
|
| 17 |
-
# Initialize lists to store extracted data
|
| 18 |
-
toshiba_data = []
|
| 19 |
-
bhel_data = []
|
| 20 |
-
|
| 21 |
-
# Load PDF
|
| 22 |
with fitz.open(pdf_file.name) as pdf:
|
| 23 |
for page_num in range(pdf.page_count):
|
| 24 |
page = pdf[page_num]
|
| 25 |
text = page.get_text("text")
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
for match in matches:
|
| 31 |
-
pos, item_code, unit, delivery_date, quantity, basic_price, discount, currency, amount = match
|
| 32 |
-
toshiba_data.append({
|
| 33 |
-
"Position": pos,
|
| 34 |
-
"Item Code": item_code,
|
| 35 |
-
"Unit": unit,
|
| 36 |
-
"Delivery Date": delivery_date,
|
| 37 |
-
"Quantity": quantity,
|
| 38 |
-
"Basic Price": basic_price,
|
| 39 |
-
"Discount": discount,
|
| 40 |
-
"Currency": currency,
|
| 41 |
-
"Amount": amount
|
| 42 |
-
})
|
| 43 |
-
|
| 44 |
-
# Check for BHEL PO format and extract data
|
| 45 |
-
elif bhel_data_pattern.search(text):
|
| 46 |
-
matches = bhel_data_line_pattern.findall(text)
|
| 47 |
-
for match in matches:
|
| 48 |
-
sl_no, material_desc, unit, qty, dely_qty, dely_date, unit_rate, value = match
|
| 49 |
-
bhel_data.append({
|
| 50 |
-
"Sl No": sl_no,
|
| 51 |
-
"Material Description": material_desc,
|
| 52 |
-
"Unit": unit,
|
| 53 |
-
"Quantity": qty,
|
| 54 |
-
"Dely Qty": dely_qty,
|
| 55 |
-
"Dely Date": dely_date,
|
| 56 |
-
"Unit Rate": unit_rate,
|
| 57 |
-
"Value": value
|
| 58 |
-
})
|
| 59 |
-
|
| 60 |
-
# Check if any data was extracted
|
| 61 |
-
if not toshiba_data and not bhel_data:
|
| 62 |
-
return None, "No data extracted from the PDF. Please check the format of the uploaded document."
|
| 63 |
-
|
| 64 |
-
# Save data to Excel sheets
|
| 65 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
|
| 66 |
-
with pd.ExcelWriter(temp_file.name) as writer:
|
| 67 |
-
if toshiba_data:
|
| 68 |
-
pd.DataFrame(toshiba_data).to_excel(writer, sheet_name="Toshiba PO", index=False)
|
| 69 |
-
if bhel_data:
|
| 70 |
-
pd.DataFrame(bhel_data).to_excel(writer, sheet_name="BHEL PO", index=False)
|
| 71 |
-
|
| 72 |
-
return temp_file.name, None
|
| 73 |
|
| 74 |
def main(pdf_file):
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
return excel_file_path, "Excel file created successfully."
|
| 79 |
|
| 80 |
-
# Gradio interface
|
| 81 |
interface = gr.Interface(
|
| 82 |
fn=main,
|
| 83 |
inputs=gr.File(label="Upload PO PDF"),
|
| 84 |
-
outputs=[gr.File(label="Download Excel File"), gr.Textbox(label="
|
| 85 |
-
title="
|
| 86 |
-
description="Upload a
|
| 87 |
)
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import tempfile
|
| 5 |
import re
|
|
|
|
| 6 |
|
| 7 |
+
def extract_po_text(pdf_file):
|
| 8 |
+
# Initialize list to store text data from each page
|
| 9 |
+
text_data = []
|
|
|
|
| 10 |
|
| 11 |
+
# Load PDF and extract text page by page
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
with fitz.open(pdf_file.name) as pdf:
|
| 13 |
for page_num in range(pdf.page_count):
|
| 14 |
page = pdf[page_num]
|
| 15 |
text = page.get_text("text")
|
| 16 |
+
text_data.append(f"Page {page_num + 1}:\n{text}\n")
|
| 17 |
|
| 18 |
+
# Combine all page texts into one for inspection
|
| 19 |
+
full_text = "\n".join(text_data)
|
| 20 |
+
return full_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def main(pdf_file):
|
| 23 |
+
# Extract and display raw text for debugging purposes
|
| 24 |
+
extracted_text = extract_po_text(pdf_file)
|
| 25 |
+
return None, "Raw text extracted from PDF:\n\n" + extracted_text
|
|
|
|
| 26 |
|
| 27 |
+
# Gradio interface to display raw text output
|
| 28 |
interface = gr.Interface(
|
| 29 |
fn=main,
|
| 30 |
inputs=gr.File(label="Upload PO PDF"),
|
| 31 |
+
outputs=[gr.File(label="Download Excel File (will not work for now)"), gr.Textbox(label="Raw Text from PDF")],
|
| 32 |
+
title="PDF Text Extractor",
|
| 33 |
+
description="Upload a PDF file to view its raw text content for troubleshooting extraction issues."
|
| 34 |
)
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|