File size: 1,967 Bytes
c051cf9
 
 
d2251a9
c051cf9
 
 
 
 
 
 
 
 
 
 
2659c0a
c051cf9
 
 
 
2659c0a
c051cf9
 
 
2659c0a
c051cf9
 
 
 
 
 
 
 
 
 
 
 
 
 
2659c0a
c051cf9
 
 
 
 
 
 
 
2659c0a
80b61aa
c051cf9
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
import re
from typing import Dict, List, Union
import gradio as gr

def parse_federal_transformers(file_text: str) -> Dict[str, Union[str, List[Dict[str, Union[str, int, float]]]]]:
    parsed_data = {
        "Purchase Order No": "",
        "Date": "",
        "Supplier": "Federal Transformers Co. LLC",
        "Invoice Address": "",
        "Delivery Address": "",
        "Currency": "",
        "Payment Terms": "",
        "Items": []
    }

    try:
        # Extract Purchase Order No and Date
        parsed_data["Purchase Order No"] = re.search(r"Purchase Order No\.\s(\d+)", file_text).group(1)
        parsed_data["Date"] = re.search(r"Date:\s+(\d{2}-\w{3}-\d{2})", file_text).group(1)

        # Extract Invoice Address and Delivery Address
        parsed_data["Invoice Address"] = re.search(r"Invoice Address\s*:\s*(.*?)(?=\sDelivery Address)", file_text, re.DOTALL).group(1).strip()
        parsed_data["Delivery Address"] = re.search(r"Delivery Address\s*:\s*(.*?)(?=\sNote)", file_text, re.DOTALL).group(1).strip()

        # Define pattern for extracting item details
        item_pattern = re.compile(r"(\d+)\s+([\w\s]+)\s+(\d+)\s+([\d.]+)\s+([\d.]+)")
        for match in item_pattern.finditer(file_text):
            parsed_data["Items"].append({
                "Item No": match.group(1),
                "Description": match.group(2).strip(),
                "Quantity": int(match.group(3)),
                "Unit Price": float(match.group(4)),
                "Total Price": float(match.group(5)),
            })
    except Exception as e:
        print(f"Error parsing Federal Transformers PO: {e}")
    
    return parsed_data

# Create Gradio interface
iface = gr.Interface(
    fn=parse_federal_transformers,
    inputs="text",
    outputs="json",
    title="Federal Transformers PO Parser",
    description="Upload the text of a Federal Transformers purchase order to extract details."
)

if __name__ == "__main__":
    iface.launch()