Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,11 +9,25 @@ 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",
|
|
@@ -95,23 +109,34 @@ if category_choice == "Flooring":
|
|
| 95 |
f"Description: <A concise, technical description in 100 words or less>"
|
| 96 |
)
|
| 97 |
|
| 98 |
-
st.
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
import os
|
| 10 |
from PIL import Image
|
| 11 |
import io
|
| 12 |
+
from datetime import datetime
|
| 13 |
+
import pandas as pd
|
| 14 |
st.set_page_config(page_title="Wall Defect Classifier", layout="centered")
|
| 15 |
+
if "dpoc_list" not in st.session_state:
|
| 16 |
+
st.session_state.dpoc_list = []
|
| 17 |
# Set OpenAI API Key
|
| 18 |
openai.api_key = os.getenv('OPEN_AI')
|
| 19 |
+
def add_dpoc_entry(title, description):
|
| 20 |
+
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 21 |
+
entry = {
|
| 22 |
+
"id": len(st.session_state.dpoc_list) + 1,
|
| 23 |
+
"dpoc_category_id": 1, # You can dynamically set this if needed
|
| 24 |
+
"title": title,
|
| 25 |
+
"description": description,
|
| 26 |
+
"created_at": now,
|
| 27 |
+
"updated_at": now,
|
| 28 |
+
"deleted_at": ""
|
| 29 |
+
}
|
| 30 |
+
st.session_state.dpoc_list.append(entry)
|
| 31 |
# Defect categories
|
| 32 |
class_labels = [
|
| 33 |
"Floor Cracks",
|
|
|
|
| 109 |
f"Description: <A concise, technical description in 100 words or less>"
|
| 110 |
)
|
| 111 |
|
| 112 |
+
with st.spinner("π Generating AI description..."):
|
| 113 |
+
try:
|
| 114 |
+
response = openai.ChatCompletion.create(
|
| 115 |
+
model="gpt-4o",
|
| 116 |
+
messages=[
|
| 117 |
+
{
|
| 118 |
+
"role": "user",
|
| 119 |
+
"content": [
|
| 120 |
+
{"type": "text", "text": ai_prompt},
|
| 121 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{compressed_base64}"}}
|
| 122 |
+
]
|
| 123 |
+
}
|
| 124 |
+
],
|
| 125 |
+
max_tokens=300,
|
| 126 |
+
)
|
| 127 |
+
ai_description = response.choices[0].message.content
|
| 128 |
+
st.subheader("π AI-Generated Defect Description")
|
| 129 |
+
st.text_area("Output", value=ai_description.strip(), height=250)
|
| 130 |
+
except Exception as e:
|
| 131 |
+
st.error(f"β An error occurred while generating the description:\n{e}")
|
| 132 |
+
add_dpoc_entry(class_name, ai_description.strip())
|
| 133 |
+
st.subheader("π DPOC Log")
|
| 134 |
+
if st.session_state.dpoc_list:
|
| 135 |
+
dpoc_df = pd.DataFrame(st.session_state.dpoc_list)
|
| 136 |
+
st.dataframe(dpoc_df, use_container_width=True)
|
| 137 |
+
st.download_button("π₯ Download CSV", dpoc_df.to_csv(index=False), "dpoc_log.csv", "text/csv")
|
| 138 |
+
else:
|
| 139 |
+
st.info("No entries yet. Upload an image to start.")
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|