capradeepgujaran commited on
Commit
5932cce
·
verified ·
1 Parent(s): e8dabed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -20
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 snags, categorize them, provide a detailed description, and suggest steps to resolve them. Format your response as a JSON object with keys 'snag_category', 'snag_description', and 'desnag_steps' (as an array)."
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
- # Try to parse the result as JSON
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
- snag_category = str(parsed_result.get('snag_category', 'N/A'))
89
- snag_description = str(parsed_result.get('snag_description', 'N/A'))
90
-
91
- # Ensure desnag_steps is a list of strings
92
- desnag_steps = parsed_result.get('desnag_steps', ['N/A'])
93
- if not isinstance(desnag_steps, list):
94
- desnag_steps = [str(desnag_steps)]
95
- else:
96
- desnag_steps = [str(step) for step in desnag_steps]
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)}")]