Update app.py
Browse files
app.py
CHANGED
@@ -50,7 +50,7 @@ def analyze_construction_image(image):
|
|
50 |
"content": [
|
51 |
{
|
52 |
"type": "text",
|
53 |
-
"text": "Analyze this construction site image. Identify any issues or
|
54 |
},
|
55 |
{
|
56 |
"type": "image_url",
|
@@ -78,33 +78,26 @@ def analyze_construction_image(image):
|
|
78 |
result = completion.choices[0].message.content
|
79 |
logger.debug(f"Raw API response: {result}")
|
80 |
|
81 |
-
#
|
82 |
try:
|
83 |
parsed_result = json.loads(result)
|
|
|
84 |
except json.JSONDecodeError:
|
85 |
logger.error("Failed to parse API response as JSON")
|
86 |
return [(None, "Error: Invalid response format")]
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
desnag_steps_str = '\n'.join(desnag_steps)
|
99 |
|
100 |
logger.info("Analysis completed successfully")
|
101 |
-
|
102 |
-
# Initialize chat history with analysis results
|
103 |
-
chat_history = [
|
104 |
-
(None, f"Image Analysis Results:\n\nSnag Category: {snag_category}\n\nSnag Description: {snag_description}\n\nSteps to Desnag:\n{desnag_steps_str}")
|
105 |
-
]
|
106 |
-
|
107 |
-
return chat_history
|
108 |
except Exception as e:
|
109 |
logger.error(f"Error during image analysis: {str(e)}")
|
110 |
return [(None, f"Error: {str(e)}")]
|
|
|
50 |
"content": [
|
51 |
{
|
52 |
"type": "text",
|
53 |
+
"text": "Analyze this construction site image. Identify any safety issues or hazards, categorize them, provide a detailed description, and suggest steps to resolve them. Format your response as a JSON object with a key 'issues' containing an array of issue objects. Each issue object should have 'category', 'description', and 'resolution' keys."
|
54 |
},
|
55 |
{
|
56 |
"type": "image_url",
|
|
|
78 |
result = completion.choices[0].message.content
|
79 |
logger.debug(f"Raw API response: {result}")
|
80 |
|
81 |
+
# Parse the result as JSON
|
82 |
try:
|
83 |
parsed_result = json.loads(result)
|
84 |
+
issues = parsed_result.get('issues', [])
|
85 |
except json.JSONDecodeError:
|
86 |
logger.error("Failed to parse API response as JSON")
|
87 |
return [(None, "Error: Invalid response format")]
|
88 |
|
89 |
+
# Format the analysis results
|
90 |
+
analysis_text = "Image Analysis Results:\n\n"
|
91 |
+
for issue in issues:
|
92 |
+
analysis_text += f"Category: {issue.get('category', 'N/A')}\n"
|
93 |
+
analysis_text += f"Description: {issue.get('description', 'N/A')}\n"
|
94 |
+
analysis_text += "Resolution Steps:\n"
|
95 |
+
for step in issue.get('resolution', ['N/A']):
|
96 |
+
analysis_text += f"- {step}\n"
|
97 |
+
analysis_text += "\n"
|
|
|
|
|
98 |
|
99 |
logger.info("Analysis completed successfully")
|
100 |
+
return [(None, analysis_text)]
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
except Exception as e:
|
102 |
logger.error(f"Error during image analysis: {str(e)}")
|
103 |
return [(None, f"Error: {str(e)}")]
|