Sahil Garg commited on
Commit
452e581
·
1 Parent(s): 5fd0efa

/bs_from_notes working perfectly

Browse files
Files changed (3) hide show
  1. app/api.py +9 -4
  2. pnlbs/bl_llm.py +39 -10
  3. pnlbs/sircodebs.py +8 -2
app/api.py CHANGED
@@ -328,7 +328,7 @@ async def bs_from_notes(file: UploadFile = File(...)):
328
  if os.getenv("OPENROUTER_API_KEY"):
329
  env["OPENROUTER_API_KEY"] = os.getenv("OPENROUTER_API_KEY")
330
  env["INPUT_FILE"] = "clean_financial_data_bs.json"
331
- cwd = "C:/SAHIL/NOTES"
332
 
333
  # Run sircodebs.py
334
  run_subprocess("pnlbs/sircodebs.py", [input_excel_path], env, cwd)
@@ -341,13 +341,18 @@ async def bs_from_notes(file: UploadFile = File(...)):
341
  # Run bl_llm.py
342
  result = run_subprocess("pnlbs/bl_llm.py", [], env, cwd)
343
  output_file = extract_output_file(result.stdout)
344
- if not output_file or not os.path.exists(output_file):
 
 
 
 
 
345
  debug_msg = f"\nSTDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}"
346
  logger.error(f"Could not determine output file from bl_llm.py output.{debug_msg}")
347
  raise HTTPException(status_code=500, detail=f"Could not determine output file from bl_llm.py output.{debug_msg}")
348
 
349
- logger.info(f"Pipeline completed. Output file: {output_file}")
350
- return {"message": "Balance Sheet generated successfully.", "file": output_file}
351
 
352
 
353
 
 
328
  if os.getenv("OPENROUTER_API_KEY"):
329
  env["OPENROUTER_API_KEY"] = os.getenv("OPENROUTER_API_KEY")
330
  env["INPUT_FILE"] = "clean_financial_data_bs.json"
331
+ cwd = "C:/SAHIL/production_notes"
332
 
333
  # Run sircodebs.py
334
  run_subprocess("pnlbs/sircodebs.py", [input_excel_path], env, cwd)
 
341
  # Run bl_llm.py
342
  result = run_subprocess("pnlbs/bl_llm.py", [], env, cwd)
343
  output_file = extract_output_file(result.stdout)
344
+ # If output_file is not absolute, resolve relative to cwd
345
+ if output_file and not os.path.isabs(output_file):
346
+ output_file_path = os.path.join(cwd, output_file)
347
+ else:
348
+ output_file_path = output_file
349
+ if not output_file or not os.path.exists(output_file_path):
350
  debug_msg = f"\nSTDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}"
351
  logger.error(f"Could not determine output file from bl_llm.py output.{debug_msg}")
352
  raise HTTPException(status_code=500, detail=f"Could not determine output file from bl_llm.py output.{debug_msg}")
353
 
354
+ logger.info(f"Pipeline completed. Output file: {output_file_path}")
355
+ return {"message": "Balance Sheet generated successfully.", "file": output_file_path}
356
 
357
 
358
 
pnlbs/bl_llm.py CHANGED
@@ -1,3 +1,7 @@
 
 
 
 
1
  import os
2
  import json
3
  import re
@@ -14,7 +18,11 @@ from pydantic_settings import BaseSettings
14
  load_dotenv()
15
 
16
  # Configure logging
17
- logging.basicConfig(level=logging.INFO)
 
 
 
 
18
  logger = logging.getLogger(__name__)
19
 
20
  class Settings(BaseSettings):
@@ -745,6 +753,7 @@ Return ONLY this JSON format:
745
  output_file = os.path.join(output_dir, f"balance_sheet_{timestamp}.xlsx")
746
  wb.save(output_file)
747
  logger.info(f"Output file: {output_file}")
 
748
  return output_file
749
 
750
  def process(self, input_file: str, output_dir: str = "output") -> Optional[str]:
@@ -816,20 +825,40 @@ Return ONLY this JSON format:
816
  return None
817
 
818
  def main() -> None:
819
- """Main function for running the balance sheet generator."""
820
- logger.info("ENHANCED BALANCE SHEET GENERATOR v2.0")
 
 
 
 
 
821
  api_key = settings.api_key
 
 
 
 
 
 
822
  if not api_key:
823
  logger.error("Missing OPENROUTER_API_KEY environment variable. Please set your OpenRouter API key in the .env file.")
824
  return
825
- input_file = settings.input_file
826
  if not os.path.exists(input_file):
827
  logger.error(f"Input file not found: {input_file}. Please ensure your JSON data file exists.")
828
  return
829
  generator = EnhancedBalanceSheetGenerator(api_key)
830
- result = generator.process(input_file, settings.output_dir)
831
- if result:
832
- logger.info(f"COMPLETED SUCCESSFULLY! Output file: {result}")
833
- logger.info("Open the Excel file to view your balance sheet")
834
- else:
835
- logger.error("PROCESSING FAILED. Please check the error messages above and try again.")
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Enhanced Balance Sheet Generator
3
+ Production-ready script for extracting, analyzing, and generating Excel balance sheets from financial JSON data.
4
+ """
5
  import os
6
  import json
7
  import re
 
18
  load_dotenv()
19
 
20
  # Configure logging
21
+ logging.basicConfig(
22
+ level=logging.INFO,
23
+ format='%(asctime)s %(levelname)s %(name)s: %(message)s',
24
+ datefmt='%Y-%m-%d %H:%M:%S'
25
+ )
26
  logger = logging.getLogger(__name__)
27
 
28
  class Settings(BaseSettings):
 
753
  output_file = os.path.join(output_dir, f"balance_sheet_{timestamp}.xlsx")
754
  wb.save(output_file)
755
  logger.info(f"Output file: {output_file}")
756
+ print(f"Output file: {os.path.abspath(output_file)}") # For API subprocess parsing
757
  return output_file
758
 
759
  def process(self, input_file: str, output_dir: str = "output") -> Optional[str]:
 
825
  return None
826
 
827
  def main() -> None:
828
+ """
829
+ Main function for running the balance sheet generator.
830
+ Accepts input file and output directory from command-line arguments or environment variables.
831
+ Handles errors gracefully and logs all major events.
832
+ """
833
+ logger.info("ENHANCED BALANCE SHEET GENERATOR v2.0 started.")
834
+ import sys
835
  api_key = settings.api_key
836
+ input_file = settings.input_file
837
+ output_dir = settings.output_dir
838
+ if len(sys.argv) > 1:
839
+ input_file = sys.argv[1]
840
+ if len(sys.argv) > 2:
841
+ output_dir = sys.argv[2]
842
  if not api_key:
843
  logger.error("Missing OPENROUTER_API_KEY environment variable. Please set your OpenRouter API key in the .env file.")
844
  return
 
845
  if not os.path.exists(input_file):
846
  logger.error(f"Input file not found: {input_file}. Please ensure your JSON data file exists.")
847
  return
848
  generator = EnhancedBalanceSheetGenerator(api_key)
849
+ try:
850
+ result = generator.process(input_file, output_dir)
851
+ if result:
852
+ abs_path = os.path.abspath(result)
853
+ if os.path.exists(abs_path):
854
+ logger.info(f"COMPLETED SUCCESSFULLY! Output file: {abs_path}")
855
+ print(f"Output file: {abs_path}") # For API subprocess parsing
856
+ else:
857
+ logger.error(f"PROCESSING FAILED. Output file not created: {abs_path}")
858
+ else:
859
+ logger.error("PROCESSING FAILED. Please check the error messages above and try again.")
860
+ except Exception as e:
861
+ logger.error(f"Fatal error: {e}", exc_info=True)
862
+
863
+ if __name__ == "__main__":
864
+ main()
pnlbs/sircodebs.py CHANGED
@@ -49,9 +49,15 @@ def main() -> None:
49
  """
50
  Main function to extract notes from Excel and export as CSVs.
51
  """
52
- logger.info("Loading Excel file: %s", settings.excel_file_path)
 
 
 
 
 
 
53
  global xls
54
- xls = pd.ExcelFile(settings.excel_file_path)
55
 
56
  # Clean each sheet
57
  note_2_8_df = clean_note(settings.note_2_8_sheet, settings.skiprows)
 
49
  """
50
  Main function to extract notes from Excel and export as CSVs.
51
  """
52
+ # Use command-line argument for Excel file path if provided
53
+ excel_path = settings.excel_file_path
54
+ if len(sys.argv) > 1:
55
+ excel_path = sys.argv[1]
56
+ logger.info(f"Excel file path from argument: {excel_path}")
57
+ else:
58
+ logger.info(f"Excel file path from settings: {excel_path}")
59
  global xls
60
+ xls = pd.ExcelFile(excel_path)
61
 
62
  # Clean each sheet
63
  note_2_8_df = clean_note(settings.note_2_8_sheet, settings.skiprows)