Tally SQLCoder - Fine-tuned for TallyPrime ERP
Created by: Jay Viramgami
A fine-tuned LLaMA 3 SQLCoder model specialized for converting natural language questions to PostgreSQL queries for TallyPrime ERP systems.
🎯 Model Description
This model is specifically trained to understand accounting and business terminology used in TallyPrime ERP and generate accurate SQL queries for a PostgreSQL database migrated from Tally.
Key Features
🏦 Accounting Domain Expertise - Understands financial terms, GST, vouchers, ledgers
📊 28 Database Tables - Covers all master and transaction tables from Tally
🎓 ICAI Compliant - Based on Indian accounting standards
🚀 Fast Inference - Optimized with QLoRA for efficient deployment
💯 High Accuracy - Fine-tuned on 5,000+ Tally-specific query pairs
Use Cases
Customer receivables and vendor payables analysis
Sales and purchase reporting
Inventory and stock management queries
GST and tax compliance reports
Financial statements (Profit & Loss, Balance Sheet)
Voucher and transaction searches
📊 Model Details
Base Model: defog/llama-3-sqlcoder-8b
Fine-tuning Method: QLoRA (4-bit quantization)
Training Data: 5,000 synthetic Tally accounting text-to-SQL pairs
Target Database: PostgreSQL (Tally migration schema with 28 tables)
Training Platform: Kaggle (NVIDIA T4 GPU)
Training Time: ~4 hours
Final Training Loss: 0.05-0.07
Query Categories Supported
Category Templates Examples Simple Filters 20 "Show all customers", "List bank accounts" Date Ranges 20 "Sales in March 2024", "Payments this quarter" Aggregations 25 "Total sales amount", "Top 10 customers" Joins 25 "Customer-wise outstanding", "Item sales by godown" Accounting 20 "P&L items", "Assets and liabilities" GST/Tax 15 "GST collected", "TDS deducted" Inventory 15 "Stock movements", "Items with zero balance" Financial Statements 10 "Trial balance", "Balance sheet data"
🚀 Quick Start
Installation
pip install transformers peft torch accelerate bitsandbytes
Basic Usage
1 from transformers import AutoModelForCausalLM , AutoTokenizer
2 from peft import PeftModel
3 import torch
4
5 # Load base model
6 base_model = AutoModelForCausalLM . from_pretrained (
7 "defog/llama-3-sqlcoder-8b" ,
8 device_map = "auto" ,
9 torch_dtype = torch . float16
10 )
11
12 # Load fine-tuned adapter
13 model = PeftModel . from_pretrained ( base_model , "jaykv/tally-sqlcoder-finetuned" )
14 tokenizer = AutoTokenizer . from_pretrained ( "jaykv/tally-sqlcoder-finetuned" )
15
16 # Generate SQL
17 question = "Show all customers with outstanding balance above 50000"
18 schema = """CREATE TABLE mst_ledger (
19 name VARCHAR(1024),
20 parent VARCHAR(1024),
21 closing_balance DECIMAL(17,2)
22 );"""
23
24 prompt = f"""### Task
25 Generate a SQL query to answer [QUESTION] { question } [/QUESTION]
26
27 ### Database Schema
28 The query will run on a database with the following schema:
29 { schema }
30
31 ### Answer
32 Given the database schema, here is the SQL query that answers [QUESTION] { question } [/QUESTION]
33 [SQL]"""
34
35 inputs = tokenizer ( prompt , return_tensors = "pt" , truncation = True , max_length = 2048 ) . to ( model . device )
36
37 with torch . no_grad ( ) :
38 outputs = model . generate (
39 ** inputs ,
40 max_new_tokens = 300 ,
41 temperature = 0.1 ,
42 do_sample = True ,
43 pad_token_id = tokenizer . eos_token_id
44 )
45
46 result = tokenizer . decode ( outputs [ 0 ] , skip_special_tokens = True )
47 sql = result . split ( "[SQL]" ) [ - 1 ] . strip ( )
48 print ( sql )
Output:
1 SELECT name , closing_balance
2 FROM mst_ledger
3 WHERE parent = 'Sundry Debtors'
4 AND closing_balance > 50000
📝 Example Queries
Simple Customer Query
Question: "Show all customers"
Generated SQL:
SELECT name FROM mst_ledger WHERE parent = 'Sundry Debtors'
Sales Analysis
Question: "What is the total sales amount for March 2024?"
Generated SQL:
1 SELECT SUM ( ABS ( amount ) ) as total_sales
2 FROM trn_accounting ta
3 JOIN trn_voucher tv ON ta . guid = tv . guid
4 WHERE tv . voucher_type = 'Sales'
5 AND tv . date BETWEEN '2024-03-01' AND '2024-03-31'
Top Customers
Question: "Show top 10 customers by sales"
Generated SQL:
1 SELECT tv . party_name , SUM ( ABS ( ta . amount ) ) as total_sales
2 FROM trn_voucher tv
3 JOIN trn_accounting ta ON tv . guid = ta . guid
4 WHERE tv . voucher_type = 'Sales'
5 GROUP BY tv . party_name
6 ORDER BY total_sales DESC
7 LIMIT 10
GST Query
Question: "Show GST payable amount"
Generated SQL:
1 SELECT name , closing_balance
2 FROM mst_ledger
3 WHERE parent = 'Duties & Taxes'
4 AND name LIKE '%GST%'
🗄️ Database Schema
The model is trained on a PostgreSQL schema with 28 tables from TallyPrime:
Master Tables (15)
mst_ledger - Customers, vendors, banks, expenses, incomes
mst_group - Account group hierarchy
mst_stock_item - Inventory items with GST details
mst_stock_group - Stock categories
mst_vouchertype - Voucher type definitions
mst_godown - Warehouse locations
mst_cost_centre - Cost centers
And 8 more...
Transaction Tables (13)
trn_voucher - All financial transactions
trn_accounting - Ledger-wise entries
trn_inventory - Item-wise stock movements
trn_bill - Bill allocations
trn_bank - Bank transaction details
And 8 more...
📈 Training Details
Dataset
Size: 5,000 text-to-SQL pairs
Source: Synthetically generated using 150 query templates
Split: 90/10 train/test
Categories: 8 query types covering all Tally operations
Training Configuration
Method: QLoRA (Quantized Low-Rank Adaptation)
Quantization: 4-bit (NF4)
LoRA Rank: 16
LoRA Alpha: 32
Target Modules: q_proj, k_proj, v_proj, o_proj
Batch Size: 2 per device
Gradient Accumulation: 4 steps
Learning Rate: 2e-4
Epochs: ~3 (1,600 steps)
Optimizer: PagedAdamW 8-bit
Max Sequence Length: 2048 tokens
Hardware
Platform: Kaggle Notebooks
GPU: NVIDIA T4 (16GB)
Training Time: ~4 hours
📊 Performance
Valid SQL Syntax: >95%
Keyword Match: >85%
Exact Match (normalized): >70%
⚠️ Limitations
Tally-Specific: Optimized for TallyPrime PostgreSQL schema
PostgreSQL Only: SQL generated for PostgreSQL dialect
Schema Required: Needs database schema in the prompt
Context Window: Limited to 2048 tokens
Custom Schemas: May require additional fine-tuning for non-Tally schemas
🔧 Deployment Tips
For Production Use:
Add validation - Verify generated SQL before execution
Read-only mode - Restrict to SELECT queries only
Query timeout - Set execution time limits
Error handling - Catch and handle syntax errors
Logging - Track all queries for audit
Optimization:
Use GPU for faster inference (2-3 seconds per query)
CPU inference works but is slower (~10-15 seconds)
Consider caching frequently asked queries
📄 License
This model is released under the Apache 2.0 license, inheriting from the base model.
🙏 Acknowledgments
Base Model: defog/llama-3-sqlcoder-8b by Defog.ai
Training Standards: ICAI (Institute of Chartered Accountants of India) Foundation Course
Platform: Trained on Kaggle's free GPU infrastructure
📧 Contact
Author: Jay Viramgami
For questions, feedback, or collaboration inquiries, please open an issue on the model's discussion page.
🔗 Related Resources
Citation
If you use this model in your work, please cite:
1 @misc{tally-sqlcoder-finetuned,
2 author = {Jay Viramgami},
3 title = {Tally SQLCoder - Fine-tuned for TallyPrime ERP},
4 year = {2024},
5 publisher = {HuggingFace},
6 url = {https://huggingface.co/jaykv/tally-sqlcoder-finetuned}
7 }
Model Card created by Jay Viramgami | March 2024