Update app.py
Browse files
app.py
CHANGED
@@ -38,7 +38,7 @@ def encode_image(image):
|
|
38 |
def analyze_construction_image(image):
|
39 |
if image is None:
|
40 |
logger.warning("No image provided")
|
41 |
-
return [(None, "Error: No image uploaded")]
|
42 |
|
43 |
try:
|
44 |
logger.info("Starting image analysis")
|
@@ -80,30 +80,39 @@ def analyze_construction_image(image):
|
|
80 |
|
81 |
if not result:
|
82 |
logger.warning("Received empty response from API")
|
83 |
-
return [(None, "Error: Received empty response from API")]
|
84 |
|
85 |
# Parse the result
|
86 |
analysis_text = "Safety and Hazard Analysis of the Construction Site Image:\n\n"
|
|
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
logger.info("Analysis completed successfully")
|
101 |
-
return [(None, analysis_text)]
|
102 |
except Exception as e:
|
103 |
logger.error(f"Error during image analysis: {str(e)}")
|
104 |
logger.error(traceback.format_exc())
|
105 |
error_message = f"Error during analysis: {str(e)}. Please try again or contact support if the issue persists."
|
106 |
-
return [(None, error_message)]
|
107 |
|
108 |
def chat_about_image(message, chat_history):
|
109 |
try:
|
|
|
38 |
def analyze_construction_image(image):
|
39 |
if image is None:
|
40 |
logger.warning("No image provided")
|
41 |
+
return [(None, "Error: No image uploaded")], "Error: No image uploaded"
|
42 |
|
43 |
try:
|
44 |
logger.info("Starting image analysis")
|
|
|
80 |
|
81 |
if not result:
|
82 |
logger.warning("Received empty response from API")
|
83 |
+
return [(None, "Error: Received empty response from API")], "Error: Received empty response from API"
|
84 |
|
85 |
# Parse the result
|
86 |
analysis_text = "Safety and Hazard Analysis of the Construction Site Image:\n\n"
|
87 |
+
categories = result.split("Category")[1:] # Split by categories
|
88 |
|
89 |
+
if not categories:
|
90 |
+
logger.warning("No categories found in the response")
|
91 |
+
return [(None, "Error: Unable to parse the response. No categories found.")], "Error: Unable to parse the response"
|
92 |
+
|
93 |
+
for category in categories:
|
94 |
+
lines = category.split("\n")
|
95 |
+
category_name = lines[0].split(":")[1].strip() if ":" in lines[0] else lines[0].strip()
|
96 |
+
analysis_text += f"Category: {category_name}\n"
|
97 |
+
|
98 |
+
description = next((line.split("Description:")[1].strip() for line in lines if "Description:" in line), "N/A")
|
99 |
+
hazard = next((line.split("Hazard:")[1].strip() for line in lines if "Hazard:" in line), "N/A")
|
100 |
+
resolution_steps = [line.strip() for line in lines if line.strip().startswith("*")]
|
101 |
+
|
102 |
+
analysis_text += f"Description: {description}\n"
|
103 |
+
analysis_text += f"Hazard: {hazard}\n"
|
104 |
+
analysis_text += "Resolution Steps:\n"
|
105 |
+
for step in resolution_steps:
|
106 |
+
analysis_text += f"{step}\n"
|
107 |
+
analysis_text += "\n"
|
108 |
|
109 |
logger.info("Analysis completed successfully")
|
110 |
+
return [(None, analysis_text)], ""
|
111 |
except Exception as e:
|
112 |
logger.error(f"Error during image analysis: {str(e)}")
|
113 |
logger.error(traceback.format_exc())
|
114 |
error_message = f"Error during analysis: {str(e)}. Please try again or contact support if the issue persists."
|
115 |
+
return [(None, error_message)], error_message
|
116 |
|
117 |
def chat_about_image(message, chat_history):
|
118 |
try:
|