capradeepgujaran
commited on
Commit
•
88efb3f
1
Parent(s):
8a33d7b
Update app.py
Browse files
app.py
CHANGED
@@ -38,11 +38,12 @@ 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")
|
45 |
image_data_url = f"data:image/png;base64,{encode_image(image)}"
|
|
|
46 |
|
47 |
messages = [
|
48 |
{
|
@@ -50,7 +51,7 @@ def analyze_construction_image(image):
|
|
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.
|
54 |
},
|
55 |
{
|
56 |
"type": "image_url",
|
@@ -77,28 +78,42 @@ def analyze_construction_image(image):
|
|
77 |
result = completion.choices[0].message.content
|
78 |
logger.debug(f"Raw API response: {result}")
|
79 |
|
|
|
|
|
|
|
|
|
80 |
# Parse the result
|
81 |
-
analysis_text = "
|
82 |
-
categories = result.split("Category
|
|
|
|
|
|
|
|
|
|
|
83 |
for category in categories:
|
84 |
lines = category.split("\n")
|
85 |
-
category_name = lines[0].strip()
|
86 |
analysis_text += f"Category: {category_name}\n"
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
91 |
analysis_text += f"Description: {description}\n"
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
95 |
|
96 |
logger.info("Analysis completed successfully")
|
97 |
-
return [(None, analysis_text)]
|
98 |
except Exception as e:
|
99 |
logger.error(f"Error during image analysis: {str(e)}")
|
100 |
-
|
101 |
-
|
|
|
|
|
102 |
def chat_about_image(message, chat_history):
|
103 |
try:
|
104 |
# Prepare the conversation history for the API
|
|
|
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")
|
45 |
image_data_url = f"data:image/png;base64,{encode_image(image)}"
|
46 |
+
logger.debug("Image encoded successfully")
|
47 |
|
48 |
messages = [
|
49 |
{
|
|
|
51 |
"content": [
|
52 |
{
|
53 |
"type": "text",
|
54 |
+
"text": "Analyze this construction site image. Identify any safety issues or hazards, categorize them, provide a detailed description, and suggest steps to resolve them."
|
55 |
},
|
56 |
{
|
57 |
"type": "image_url",
|
|
|
78 |
result = completion.choices[0].message.content
|
79 |
logger.debug(f"Raw API response: {result}")
|
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:
|
119 |
# Prepare the conversation history for the API
|