capradeepgujaran
commited on
Commit
•
2799c43
1
Parent(s):
b6eb3bf
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ import logging
|
|
9 |
import cv2
|
10 |
import numpy as np
|
11 |
import traceback
|
|
|
12 |
|
13 |
# Set up logging
|
14 |
logging.basicConfig(level=logging.DEBUG)
|
@@ -193,6 +194,31 @@ def chat_about_image(message, chat_history):
|
|
193 |
logger.error(f"Error during chat: {str(e)}")
|
194 |
return "", chat_history + [(message, f"Error: {str(e)}")]
|
195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
# Custom CSS for improved styling
|
198 |
custom_css = """
|
@@ -258,9 +284,14 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
|
258 |
elem_classes="chat-input"
|
259 |
)
|
260 |
|
261 |
-
# Fifth row: Clear Chat
|
262 |
with gr.Row():
|
263 |
-
clear = gr.Button("🗑️ Clear", elem_classes="clear-button")
|
|
|
|
|
|
|
|
|
|
|
264 |
|
265 |
def update_chat(history, new_messages):
|
266 |
history = history or []
|
@@ -278,25 +309,24 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
|
|
278 |
msg.submit(chat_about_image, [msg, chatbot], [msg, chatbot])
|
279 |
clear.click(lambda: None, None, chatbot, queue=False)
|
280 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
281 |
gr.HTML(
|
282 |
"""
|
283 |
<div class="groq-badge">Powered by Groq</div>
|
284 |
"""
|
285 |
)
|
286 |
|
|
|
287 |
if __name__ == "__main__":
|
288 |
try:
|
289 |
-
|
290 |
-
iface.launch(debug=True, share=True)
|
291 |
except Exception as e:
|
292 |
-
logger.error(f"Error when trying to launch
|
293 |
logger.error(traceback.format_exc())
|
294 |
-
|
295 |
-
# If sharing fails, try to launch without sharing
|
296 |
-
try:
|
297 |
-
logger.info("Attempting to launch without sharing...")
|
298 |
-
iface.launch(debug=True, share=False)
|
299 |
-
except Exception as e:
|
300 |
-
logger.error(f"Error when trying to launch without sharing: {str(e)}")
|
301 |
-
logger.error(traceback.format_exc())
|
302 |
-
print("Failed to launch the Gradio interface. Please check the logs for more information.")
|
|
|
9 |
import cv2
|
10 |
import numpy as np
|
11 |
import traceback
|
12 |
+
from datetime import datetime
|
13 |
|
14 |
# Set up logging
|
15 |
logging.basicConfig(level=logging.DEBUG)
|
|
|
194 |
logger.error(f"Error during chat: {str(e)}")
|
195 |
return "", chat_history + [(message, f"Error: {str(e)}")]
|
196 |
|
197 |
+
def generate_summary_report(chat_history):
|
198 |
+
"""
|
199 |
+
Generate a summary report from the chat history.
|
200 |
+
"""
|
201 |
+
report = "Construction Site Safety Analysis Report\n"
|
202 |
+
report += "=" * 40 + "\n"
|
203 |
+
report += f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
|
204 |
+
|
205 |
+
for i, (user, ai) in enumerate(chat_history, 1):
|
206 |
+
if user:
|
207 |
+
report += f"Query {i}:\n{user}\n\n"
|
208 |
+
if ai:
|
209 |
+
report += f"Analysis {i}:\n{ai}\n\n"
|
210 |
+
report += "-" * 40 + "\n"
|
211 |
+
|
212 |
+
return report
|
213 |
+
|
214 |
+
def download_report(chat_history):
|
215 |
+
"""
|
216 |
+
Generate and provide a download link for the summary report.
|
217 |
+
"""
|
218 |
+
report = generate_summary_report(chat_history)
|
219 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
220 |
+
filename = f"safety_analysis_report_{timestamp}.txt"
|
221 |
+
return (filename, report)
|
222 |
|
223 |
# Custom CSS for improved styling
|
224 |
custom_css = """
|
|
|
284 |
elem_classes="chat-input"
|
285 |
)
|
286 |
|
287 |
+
# Fifth row: Clear Chat and Download Report Buttons
|
288 |
with gr.Row():
|
289 |
+
clear = gr.Button("🗑️ Clear Chat", elem_classes="clear-button")
|
290 |
+
download_button = gr.Button("📥 Download Report", elem_classes="download-button")
|
291 |
+
|
292 |
+
# Add a file component to handle the download
|
293 |
+
report_file = gr.File(label="Download Safety Analysis Report")
|
294 |
+
|
295 |
|
296 |
def update_chat(history, new_messages):
|
297 |
history = history or []
|
|
|
309 |
msg.submit(chat_about_image, [msg, chatbot], [msg, chatbot])
|
310 |
clear.click(lambda: None, None, chatbot, queue=False)
|
311 |
|
312 |
+
|
313 |
+
download_button.click(
|
314 |
+
download_report,
|
315 |
+
inputs=[chatbot],
|
316 |
+
outputs=[report_file]
|
317 |
+
)
|
318 |
+
|
319 |
gr.HTML(
|
320 |
"""
|
321 |
<div class="groq-badge">Powered by Groq</div>
|
322 |
"""
|
323 |
)
|
324 |
|
325 |
+
# Launch the app
|
326 |
if __name__ == "__main__":
|
327 |
try:
|
328 |
+
iface.launch(debug=True)
|
|
|
329 |
except Exception as e:
|
330 |
+
logger.error(f"Error when trying to launch the interface: {str(e)}")
|
331 |
logger.error(traceback.format_exc())
|
332 |
+
print("Failed to launch the Gradio interface. Please check the logs for more information.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|