Spaces:
Runtime error
Runtime error
Commit ·
5ab65a7
1
Parent(s): 402122a
C IMP
Browse files- app/Document Generation/pdf_generator.py +2 -2
- app/main.py +4 -35
- app/utils.py +42 -0
app/Document Generation/pdf_generator.py
CHANGED
|
@@ -32,8 +32,8 @@ if currency_mapping_path not in sys.path:
|
|
| 32 |
sys.path.append(currency_mapping_path)
|
| 33 |
from currency_mapping import convert_currency_symbols_to_iso
|
| 34 |
|
| 35 |
-
# Import sort_sections_by_order from
|
| 36 |
-
from
|
| 37 |
|
| 38 |
def create_pdf_document(request: ExportRequest):
|
| 39 |
"""Create a PROFESSIONAL PDF document from the business plan data using FPDF2
|
|
|
|
| 32 |
sys.path.append(currency_mapping_path)
|
| 33 |
from currency_mapping import convert_currency_symbols_to_iso
|
| 34 |
|
| 35 |
+
# Import sort_sections_by_order from utils module
|
| 36 |
+
from utils import sort_sections_by_order
|
| 37 |
|
| 38 |
def create_pdf_document(request: ExportRequest):
|
| 39 |
"""Create a PROFESSIONAL PDF document from the business plan data using FPDF2
|
app/main.py
CHANGED
|
@@ -100,6 +100,9 @@ from models import ExportRequest, ExportSection, ExportResponse, SectionJob, Bat
|
|
| 100 |
# Import constants
|
| 101 |
from constants import SECTION_ORDER, SECTION_MAP, SECTION_COLUMNS
|
| 102 |
|
|
|
|
|
|
|
|
|
|
| 103 |
# Now import the modules
|
| 104 |
from pdf_generator import create_pdf_document
|
| 105 |
from word_generator import create_word_document
|
|
@@ -245,41 +248,7 @@ class SectionJob(BaseModel):
|
|
| 245 |
|
| 246 |
# Database helper functions
|
| 247 |
|
| 248 |
-
|
| 249 |
-
"""Sort sections according to the predefined SECTION_ORDER to ensure correct numerical ordering"""
|
| 250 |
-
# Create a mapping from section title to order index
|
| 251 |
-
order_map = {title: index for index, title in enumerate(SECTION_ORDER)}
|
| 252 |
-
|
| 253 |
-
def get_section_order(section: ExportSection) -> int:
|
| 254 |
-
# Try to match by exact title first
|
| 255 |
-
if section.title in order_map:
|
| 256 |
-
order_index = order_map[section.title]
|
| 257 |
-
return order_index
|
| 258 |
-
|
| 259 |
-
# Try case-insensitive matching
|
| 260 |
-
section_title_lower = section.title.lower()
|
| 261 |
-
for order_title, order_index in order_map.items():
|
| 262 |
-
if order_title.lower() == section_title_lower:
|
| 263 |
-
return order_index
|
| 264 |
-
|
| 265 |
-
# Try to match by key (remove 'prompt_' prefix if present)
|
| 266 |
-
key_clean = section.key.replace('prompt_', '') if section.key.startswith('prompt_') else section.key
|
| 267 |
-
if key_clean in order_map:
|
| 268 |
-
order_index = order_map[key_clean]
|
| 269 |
-
return order_index
|
| 270 |
-
|
| 271 |
-
# Try partial matching for keys
|
| 272 |
-
for order_title, order_index in order_map.items():
|
| 273 |
-
if key_clean.lower() in order_title.lower() or order_title.lower() in key_clean.lower():
|
| 274 |
-
return order_index
|
| 275 |
-
|
| 276 |
-
# If no match found, put at the end
|
| 277 |
-
return len(SECTION_ORDER)
|
| 278 |
-
|
| 279 |
-
# Sort sections by their order
|
| 280 |
-
sorted_sections = sorted(sections, key=get_section_order)
|
| 281 |
-
|
| 282 |
-
return sorted_sections
|
| 283 |
|
| 284 |
def extract_summary_from_markdown(markdown_text: str, max_length: int = 200) -> str:
|
| 285 |
"""Extract a summary from markdown text, removing headers and formatting"""
|
|
|
|
| 100 |
# Import constants
|
| 101 |
from constants import SECTION_ORDER, SECTION_MAP, SECTION_COLUMNS
|
| 102 |
|
| 103 |
+
# Import utilities
|
| 104 |
+
from utils import sort_sections_by_order
|
| 105 |
+
|
| 106 |
# Now import the modules
|
| 107 |
from pdf_generator import create_pdf_document
|
| 108 |
from word_generator import create_word_document
|
|
|
|
| 248 |
|
| 249 |
# Database helper functions
|
| 250 |
|
| 251 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
| 253 |
def extract_summary_from_markdown(markdown_text: str, max_length: int = 200) -> str:
|
| 254 |
"""Extract a summary from markdown text, removing headers and formatting"""
|
app/utils.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Utility functions shared across modules
|
| 2 |
+
# This file helps break circular imports between main.py and pdf_generator.py
|
| 3 |
+
|
| 4 |
+
from typing import List
|
| 5 |
+
from models import ExportSection
|
| 6 |
+
from constants import SECTION_ORDER
|
| 7 |
+
|
| 8 |
+
def sort_sections_by_order(sections: List[ExportSection]) -> List[ExportSection]:
|
| 9 |
+
"""Sort sections according to the predefined SECTION_ORDER to ensure correct numerical ordering"""
|
| 10 |
+
# Create a mapping from section title to order index
|
| 11 |
+
order_map = {title: index for index, title in enumerate(SECTION_ORDER)}
|
| 12 |
+
|
| 13 |
+
def get_section_order(section: ExportSection) -> int:
|
| 14 |
+
# Try to match by exact title first
|
| 15 |
+
if section.title in order_map:
|
| 16 |
+
order_index = order_map[section.title]
|
| 17 |
+
return order_index
|
| 18 |
+
|
| 19 |
+
# Try case-insensitive matching
|
| 20 |
+
section_title_lower = section.title.lower()
|
| 21 |
+
for order_title, order_index in order_map.items():
|
| 22 |
+
if order_title.lower() == section_title_lower:
|
| 23 |
+
return order_index
|
| 24 |
+
|
| 25 |
+
# Try to match by key (remove 'prompt_' prefix if present)
|
| 26 |
+
key_clean = section.key.replace('prompt_', '') if section.key.startswith('prompt_') else section.key
|
| 27 |
+
if key_clean in order_map:
|
| 28 |
+
order_index = order_map[key_clean]
|
| 29 |
+
return order_index
|
| 30 |
+
|
| 31 |
+
# Try partial matching for keys
|
| 32 |
+
for order_title, order_index in order_map.items():
|
| 33 |
+
if key_clean.lower() in order_title.lower() or order_title.lower() in key_clean.lower():
|
| 34 |
+
return order_index
|
| 35 |
+
|
| 36 |
+
# If no match found, put at the end
|
| 37 |
+
return len(SECTION_ORDER)
|
| 38 |
+
|
| 39 |
+
# Sort sections by their order
|
| 40 |
+
sorted_sections = sorted(sections, key=get_section_order)
|
| 41 |
+
|
| 42 |
+
return sorted_sections
|