File size: 5,685 Bytes
af12c9d 8773f23 af12c9d 8773f23 af12c9d 3e29b7e 732911b 3e29b7e 1d4ebe5 3e29b7e 732911b 3e29b7e 732911b 8773f23 732911b af12c9d 1d4ebe5 af12c9d 1d4ebe5 af12c9d 1d4ebe5 af12c9d 4b3c68c af12c9d 1d4ebe5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | """
Netra AI - Public Interface
Calls protected backend space for classification
"""
import gradio as gr
from gradio_client import Client
from huggingface_hub import login
import os
# Global backend client
backend = None
def get_backend():
"""Lazy initialization of backend client"""
global backend
if backend is None:
HF_TOKEN = os.getenv("HF_TOKEN")
if HF_TOKEN:
login(token=HF_TOKEN)
backend = Client("rg321/Construction_Material_Classification")
return backend
def classify_material(image):
"""Proxy function that calls protected backend"""
if image is None:
return "Please upload an image", "", ""
try:
import tempfile
from PIL import Image as PILImage
from gradio_client import handle_file
print(f"π€ Calling backend API with image type: {type(image)}")
# Convert PIL Image to temp file for gradio_client
if isinstance(image, PILImage.Image):
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg', mode='wb') as tmp:
image.save(tmp, 'JPEG')
image_path = tmp.name
else:
image_path = image
print(f"π€ Sending file: {image_path}")
# Call protected space API with properly formatted file
client = get_backend()
result = client.predict(
image=handle_file(image_path),
api_name="/classify_material"
)
print(f"π₯ Backend response: {result}")
# result is tuple: (classification, confidence, reasoning)
return result[0], result[1], result[2]
except Exception as e:
import traceback
error_msg = str(e)
full_error = traceback.format_exc()
print(f"β Error: {full_error}")
return f"β Classification Error", "", f"Error: {error_msg}"
# Custom CSS (same as original)
custom_css = """
#header {
text-align: center;
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
padding: 20px;
border-radius: 10px;
color: white;
}
#result {
font-size: 72px;
font-weight: 900;
text-align: center;
padding: 50px 30px;
border-radius: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
margin-bottom: 25px;
box-shadow: 0 8px 16px rgba(0,0,0,0.2);
text-transform: uppercase;
letter-spacing: 2px;
line-height: 1.2;
}
#confidence {
font-size: 24px;
font-weight: 600;
text-align: center;
padding: 15px;
background: #f0f9ff;
border-radius: 10px;
margin-bottom: 20px;
}
#reasoning {
font-size: 14px;
text-align: left;
padding: 15px;
background: #f9fafb;
border-radius: 8px;
margin-bottom: 20px;
color: #4b5563;
}
#about {
font-size: 13px;
color: #6b7280;
padding-top: 20px;
border-top: 1px solid #e5e7eb;
}
"""
# Build Gradio interface
with gr.Blocks(css=custom_css, title="Netra AI - Material Classifier") as demo:
gr.Markdown(
"""
<div id="header">
<h1>ποΈ Netra AI - Construction Material Classifier</h1>
<p>AI-powered material identification for construction sites</p>
</div>
""",
elem_id="header"
)
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### πΈ Upload Material Image")
image_input = gr.Image(
type="pil",
label="Construction Material Photo",
height=400
)
classify_btn = gr.Button("π Classify Material", variant="primary", size="lg")
gr.Markdown(
"""
**Supported Materials:**
- ποΈ Reet (Sand)
- π· 12mm VSI (Uniform Aggregate)
- πͺ¨ Stone (Large Boulders)
- ποΈ GSB (Mixed Graded Base)
"""
)
gr.Markdown("### π· Try Sample Images")
gr.Examples(
examples=[
["sanity_test_dataset/12mm_VSI_01.jpeg"],
["sanity_test_dataset/Reet_01_fine.jpeg"],
["sanity_test_dataset/Stone_01.jpeg"],
["sanity_test_dataset/GSB_01.jpeg"],
],
inputs=image_input,
label=""
)
with gr.Column(scale=1):
result_output = gr.Markdown("", elem_id="result")
confidence_output = gr.Markdown("", elem_id="confidence")
reasoning_output = gr.Markdown("", elem_id="reasoning")
gr.Markdown(
"""
<div id="about">
### π About This Demo
This is a proof-of-concept for **Netra AI's** automated material classification system.
**Technology:**
- Vision AI for real-time material identification
- Prevents grade fraud at construction sites
- Automates dispatch logging
**Use Cases:**
- Gate monitoring at crusher plants
- Dispatch verification
- Quality control
---
**Netra AI** - *Making every tonne count.*
For commercial inquiries: [Contact Us](mailto:guptaraghu321@gmail.com)
</div>
"""
)
# Connect button
classify_btn.click(
fn=classify_material,
inputs=[image_input],
outputs=[result_output, confidence_output, reasoning_output]
)
# Launch
if __name__ == "__main__":
demo.launch(show_error=True)
|