Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import base64
|
3 |
+
import openai
|
4 |
+
import numpy as np
|
5 |
+
import cv2
|
6 |
+
from tensorflow.keras.models import load_model
|
7 |
+
from keras.preprocessing.image import img_to_array
|
8 |
+
from keras.applications.inception_v3 import preprocess_input
|
9 |
+
import os
|
10 |
+
from PIL import Image
|
11 |
+
import io
|
12 |
+
st.set_page_config(page_title="Wall Defect Classifier", layout="centered")
|
13 |
+
|
14 |
+
# Set OpenAI API Key
|
15 |
+
openai.api_key = os.getenv('OPEN_AI')
|
16 |
+
|
17 |
+
# Defect categories
|
18 |
+
class_labels = [
|
19 |
+
"Floor Cracks",
|
20 |
+
"Floor Peel",
|
21 |
+
"Shade Fading",
|
22 |
+
"Air Bubble (Single Defect)",
|
23 |
+
"Spike Roller (Single Defect)"
|
24 |
+
]
|
25 |
+
|
26 |
+
@st.cache_resource
|
27 |
+
def load_trained_model():
|
28 |
+
return load_model('my_inceptionmodelwithoutaug (1).h5')
|
29 |
+
|
30 |
+
def compress_image(image_bytes, max_size_kb=500):
|
31 |
+
img = Image.open(io.BytesIO(image_bytes))
|
32 |
+
quality = 95
|
33 |
+
output_bytes = io.BytesIO()
|
34 |
+
while True:
|
35 |
+
output_bytes.seek(0)
|
36 |
+
output_bytes.truncate()
|
37 |
+
img.save(output_bytes, format='JPEG', quality=quality)
|
38 |
+
if len(output_bytes.getvalue()) <= max_size_kb * 1024 or quality <= 5:
|
39 |
+
break
|
40 |
+
quality -= 5
|
41 |
+
return output_bytes.getvalue()
|
42 |
+
|
43 |
+
def process_image_for_openai(image_bytes):
|
44 |
+
compressed_image = compress_image(image_bytes)
|
45 |
+
return base64.b64encode(compressed_image).decode('utf-8')
|
46 |
+
|
47 |
+
# Load model once
|
48 |
+
loaded_model = load_trained_model()
|
49 |
+
|
50 |
+
# App UI
|
51 |
+
|
52 |
+
st.title("🧠 Wall Defect Classification & AI-Based Description")
|
53 |
+
st.markdown("Upload a wall surface image to detect potential defects and generate a structured AI analysis.")
|
54 |
+
|
55 |
+
uploaded_file = st.file_uploader("📤 Upload an Image", type=["jpg", "jpeg", "png"])
|
56 |
+
|
57 |
+
if uploaded_file is not None:
|
58 |
+
file_bytes = uploaded_file.getvalue()
|
59 |
+
|
60 |
+
st.image(file_bytes, caption="🖼️ Uploaded Image", use_column_width=True)
|
61 |
+
|
62 |
+
# Preprocess for prediction
|
63 |
+
input_img = cv2.imdecode(np.frombuffer(file_bytes, np.uint8), cv2.IMREAD_COLOR)
|
64 |
+
input_img_resized = cv2.resize(input_img, (256, 256))
|
65 |
+
x = img_to_array(input_img_resized)
|
66 |
+
x = np.expand_dims(x, axis=0)
|
67 |
+
x = preprocess_input(x)
|
68 |
+
|
69 |
+
preds = loaded_model.predict(x)
|
70 |
+
class_index = np.argmax(preds[0])
|
71 |
+
max_probability = preds[0][class_index]
|
72 |
+
class_name = class_labels[class_index]
|
73 |
+
|
74 |
+
# Classification Result Display
|
75 |
+
st.subheader("🔍 Classification Result")
|
76 |
+
st.success(f"**Predicted Defect:** {class_name}")
|
77 |
+
st.progress(min(int(max_probability * 100), 100))
|
78 |
+
st.markdown(f"**Confidence Level:** {max_probability:.2%}")
|
79 |
+
|
80 |
+
if max_probability < 0.59:
|
81 |
+
st.warning("⚠️ The confidence score is below 59%. Please manually verify this result.")
|
82 |
+
else:
|
83 |
+
compressed_base64 = process_image_for_openai(file_bytes)
|
84 |
+
ai_prompt = (
|
85 |
+
f"Our trained model predicts the following defect: {class_name}. "
|
86 |
+
f"Can you analyze the following image and generate AI-based descriptions "
|
87 |
+
|
88 |
+
f"for this defect? The output format should be:\n"
|
89 |
+
f"Category ID: <Insert Category ID>\n"
|
90 |
+
f"Title: <Short Title of the Defect>\n"
|
91 |
+
f"Description: <A concise, technical description in 100 words or less>"
|
92 |
+
)
|
93 |
+
|
94 |
+
st.subheader("Generating AI-Based Analysis...")
|
95 |
+
try:
|
96 |
+
response = openai.ChatCompletion.create(
|
97 |
+
model="gpt-4o",
|
98 |
+
messages=[
|
99 |
+
{
|
100 |
+
"role": "user",
|
101 |
+
"content": [
|
102 |
+
{"type": "text", "text": ai_prompt},
|
103 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{compressed_base64}"}}
|
104 |
+
]
|
105 |
+
}
|
106 |
+
],
|
107 |
+
max_tokens=300,
|
108 |
+
)
|
109 |
+
ai_description = response.choices[0].message.content
|
110 |
+
st.subheader("📝 AI-Generated Defect Description")
|
111 |
+
st.text_area("Output", value=ai_description.strip(), height=250)
|
112 |
+
except Exception as e:
|
113 |
+
st.error(f"❌ An error occurred while generating the description:\n{e}")
|