Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -14,6 +14,27 @@ logging.basicConfig(
|
|
14 |
)
|
15 |
logger = logging.getLogger(__name__)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# Function to calculate materials based on blueprint dimensions
|
18 |
def calculate_materials_from_dimensions(wall_area, foundation_area):
|
19 |
"""
|
@@ -86,6 +107,10 @@ def process_blueprint(pdf_path, blueprint_width_m=27, blueprint_height_m=9.78, p
|
|
86 |
logger.error("Invalid blueprint dimensions: width=%s, height=%s", blueprint_width_m, blueprint_height_m)
|
87 |
return {"error": "Blueprint width and height must be positive"}
|
88 |
|
|
|
|
|
|
|
|
|
89 |
try:
|
90 |
# Convert PDF to images
|
91 |
logger.debug("Converting PDF to image with poppler_path=%s", poppler_path)
|
@@ -199,7 +224,7 @@ def process_blueprint(pdf_path, blueprint_width_m=27, blueprint_height_m=9.78, p
|
|
199 |
"- Windows: Download from https://github.com/oschwartz10612/poppler-windows and add to PATH.\n"
|
200 |
"- Linux: Run 'sudo apt-get install poppler-utils'.\n"
|
201 |
"- Mac: Run 'brew install poppler'.\n"
|
202 |
-
"Alternatively, specify poppler_path in the input (e.g., C:/poppler/bin)."
|
203 |
)
|
204 |
logger.error("Processing failed: %s", error_msg)
|
205 |
return {"error": f"Failed to process PDF: {error_msg}"}
|
@@ -217,7 +242,7 @@ interface = gr.Interface(
|
|
217 |
title="Blueprint Material Estimator",
|
218 |
description=(
|
219 |
"Upload a PDF containing a blueprint to estimate construction materials. "
|
220 |
-
"Specify blueprint dimensions and
|
221 |
)
|
222 |
)
|
223 |
|
|
|
14 |
)
|
15 |
logger = logging.getLogger(__name__)
|
16 |
|
17 |
+
# Function to validate poppler_path
|
18 |
+
def validate_poppler_path(poppler_path):
|
19 |
+
"""
|
20 |
+
Validate if the provided poppler_path is valid and contains pdftoppm.
|
21 |
+
Args:
|
22 |
+
poppler_path (str): Path to poppler binaries.
|
23 |
+
Returns:
|
24 |
+
str or None: Valid poppler_path or None if invalid.
|
25 |
+
"""
|
26 |
+
if not poppler_path:
|
27 |
+
return None
|
28 |
+
if not os.path.isdir(poppler_path):
|
29 |
+
logger.error("Invalid poppler_path: %s is not a directory", poppler_path)
|
30 |
+
return None
|
31 |
+
pdftoppm_path = os.path.join(poppler_path, "pdftoppm" + (".exe" if os.name == "nt" else ""))
|
32 |
+
if not os.path.isfile(pdftoppm_path):
|
33 |
+
logger.error("pdftoppm not found in poppler_path: %s", poppler_path)
|
34 |
+
return None
|
35 |
+
logger.debug("Valid poppler_path: %s", poppler_path)
|
36 |
+
return poppler_path
|
37 |
+
|
38 |
# Function to calculate materials based on blueprint dimensions
|
39 |
def calculate_materials_from_dimensions(wall_area, foundation_area):
|
40 |
"""
|
|
|
107 |
logger.error("Invalid blueprint dimensions: width=%s, height=%s", blueprint_width_m, blueprint_height_m)
|
108 |
return {"error": "Blueprint width and height must be positive"}
|
109 |
|
110 |
+
# Validate poppler_path
|
111 |
+
poppler_path = validate_poppler_path(poppler_path)
|
112 |
+
logger.debug("Using poppler_path: %s", poppler_path)
|
113 |
+
|
114 |
try:
|
115 |
# Convert PDF to images
|
116 |
logger.debug("Converting PDF to image with poppler_path=%s", poppler_path)
|
|
|
224 |
"- Windows: Download from https://github.com/oschwartz10612/poppler-windows and add to PATH.\n"
|
225 |
"- Linux: Run 'sudo apt-get install poppler-utils'.\n"
|
226 |
"- Mac: Run 'brew install poppler'.\n"
|
227 |
+
"Alternatively, specify a valid poppler_path in the input (e.g., C:/poppler/bin)."
|
228 |
)
|
229 |
logger.error("Processing failed: %s", error_msg)
|
230 |
return {"error": f"Failed to process PDF: {error_msg}"}
|
|
|
242 |
title="Blueprint Material Estimator",
|
243 |
description=(
|
244 |
"Upload a PDF containing a blueprint to estimate construction materials. "
|
245 |
+
"Specify blueprint dimensions and provide the path to poppler binaries if not in PATH."
|
246 |
)
|
247 |
)
|
248 |
|