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()