Update app.py
Browse files
app.py
CHANGED
@@ -82,28 +82,10 @@ def analyze_construction_image(image):
|
|
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 |
-
#
|
103 |
-
|
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())
|
@@ -161,7 +143,6 @@ custom_css = """
|
|
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,17 +171,16 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
|
190 |
)
|
191 |
clear = gr.Button("🗑️ Clear Chat", elem_classes="clear-button")
|
192 |
|
193 |
-
def
|
194 |
-
|
195 |
-
|
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=
|
204 |
)
|
205 |
|
206 |
msg.submit(chat_about_image, [msg, chatbot], [msg, chatbot])
|
|
|
82 |
logger.warning("Received empty response from API")
|
83 |
return [("Image analysis request", "Error: Received empty response from API")]
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
logger.info("Analysis completed successfully")
|
86 |
|
87 |
+
# Return the result directly, without any parsing
|
88 |
+
return [("Image analysis request", result)]
|
|
|
|
|
|
|
89 |
except Exception as e:
|
90 |
logger.error(f"Error during image analysis: {str(e)}")
|
91 |
logger.error(traceback.format_exc())
|
|
|
143 |
.groq-badge { position: fixed; bottom: 10px; right: 10px; background-color: #f39c12; color: white; padding: 5px 10px; border-radius: 5px; font-weight: bold; }
|
144 |
"""
|
145 |
|
|
|
146 |
# Create the Gradio interface
|
147 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
148 |
gr.HTML(
|
|
|
171 |
)
|
172 |
clear = gr.Button("🗑️ Clear Chat", elem_classes="clear-button")
|
173 |
|
174 |
+
def update_chat(history, new_message):
|
175 |
+
history = history or []
|
176 |
+
history.append(new_message)
|
|
|
177 |
return history
|
178 |
|
179 |
analyze_button.click(
|
180 |
analyze_construction_image,
|
181 |
inputs=[image_input],
|
182 |
outputs=[chatbot],
|
183 |
+
postprocess=lambda x: update_chat(chatbot.value, x[0])
|
184 |
)
|
185 |
|
186 |
msg.submit(chat_about_image, [msg, chatbot], [msg, chatbot])
|