Update app.py
Browse files
app.py
CHANGED
@@ -35,10 +35,10 @@ def encode_image(image):
|
|
35 |
logger.error(f"Error encoding image: {str(e)}")
|
36 |
raise
|
37 |
|
38 |
-
def analyze_construction_image(image
|
39 |
if image is None:
|
40 |
logger.warning("No image provided")
|
41 |
-
return "Error: No image uploaded", "", ""
|
42 |
|
43 |
try:
|
44 |
logger.info("Starting image analysis")
|
@@ -62,12 +62,6 @@ def analyze_construction_image(image, follow_up_question=""):
|
|
62 |
}
|
63 |
]
|
64 |
|
65 |
-
if follow_up_question:
|
66 |
-
messages.append({
|
67 |
-
"role": "user",
|
68 |
-
"content": follow_up_question
|
69 |
-
})
|
70 |
-
|
71 |
logger.info("Sending request to Groq API")
|
72 |
completion = client.chat.completions.create(
|
73 |
model="llama-3.2-90b-vision-preview",
|
@@ -89,39 +83,84 @@ def analyze_construction_image(image, follow_up_question=""):
|
|
89 |
parsed_result = json.loads(result)
|
90 |
except json.JSONDecodeError:
|
91 |
logger.error("Failed to parse API response as JSON")
|
92 |
-
return "Error: Invalid response format", "", ""
|
93 |
|
94 |
snag_category = parsed_result.get('snag_category', 'N/A')
|
95 |
snag_description = parsed_result.get('snag_description', 'N/A')
|
96 |
desnag_steps = '\n'.join(parsed_result.get('desnag_steps', ['N/A']))
|
97 |
|
98 |
logger.info("Analysis completed successfully")
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
except Exception as e:
|
101 |
logger.error(f"Error during image analysis: {str(e)}")
|
102 |
-
return f"Error: {str(e)}", "", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
# Create the Gradio interface
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
gr.
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
)
|
|
|
|
|
|
|
125 |
|
126 |
# Launch the app
|
127 |
if __name__ == "__main__":
|
|
|
35 |
logger.error(f"Error encoding image: {str(e)}")
|
36 |
raise
|
37 |
|
38 |
+
def analyze_construction_image(image):
|
39 |
if image is None:
|
40 |
logger.warning("No image provided")
|
41 |
+
return "Error: No image uploaded", "", "", []
|
42 |
|
43 |
try:
|
44 |
logger.info("Starting image analysis")
|
|
|
62 |
}
|
63 |
]
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
logger.info("Sending request to Groq API")
|
66 |
completion = client.chat.completions.create(
|
67 |
model="llama-3.2-90b-vision-preview",
|
|
|
83 |
parsed_result = json.loads(result)
|
84 |
except json.JSONDecodeError:
|
85 |
logger.error("Failed to parse API response as JSON")
|
86 |
+
return "Error: Invalid response format", "", "", []
|
87 |
|
88 |
snag_category = parsed_result.get('snag_category', 'N/A')
|
89 |
snag_description = parsed_result.get('snag_description', 'N/A')
|
90 |
desnag_steps = '\n'.join(parsed_result.get('desnag_steps', ['N/A']))
|
91 |
|
92 |
logger.info("Analysis completed successfully")
|
93 |
+
|
94 |
+
# Initialize chat history with analysis results
|
95 |
+
chat_history = [
|
96 |
+
(None, f"Snag Category: {snag_category}\n\nSnag Description: {snag_description}\n\nSteps to Desnag:\n{desnag_steps}")
|
97 |
+
]
|
98 |
+
|
99 |
+
return snag_category, snag_description, desnag_steps, chat_history
|
100 |
except Exception as e:
|
101 |
logger.error(f"Error during image analysis: {str(e)}")
|
102 |
+
return f"Error: {str(e)}", "", "", []
|
103 |
+
|
104 |
+
def chat_about_image(message, chat_history):
|
105 |
+
try:
|
106 |
+
# Prepare the conversation history for the API
|
107 |
+
messages = [
|
108 |
+
{"role": "system", "content": "You are an AI assistant specialized in analyzing construction site images and answering questions about them. Use the information from the initial analysis to answer user queries."},
|
109 |
+
]
|
110 |
+
|
111 |
+
# Add chat history to messages
|
112 |
+
for human, ai in chat_history:
|
113 |
+
if human:
|
114 |
+
messages.append({"role": "user", "content": human})
|
115 |
+
if ai:
|
116 |
+
messages.append({"role": "assistant", "content": ai})
|
117 |
+
|
118 |
+
# Add the new user message
|
119 |
+
messages.append({"role": "user", "content": message})
|
120 |
+
|
121 |
+
# Make API call
|
122 |
+
completion = client.chat.completions.create(
|
123 |
+
model="llama-3.2-90b-chat", # Using the chat model here
|
124 |
+
messages=messages,
|
125 |
+
temperature=0.7,
|
126 |
+
max_tokens=500,
|
127 |
+
top_p=1,
|
128 |
+
stream=False,
|
129 |
+
stop=None
|
130 |
+
)
|
131 |
+
|
132 |
+
response = completion.choices[0].message.content
|
133 |
+
chat_history.append((message, response))
|
134 |
+
|
135 |
+
return "", chat_history
|
136 |
+
except Exception as e:
|
137 |
+
logger.error(f"Error during chat: {str(e)}")
|
138 |
+
return "", chat_history + [(message, f"Error: {str(e)}")]
|
139 |
|
140 |
# Create the Gradio interface
|
141 |
+
with gr.Blocks() as iface:
|
142 |
+
gr.Markdown("# Construction Image Analyzer with Chat")
|
143 |
+
with gr.Row():
|
144 |
+
with gr.Column(scale=1):
|
145 |
+
image_input = gr.Image(type="pil", label="Upload Construction Image")
|
146 |
+
analyze_button = gr.Button("Analyze Image")
|
147 |
+
with gr.Column(scale=2):
|
148 |
+
snag_category = gr.Textbox(label="Snag Category")
|
149 |
+
snag_description = gr.Textbox(label="Snag Description")
|
150 |
+
desnag_steps = gr.Textbox(label="Steps to Desnag")
|
151 |
+
|
152 |
+
chatbot = gr.Chatbot(label="Chat about the Image")
|
153 |
+
msg = gr.Textbox(label="Ask a question about the image")
|
154 |
+
clear = gr.Button("Clear")
|
155 |
+
|
156 |
+
analyze_button.click(
|
157 |
+
analyze_construction_image,
|
158 |
+
inputs=[image_input],
|
159 |
+
outputs=[snag_category, snag_description, desnag_steps, chatbot]
|
160 |
+
)
|
161 |
+
|
162 |
+
msg.submit(chat_about_image, [msg, chatbot], [msg, chatbot])
|
163 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
164 |
|
165 |
# Launch the app
|
166 |
if __name__ == "__main__":
|