Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,66 +53,66 @@ loaded_model = load_trained_model()
|
|
| 53 |
|
| 54 |
st.title("๐ง Wall Defect Classification & AI-Based Description")
|
| 55 |
category_choice = st.selectbox("๐ ๏ธ Select Defect Category Type:", ["Flooring"], index=0)
|
| 56 |
-
|
| 57 |
-
st.markdown("Upload a wall surface image to detect potential defects and generate a structured AI analysis.")
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
uploaded_file = st.file_uploader("๐ค Upload an Image", type=["jpg", "jpeg", "png"])
|
| 61 |
-
|
| 62 |
-
if uploaded_file is not None:
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
if max_probability < 0.59:
|
| 86 |
-
st.warning("โ ๏ธ The confidence score is below 59%. Please manually verify this result.")
|
| 87 |
-
else:
|
| 88 |
-
compressed_base64 = process_image_for_openai(file_bytes)
|
| 89 |
-
ai_prompt = (
|
| 90 |
-
f"Our trained model predicts the following defect: {class_name}. "
|
| 91 |
-
f"Can you analyze the following image and generate AI-based descriptions "
|
| 92 |
-
|
| 93 |
-
f"for this defect? The output format should be:\n"
|
| 94 |
-
f"Category ID: <Insert Category ID>\n"
|
| 95 |
-
f"Title: <Short Title of the Defect>\n"
|
| 96 |
-
f"Description: <A concise, technical description in 100 words or less>"
|
| 97 |
-
)
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
],
|
| 112 |
-
max_tokens=300,
|
| 113 |
)
|
| 114 |
-
|
| 115 |
-
st.subheader("
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
st.title("๐ง Wall Defect Classification & AI-Based Description")
|
| 55 |
category_choice = st.selectbox("๐ ๏ธ Select Defect Category Type:", ["Flooring"], index=0)
|
| 56 |
+
if category_choice == "Flooring":
|
| 57 |
+
st.markdown("Upload a wall surface image to detect potential defects and generate a structured AI analysis.")
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
uploaded_file = st.file_uploader("๐ค Upload an Image", type=["jpg", "jpeg", "png"])
|
| 61 |
+
|
| 62 |
+
if uploaded_file is not None:
|
| 63 |
+
file_bytes = uploaded_file.getvalue()
|
| 64 |
+
|
| 65 |
+
st.image(file_bytes, caption="๐ผ๏ธ Uploaded Image", use_column_width=True)
|
| 66 |
+
|
| 67 |
+
# Preprocess for prediction
|
| 68 |
+
input_img = cv2.imdecode(np.frombuffer(file_bytes, np.uint8), cv2.IMREAD_COLOR)
|
| 69 |
+
input_img_resized = cv2.resize(input_img, (256, 256))
|
| 70 |
+
x = img_to_array(input_img_resized)
|
| 71 |
+
x = np.expand_dims(x, axis=0)
|
| 72 |
+
x = preprocess_input(x)
|
| 73 |
+
|
| 74 |
+
preds = loaded_model.predict(x)
|
| 75 |
+
class_index = np.argmax(preds[0])
|
| 76 |
+
max_probability = preds[0][class_index]
|
| 77 |
+
class_name = class_labels[class_index]
|
| 78 |
+
|
| 79 |
+
# Classification Result Display
|
| 80 |
+
st.subheader("๐ Classification Result")
|
| 81 |
+
st.success(f"**Predicted Defect:** {class_name}")
|
| 82 |
+
st.progress(min(int(max_probability * 100), 100))
|
| 83 |
+
st.markdown(f"**Confidence Level:** {max_probability:.2%}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
if max_probability < 0.59:
|
| 86 |
+
st.warning("โ ๏ธ The confidence score is below 59%. Please manually verify this result.")
|
| 87 |
+
else:
|
| 88 |
+
compressed_base64 = process_image_for_openai(file_bytes)
|
| 89 |
+
ai_prompt = (
|
| 90 |
+
f"Our trained model predicts the following defect: {class_name}. "
|
| 91 |
+
f"Can you analyze the following image and generate AI-based descriptions "
|
| 92 |
+
|
| 93 |
+
f"for this defect? The output format should be:\n"
|
| 94 |
+
f"Category ID: <Insert Category ID>\n"
|
| 95 |
+
f"Title: <Short Title of the Defect>\n"
|
| 96 |
+
f"Description: <A concise, technical description in 100 words or less>"
|
|
|
|
|
|
|
| 97 |
)
|
| 98 |
+
|
| 99 |
+
st.subheader("Generating AI-Based Analysis...")
|
| 100 |
+
try:
|
| 101 |
+
response = openai.ChatCompletion.create(
|
| 102 |
+
model="gpt-4o",
|
| 103 |
+
messages=[
|
| 104 |
+
{
|
| 105 |
+
"role": "user",
|
| 106 |
+
"content": [
|
| 107 |
+
{"type": "text", "text": ai_prompt},
|
| 108 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{compressed_base64}"}}
|
| 109 |
+
]
|
| 110 |
+
}
|
| 111 |
+
],
|
| 112 |
+
max_tokens=300,
|
| 113 |
+
)
|
| 114 |
+
ai_description = response.choices[0].message.content
|
| 115 |
+
st.subheader("๐ AI-Generated Defect Description")
|
| 116 |
+
st.text_area("Output", value=ai_description.strip(), height=250)
|
| 117 |
+
except Exception as e:
|
| 118 |
+
st.error(f"โ An error occurred while generating the description:\n{e}")
|