methunraj
commited on
Commit
Β·
37440f2
1
Parent(s):
745273a
refactor(WorkflowUI): improve zip file handling with cross-platform support
Browse files- Replace system temp directory with application's TEMP_DIR for better compatibility
- Add automatic cleanup of old download files to prevent disk space issues
- Improve error handling for file permission operations
- Simplify documentation in code_generator.json by removing redundant instructions
- app.py +26 -11
- instructions/agents/code_generator.json +28 -100
app.py
CHANGED
|
@@ -1647,7 +1647,6 @@ class WorkflowUI:
|
|
| 1647 |
|
| 1648 |
try:
|
| 1649 |
import zipfile
|
| 1650 |
-
import tempfile
|
| 1651 |
import os
|
| 1652 |
import shutil
|
| 1653 |
from datetime import datetime
|
|
@@ -1659,14 +1658,27 @@ class WorkflowUI:
|
|
| 1659 |
logger.warning(f"Output directory does not exist: {session_output_dir}")
|
| 1660 |
return None
|
| 1661 |
|
| 1662 |
-
# Create a properly named zip file in a
|
| 1663 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 1664 |
zip_filename = f"processed_files_{self.session_id}_{timestamp}.zip"
|
| 1665 |
|
| 1666 |
-
# Use
|
| 1667 |
-
# This ensures Gradio can access it properly
|
| 1668 |
-
|
| 1669 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1670 |
|
| 1671 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 1672 |
# Add all files from output directory
|
|
@@ -1703,16 +1715,19 @@ class WorkflowUI:
|
|
| 1703 |
|
| 1704 |
# Ensure the file exists and has content
|
| 1705 |
if zip_path.exists() and zip_path.stat().st_size > 0:
|
| 1706 |
-
# For
|
| 1707 |
-
abs_path = str(zip_path.
|
| 1708 |
logger.info(f"Returning zip file path for download: {abs_path}")
|
| 1709 |
logger.info(f"File size: {zip_path.stat().st_size} bytes")
|
| 1710 |
|
| 1711 |
-
#
|
| 1712 |
-
|
|
|
|
|
|
|
|
|
|
| 1713 |
|
| 1714 |
# Return the file path for Gradio to handle
|
| 1715 |
-
#
|
| 1716 |
return abs_path
|
| 1717 |
else:
|
| 1718 |
logger.error("Zip file was created but is empty or doesn't exist")
|
|
|
|
| 1647 |
|
| 1648 |
try:
|
| 1649 |
import zipfile
|
|
|
|
| 1650 |
import os
|
| 1651 |
import shutil
|
| 1652 |
from datetime import datetime
|
|
|
|
| 1658 |
logger.warning(f"Output directory does not exist: {session_output_dir}")
|
| 1659 |
return None
|
| 1660 |
|
| 1661 |
+
# Create a properly named zip file in a location that Gradio can access
|
| 1662 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 1663 |
zip_filename = f"processed_files_{self.session_id}_{timestamp}.zip"
|
| 1664 |
|
| 1665 |
+
# Use application's TEMP_DIR instead of system temp for cross-platform compatibility
|
| 1666 |
+
# This ensures Gradio can access it properly across all systems
|
| 1667 |
+
downloads_dir = Path(settings.TEMP_DIR) / "downloads"
|
| 1668 |
+
downloads_dir.mkdir(parents=True, exist_ok=True)
|
| 1669 |
+
|
| 1670 |
+
# Clean up old download files (older than 1 hour) to prevent disk space issues
|
| 1671 |
+
try:
|
| 1672 |
+
import time
|
| 1673 |
+
current_time = time.time()
|
| 1674 |
+
for old_file in downloads_dir.glob("*.zip"):
|
| 1675 |
+
if current_time - old_file.stat().st_mtime > 3600: # 1 hour
|
| 1676 |
+
old_file.unlink()
|
| 1677 |
+
logger.debug(f"Cleaned up old download file: {old_file.name}")
|
| 1678 |
+
except Exception as cleanup_error:
|
| 1679 |
+
logger.warning(f"Could not clean up old download files: {cleanup_error}")
|
| 1680 |
+
|
| 1681 |
+
zip_path = downloads_dir / zip_filename
|
| 1682 |
|
| 1683 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 1684 |
# Add all files from output directory
|
|
|
|
| 1715 |
|
| 1716 |
# Ensure the file exists and has content
|
| 1717 |
if zip_path.exists() and zip_path.stat().st_size > 0:
|
| 1718 |
+
# For cross-platform compatibility, use forward slashes and resolve path
|
| 1719 |
+
abs_path = str(zip_path.resolve())
|
| 1720 |
logger.info(f"Returning zip file path for download: {abs_path}")
|
| 1721 |
logger.info(f"File size: {zip_path.stat().st_size} bytes")
|
| 1722 |
|
| 1723 |
+
# Set proper permissions for cross-platform access
|
| 1724 |
+
try:
|
| 1725 |
+
os.chmod(abs_path, 0o644)
|
| 1726 |
+
except (OSError, PermissionError) as e:
|
| 1727 |
+
logger.warning(f"Could not set file permissions: {e}")
|
| 1728 |
|
| 1729 |
# Return the file path for Gradio to handle
|
| 1730 |
+
# Use resolved absolute path for better cross-platform compatibility
|
| 1731 |
return abs_path
|
| 1732 |
else:
|
| 1733 |
logger.error("Zip file was created but is empty or doesn't exist")
|
instructions/agents/code_generator.json
CHANGED
|
@@ -17,16 +17,6 @@
|
|
| 17 |
"SEVENTH, verify Excel file creation using list_files and file size validation.",
|
| 18 |
"EIGHTH, report success only after confirming Excel file exists and is >10KB.",
|
| 19 |
"",
|
| 20 |
-
"CRITICAL: Always start Python scripts with:",
|
| 21 |
-
"import os",
|
| 22 |
-
"os.chdir(os.path.dirname(os.path.abspath(__file__)) or '.')",
|
| 23 |
-
"This ensures the script runs in the correct directory regardless of OS.",
|
| 24 |
-
"",
|
| 25 |
-
"=== AVAILABLE TOOLS ===",
|
| 26 |
-
"- FileTools: read_file, save_file, list_files",
|
| 27 |
-
"- PythonTools: pip_install_package (ONLY for package installation)",
|
| 28 |
-
"- ShellTools: run_shell_command (PRIMARY execution tool)",
|
| 29 |
-
"",
|
| 30 |
"=== EXCEL WORKBOOK REQUIREMENTS ===",
|
| 31 |
"Create comprehensive worksheets based on JSON categories:",
|
| 32 |
"π 1. Executive Summary (key metrics, charts, highlights)",
|
|
@@ -69,104 +59,42 @@
|
|
| 69 |
"β’ Financial ratios: Column charts",
|
| 70 |
"β’ Cash flow: Waterfall charts (if possible)",
|
| 71 |
"",
|
| 72 |
-
"
|
| 73 |
-
"
|
| 74 |
-
"```python",
|
| 75 |
-
"import os, json, datetime, logging",
|
| 76 |
-
"from openpyxl import Workbook",
|
| 77 |
-
"from openpyxl.styles import Font, PatternFill, Border, Alignment, NamedStyle",
|
| 78 |
-
"from openpyxl.chart import BarChart, LineChart, PieChart",
|
| 79 |
-
"",
|
| 80 |
-
"# CRITICAL: Set working directory first",
|
| 81 |
"os.chdir(os.path.dirname(os.path.abspath(__file__)) or '.')",
|
|
|
|
| 82 |
"",
|
| 83 |
-
"
|
| 84 |
-
"
|
| 85 |
-
"
|
| 86 |
-
"",
|
| 87 |
-
"def load_financial_data():",
|
| 88 |
-
" with open('arranged_financial_data.json', 'r') as f:",
|
| 89 |
-
" return json.load(f)",
|
| 90 |
-
"",
|
| 91 |
-
"def create_professional_styles(wb):",
|
| 92 |
-
" # Define all formatting styles",
|
| 93 |
-
" pass",
|
| 94 |
-
"",
|
| 95 |
-
"def create_worksheets(wb, data):",
|
| 96 |
-
" # Create all required worksheets",
|
| 97 |
-
" pass",
|
| 98 |
-
"",
|
| 99 |
-
"def add_charts(wb, data):",
|
| 100 |
-
" # Add appropriate charts",
|
| 101 |
-
" pass",
|
| 102 |
-
"",
|
| 103 |
-
"def main():",
|
| 104 |
-
" try:",
|
| 105 |
-
" logger.info('Starting Excel report generation...')",
|
| 106 |
-
" data = load_financial_data()",
|
| 107 |
-
" wb = Workbook()",
|
| 108 |
-
" create_professional_styles(wb)",
|
| 109 |
-
" create_worksheets(wb, data)",
|
| 110 |
-
" add_charts(wb, data)",
|
| 111 |
-
" ",
|
| 112 |
-
" # Save with timestamp",
|
| 113 |
-
" timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')",
|
| 114 |
-
" filename = f'Financial_Report_{timestamp}.xlsx'",
|
| 115 |
-
" wb.save(filename)",
|
| 116 |
-
" logger.info(f'SUCCESS: Report saved as {filename}')",
|
| 117 |
-
" return filename",
|
| 118 |
-
" except Exception as e:",
|
| 119 |
-
" logger.error(f'ERROR: {e}')",
|
| 120 |
-
" raise",
|
| 121 |
-
"",
|
| 122 |
-
"if __name__ == '__main__':",
|
| 123 |
-
" main()",
|
| 124 |
-
"```",
|
| 125 |
-
"",
|
| 126 |
-
"=== CROSS-PLATFORM EXECUTION ===",
|
| 127 |
-
"Try execution methods in this order:",
|
| 128 |
-
"1. run_shell_command('python generate_excel_report.py 2>&1')",
|
| 129 |
-
"2. If fails on Windows: run_shell_command('python.exe generate_excel_report.py 2>&1')",
|
| 130 |
-
"3. PowerShell alternative: run_shell_command('powershell -Command \"python generate_excel_report.py\" 2>&1')",
|
| 131 |
-
"",
|
| 132 |
-
"=== VERIFICATION COMMANDS ===",
|
| 133 |
-
"Linux/Mac:",
|
| 134 |
-
"β’ run_shell_command('ls -la *.xlsx')",
|
| 135 |
-
"β’ run_shell_command('file Financial_Report*.xlsx')",
|
| 136 |
-
"β’ run_shell_command('du -h *.xlsx')",
|
| 137 |
"",
|
| 138 |
-
"
|
| 139 |
-
"
|
| 140 |
-
"
|
| 141 |
-
"
|
| 142 |
"",
|
| 143 |
-
"
|
| 144 |
-
"
|
| 145 |
-
"
|
| 146 |
-
"
|
| 147 |
-
"β’ List files: run_shell_command('dir') or run_shell_command('ls')",
|
| 148 |
"",
|
| 149 |
-
"
|
| 150 |
-
"
|
| 151 |
-
"
|
| 152 |
-
"
|
| 153 |
"",
|
| 154 |
-
"
|
| 155 |
-
"
|
| 156 |
-
"
|
| 157 |
-
"
|
| 158 |
-
"β
Charts and visualizations included",
|
| 159 |
-
"β
No execution errors in logs",
|
| 160 |
-
"β
Data accurately transferred from JSON to Excel",
|
| 161 |
"",
|
| 162 |
-
"
|
| 163 |
-
"
|
| 164 |
-
"
|
| 165 |
-
"
|
| 166 |
-
"3. No errors in execution logs",
|
| 167 |
-
"4. Success message is logged",
|
| 168 |
"",
|
| 169 |
-
"
|
| 170 |
],
|
| 171 |
"agent_type": "code_generator",
|
| 172 |
"description": "Excel report generator with mandatory completion and cross-platform shell execution",
|
|
|
|
| 17 |
"SEVENTH, verify Excel file creation using list_files and file size validation.",
|
| 18 |
"EIGHTH, report success only after confirming Excel file exists and is >10KB.",
|
| 19 |
"",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
"=== EXCEL WORKBOOK REQUIREMENTS ===",
|
| 21 |
"Create comprehensive worksheets based on JSON categories:",
|
| 22 |
"π 1. Executive Summary (key metrics, charts, highlights)",
|
|
|
|
| 59 |
"β’ Financial ratios: Column charts",
|
| 60 |
"β’ Cash flow: Waterfall charts (if possible)",
|
| 61 |
"",
|
| 62 |
+
"CRITICAL: Always start Python scripts with:",
|
| 63 |
+
"import os",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
"os.chdir(os.path.dirname(os.path.abspath(__file__)) or '.')",
|
| 65 |
+
"This ensures the script runs in the correct directory regardless of OS.",
|
| 66 |
"",
|
| 67 |
+
"Available Tools:",
|
| 68 |
+
"- FileTools: read_file, save_file, list_files",
|
| 69 |
+
"- PythonTools: pip_install_package (ONLY for package installation)",
|
| 70 |
+
"- ShellTools: run_shell_command (PRIMARY execution tool)",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
"",
|
| 72 |
+
"Cross-Platform Execution:",
|
| 73 |
+
"- Try: run_shell_command('python script.py 2>&1')",
|
| 74 |
+
"- If fails on Windows: run_shell_command('python.exe script.py 2>&1')",
|
| 75 |
+
"- PowerShell alternative: run_shell_command('powershell -Command \"python script.py\" 2>&1')",
|
| 76 |
"",
|
| 77 |
+
"Verification Commands (Linux/Mac):",
|
| 78 |
+
"- run_shell_command('ls -la *.xlsx')",
|
| 79 |
+
"- run_shell_command('file Financial_Report*.xlsx')",
|
| 80 |
+
"- run_shell_command('du -h *.xlsx')",
|
|
|
|
| 81 |
"",
|
| 82 |
+
"Verification Commands (Windows/PowerShell):",
|
| 83 |
+
"- run_shell_command('dir *.xlsx')",
|
| 84 |
+
"- run_shell_command('powershell -Command \"Get-ChildItem *.xlsx\"')",
|
| 85 |
+
"- run_shell_command('powershell -Command \"(Get-Item *.xlsx).Length\"')",
|
| 86 |
"",
|
| 87 |
+
"Debug Commands (Cross-Platform):",
|
| 88 |
+
"- Current directory: run_shell_command('pwd') or run_shell_command('cd')",
|
| 89 |
+
"- Python location: run_shell_command('where python') or run_shell_command('which python')",
|
| 90 |
+
"- List files: run_shell_command('dir') or run_shell_command('ls')",
|
|
|
|
|
|
|
|
|
|
| 91 |
"",
|
| 92 |
+
"Package Installation:",
|
| 93 |
+
"- pip_install_package('openpyxl')",
|
| 94 |
+
"- Or via shell: run_shell_command('pip install openpyxl')",
|
| 95 |
+
"- Windows: run_shell_command('python -m pip install openpyxl')",
|
|
|
|
|
|
|
| 96 |
"",
|
| 97 |
+
"Success Criteria: Excel file exists, size >5KB, no errors in output."
|
| 98 |
],
|
| 99 |
"agent_type": "code_generator",
|
| 100 |
"description": "Excel report generator with mandatory completion and cross-platform shell execution",
|