1from transformers import AutoTokenizer, AutoModelForCausalLM
2from transformers import TextStreamer
3
4model_id = "yasserrmd/LFM2-350M-Extract-TOON"
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
7
8schema = """
9"$schema": "http://json-schema.org/draft-07/schema#"
10type: object
11properties:
12id:
13type: string
14pattern: "^(\\d+\\.\\d+) disturbing$"
15description: Dot-separated integers representing the unique ID of each element in the hierarchy
16title:
17type: string
18description: Descriptive title of the section or element
19level:
20type: integer
21minimum: 0
22maximum: 9
23description: "Hierarchical level (0 - ROOT, 1 - SECTION, 2 - SUBSECTION, 3+ - DETAIL_N)"
24level_type:
25type: string
26enum[4]: ROOT,SECTION,SUBSECTION,DETAIL_N
27description: Type of the hierarchical element
28component:
29type: array
30items:
31type: object
32properties:
33idc:
34type: integer
35description: Component ID
36component_type:
37type: string
38enum[4]: PARAGRAPH,TABLE,CALCULATION,CHECKBOX
39description: Type of component
40metadata:
41type: string
42description: "Additional metadata (e.g., title, note, or overview)"
43properties:
44type: object
45properties:
46variables:
47type: array
48items:
49type: object
50properties:
51idx:
52type: string
53description: Unique row-column identifier (X.Y format)
54name:
55type: string
56description: Attribute name
57value:
58type: string
59description: Attribute value
60unit:
61type[2]: string,"null"
62description: Optional unit for the value
63metrics:
64type: boolean
65description: Boolean flag indicating if the attribute is a metric
66formula:
67type: boolean
68description: Boolean flag indicating if the attribute is a formula
69content:
70type: array
71items:
72type[2]: string,"null"
73description: Text content
74children:
75type: array
76items:
77"$ref": #
78required[6]: id,title,level,level_type,component,children
79"""
80text = """
81SUBSECTION component[1]: - idc: 1 component_type: PARAGRAPH metadata: "<note>Note: Specific to debtor risk.</note>" properties: variables[0]: content[1]: The risk of debtors failing to make payments on time. - id: "2.2" title: Liquidity Risk level: 2 level_type: SUBSECTION component[1]: - idc: 1 component_type: PARAGRAPH metadata: "<note>Note: Specific to liquidity risk.</note>" properties: variables[0]: content[1]: Liquidity risk is related to the difficulty in selling assets quickly without a significant loss.
82
83The document begins with an inclusive overview, elucidating the purpose of the report and its objective to assess risks and propose mitigations for financial operations, such as compliance, fraud detection, and performance metrics. The overall framework is meticulously divided into several sections and subsections reflecting detailed and structured analysis.
84
85This report is intended to provide a comprehensive understanding of risk exposure within financial operations. We will now delve into the first section of the report, which covers a vast array of compliance regulations critical for maintaining financial accountability.
86
87Firstly, let’s examine the **Compliance Section**. The section’s primary aim is to highlight the key compliance regulations applicable to financial operations. Notably, this includes the **Anti-Money Laundering (AML) Regulation (RC.1)** and the **Data Privacy Act (RC.2)**. Highlighting the significance of these regulations, the Subsection on Anti-Money Laundering identifies several gaps within the current system. These gaps need to be addressed to ensure robust compliance. The analysis suggests the presence of several risk points where the current practices might fall short of regulatory standards.
88
89Next, we have a **Detailed Risk Analysis** for the Anti-Money Laundering Regulation. This component outlines the specific risks and potential impacts on financial operations. In the document, a table detailing the risk assessment is provided outlining two primary risks, **Fraudulent Transactions (RA.1)**, and **Non-Compliance with AML (RA.2)**, each with a brief description of the risk and its possible consequences. Addressing these risks requires a systematic approach, ensuring all preventive measures are in place to mitigate financial risks effectively.
90
91Moreover, a **Checklist** is included to assess the current status concerning the Anti-Money Laundering Regulation. The Checklist requires the selection of the best option that describes the current status as either **Option 1 (true)** or **Option 2 (false)**. This selection is pivotal in making informed decisions about regulatory compliance and operational adjustments.
92
93In parallel, the **Data Privacy Act** (RC.2) Subsection identifies several issues in handling personal data. These issues need to be corrected to fully comply with the Data Privacy Act. The **Fraud Detection Section** and its **Subsections on Misrepresentation and Theft of Data** follow a similar structure, detailing the critical risks associated with these vulnerabilities and emphasizing the necessity for mitigation strategies.
94
95In the **Fraud Detection Section**, we have a table outlining two major cases of fraud: **Misrepresentation (FC.1)** and **Theft of Data (FC.2)**. These cases are significant due to their impact on financial integrity and operational continuity. The analysis of these cases includes detailed descriptions of the nature and extent of the fraud, highlighting the importance of robust fraud detection mechanisms.
96
97Each regulatory and fraud-related section is equipped with thorough analysis and checks, ensuring that every risk is identified and addressed. While the sections provide detailed tables and checklists, they also reflect the broader context of financial operations and the mitigation strategies required to ensure compliance and prevent fraud.
98
99By providing these detailed sections and sub-sections, the report aims to equip stakeholders with the necessary information to assess and improve the risk management framework. This ensures that all financial operations are conducted in a compliant, transparent, and secure manner, thereby safeguarding the interests of all stakeholders involved.
100
101"""
102
103system_instruction = (
104 "You are an intelligent model specialized in converting natural language text"
105 "into valid TOON (Token-Oriented Object Notation) format. "
106 "Always follow the given schema strictly, emit the correct header "
107 "in the form <label>[1]{fields}: followed by exactly one values row. "
108 "Do not include explanations or additional commentary."
109 )
110
111
112user_prompt = (
113 f'Generate TOON format using the schema {schema} '
114 f'for the below text "{text}".'
115)
116
117
118messages = [
119 {"role": "system", "content": system_instruction},
120 {"role": "user", "content": user_prompt}
121]
122
123
124inputs = tokenizer.apply_chat_template(
125 messages,
126 add_generation_prompt = True, # Must add for generation
127 return_tensors = "pt",
128 tokenize = True,
129 return_dict = True,
130).to("cuda")
131
132
133_ = model.generate(
134 **inputs,
135 max_new_tokens = 2046, # Increase for longer outputs!
136 # Recommended Liquid settings!
137 temperature = 0.3, min_p = 0.15, repetition_penalty = 1.05,
138 streamer = TextStreamer(tokenizer, skip_prompt = True),
139)