Spaces:
Runtime error
Runtime error
Update app.py
Browse files5 unmodified lines67891011121314151617181920212223242526272829303132333435363738394041424344454647488 unmodified lines575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383395 unmodified linesfrom tensorflow.keras.models import load_modelfrom keras.preprocessing.image import img_to_arrayfrom keras.applications.inception_v3 import preprocess_inputimport osfrom PIL import Imageimport iofrom datetime import datetimeimport pandas as pdst.set_page_config(page_title="Wall Defect Classifier", layout="centered")if "dpoc_list" not in st.session_state:st.session_state.dpoc_list = []# Set OpenAI API Keyopenai.api_key = os.getenv('OPEN_AI')def add_dpoc_entry(title, description):now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")entry = {"id": len(st.session_state.dpoc_list) + 1,"dpoc_category_id": 1, # You can dynamically set this if needed"title": title,"description": description,"created_at": now,"updated_at": now,"deleted_at": ""}st.session_state.dpoc_list.append(entry)# Defect categoriesclass_labels = ["Construction Joint","Floor Cracks","Floor Debonding","Floor peeling","Floor oil","Flooring Bubble","Pot holes","Shade fading"]@st.cache_resourcedef load_trained_model():return load_model('my_inceptionmodelwithoutaug8a.h5')def compress_image(image_bytes, max_size_kb=500):img = Image.open(io.BytesIO(image_bytes))8 unmodified linesquality -= 5return output_bytes.getvalue()def process_image_for_openai(image_bytes):compressed_image = compress_image(image_bytes)return base64.b64encode(compressed_image).decode('utf-8')# Load model onceloaded_model = load_trained_model()# App UIst.title("π§ Wall Defect Classification & AI-Based Description")category_choice = st.selectbox("π οΈ Select Defect Category Type:", ["Painting","Flooring"], index=0)if category_choice == "Flooring":st.markdown("Upload a wall surface image to detect potential defects and generate a structured AI analysis.")uploaded_file = st.file_uploader("π€ Upload an Image", type=["jpg", "jpeg", "png"])if uploaded_file is not None:file_bytes = uploaded_file.getvalue()st.image(file_bytes, caption="πΌοΈ Uploaded Image", use_column_width=True)# Preprocess for predictioninput_img = cv2.imdecode(np.frombuffer(file_bytes, np.uint8), cv2.IMREAD_COLOR)input_img_resized = cv2.resize(input_img, (256, 256))x = img_to_array(input_img_resized)x = np.expand_dims(x, axis=0)x = preprocess_input(x)preds = loaded_model.predict(x)print(preds)class_index = np.argmax(preds[0])print(class_index)max_probability = preds[0][class_index]print(max_probability)class_name = class_labels[class_index]print(class_name)# Classification Result Displayst.subheader("π Classification Result")st.success(f"**Predicted Defect:** {class_name}")st.progress(min(int(max_probability * 100), 100))st.markdown(f"**Confidence Level:** {max_probability:.2%}")if max_probability < 0.59:st.warning("β οΈ The confidence score is below 59%. Please manually verify this result.")else:compressed_base64 = process_image_for_openai(file_bytes)ai_content = """You are an expert in identifying and resolving wall and flooring defects. Use the dataset below to generate all applicable recommended solutions for any provided defect or combination of defects.Instructions:Accept a defect name as input.Search the dataset for matches and return all recommended solutions, grouped by Sub-Defect Type.If the defect appears under multiple subtypes, combine and list all applicable solutions.If the defect is not found in the dataset, use semantic analysis and your domain expertise to generate a logical and professional solution.Do not mention whether the defect is listed in the dataset or not β simply return the recommended solution.Use clear steps such as: surface preparation, grinding, priming, coating, sealing, repairing, joint treatment, or finishing, depending on the nature of the defectDataset:Defect: Spike Roller MarkSub-Defect Type: EpoxyRecommended Solution:1. Surface preparation on existing resinious flooring by light grinding.2. Application of Primer.3. Application of thin film coating with Apcoflor TC 510Defect: Spike Roller MarkSub-Defect Type: PURecommended Solution:1. Surface preparation on existing resinious flooring by light grinding.2. Application of Primer.3. Application of thin film coating with Apcoflor PUVDefect: Trowel MarkSub-Defect Type: EpoxyRecommended Solution:1. Surface preparation on existing resinious flooring by light grinding.2. Application of Primer.3. Application of epoxy topcoat with 1mm thickness using Apcoflor SL1TCDefect: Trowel MarkSub-Defect Type: PURecommended Solution:1. Surface preparation on existing resinious flooring by light grinding2. Groove cutting3. Application of PU scratch coat4. Application of Topcoat with 1 to 2 mm thicknessDefect: Bubble FormationSub-Defect Type: EpoxyRecommended Solution:1. Surface preparation on existing resinious flooring by light grinding.2. Application of Primer.3. Application of thin film coating with Apcoflor TC 510Defect: Bubble FormationSub-Defect Type: PURecommended Solution:1. Surface preparation on existing resinious flooring by light grinding2. Groove cutting3. Application of PU scratch coat4. Application of Topcoat with 1 to 5 mm thicknessDefect: Peel off/DebondingSub-Defect Type: EpoxyRecommended Solution:1. Surface preparation on existing resinious flooring by light grinding2. Removing all loose resinious flooring3. Application of Primer4. Application of thin film coating with Apcoflor TC 510/Apcoflor SL1TCDefect: Peel off/DebondingSub-Defect Type: PURecommended Solution:1. Surface preparation on existing resinious flooring by light grinding2. Groove cutting3. Application of PU scratch coat4. Application of Topcoat with 1 to 5 mm thicknessDefect: Shade VariationSub-Defect Type: EpoxyRecommended Solution:1. Surface preparation on existing resinious flooring by light grinding2. Application of Primer3. Application of thin film coating with Apcoflor TC 510/Apcoflor SL1 TCDefect: Shade VariationSub-Defect Type: PURecommended Solution:1. Surface preparation on existing resinious flooring by light grinding2. Application of Primer3. Application of thin film coating with Apcoflor PUVDefect: Cracks on FloorsSub-Defect Type: Epoxy and PURecommended Solution:1. Surface preparation - opening the cracks in "V" groove2. Application of Primer3. Application of epoxy putty using Apcoflor LSC3 XLDefect: Expansion JointSub-Defect Type: Cracking & Spalling of Joint EdgeRecommended Solution:1. Crack Treatment - Open cracks in "V" groove- Apply Epoxy Primer- Apply epoxy putty using Apcoflor LSC3 XL2. Joint Edge Treatment- Remove loose concrete- Apply Epoxy mortar using Apcoflor HFP 120 + special sand at 1:3.5 mix ratio- Apply at damaged joint edgesDefect: Expansion JointSub-Defect Type: Sealant FailureRecommended Solution:1. Apply PU-based backer rod2. Apply PU/Polysulphide Sealant over the backer rod up to the top of the joint edges3. Provide protection using self-adhesive tapes or aluminum strippingDefect: Construction JointSub-Defect Type: -Recommended Solution:1. Remove dust/impurities from joints2. Apply Primer3. Apply Epoxy SL Screed or PU ScreedDefect: Wall & Floor Junction CovingSub-Defect Type: -Recommended Solution:1. Surface preparation2. Apply Epoxy Primer or scratch coat3. Apply Epoxy mortar using Apcoflor HFP 120 + special graded sand at 1:3 mixing ratioDefect: Flooring - POT HolesSub-Defect Type: -Recommended Solution:1. Surface preparation - Cut the pothole in square/rectangle shape, remove dust/oil2. Apply Epoxy Primer3. Apply Apcoflor HFP 120 + special graded sand at 1:3 mix ratio4. Finish/level the surfaceFallback Logic for Unknown Defects:Use these patterns to infer the solution without stating the defect is not listed.Structural Defect:Cut/clean damaged areaSurface preparation (light grinding or cutting)Apply epoxy/PU primerApply screed or mortar (e.g., Apcoflor HFP 120 + sand)Finish and level as neededAesthetic Defect (e.g., fading, discoloration):Surface preparation by light grindingApplication of appropriate primerApplication of thin film coating (Epoxy: Apcoflor TC 510, PU: Apcoflor PUV, Hybrid as needed)Final finish for uniform appearanceCracks & Breaks:Open in "V" grooveApply primerFill with epoxy putty (e.g., Apcoflor LSC3 XL)Joint Issues:Remove loose particlesApply epoxy primerJoint treatment with sealants or epoxy mortarsProtect with tape or metal strip if needed"""ai_prompt = (f"Our trained model predicts the following defect: {class_name}. "f"for this defect? The output format should be:\n"f"Category ID: <Insert Category ID>\n"f"Title: <Short Title of the Defect>\n"f"Description: <A concise, technical description in 100 words or less>")with st.spinner("π Generating AI description..."):try:response = openai.ChatCompletion.create(model="gpt-4o",messages=[{"role": "system","content": ai_content},{"role":"user","content":f"Generate recommended solutions for this defect '{class_name}'"}],max_tokens=600,)ai_description = response.choices[0].message.contentst.subheader("π AI-Generated Defect Description")st.text_area("Output", value=ai_description.strip(), height=250)except Exception as e:st.error(f"β An error occurred while generating the description:\n{e}")add_dpoc_entry(class_name, ai_description.strip())st.subheader("π DPOC")if st.session_state.dpoc_list:dpoc_df = pd.DataFrame(st.session_state.dpoc_list)st.dataframe(dpoc_df, use_container_width=True)st.download_button("π₯ Download CSV", dpoc_df.to_csv(index=False), "dpoc_log.csv", "text/csv")else:st.info("No entries yet. Upload an image to start.")5 unmodified lines6789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828 unmodified lines9192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868875 unmodified linesfrom tensorflow.keras.models import load_modelfrom keras.preprocessing.image import img_to_arrayfrom keras.applications.inception_v3 import preprocess_inputimport requestsimport json_repairimport jsonfrom PIL import Imageimport iofrom datetime import datetimeimport pandas as pdimport refrom json_repair import repair_jsonfrom dotenv import load_dotenvimport os# Load environment variablesload_dotenv()# Class labels for defect predictionclass_labels = ["algae","bubbles","Cracks","Fungus","peeling",]# Set OpenAI API keyopenai.api_key = os.getenv("OPENAI_API_KEY")# Page configurationst.set_page_config(page_title="Painting Defect Detection System",page_icon="π",layout="wide",initial_sidebar_state="expanded")# Custom CSS for better stylingst.markdown("""<style>.main {padding-top: 2rem;}.stAlert {margin-top: 1rem;}.defect-card {border: 1px solid #ddd;border-radius: 8px;padding: 1rem;margin: 1rem 0;background-color: #f9f9f9;}.metric-card {background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);padding: 1rem;border-radius: 8px;color: white;text-align: center;margin: 0.5rem 0;}</style>""", unsafe_allow_html=True)# Cache the model loading to improve performance@st.cache_resourcedef load_trained_model():try:return load_model('painting.keras')except Exception as e:st.error(f"Error loading model: {str(e)}")return Nonedef compress_image(image_bytes, max_size_kb=500):img = Image.open(io.BytesIO(image_bytes))8 unmodified linesquality -= 5return output_bytes.getvalue()def get_direct_drive_url(share_url):if "drive.google.com/file/d/" in share_url:file_id = share_url.split("/file/d/")[1].split("/")[0]return f"https://drive.google.com/uc?export=download&id={file_id}"return share_urldef process_image(url):try:file_id = re.search(r'/d/(.*?)/', url)if file_id: # Google Drive share linkurl = f'https://drive.google.com/uc?export=download&id={file_id.group(1)}'resp = requests.get(url, timeout=15)resp.raise_for_status()if 'image' not in resp.headers.get('Content-Type', ''):raise ValueError('URL does not point to an image')img = cv2.imdecode(np.frombuffer(resp.content, np.uint8), cv2.IMREAD_COLOR)input_img_resized = cv2.resize(img, (256, 256))x = img_to_array(input_img_resized)x = np.expand_dims(x, axis=0)x = preprocess_input(x)return x, resp.contentexcept Exception as e:st.error(f"Error processing image from URL: {e}")return None, Nonedef process_uploaded_image(uploaded_file):try:image_bytes = uploaded_file.read()img = Image.open(io.BytesIO(image_bytes))img_array = np.array(img)# Convert RGB to BGR for OpenCVif len(img_array.shape) == 3:img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)input_img_resized = cv2.resize(img_array, (256, 256))x = img_to_array(input_img_resized)x = np.expand_dims(x, axis=0)x = preprocess_input(x)return x, image_bytesexcept Exception as e:st.error(f"Error processing uploaded image: {e}")return None, Nonedef predict_defect(processed_image, model):try:preds = model.predict(processed_image)[0]class_index = int(np.argmax(preds))confidence = float(preds[class_index])class_name = class_labels[class_index]return {"type": class_name,"severity": confidence}, class_indexexcept Exception as e:return {"error": str(e)}, []def compress_image_to_base64(image_bytes, max_size_kb=500):img = Image.open(io.BytesIO(image_bytes)).convert("RGB")output_bytes = io.BytesIO()quality = 95while True:output_bytes.seek(0)output_bytes.truncate()img.save(output_bytes, format='JPEG', quality=quality)if len(output_bytes.getvalue()) <= max_size_kb * 1024 or quality <= 5:breakquality -= 5return base64.b64encode(output_bytes.getvalue()).decode('utf-8')def generate_openai_description(classification, image_bytes):try:if isinstance(classification, dict) and 'type' in classification:defect_type = classification['type']elif isinstance(classification, dict) and 'error' in classification:defect_type = "Unknown defect"else:defect_type = str(classification)# Compress and encode image to base64compressed_base64 = compress_image_to_base64(image_bytes)ai_prompt = (f"You are an expert painting inspector analyzing surface defects."f"DETECTED DEFECTS: {defect_type}"f"TASK: Examine the painted surface image and provide a technical description of the visible defects."f"REQUIREMENTS:"f"- Write 200 characters describing what you observe"f"- Focus on visible paint failures, surface irregularities, or coating issues"f"- Use professional painting and coating terminology"f"- Be specific about location, extent, and characteristics of defects"f"- If multiple defects are present, describe each one"f"- Maintain an objective, technical tone"f"Dont use \\n"f"IMPORTANT: Always provide a description based on what you can see in the image. "f"Even if the detected defect types seem unclear, describe the actual visible conditions "f"in the painted surface. Only respond with 'Unable to generate description due to "f"image quality issues' if the image is genuinely too blurry, dark, or corrupted to analyze."f"Begin your description now:")# Call OpenAI with image and textresponse = openai.chat.completions.create(model="gpt-4o",messages=[{"role": "user","content": [{"type": "text", "text": ai_prompt},{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{compressed_base64}"}}]}])result_text = response.choices[0].message.content.strip()return result_textexcept Exception as e:return f"Error generating description: {str(e)}"def get_sps(defect_type, flooring_type,defect_category="flooring"):ai_content = """ You are an expert in identifying and resolving painting and wall surface defects.Your task is to accept a defect name and surface type as input and return applicable solutions using the structured dataset below.IMPORTANT: You MUST respond with valid JSON only. Do not include any explanatory text before or after the JSON.Instructions:Return TWO separate categories of solutions:MANDATORY SPS: Solutions specifically matching the input defectOPTIONAL SPS: Solutions from 'All Defects' records (universal solutions)For MANDATORY SPS:Match the input defect against the 'Defects' column using:Exact matchPartial match (e.g. 'Cracks' matches all 'Cracks' records)EXCLUDE any 'All Defects' records from this sectionOnly return records where the 'Category' matches the specified surface type (NonWaterproofing or Waterproofing)For OPTIONAL SPS:ONLY include records where 'Defects' is 'All Defects', 'ALL Defects', or 'All Defect'Filter by surface type:If surface_type is "NonWaterproofing": Return records with Category="NonWaterproofing"If surface_type is "Waterproofing": Return records with Category="Waterproofing"Return only those matching the specified surface typeFor each valid record in both categories:Use the id from the dataset. If not available, generate a unique numeric ID.Use the title from the dataset. If not available, use appropriate fallback title based on surface typeUse the description from the dataset exactly (verbatim). If not found, generate fallback content based on best practices.CRITICAL FOR PRODUCT EXTRACTION:Carefully analyze the description content for ANY product names, brand names, or material referencesExtract ALL product names mentioned in the description, including:Brand names (e.g., "Asian Paints SmartCare Crack Seal")Generic product names (e.g., "Economy Primer", "Ace Suprema")Material names (e.g., "white cement", "fine sand")Tool names (e.g., "Putty Knife", "Paint Scraper")Chemical names (e.g., "oxygen bleach", "chlorinated bleach")List them in products array, assigning a unique ID per productNEVER leave products array empty - if no specific products are mentioned, include generic alternatives like "Primer", "Paint", "Cleaning Solution", etc.Include all records where Category matches the filtering conditionIMPORTANT: Remove all HTML tags from the description content. Convert HTML lists to plain text format.Product Extraction Examples:"Use Asian Paints SmartCare Crack Seal" β Extract: "Asian Paints SmartCare Crack Seal""Economy Primer, Ace Suprema" β Extract: "Economy Primer", "Ace Suprema""Use boiling water, pressure washers, oxygen bleach" β Extract: "Pressure Washer", "Oxygen Bleach""Sand with medium-grit sandpaper" β Extract: "Medium-grit Sandpaper"If no matching dataset entry is found for MANDATORY SPS, return one fallback solution following industry-standard practices for the specified surface type (without stating it is a fallback).Each solution must follow standard practices such as:Surface preparationCleaning and washingPrimingCrack fillingSandingPaint applicationSealingMoisture treatmentFinishingEach solution must follow standard practices such as:- Surface preparation- Cleaning and washing- Priming- Crack filling- Sanding- Paint application- Sealing- Moisture treatment- FinishingReturn ONLY this JSON format (no other text):{"mandatory_sps": [{"id": 181,"title": "Surface Preparation for Cracks - NonWaterproofing","content": "New masonry surfaces must be allowed to cure completely. It is recommended to allow 28 days as the curing time for new masonry surfaces. Opening the cracks with the help of a grinder/cutter and cleaning the surface with pressure washers for further treatment. FILLING FOR CRACKS For filling cracks up to 3mm, use Asian Paints SmartCare Crack Seal. For filling cracks more than 3mm, use Asian Paints SmartCare Textured Crack Filler. FILLING FOR HOLES & DENTS In case of dents and holes, use TruCare Wall Putty Suprema or a mix of white cement and fine sand in the ratio 1:3.","products": [{"id": 1,"product_name": "Asian Paints SmartCare Crack Seal"},{"id": 2,"product_name": "Asian Paints SmartCare Textured Crack Filler"},{"id": 3,"product_name": "TruCare Wall Putty Suprema"}]}],"optional_sps": [{"id": 211,"title": "4 Year Paint Warranty System - NonWaterproofing","content": "Economy Primer, Ace Suprema","products": [{"id": 4,"product_name": "Economy Primer"},{"id": 5,"product_name": "Ace Suprema"}]}]}Dataset:id: 181Defects: CracksCategory: NonWaterproofingtitle: Surface Preparation for Cracks - NonWaterproofingdescription: New masonry surfaces must be allowed to cure completely. It is recommended to allow 28 days as the curing time for new masonry surfaces. Opening the cracks with the help of a grinder/cutter and cleaning the surface with pressure washers for further treatment.FILLING FOR CRACKSFor filling cracks up to 3mm, use Asian Paints SmartCare Crack Seal.For filling cracks more than 3mm, use Asian Paints SmartCare Textured Crack Filler.FILLING FOR HOLES & DENTSIn case of dents and holes, use TruCare Wall Putty Suprema or a mix of white cement and fine sand in the ratio 1:3.id: 182Defects: CracksCategory: Waterproofingtitle: Surface Preparation for Cracks - Waterproofingdescription: New masonry surfaces must be allowed to cure completely. It is recommended to allow 28 days as the curing time for new masonry surfaces. Opening the cracks with the help of a grinder/cutter and cleaning the surface with pressure washers for further treatment.FILLING FOR CRACKSFor filling cracks up to 3mm, use Asian Paints SmartCare Crack Seal.For filling cracks more than 3mm, use Asian Paints SmartCare Textured Crack Filler.FILLING FOR HOLES & DENTSIn case of dents and holes, use TruCare Wall Putty Suprema or a mix of white cement and fine sand in the ratio 1:3.id: 183Defects: PatchinessCategory: NonWaterproofingtitle: Surface Preparation for Patchiness - NonWaterproofingdescription: Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface with pressure washers for further treatmentid: 184Defects: PatchinessCategory: Waterproofingtitle: Surface Preparation for Patchiness - Waterproofingdescription: Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface with pressure washers for further treatmentid: 185Defects: PeelingCategory: NonWaterproofingtitle: Surface Preparation for Peeling - NonWaterproofingdescription: Use Putty Knife or Paint Scraper for removing loose and peeling paint without damaging the underlying surface.Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface for further treatmentSurface should be free from any loose paint, dust or grease.Growths of fungus, algae or moss should be removed by wire brushing and water.id: 186Defects: PeelingCategory: Waterproofingtitle: Surface Preparation for Peeling - Waterproofingdescription: Use Putty Knife or Paint Scraper for removing loose and peeling paint without damaging the underlying surface.Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface for further treatmentSurface should be free from any loose paint, dust or grease.Growths of fungus, algae or moss should be removed by wire brushing and water.id: 187Defects: AlgaeCategory: NonWaterproofingtitle: Surface Preparation for Algae - NonWaterproofingdescription: Use boiling water, pressure washers, oxygen bleach, chlorinated bleach, and commercial algae removers to get rid of green algae on concrete. Pressure wash and scrape the wall with a soft brushid: 188Defects: AlgaeCategory: Waterproofingtitle: Surface Preparation for Algae - Waterproofingdescription: Use boiling water, pressure washers, oxygen bleach, chlorinated bleach, and commercial algae removers to get rid of green algae on concrete. Pressure wash and scrape the wall with a soft brushid: 189Defects: BubbleCategory: NonWaterproofingtitle: Surface Preparation for Bubble - NonWaterproofingdescription: If moisture is the issue, fix any leaks or dampness before proceeding. Moisture must be fully resolved to prevent new bubbling. Remove the buubled paint by using the scraper to peel off the loose and raised areas.Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface with pressure washers for further treatmentid: 190Defects: BubbleCategory: Waterproofingtitle: Surface Preparation for Bubble - Waterproofingdescription: If moisture is the issue, fix any leaks or dampness before proceeding. Moisture must be fully resolved to prevent new bubbling. Remove the buubled paint by using the scraper to peel off the loose and raised areas.Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface with pressure washers for further treatmentid: 191Defects: BlistersCategory: NonWaterproofingtitle: Surface Preparation for Blisters - NonWaterproofingdescription: Remove blisters by scraping, sanding or pressure-washing down to underlying coats of paint or primer.Repair loose caulking and improve ventilation of the building to prevent a recurring problemid: 192Defects: BlistersCategory: Waterproofingtitle: Surface Preparation for Blisters - Waterproofingdescription: Remove blisters by scraping, sanding or pressure-washing down to underlying coats of paint or primer.Repair loose caulking and improve ventilation of the building to prevent a recurring problemid: 193Defects: EfflorescenseCategory: NonWaterproofingtitle: Surface Preparation for Efflorescense - NonWaterproofingdescription: Stiff brushes are used to sweep away efflorescence from smoother surfaces.Apply a forceful water rinse to efflorescence with a pressure washer to clean it upid: 194Defects: EfflorescenseCategory: Waterproofingtitle: Surface Preparation for Efflorescense - Waterproofingdescription: Stiff brushes are used to sweep away efflorescence from smoother surfaces.Apply a forceful water rinse to efflorescence with a pressure washer to clean it upid: 195Defects: FungusCategory: NonWaterproofingtitle: Surface Preparation for Fungus - NonWaterproofingdescription: Use boiling water, pressure washers, oxygen bleach, chlorinated bleach, and commercial fungus removers to get rid of fungus on concrete. Pressure wash and scrape the wall with a soft brushid: 196Defects: FungusCategory: Waterproofingtitle: Surface Preparation for Fungus - Waterproofingdescription: Use boiling water, pressure washers, oxygen bleach, chlorinated bleach, and commercial fungus removers to get rid of fungus on concrete. Pressure wash and scrape the wall with a soft brushid: 197Defects: Poor HidingCategory: NonWaterproofingtitle: Surface Preparation for Poor Hiding - NonWaterproofingdescription: Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface with pressure washers for further treatmentid: 198Defects: Poor HidingCategory: Waterproofingtitle: Surface Preparation for Poor Hiding - Waterproofingdescription: Sand the surface with medium-grit sandpaper to remove major surface irregularities bumps and rough patches. Switch to fine-grit sandpaper for final smoothing. Work in small circular motions overlapping each area. Remove all sanding dust by cleaning the surface with pressure washers for further treatmentid: 199Defects: Shade variationCategory: NonWaterproofingtitle: Surface Preparation for Shade variation - NonWaterproofingdescription: Remove Loose Material: Carefully strip away loose or blistered paint to expose a firm surface.Spot Priming: Apply an appropriate primer to the affected area, ensuring adhesion.id: 200Defects: Shade variationCategory: Waterproofingtitle: Surface Preparation for Shade variation - Waterproofingdescription: Remove Loose Material: Carefully strip away loose or blistered paint to expose a firm surface.Spot Priming: Apply an appropriate primer to the affected area, ensuring adhesion.id: 211Defects: All DefectsCategory: NonWaterproofingtitle: 4 Year Paint Warranty System - NonWaterproofingdescription: Economy Primer, Ace Supremaid: 212Defects: All DefectsCategory: NonWaterproofingtitle: 7 year Paint Warranty System - NonWaterproofingdescription: Economy Primer, Apex Supremaid: 213Defects: All DefectsCategory: NonWaterproofingtitle: 8 year Paint Warranty System - NonWaterproofingdescription: Exterior Primer, Ultima Strech Supremaid: 214Defects: All DefectsCategory: NonWaterproofingtitle: 9 year Paint Warranty System - NonWaterproofingdescription: Exterior Primer, Ultima Supremaid: 215Defects: All DefectsCategory: NonWaterproofingtitle: 12 year Paint Warranty System - NonWaterproofingdescription: Exterior Primer, Protek Topcoatid: 216Defects: All DefectsCategory: NonWaterproofingtitle: 15 year Paint Warranty System - NonWaterproofingdescription: Exterior Primer, Duralife Topcoatid: 217Defects: All DefectsCategory: Waterproofingtitle: 4 year Complete Paint Warranty System - Waterproofingdescription: Damp Sheath suprema, Ace Supremaid: 218Defects: All DefectsCategory: Waterproofingtitle: 6 year Complete Paint Warranty System - Waterproofingdescription: Damp Proof Suprema, Apex Supremaid: 219Defects: All DefectsCategory: Waterproofingtitle: 8 year Complete Paint Warranty System - Waterproofingdescription: Damp Proof Suprema, Ultima Strech Supremaid: 220Defects: All DefectsCategory: Waterproofingtitle: 10 year Complete Paint Warranty System - Waterproofingdescription: Damp Prime Xtreme, Ultima Stretch Supremaid: 221Defects: All DefectsCategory: Waterproofingtitle: 12 year Complete Paint Warranty System - Waterproofingdescription: Protek Basecoat, Protek Topcoatid: 222Defects: All DefectsCategory: Waterproofingtitle: 15 year Complete Paint Warranty System - Waterproofingdescription: Duralife Basecoat, Duralife Topcoat"""def remove_html_tags(text):"""Remove HTML tags and convert lists to plain text format"""if not text:return texttext = re.sub(r'<li>(.*?)</li>', r'β’ \1', text, flags=re.IGNORECASE | re.DOTALL)text = re.sub(r'<[^>]+>', '', text)text = re.sub(r'\s+', ' ', text).strip()text = text.replace('\n', ' ').replace('\r', '')return texttry:response = openai.chat.completions.create(model="gpt-4o",messages=[{"role": "system","content": ai_content},{"role": "user","content": f"Generate recommended solutions for defect '{defect_type}' with painting type '{flooring_type}'. Return valid JSON only with both mandatory SPS and optional SPS that match the '{type}' . Remove HTML tags from content."}],max_tokens=2000, # Increased token limittemperature=0.1, # Lower temperature for more consistent responses)ai_description = response.choices[0].message.content.strip()ai_description = ai_description.replace('\n', ' ').replace('\r', '')clean_response = ai_descriptionif clean_response.startswith('```json'):clean_response = clean_response[7:]elif clean_response.startswith('```'):clean_response = clean_response[3:]if clean_response.endswith('```'):clean_response = clean_response[:-3]clean_response = clean_response.strip()try:import jsonresponse_data = json.loads(clean_response)mandatory_sps = []if 'mandatory_sps' in response_data and isinstance(response_data['mandatory_sps'], list):for item in response_data['mandatory_sps']:if isinstance(item, dict) and 'content' in item:item['content'] = remove_html_tags(item['content'])mandatory_sps.append(item)optional_sps = []if 'optional_sps' in response_data and isinstance(response_data['optional_sps'], list):for item in response_data['optional_sps']:if isinstance(item, dict) and 'content' in item:item['content'] = remove_html_tags(item['content'])optional_sps.append(item)if not mandatory_sps and not optional_sps:print("AI returned empty response")# return create_fallback_response(defect_type, flooring_type)return mandatory_sps, optional_spsexcept json.JSONDecodeError as json_error:try:json_start = clean_response.find('{')json_end = clean_response.rfind('}')if json_start != -1 and json_end != -1 and json_end > json_start:potential_json = clean_response[json_start:json_end+1]potential_json = potential_json.replace("'", '"') # Replace single quotespotential_json = re.sub(r',\s*}', '}', potential_json) # Remove trailing commaspotential_json = re.sub(r',\s*]', ']', potential_json) # Remove trailing commas in arraysresponse_data = json.loads(potential_json)mandatory_sps = []if 'mandatory_sps' in response_data and isinstance(response_data['mandatory_sps'], list):for item in response_data['mandatory_sps']:if isinstance(item, dict) and 'content' in item:item['content'] = remove_html_tags(item['content'])mandatory_sps.append(item)optional_sps = []if 'optional_sps' in response_data and isinstance(response_data['optional_sps'], list):for item in response_data['optional_sps']:if isinstance(item, dict) and 'content' in item:item['content'] = remove_html_tags(item['content'])optional_sps.append(item)return mandatory_sps, optional_spsexcept Exception as recovery_error:print(f"JSON recovery failed: {recovery_error}")print("Using fallback response due to JSON parsing failure")except Exception as e:print(f"Error in OpenAI API call: {str(e)}")def main():st.title("π Painting Defect Detection System")st.markdown("---")# Load the modelmodel = load_trained_model()if model is None:st.error("Failed to load the defect detection model. Please check if the model file exists.")return# Sidebar for input optionsst.sidebar.header("π Input Configuration")# Report detailsst.sidebar.subheader("Report Details")report_submission_id = st.sidebar.text_input("Report Submission Id", value=1)report_section_page_id = st.sidebar.text_input("Page ID", value=7)report_section_id = st.sidebar.text_input("Section ID", value=10)defect_type = st.sidebar.text_input("Defect Type", value="painting")type = st.sidebar.text_input("Type", value="NonWaterproofing")# Input method selectioninput_method = "Image URLs"# Main content areacol1, col2 = st.columns([2, 1])with col1:st.header("πΈ Image Input")st.subheader("Enter Image URLs")image_data_list = []classifications = []num_urls = st.number_input("Number of images", min_value=1, max_value=10, value=1)urls = []for i in range(num_urls):url = st.text_input(f"Image URL {i+1}", key=f"url_{i}")if url:urls.append(url)if st.button("Process URLs", type="primary"):for i, url in enumerate(urls):if url.strip():st.subheader(f"Image {i+1}")with st.spinner(f"Processing image {i+1}..."):processed_img, image_bytes = process_image(url)if processed_img is not None and image_bytes is not None:# Display the imageimage = Image.open(io.BytesIO(image_bytes))st.image(image, caption=f"Image {i+1}", width=300)classification, dpoc = predict_defect(processed_img, model)classifications.append(classification['type'] if 'type' in classification else 'Unknown')# Generate OpenAI descriptionopenai_desc = generate_openai_description(classification, image_bytes)image_data_list.append({"url": url,"defect_classification": classification,"individual_dopc": dpoc,"tags": [classification['type']] if 'type' in classification else ["Unknown"],"openai_desc": openai_desc,})# Display resultsif 'type' in classification:st.success(f"**Detected Defect:** {classification['type']}")st.info(f"**Confidence:** {classification['severity']:.2%}")else:st.error(f"**Error:** {classification.get('error', 'Unknown error')}")st.markdown("---")with col2:st.header("π Analysis Summary")if classifications:# Display metricsst.markdown('<div class="metric-card">', unsafe_allow_html=True)st.metric("Total Images", len(classifications))st.markdown('</div>', unsafe_allow_html=True)st.markdown('<div class="metric-card">', unsafe_allow_html=True)st.metric("Unique Defects", len(set(classifications)))st.markdown('</div>', unsafe_allow_html=True)# Defect distributionif len(classifications) > 1:st.subheader("Defect Distribution")defect_counts = pd.Series(classifications).value_counts()st.bar_chart(defect_counts)# Display class labelsst.subheader("π·οΈ Supported Defect Types")for label in class_labels:st.write(f"β’ {label}")# Generate comprehensive reportif image_data_list:st.markdown("---")st.header("π Detailed Analysis Report")# Generate SPS recommendationsif classifications:with st.spinner("Generating recommended solutions..."):user_message = ", ".join(set(classifications))mandatory_sps, optional_sps = get_sps(user_message,type)# Display detailed results for each imagefor i, image_data in enumerate(image_data_list):with st.expander(f"π· Image {i+1} - Detailed Analysis", expanded=True):col1, col2 = st.columns([1, 2])with col1:if 'file_name' in image_data:st.write(f"**File:** {image_data['file_name']}")if 'url' in image_data:st.write(f"**URL:** {image_data['url'][:50]}...")classification = image_data['defect_classification']if 'type' in classification:st.write(f"**Defect Type:** {classification['type']}")st.write(f"**Severity:** {classification['severity']:.2%}")with col2:st.write("**OpenAI Analysis:**")st.text_area("", image_data['openai_desc'], height=150, key=f"desc_{i}")# Display SPS recommendations# Export results# Export resultsai_response = {}boq_list = []ids = [sps["id"] for sps in optional_sps]sps_boq_map = {211:58,212:59,213:60,214:61,215:62,216:63,217:64,218:65,219:66,220:67,221:57,222:49}boq_list = list([sps_boq_map[sps_id] for sps_id in ids if sps_id in sps_boq_map])ai_response = {"report_id": report_submission_id,"page_id": report_section_page_id,"section_id": report_section_id,"defect_type": defect_type,"images": image_data_list,"mandatory_sps": mandatory_sps,"optional_sps": optional_sps,"boq_list": boq_list}# if st.button("π€ Export Results as JSON", type="secondary"):# ai_response = {# "report_id": report_submission_id,# "page_id": report_section_page_id,# "section_id": report_section_id,# "defect_type": defect_type,# "images": image_data_list,# "mandatory_sps": mandatory_sps,# "optional_sps": optional_sps# }st.subheader("π¦ JSON Response Preview")with st.expander("View JSON Response", expanded=False):st.json(json.loads(json.dumps(ai_response, indent=2, ensure_ascii=False)))# π₯ Download buttonst.download_button(label="Download JSON Report",data=json.dumps(ai_response, indent=2),file_name=f"defect_report_{report_submission_id}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",mime="application/json")if __name__ == "__main__":main()
app.py+823-275