Spaces:
Sleeping
Sleeping
Update agents/simple_tools.py
#6
by
sahil-1-garg
- opened
- agents/simple_tools.py +40 -15
agents/simple_tools.py
CHANGED
|
@@ -77,7 +77,7 @@ def generate_notes_full_pipeline_from_path(file_path: str, note_numbers: str = "
|
|
| 77 |
|
| 78 |
|
| 79 |
@tool
|
| 80 |
-
def generate_balance_sheet(file_path: str) -> Dict[str, Any]:
|
| 81 |
"""
|
| 82 |
Generate balance sheet from trial balance file using complete pipeline
|
| 83 |
Args:
|
|
@@ -93,6 +93,10 @@ def generate_balance_sheet(file_path: str) -> Dict[str, Any]:
|
|
| 93 |
env = os.environ.copy()
|
| 94 |
if os.getenv("OPENROUTER_API_KEY"):
|
| 95 |
env["OPENROUTER_API_KEY"] = os.getenv("OPENROUTER_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
env["INPUT_FILE"] = "data/clean_financial_data_bs.json"
|
| 97 |
cwd = os.getcwd()
|
| 98 |
|
|
@@ -136,22 +140,43 @@ def generate_balance_sheet(file_path: str) -> Dict[str, Any]:
|
|
| 136 |
)
|
| 137 |
|
| 138 |
if result3.returncode == 0:
|
| 139 |
-
#
|
| 140 |
-
|
| 141 |
-
if
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
else:
|
| 156 |
execution_time = round(time.time() - start_time, 2)
|
| 157 |
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
@tool
|
| 80 |
+
def generate_balance_sheet(file_path: str, **kwargs) -> Dict[str, Any]:
|
| 81 |
"""
|
| 82 |
Generate balance sheet from trial balance file using complete pipeline
|
| 83 |
Args:
|
|
|
|
| 93 |
env = os.environ.copy()
|
| 94 |
if os.getenv("OPENROUTER_API_KEY"):
|
| 95 |
env["OPENROUTER_API_KEY"] = os.getenv("OPENROUTER_API_KEY")
|
| 96 |
+
# Override with user-provided API key if available
|
| 97 |
+
user_api_key = kwargs.get("user_api_key")
|
| 98 |
+
if user_api_key:
|
| 99 |
+
env["OPENROUTER_API_KEY"] = user_api_key
|
| 100 |
env["INPUT_FILE"] = "data/clean_financial_data_bs.json"
|
| 101 |
cwd = os.getcwd()
|
| 102 |
|
|
|
|
| 140 |
)
|
| 141 |
|
| 142 |
if result3.returncode == 0:
|
| 143 |
+
# Parse the output file path from stdout
|
| 144 |
+
output_file_path = None
|
| 145 |
+
if result3.stdout:
|
| 146 |
+
for line in result3.stdout.strip().split('\n'):
|
| 147 |
+
if line.startswith('Output file:'):
|
| 148 |
+
output_file_path = line.split('Output file:', 1)[1].strip()
|
| 149 |
+
break
|
| 150 |
|
| 151 |
+
# Fallback: check for output files in data/output directory
|
| 152 |
+
if not output_file_path or not os.path.exists(output_file_path):
|
| 153 |
+
output_dir = "data/output"
|
| 154 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 155 |
+
output_files = []
|
| 156 |
+
if os.path.exists(output_dir):
|
| 157 |
+
output_files = [f for f in os.listdir(output_dir) if f.endswith('.xlsx') and f.startswith('balance_sheet')]
|
| 158 |
+
# Sort by modification time, get the most recent
|
| 159 |
+
if output_files:
|
| 160 |
+
output_files.sort(key=lambda x: os.path.getmtime(os.path.join(output_dir, x)), reverse=True)
|
| 161 |
+
output_file_path = os.path.join(output_dir, output_files[0])
|
| 162 |
|
| 163 |
+
if output_file_path and os.path.exists(output_file_path):
|
| 164 |
+
execution_time = round(time.time() - start_time, 2)
|
| 165 |
+
return {
|
| 166 |
+
"status": "success",
|
| 167 |
+
"message": "Balance sheet generated successfully",
|
| 168 |
+
"output_path": output_file_path,
|
| 169 |
+
"execution_id": execution_id,
|
| 170 |
+
"execution_time": execution_time
|
| 171 |
+
}
|
| 172 |
+
else:
|
| 173 |
+
execution_time = round(time.time() - start_time, 2)
|
| 174 |
+
return {
|
| 175 |
+
"status": "error",
|
| 176 |
+
"error": "Balance sheet generation completed but output file not found",
|
| 177 |
+
"execution_id": execution_id,
|
| 178 |
+
"execution_time": execution_time
|
| 179 |
+
}
|
| 180 |
else:
|
| 181 |
execution_time = round(time.time() - start_time, 2)
|
| 182 |
|