capradeepgujaran commited on
Commit
c17d63e
·
verified ·
1 Parent(s): beb288f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -15
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
- sections = ["Hazard Identification", "Categorization", "Detailed Description", "Resolution Steps"]
89
- for section in sections:
90
- start = result.find(section)
91
- if start != -1:
92
- end = result.find(next((s for s in sections if s != section and result.find(s) > start), None))
93
- content = result[start:end if end != -1 else None].strip()
94
- analysis_text += f"{content}\n\n"
95
-
96
- if analysis_text == "Safety and Hazard Analysis of the Construction Site Image:\n\n":
97
- logger.warning("No sections found in the response")
98
- return [(None, "Error: Unable to parse the response. No sections found.")]
 
 
 
 
 
 
 
 
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: