ZeeAI1 commited on
Commit
aa6544c
·
verified ·
1 Parent(s): 78f4f24

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import xmlrpc.client
3
+ from transformers import pipeline
4
+
5
+ # --- CONFIGURATION ---
6
+ ODOO_URL = "https://your-odoo-instance.com"
7
+ ODOO_DB = "your-database"
8
+ ODOO_USERNAME = "your-username"
9
+ ODOO_PASSWORD = "your-password"
10
+ ACCOUNT_ID = 41 # Replace with correct expense account ID
11
+
12
+ # --- 1. LOAD NLP MODEL ---
13
+ print("Loading Hugging Face model...")
14
+ model = pipeline("text2text-generation", model="google/flan-t5-base")
15
+
16
+ # --- 2. GET USER PROMPT ---
17
+ user_prompt = input("Enter your accounting prompt:\n> ")
18
+
19
+ # --- 3. NLP PROCESSING ---
20
+ formatted_prompt = (
21
+ f"Extract as JSON with fields transaction_type, vendor_name, amount, date, description from: {user_prompt}"
22
+ )
23
+
24
+ result = model(formatted_prompt, max_length=256)[0]['generated_text']
25
+ print("\nRaw Model Output:\n", result)
26
+
27
+ # --- 4. PARSE JSON ---
28
+ try:
29
+ output_data = json.loads(result)
30
+ except json.JSONDecodeError:
31
+ print("\nERROR: Failed to parse output as JSON. Please adjust model or prompt.")
32
+ exit(1)
33
+
34
+ # Debug print
35
+ print("\nParsed Data:\n", output_data)
36
+
37
+ vendor_name = output_data['vendor_name']
38
+ amount = float(output_data['amount'])
39
+ invoice_date = output_data['date']
40
+ description = output_data['description']
41
+
42
+ # --- 5. CONNECT TO ODOO ---
43
+ print("\nConnecting to Odoo...")
44
+ common = xmlrpc.client.ServerProxy(f"{ODOO_URL}/xmlrpc/2/common")
45
+ uid = common.authenticate(ODOO_DB, ODOO_USERNAME, ODOO_PASSWORD, {})
46
+ models = xmlrpc.client.ServerProxy(f"{ODOO_URL}/xmlrpc/2/object")
47
+
48
+ # --- 6. FIND OR CREATE VENDOR ---
49
+ partner_id = models.execute_kw(ODOO_DB, uid, ODOO_PASSWORD, 'res.partner', 'search', [[['name', '=', vendor_name]]])
50
+ if not partner_id:
51
+ partner_id = models.execute_kw(ODOO_DB, uid, ODOO_PASSWORD, 'res.partner', 'create', [{
52
+ 'name': vendor_name,
53
+ 'supplier_rank': 1
54
+ }])
55
+ print(f"Vendor '{vendor_name}' created with ID {partner_id}")
56
+ else:
57
+ partner_id = partner_id[0]
58
+ print(f"Vendor '{vendor_name}' found with ID {partner_id}")
59
+
60
+ # --- 7. CREATE VENDOR BILL ---
61
+ invoice_id = models.execute_kw(ODOO_DB, uid, ODOO_PASSWORD, 'account.move', 'create', [{
62
+ 'move_type': 'in_invoice',
63
+ 'partner_id': partner_id,
64
+ 'invoice_date': invoice_date,
65
+ 'invoice_line_ids': [(0, 0, {
66
+ 'name': description,
67
+ 'quantity': 1,
68
+ 'price_unit': amount,
69
+ 'account_id': ACCOUNT_ID,
70
+ })],
71
+ }])
72
+
73
+ print(f"\nVendor Bill Created Successfully in Odoo! Invoice ID: {invoice_id}")