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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Netra AI - Public Interface
|
| 3 |
+
Calls protected backend space for classification
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from gradio_client import Client
|
| 8 |
+
from huggingface_hub import login
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# Authenticate and connect to protected backend
|
| 12 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 13 |
+
if HF_TOKEN:
|
| 14 |
+
login(token=HF_TOKEN)
|
| 15 |
+
|
| 16 |
+
backend = Client("rg321/Construction_Material_Classification")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def classify_material(image):
|
| 20 |
+
"""Proxy function that calls protected backend"""
|
| 21 |
+
if image is None:
|
| 22 |
+
return "Please upload an image", "", ""
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
# Call protected space API
|
| 26 |
+
result = backend.predict(image=image, api_name="/classify_material")
|
| 27 |
+
|
| 28 |
+
# result is tuple: (classification, confidence, reasoning)
|
| 29 |
+
return result[0], result[1], result[2]
|
| 30 |
+
|
| 31 |
+
except Exception as e:
|
| 32 |
+
error_msg = str(e)
|
| 33 |
+
return f"β Classification Error", "", f"Please try again or contact support. Error: {error_msg[:100]}"
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# Custom CSS (same as original)
|
| 37 |
+
custom_css = """
|
| 38 |
+
#header {
|
| 39 |
+
text-align: center;
|
| 40 |
+
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
|
| 41 |
+
padding: 20px;
|
| 42 |
+
border-radius: 10px;
|
| 43 |
+
color: white;
|
| 44 |
+
}
|
| 45 |
+
#result {
|
| 46 |
+
font-size: 72px;
|
| 47 |
+
font-weight: 900;
|
| 48 |
+
text-align: center;
|
| 49 |
+
padding: 50px 30px;
|
| 50 |
+
border-radius: 20px;
|
| 51 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 52 |
+
color: white;
|
| 53 |
+
margin-bottom: 25px;
|
| 54 |
+
box-shadow: 0 8px 16px rgba(0,0,0,0.2);
|
| 55 |
+
text-transform: uppercase;
|
| 56 |
+
letter-spacing: 2px;
|
| 57 |
+
line-height: 1.2;
|
| 58 |
+
}
|
| 59 |
+
#confidence {
|
| 60 |
+
font-size: 24px;
|
| 61 |
+
font-weight: 600;
|
| 62 |
+
text-align: center;
|
| 63 |
+
padding: 15px;
|
| 64 |
+
background: #f0f9ff;
|
| 65 |
+
border-radius: 10px;
|
| 66 |
+
margin-bottom: 20px;
|
| 67 |
+
}
|
| 68 |
+
#reasoning {
|
| 69 |
+
font-size: 14px;
|
| 70 |
+
text-align: left;
|
| 71 |
+
padding: 15px;
|
| 72 |
+
background: #f9fafb;
|
| 73 |
+
border-radius: 8px;
|
| 74 |
+
margin-bottom: 20px;
|
| 75 |
+
color: #4b5563;
|
| 76 |
+
}
|
| 77 |
+
#about {
|
| 78 |
+
font-size: 13px;
|
| 79 |
+
color: #6b7280;
|
| 80 |
+
padding-top: 20px;
|
| 81 |
+
border-top: 1px solid #e5e7eb;
|
| 82 |
+
}
|
| 83 |
+
"""
|
| 84 |
+
|
| 85 |
+
# Build Gradio interface
|
| 86 |
+
with gr.Blocks(css=custom_css, title="Netra AI - Material Classifier") as demo:
|
| 87 |
+
|
| 88 |
+
gr.Markdown(
|
| 89 |
+
"""
|
| 90 |
+
<div id="header">
|
| 91 |
+
<h1>ποΈ Netra AI - Construction Material Classifier</h1>
|
| 92 |
+
<p>AI-powered material identification for construction sites</p>
|
| 93 |
+
</div>
|
| 94 |
+
""",
|
| 95 |
+
elem_id="header"
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
with gr.Row():
|
| 99 |
+
with gr.Column(scale=1):
|
| 100 |
+
gr.Markdown("### πΈ Upload Material Image")
|
| 101 |
+
image_input = gr.Image(
|
| 102 |
+
type="pil",
|
| 103 |
+
label="Construction Material Photo",
|
| 104 |
+
height=400
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
classify_btn = gr.Button("π Classify Material", variant="primary", size="lg")
|
| 108 |
+
|
| 109 |
+
gr.Markdown(
|
| 110 |
+
"""
|
| 111 |
+
**Supported Materials:**
|
| 112 |
+
- ποΈ Reet (Sand)
|
| 113 |
+
- π· 12mm VSI (Uniform Aggregate)
|
| 114 |
+
- πͺ¨ Stone (Large Boulders)
|
| 115 |
+
- ποΈ GSB (Mixed Graded Base)
|
| 116 |
+
"""
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
with gr.Column(scale=1):
|
| 120 |
+
result_output = gr.Markdown("", elem_id="result")
|
| 121 |
+
confidence_output = gr.Markdown("", elem_id="confidence")
|
| 122 |
+
reasoning_output = gr.Markdown("", elem_id="reasoning")
|
| 123 |
+
|
| 124 |
+
gr.Markdown(
|
| 125 |
+
"""
|
| 126 |
+
<div id="about">
|
| 127 |
+
|
| 128 |
+
### π About This Demo
|
| 129 |
+
|
| 130 |
+
This is a proof-of-concept for **Netra AI's** automated material classification system.
|
| 131 |
+
|
| 132 |
+
**Technology:**
|
| 133 |
+
- Vision AI for real-time material identification
|
| 134 |
+
- Prevents grade fraud at construction sites
|
| 135 |
+
- Automates dispatch logging
|
| 136 |
+
|
| 137 |
+
**Use Cases:**
|
| 138 |
+
- Gate monitoring at crusher plants
|
| 139 |
+
- Dispatch verification
|
| 140 |
+
- Quality control
|
| 141 |
+
|
| 142 |
+
---
|
| 143 |
+
**Netra AI** - *Making every tonne count.*
|
| 144 |
+
|
| 145 |
+
For commercial inquiries: [Contact Us](mailto:guptaraghu321@gmail.com)
|
| 146 |
+
|
| 147 |
+
</div>
|
| 148 |
+
"""
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
# Connect button
|
| 153 |
+
classify_btn.click(
|
| 154 |
+
fn=classify_material,
|
| 155 |
+
inputs=[image_input],
|
| 156 |
+
outputs=[result_output, confidence_output, reasoning_output]
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
# Launch
|
| 160 |
+
if __name__ == "__main__":
|
| 161 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==5.9.1
|
| 2 |
+
gradio_client
|
| 3 |
+
huggingface_hub
|