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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -25
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")], "Error: No image uploaded"
42
 
43
  try:
44
  logger.info("Starting image analysis")
@@ -80,39 +80,35 @@ 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")], "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:
@@ -165,6 +161,7 @@ custom_css = """
165
  .groq-badge { position: fixed; bottom: 10px; right: 10px; background-color: #f39c12; color: white; padding: 5px 10px; border-radius: 5px; font-weight: bold; }
166
  """
167
 
 
168
  # Create the Gradio interface
169
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
170
  gr.HTML(
@@ -193,10 +190,17 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
193
  )
194
  clear = gr.Button("🗑️ Clear Chat", elem_classes="clear-button")
195
 
 
 
 
 
 
 
196
  analyze_button.click(
197
  analyze_construction_image,
198
  inputs=[image_input],
199
- outputs=[chatbot]
 
200
  )
201
 
202
  msg.submit(chat_about_image, [msg, chatbot], [msg, chatbot])
 
38
  def analyze_construction_image(image):
39
  if image is None:
40
  logger.warning("No image provided")
41
+ return [("No image uploaded", "Error: Please upload an image for analysis.")]
42
 
43
  try:
44
  logger.info("Starting image analysis")
 
80
 
81
  if not result:
82
  logger.warning("Received empty response from API")
83
+ return [("Image analysis request", "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 [("Image analysis request", "Error: Unable to parse the response. No sections found.")]
 
 
 
 
 
 
 
 
99
 
100
  logger.info("Analysis completed successfully")
101
+
102
+ # Split the analysis into chunks if it's too long
103
+ max_chunk_length = 1000 # Adjust this value as needed
104
+ chunks = [analysis_text[i:i+max_chunk_length] for i in range(0, len(analysis_text), max_chunk_length)]
105
+
106
+ return [("Image analysis request", chunk) for chunk in chunks]
107
  except Exception as e:
108
  logger.error(f"Error during image analysis: {str(e)}")
109
  logger.error(traceback.format_exc())
110
  error_message = f"Error during analysis: {str(e)}. Please try again or contact support if the issue persists."
111
+ return [("Image analysis request", error_message)]
112
 
113
  def chat_about_image(message, chat_history):
114
  try:
 
161
  .groq-badge { position: fixed; bottom: 10px; right: 10px; background-color: #f39c12; color: white; padding: 5px 10px; border-radius: 5px; font-weight: bold; }
162
  """
163
 
164
+ # Create the Gradio interface
165
  # Create the Gradio interface
166
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
167
  gr.HTML(
 
190
  )
191
  clear = gr.Button("🗑️ Clear Chat", elem_classes="clear-button")
192
 
193
+ def append_to_chat(history, new_messages):
194
+ if history is None:
195
+ history = []
196
+ history.extend(new_messages)
197
+ return history
198
+
199
  analyze_button.click(
200
  analyze_construction_image,
201
  inputs=[image_input],
202
+ outputs=[chatbot],
203
+ postprocess=append_to_chat
204
  )
205
 
206
  msg.submit(chat_about_image, [msg, chatbot], [msg, chatbot])