Spaces:
Sleeping
Sleeping
File size: 42,449 Bytes
f39814a eb53091 c79824c eb53091 f39814a c79824c eb53091 c79824c f39814a c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c c094882 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c eb53091 c79824c |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 |
# Minimal placeholder for FlexibleFinancialNoteGenerator
class FlexibleFinancialNoteGenerator:
def __init__(self):
pass
def generate_note(self, note_number, trial_balance_path=None):
# Placeholder logic
return True
def generate_all_notes(self, trial_balance_path=None):
# Placeholder logic
return {"dummy": True}
import json
import os
import logging
import requests
from datetime import datetime
from pathlib import Path
from dotenv import load_dotenv
import re
import sys
from typing import Dict, List, Any, Optional, Tuple
import pandas as pd
from pydantic import BaseModel, ValidationError
from pydantic_settings import BaseSettings
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from utils.utils import convert_note_json_to_lakhs
# Load environment variables
load_dotenv(dotenv_path=Path(__file__).parent.parent / '.env')
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class Settings(BaseSettings):
"""Application settings loaded from environment variables or .env file."""
openrouter_api_key: str = os.getenv('OPENROUTER_API_KEY', '')
api_url: str = "https://openrouter.ai/api/v1/chat/completions"
output_dir: str = "data/generated_notes"
trial_balance_json: str = "data/output1/parsed_trial_balance.json"
settings = Settings()
class Account(BaseModel):
account_name: str
amount: float
group: Optional[str] = None
class NoteTemplate(BaseModel):
title: str
full_title: str
# Add other fields as needed for your template structure
class GeneratedNote(BaseModel):
note_number: str
markdown_content: str
grand_total_lakhs: float
generated_on: str
assumptions: Optional[str] = None
# Add other fields as needed
class FlexibleFinancialNoteGenerator:
def __init__(self):
self.openrouter_api_key = settings.openrouter_api_key
if not self.openrouter_api_key:
logger.error("OPENROUTER_API_KEY not found in .env file")
raise ValueError("OPENROUTER_API_KEY not found in .env file")
self.api_url = settings.api_url
self.headers = {
"Authorization": f"Bearer {self.openrouter_api_key}",
"Content-Type": "application/json",
"HTTP-Referer": "https://localhost:3000",
"X-Title": "Financial Note Generator"
}
self.note_templates = self.load_note_templates()
# Updated model list with DeepSeek as first choice
self.recommended_models = [
"deepseek/deepseek-r1",
#"deepseek/deepseek-coder",
"mistralai/mixtral-8x7b-instruct"
]
def load_note_templates(self) -> Dict[str, Any]:
"""Load note templates from notes_template.py file."""
try:
# Add parent directory to path for imports when run as script
if __name__ == "__main__":
sys.path.append(str(Path(__file__).parent.parent))
from notes_template import note_templates
return note_templates
except ImportError as e:
logger.error(f"Error importing note_templates from notes_template: {e}")
return {}
except Exception as e:
logger.error(f"Unexpected error loading note_templates: {e}")
return {}
def load_trial_balance(self, file_path: str = settings.trial_balance_json) -> Optional[Dict[str, Any]]:
"""Load the complete trial balance from Excel or JSON."""
try:
if file_path.endswith('.json'):
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
if isinstance(data, list):
accounts = data
elif isinstance(data, dict):
accounts = data.get('accounts', [])
else:
logger.error(f"Unexpected trial balance format: {type(data)}")
return None
logger.info(f"Loaded trial balance with {len(accounts)} accounts")
return {"accounts": accounts}
elif file_path.endswith('.xlsx'):
from notes.data_extraction import extract_trial_balance_data
accounts = extract_trial_balance_data(file_path)
logger.info(f"Extracted trial balance with {len(accounts)} accounts from Excel")
return {"accounts": accounts}
else:
logger.error(f"Unsupported file type: {file_path}")
return None
except FileNotFoundError:
logger.error(f"Trial balance file not found: {file_path}")
return None
except Exception as e:
logger.error(f"Error loading trial balance: {e}")
return None
def build_llm_prompt(self, note_number: str, trial_balance_data: Dict[str, Any]) -> Optional[str]:
"""Build comprehensive LLM prompt with strict JSON output requirements"""
if note_number not in self.note_templates:
return None
template = self.note_templates[note_number]
all_accounts = trial_balance_data.get("accounts", [])
# Build context with full trial balance
context = {
"note_info": {
"number": note_number,
"title": template.get("title", ""),
"full_title": template.get("full_title", "")
},
"trial_balance": {
"total_accounts": len(all_accounts),
"accounts": all_accounts
},
"current_date": datetime.now().strftime("%Y-%m-%d"),
"financial_year": "2023-24"
}
# Get note-specific classification guidance
classification_guide = self._get_classification_guide(note_number)
prompt = f"""You are a senior financial analyst and chartered accountant with expertise in Indian accounting standards and Schedule III of the Companies Act 2013.
🔴 CRITICAL INSTRUCTIONS - MUST FOLLOW EXACTLY:
1. OUTPUT ONLY VALID JSON - NO MARKDOWN, NO EXPLANATIONS, NO TEXT OUTSIDE JSON
2. START YOUR RESPONSE WITH {{ and END WITH }}
3. DO NOT USE ```json``` CODE BLOCKS
4. DO NOT ADD ANY COMMENTARY OR EXPLANATIONS
🔴 REQUIRED JSON STRUCTURE - ALL FIELDS MANDATORY:
{{
"title": "{template.get('title', '')}",
"full_title": "{template.get('full_title', '')}",
"structure": [
{{
"category": "In Lakhs",
"subcategories": [
{{ "label": "March 31, 2024", "value": 0.00 }},
{{ "label": "March 31, 2023", "value": 0.00 }}
]
}},
{{
"category": "Category Name",
"subcategories": [
{{ "label": "Subcategory Item", "value": 0.00, "previous_value": 0.00 }}
],
"total": 0.00,
"previous_total": 0.00
}}
],
"metadata": {{
"note_number": {note_number},
"generated_on": "{datetime.now().isoformat()}"
}},
"assumptions": "List any assumptions made during classification"
}}
🔴 STRUCTURE ARRAY EXPLAINED:
- First element: Header row with column labels (March 31, 2024, March 31, 2023)
- Subsequent elements: Data categories with subcategories
- Each data category must have:
* "category": Main category name
* "subcategories": Array of line items with "label", "value", "previous_value"
* "total": Sum of current year values in subcategories
* "previous_total": Sum of previous year values in subcategories
🔴 YOUR TASK:
1. Analyze ALL trial balance accounts provided below
2. Identify accounts that belong to "{template['full_title']}"
3. Classify into appropriate subcategories per Schedule III
4. Convert all amounts to lakhs (₹ ÷ 100,000) with 2 decimal places
5. Calculate accurate totals ensuring mathematical consistency
6. Structure output in hierarchical "structure" array format
🔴 MATHEMATICAL REQUIREMENTS:
- All amounts MUST be in lakhs (divide original by 100,000)
- All subtotals MUST equal the grand total exactly
- Use 0.00 for March 2023 if data missing
- Round to 2 decimal places consistently
- Ensure "total" = sum of "value" in subcategories
- Ensure "previous_total" = sum of "previous_value" in subcategories
🔴 CLASSIFICATION GUIDANCE FOR NOTE {note_number}:
{classification_guide}
🔴 COMPLETE TRIAL BALANCE DATA:
{json.dumps(context, indent=2)}
🔴 TEMPLATE STRUCTURE TO FOLLOW:
{json.dumps(template, indent=2)}
🔴 VALIDATION RULES:
- If no accounts match this note category, use empty categories with 0.00 totals
- Ensure "metadata.note_number" exactly matches {note_number}
- Document classification logic in "assumptions" field
- Structure must have at least 2 elements (header + data)
GENERATE VALID JSON NOW (NO ADDITIONAL TEXT):"""
return prompt
def _get_classification_guide(self, note_number: str) -> str:
"""Get note-specific classification guidance"""
guides = {
"10": """
**Note 10 - Long Term Loans and Advances:**
- Include: Security deposits, long-term advances to suppliers/employees, deposits with utilities
- Categories: Unsecured considered good, Unsecured considered doubtful, Doubtful (provision)
- Exclude: Short-term advances, trade receivables, prepaid expenses under 1 year
""",
"11": """
**Note 11 - Inventories:**
- Include: Raw materials, work-in-progress, finished goods, stores and spares, consumables
- Value at lower of cost or net realizable value
- Exclude: Advances for inventory purchases (classify as advances)
""",
"12": """
**Note 12 - Trade Receivables:**
- Include: Amounts due from customers for goods/services, bills receivable
- Categories: Unsecured considered good, Unsecured considered doubtful, Provision for doubtful debts
- Exclude: Advances, deposits, other receivables
""",
"13": """
**Note 13 - Cash and Cash Equivalents:**
- Include: Cash on hand, balances with banks (current/savings), short-term deposits (≤3 months)
- Separate: Cash and cash equivalents vs Other bank balances (FDs >3 months)
- Show: Balances in current accounts, savings accounts, fixed deposits separately
""",
"14": """
**Note 14 - Short Term Loans and Advances:**
- Include: Prepaid expenses, advances to suppliers, employee advances, advance tax, TDS receivable
- Categories:
* Unsecured, considered good: Prepaid expenses, Other advances
* Other loans and advances: Advance tax, Balances with statutory/govt authorities
- Exclude: Long-term advances, trade receivables
""",
"15": """
**Note 15 - Other Current Assets:**
- Include: Interest accrued, export incentives receivable, insurance claims, other miscellaneous current assets
- Exclude: Items that fit into specific categories like trade receivables, advances, cash
"""
}
return guides.get(note_number, f"**Note {note_number}:** Classify accounts logically based on their nature and the note title.")
def call_openrouter_api(self, prompt: str) -> Optional[str]:
"""Make API call to OpenRouter with model fallback"""
for model in self.recommended_models:
logger.info(f"Trying model: {model}")
payload = {
"model": model,
"messages": [
{
"role": "system",
"content": "You are an expert chartered accountant specializing in Indian accounting standards. You MUST respond with ONLY valid JSON, never with markdown code blocks or explanations. Start with { and end with }."
},
{"role": "user", "content": prompt}
],
"max_tokens": 12000,
"temperature": 0.1,
"top_p": 0.9
}
try:
response = requests.post(
self.api_url,
headers=self.headers,
json=payload,
timeout=60
)
response.raise_for_status()
result = response.json()
content = result['choices'][0]['message']['content']
logger.info(f"Successful response from {model}")
return content
except requests.exceptions.HTTPError as e:
if e.response.status_code == 404:
logger.warning(f"Model {model} not found (404), trying next model")
elif e.response.status_code == 402:
logger.warning(f"Model {model} requires payment (402), trying next model")
else:
logger.error(f"HTTP error with {model}: {e}")
except Exception as e:
logger.error(f"Failed with {model}: {e}")
continue
logger.error("All models failed")
return None
def extract_json_from_markdown(self, response_text: str) -> Tuple[Optional[Dict[str, Any]], Optional[str]]:
"""Extract JSON from response, handling markdown code blocks and cleaning"""
response_text = response_text.strip()
# CRITICAL FIX: Handle concatenated/duplicate JSON (e.g., "}{\n{")
# Find the first complete JSON object
json_objects = []
brace_count = 0
start_idx = -1
for i, char in enumerate(response_text):
if char == '{':
if brace_count == 0:
start_idx = i
brace_count += 1
elif char == '}':
brace_count -= 1
if brace_count == 0 and start_idx != -1:
# Found complete JSON object
potential_json = response_text[start_idx:i+1]
try:
parsed = json.loads(potential_json)
json_objects.append((parsed, potential_json))
# Use the first valid JSON object
break
except json.JSONDecodeError:
continue
if json_objects:
logger.info("Successfully extracted first valid JSON object from response")
return json_objects[0]
# Fallback: Try original extraction methods
# Remove any leading/trailing text outside JSON
json_patterns = [
r'```json\s*(.*?)\s*```',
r'```\s*(.*?)\s*```',
r'(\{.*?\})'
]
for pattern in json_patterns:
match = re.search(pattern, response_text, re.DOTALL)
if match:
try:
json_content = match.group(1).strip()
json_data = json.loads(json_content)
return json_data, json_content
except json.JSONDecodeError:
continue
# Try parsing the entire response as JSON
try:
json_data = json.loads(response_text)
return json_data, response_text
except json.JSONDecodeError:
# Last attempt: find JSON-like structure
try:
start = response_text.find('{')
end = response_text.rfind('}') + 1
if start != -1 and end > start:
json_part = response_text[start:end]
json_data = json.loads(json_part)
return json_data, json_part
except json.JSONDecodeError:
pass
return None, None
def validate_and_fix_json(self, json_data: Dict[str, Any], note_number: str) -> Dict[str, Any]:
"""Validate JSON structure and auto-fix missing required fields"""
fixed_data = json_data.copy()
# Get template for this note
template = self.note_templates.get(note_number, {})
# Auto-fix title fields
if "title" not in fixed_data or not fixed_data["title"]:
fixed_data["title"] = template.get("title", f"Note {note_number}")
logger.info(f"Auto-fixed missing title field")
if "full_title" not in fixed_data or not fixed_data["full_title"]:
fixed_data["full_title"] = template.get("full_title", f"{note_number}. {fixed_data.get('title', 'Financial Note')}")
logger.info(f"Auto-fixed missing full_title field")
# Auto-fix or create metadata
if "metadata" not in fixed_data or not isinstance(fixed_data["metadata"], dict):
fixed_data["metadata"] = {}
logger.info("Auto-created metadata object")
# CRITICAL FIX: Ensure note_number is correct integer, not 0.0
metadata_note_num = fixed_data["metadata"].get("note_number")
try:
# Convert note_number string to int
expected_note_num = int(note_number)
# Check if metadata note_number is wrong (0, 0.0, or mismatch)
if (metadata_note_num is None or
metadata_note_num == 0 or
metadata_note_num == 0.0 or
int(metadata_note_num) != expected_note_num):
fixed_data["metadata"]["note_number"] = expected_note_num
logger.info(f"Auto-corrected metadata.note_number from {metadata_note_num} to {expected_note_num}")
except ValueError:
fixed_data["metadata"]["note_number"] = note_number
logger.info(f"Auto-set metadata.note_number to string: {note_number}")
if "generated_on" not in fixed_data["metadata"]:
fixed_data["metadata"]["generated_on"] = datetime.now().isoformat()
logger.info("Auto-fixed missing metadata.generated_on field")
# Auto-fix or create structure array
if "structure" not in fixed_data or not isinstance(fixed_data["structure"], list):
logger.warning("Structure array missing, creating default structure")
fixed_data["structure"] = [
{
"category": "In Lakhs",
"subcategories": [
{"label": "March 31, 2024", "value": 0.00},
{"label": "March 31, 2023", "value": 0.00}
]
},
{
"category": "No data available",
"subcategories": [
{"label": "Items", "value": 0.00, "previous_value": 0.00}
],
"total": 0.00,
"previous_total": 0.00
}
]
else:
# Validate and fix structure elements
if len(fixed_data["structure"]) == 0:
logger.warning("Empty structure array, adding default elements")
fixed_data["structure"] = [
{
"category": "In Lakhs",
"subcategories": [
{"label": "March 31, 2024", "value": 0.00},
{"label": "March 31, 2023", "value": 0.00}
]
}
]
# Ensure each structure element has required fields
for i, struct_elem in enumerate(fixed_data["structure"]):
if not isinstance(struct_elem, dict):
continue
if "category" not in struct_elem:
struct_elem["category"] = f"Category {i}"
if "subcategories" not in struct_elem or not isinstance(struct_elem["subcategories"], list):
struct_elem["subcategories"] = []
# For data rows (not header), ensure totals exist
if i > 0 and struct_elem.get("subcategories"):
if "total" not in struct_elem:
struct_elem["total"] = sum(
sub.get("value", 0.0)
for sub in struct_elem["subcategories"]
if isinstance(sub, dict)
)
if "previous_total" not in struct_elem:
struct_elem["previous_total"] = sum(
sub.get("previous_value", 0.0)
for sub in struct_elem["subcategories"]
if isinstance(sub, dict)
)
# Auto-fix assumptions
if "assumptions" not in fixed_data:
fixed_data["assumptions"] = "Classification based on account names and standard accounting practices"
logger.info("Auto-added default assumptions")
return fixed_data
def validate_json_structure(self, json_data: Dict[str, Any], note_number: str) -> Tuple[bool, str]:
"""Validate that the JSON matches expected structure"""
required_fields = ["title", "full_title", "structure", "metadata", "assumptions"]
# Check required fields
missing_fields = []
for field in required_fields:
if field not in json_data:
missing_fields.append(field)
if missing_fields:
return False, f"Missing required fields: {', '.join(missing_fields)}"
# Check metadata structure
if not isinstance(json_data.get("metadata"), dict):
return False, "metadata must be an object"
metadata = json_data["metadata"]
if "note_number" not in metadata:
return False, "metadata.note_number is required"
if str(metadata.get("note_number", "")) != str(note_number):
return False, f"Note number mismatch: expected {note_number}, got {metadata.get('note_number')}"
# Check structure array
if not isinstance(json_data.get("structure"), list):
return False, "structure must be an array"
if len(json_data["structure"]) == 0:
return False, "structure array cannot be empty"
return True, "Validation passed"
def _generate_markdown_from_structure(self, json_data: Dict[str, Any]) -> str:
"""Generate markdown table from structure array"""
try:
title = json_data.get("full_title", json_data.get("title", "Financial Note"))
structure = json_data.get("structure", [])
if not structure:
return f"# {title}\n\n*No data available*"
# Start markdown
md_lines = [f"# {title}\n"]
# Get header row (first element)
header_elem = structure[0] if len(structure) > 0 else None
if header_elem and header_elem.get("subcategories"):
headers = [sub.get("label", "") for sub in header_elem["subcategories"]]
md_lines.append("| Particulars | " + " | ".join(headers) + " |")
md_lines.append("|" + "---|" * (len(headers) + 1))
# Process data rows
for i in range(1, len(structure)):
elem = structure[i]
category = elem.get("category", "")
subcategories = elem.get("subcategories", [])
# Add category header if exists
if category:
md_lines.append(f"\n**{category}**\n")
# Add subcategory rows
for sub in subcategories:
label = sub.get("label", "")
value = sub.get("value", 0.00)
previous_value = sub.get("previous_value", 0.00)
md_lines.append(f"| {label} | {value:.2f} | {previous_value:.2f} |")
# Add total row if exists
if "total" in elem:
total = elem.get("total", 0.00)
previous_total = elem.get("previous_total", 0.00)
md_lines.append(f"| **Total {category}** | **{total:.2f}** | **{previous_total:.2f}** |")
# Add metadata
metadata = json_data.get("metadata", {})
md_lines.append(f"\n\n*Generated on: {metadata.get('generated_on', 'Unknown')}*")
# Add assumptions if present
assumptions = json_data.get("assumptions", "")
if assumptions:
md_lines.append(f"\n\n**Assumptions:** {assumptions}")
return "\n".join(md_lines)
except Exception as e:
logger.error(f"Error generating markdown from structure: {e}")
return f"# {json_data.get('full_title', 'Financial Note')}\n\n*Error generating markdown table*"
def save_generated_note(self, note_data: str, note_number: str, output_dir: str = settings.output_dir) -> bool:
"""Save the generated note to file with robust validation and auto-fixing"""
Path(output_dir).mkdir(parents=True, exist_ok=True)
json_output_path = f"{output_dir}/notes.json"
raw_output_path = f"{output_dir}/notes_raw.txt"
formatted_md_path = f"{output_dir}/notes_formatted.md"
try:
# Always save raw response for debugging
with open(raw_output_path, 'w', encoding='utf-8') as f:
f.write(note_data)
# Extract and validate JSON
json_data, json_string = self.extract_json_from_markdown(note_data)
if json_data:
# Auto-fix missing or incorrect fields
json_data = self.validate_and_fix_json(json_data, note_number)
# Final validation
is_valid, validation_msg = self.validate_json_structure(json_data, note_number)
if not is_valid:
logger.warning(f"JSON validation warning after auto-fix: {validation_msg}")
# Convert to lakhs if needed
json_data = convert_note_json_to_lakhs(json_data)
# Save JSON
with open(json_output_path, 'w', encoding='utf-8') as f:
json.dump(json_data, f, indent=2, ensure_ascii=False)
logger.info(f"JSON saved to {json_output_path}")
# Generate and save markdown
md_content = json_data.get('markdown_content', '')
if not md_content:
# Generate markdown from structure
md_content = self._generate_markdown_from_structure(json_data)
logger.info("Auto-generated markdown from structure array")
with open(formatted_md_path, 'w', encoding='utf-8') as f:
f.write(md_content)
return True
else:
# Create fallback JSON with all required fields
template = self.note_templates.get(note_number, {})
fallback_json = {
"title": template.get("title", f"Note {note_number}"),
"full_title": template.get("full_title", f"{note_number}. Financial Note"),
"structure": [
{
"category": "In Lakhs",
"subcategories": [
{"label": "March 31, 2024", "value": 0.00},
{"label": "March 31, 2023", "value": 0.00}
]
},
{
"category": "Error - No data",
"subcategories": [
{"label": "Could not parse response", "value": 0.00, "previous_value": 0.00}
],
"total": 0.00,
"previous_total": 0.00
}
],
"metadata": {
"note_number": int(note_number) if note_number.isdigit() else note_number,
"generated_on": datetime.now().isoformat()
},
"assumptions": "Failed to parse LLM response",
"raw_response": note_data,
"error": "Could not parse JSON from response"
}
with open(json_output_path, 'w', encoding='utf-8') as f:
json.dump(fallback_json, f, indent=2, ensure_ascii=False)
logger.warning(f"Fallback JSON with required fields saved to {json_output_path}")
return False
except Exception as e:
logger.error(f"Error saving files: {e}")
# Emergency fallback
try:
template = self.note_templates.get(note_number, {})
emergency_json = {
"title": template.get("title", f"Note {note_number}"),
"full_title": template.get("full_title", f"{note_number}. Financial Note"),
"structure": [
{
"category": "In Lakhs",
"subcategories": [
{"label": "March 31, 2024", "value": 0.00},
{"label": "March 31, 2023", "value": 0.00}
]
}
],
"metadata": {
"note_number": int(note_number) if note_number.isdigit() else note_number,
"generated_on": datetime.now().isoformat()
},
"assumptions": "Emergency fallback due to processing error",
"error": str(e)
}
with open(json_output_path, 'w', encoding='utf-8') as f:
json.dump(emergency_json, f, indent=2, ensure_ascii=False)
logger.info(f"Emergency fallback JSON saved to {json_output_path}")
except Exception as emergency_error:
logger.error(f"Emergency fallback also failed: {emergency_error}")
return False
def generate_note(self, note_number: str, trial_balance_path: str = settings.trial_balance_json) -> bool:
"""Generate a specific note based on note number"""
if note_number not in self.note_templates:
logger.error(f"Note template {note_number} not found")
return False
logger.info(f"Starting Note {note_number} generation...")
# Load complete trial balance
trial_balance = self.load_trial_balance(trial_balance_path)
if not trial_balance:
return False
# Build prompt with full trial balance
prompt = self.build_llm_prompt(note_number, trial_balance)
if not prompt:
logger.error("Failed to build prompt")
return False
# Get LLM response
response = self.call_openrouter_api(prompt)
if not response:
logger.error("Failed to get API response")
return False
# Save the generated note
success = self.save_generated_note(response, note_number)
logger.info(f"Note {note_number} {'generated successfully' if success else 'generated with issues'}")
return success
def generate_all_notes(self, trial_balance_path: str = settings.trial_balance_json) -> Dict[str, bool]:
"""Generate all available notes and save them in a single notes.json file."""
logger.info(f"Starting generation of all {len(self.note_templates)} notes...")
results = {}
all_notes = []
# Load trial balance once
trial_balance = self.load_trial_balance(trial_balance_path)
if not trial_balance:
logger.error("Failed to load trial balance")
return {note: False for note in self.note_templates.keys()}
for note_number in self.note_templates.keys():
logger.info(f"Processing Note {note_number}")
# Build prompt for this note
prompt = self.build_llm_prompt(note_number, trial_balance)
if not prompt:
results[note_number] = False
continue
# Get LLM response
response = self.call_openrouter_api(prompt)
if not response:
results[note_number] = False
continue
# Parse JSON response
json_data, _ = self.extract_json_from_markdown(response)
if json_data:
# Auto-fix and validate
json_data = self.validate_and_fix_json(json_data, note_number)
is_valid, validation_msg = self.validate_json_structure(json_data, note_number)
if is_valid:
json_data = convert_note_json_to_lakhs(json_data)
all_notes.append(json_data)
results[note_number] = True
logger.info(f"Note {note_number} processed successfully")
else:
logger.warning(f"Note {note_number} validation failed even after auto-fix: {validation_msg}")
# Still include it but mark as failed
json_data = convert_note_json_to_lakhs(json_data)
all_notes.append(json_data)
results[note_number] = False
else:
logger.error(f"Note {note_number}: Could not parse JSON from response")
# Create fallback note with new structure
template = self.note_templates.get(note_number, {})
fallback_note = {
"title": template.get("title", f"Note {note_number}"),
"full_title": template.get("full_title", f"{note_number}. Financial Note"),
"structure": [
{
"category": "In Lakhs",
"subcategories": [
{"label": "March 31, 2024", "value": 0.00},
{"label": "March 31, 2023", "value": 0.00}
]
},
{
"category": "Error",
"subcategories": [
{"label": "Failed to generate from LLM response", "value": 0.00, "previous_value": 0.00}
],
"total": 0.00,
"previous_total": 0.00
}
],
"metadata": {
"note_number": int(note_number) if note_number.isdigit() else note_number,
"generated_on": datetime.now().isoformat()
},
"assumptions": "LLM response parsing failed",
"error": "JSON parsing failed"
}
all_notes.append(fallback_note)
results[note_number] = False
# Brief pause between API calls
import time
time.sleep(2)
# Save all notes in consolidated file
output_dir = settings.output_dir
Path(output_dir).mkdir(parents=True, exist_ok=True)
consolidated_output = {
"notes": all_notes,
"generation_summary": {
"total_notes": len(self.note_templates),
"successful_notes": sum(1 for success in results.values() if success),
"failed_notes": sum(1 for success in results.values() if not success),
"generated_on": datetime.now().isoformat(),
"results": results
}
}
with open(f"{output_dir}/notes.json", "w", encoding="utf-8") as f:
json.dump(consolidated_output, f, indent=2, ensure_ascii=False)
successful = sum(1 for success in results.values() if success)
total = len(results)
logger.info(f"GENERATION SUMMARY: {successful}/{total} notes generated successfully")
logger.info(f"All notes saved to {output_dir}/notes.json")
return results
def main() -> None:
"""Main function to run the flexible note generator"""
try:
# Initialize generator
generator = FlexibleFinancialNoteGenerator()
if not generator.note_templates:
logger.error("No note templates loaded. Check notes_template.py")
return
logger.info(f"Loaded {len(generator.note_templates)} note templates")
# Check for command line arguments
if len(sys.argv) > 1:
# Command line mode
if len(sys.argv) < 3:
logger.error("Usage: python llm_notes_generator.py <mode> <note_numbers>")
logger.error(" mode: 'specific' or 'all'")
logger.error(" note_numbers: comma-separated note numbers (for specific mode)")
sys.exit(1)
mode = sys.argv[1].lower()
note_numbers = sys.argv[2] if len(sys.argv) > 2 else ""
if mode == "specific":
if not note_numbers:
logger.error("Note numbers required for specific mode")
sys.exit(1)
note_list = [n.strip() for n in note_numbers.split(",")]
all_notes = []
successful_notes = []
for note_number in note_list:
if note_number in generator.note_templates:
success = generator.generate_note(note_number)
if success:
# Load the generated note
try:
with open("data/generated_notes/notes.json", "r", encoding="utf-8") as f:
note_data = json.load(f)
all_notes.append(note_data)
successful_notes.append(note_number)
logger.info(f"Note {note_number} generated successfully")
except Exception as e:
logger.error(f"Failed to load generated note {note_number}: {e}")
else:
logger.error(f"Failed to generate note {note_number}")
else:
logger.error(f"Note {note_number} not found in templates")
# Save consolidated notes
if all_notes:
output_dir = settings.output_dir
Path(output_dir).mkdir(parents=True, exist_ok=True)
consolidated = {
"notes": all_notes,
"generation_summary": {
"requested_notes": note_list,
"successful_notes": successful_notes,
"total_successful": len(successful_notes),
"generated_on": datetime.now().isoformat()
}
}
with open(f"{output_dir}/notes.json", "w", encoding="utf-8") as f:
json.dump(consolidated, f, indent=2, ensure_ascii=False)
logger.info(f"Consolidated notes saved to {output_dir}/notes.json")
elif mode == "all":
results = generator.generate_all_notes()
successful = sum(1 for success in results.values() if success)
total = len(results)
logger.info(f"{successful}/{total} notes generated successfully")
# Print detailed results
for note, success in results.items():
status = "✅ SUCCESS" if success else "❌ FAILED"
logger.info(f" Note {note}: {status}")
else:
logger.error("Invalid mode. Use 'specific' or 'all'")
sys.exit(1)
else:
# Interactive mode
choice = input("\nGenerate (1) specific note or (2) all notes? Enter 1 or 2: ").strip()
if choice == "1":
available_notes = list(generator.note_templates.keys())
print(f"Available notes: {', '.join(available_notes)}")
note_number = input("Enter note number: ").strip()
if note_number in available_notes:
success = generator.generate_note(note_number)
logger.info(f"Note {note_number} {'generated successfully' if success else 'generated with issues'}")
else:
logger.error(f"Note {note_number} not found")
elif choice == "2":
results = generator.generate_all_notes()
successful = sum(1 for success in results.values() if success)
total = len(results)
logger.info(f"{successful}/{total} notes generated successfully")
# Print summary
print("\n" + "="*50)
print("GENERATION SUMMARY")
print("="*50)
for note, success in results.items():
status = "✅ SUCCESS" if success else "❌ FAILED"
print(f"Note {note}: {status}")
print("="*50)
else:
logger.error("Invalid choice. Enter 1 or 2.")
except KeyboardInterrupt:
logger.info("Generation interrupted by user")
sys.exit(0)
except Exception as e:
logger.error(f"Error: {e}", exc_info=True)
sys.exit(1)
if __name__ == "__main__":
main() |