Spaces:
Build error
Build error
Redesign demo with clean professional interface
Browse files- White background with #1975cf blue accents
- Removed all emojis for professional appearance
- Added model selection dropdown in top right
- Left side: upload section and 4 example image buttons
- Right side: Vela stats, model stats, and top 3 predictions
- Clean title without subtitle
- Improved layout and styling
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
app.py
CHANGED
|
@@ -7,12 +7,14 @@ import tensorflow as tf
|
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
|
| 9 |
# Download the TFLite model and labels from your Hugging Face repository
|
| 10 |
-
MODEL_REPO = "JahnaviBhansali/
|
| 11 |
-
MODEL_FILE = "mobilenet_v2_1.0_224_INT8.tflite"
|
|
|
|
| 12 |
LABELS_FILE = "labelmappings.txt"
|
| 13 |
|
| 14 |
print("Downloading model and labels from Hugging Face...")
|
| 15 |
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
|
|
|
| 16 |
labels_path = hf_hub_download(repo_id=MODEL_REPO, filename=LABELS_FILE)
|
| 17 |
|
| 18 |
# Load the TFLite model
|
|
@@ -29,6 +31,8 @@ with open(labels_path, 'r') as f:
|
|
| 29 |
|
| 30 |
print(f"Model loaded successfully! Input shape: {input_details[0]['shape']}")
|
| 31 |
print(f"Number of classes: {len(class_labels)}")
|
|
|
|
|
|
|
| 32 |
|
| 33 |
def preprocess_image(image):
|
| 34 |
"""
|
|
@@ -55,7 +59,7 @@ def preprocess_image(image):
|
|
| 55 |
|
| 56 |
def classify_image(image):
|
| 57 |
"""
|
| 58 |
-
Classify the input image and return top-
|
| 59 |
"""
|
| 60 |
if image is None:
|
| 61 |
return "Please upload an image."
|
|
@@ -93,23 +97,23 @@ def classify_image(image):
|
|
| 93 |
# Apply softmax to get probabilities
|
| 94 |
predictions = tf.nn.softmax(predictions).numpy()
|
| 95 |
|
| 96 |
-
# Get top-
|
| 97 |
-
|
| 98 |
|
| 99 |
# Format results
|
| 100 |
results = []
|
| 101 |
-
for i, idx in enumerate(
|
| 102 |
class_name = class_labels[idx] if idx < len(class_labels) else f"Class {idx}"
|
| 103 |
confidence = predictions[idx]
|
| 104 |
results.append(f"**{class_name}**: {confidence:.1%}")
|
| 105 |
|
| 106 |
# Create formatted output
|
| 107 |
-
result_text = "
|
| 108 |
|
| 109 |
return result_text
|
| 110 |
|
| 111 |
except Exception as e:
|
| 112 |
-
return f"
|
| 113 |
|
| 114 |
def load_example_image(example_path):
|
| 115 |
"""Load example images for demonstration."""
|
|
@@ -131,48 +135,75 @@ def load_example_image(example_path):
|
|
| 131 |
|
| 132 |
# Create Gradio interface
|
| 133 |
with gr.Blocks(
|
| 134 |
-
theme=gr.themes.
|
| 135 |
-
title="
|
| 136 |
css="""
|
| 137 |
.gradio-container {
|
| 138 |
-
max-width:
|
| 139 |
margin: auto !important;
|
|
|
|
| 140 |
}
|
| 141 |
.main-header {
|
| 142 |
text-align: center;
|
| 143 |
margin-bottom: 2rem;
|
| 144 |
-
|
| 145 |
-
-
|
| 146 |
-
-webkit-text-fill-color: transparent;
|
| 147 |
-
background-clip: text;
|
| 148 |
}
|
| 149 |
-
.
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
}
|
| 155 |
-
.
|
| 156 |
-
|
|
|
|
| 157 |
padding: 1rem;
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
}
|
| 161 |
"""
|
| 162 |
) as demo:
|
| 163 |
|
| 164 |
-
gr.
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
with gr.Row():
|
| 174 |
with gr.Column(scale=1):
|
| 175 |
-
gr.HTML("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
input_image = gr.Image(
|
| 178 |
label="Choose an image to classify",
|
|
@@ -181,65 +212,70 @@ with gr.Blocks(
|
|
| 181 |
)
|
| 182 |
|
| 183 |
classify_btn = gr.Button(
|
| 184 |
-
"
|
| 185 |
variant="primary",
|
| 186 |
size="lg"
|
| 187 |
)
|
| 188 |
|
| 189 |
gr.HTML("""
|
| 190 |
-
<div
|
| 191 |
-
<h3 style="margin: 0; color:
|
| 192 |
</div>
|
| 193 |
""")
|
| 194 |
|
| 195 |
with gr.Row():
|
| 196 |
example_cat = gr.Button("Cat", size="sm")
|
| 197 |
example_dog = gr.Button("Dog", size="sm")
|
| 198 |
-
example_car = gr.Button("Car", size="sm")
|
| 199 |
|
| 200 |
with gr.Row():
|
|
|
|
| 201 |
example_food = gr.Button("Food", size="sm")
|
| 202 |
-
example_nature = gr.Button("Nature", size="sm")
|
| 203 |
|
| 204 |
with gr.Column(scale=1):
|
| 205 |
-
gr.HTML("
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
-
gr.HTML(
|
| 213 |
-
<div
|
| 214 |
-
<
|
| 215 |
-
<div style="
|
| 216 |
-
<
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
</div>
|
| 221 |
-
<div style="background: #2a2a2a; padding: 1rem; border-radius: 6px;">
|
| 222 |
-
<strong style="color: #60a5fa;">⚡ Performance:</strong><br>
|
| 223 |
-
• INT8 Quantized<br>
|
| 224 |
-
• Ultra-fast inference
|
| 225 |
-
</div>
|
| 226 |
-
<div style="background: #2a2a2a; padding: 1rem; border-radius: 6px;">
|
| 227 |
-
<strong style="color: #f59e0b;">💾 Model Size:</strong><br>
|
| 228 |
-
• Size: ~4MB<br>
|
| 229 |
-
• Quantized weights
|
| 230 |
-
</div>
|
| 231 |
-
<div style="background: #2a2a2a; padding: 1rem; border-radius: 6px;">
|
| 232 |
-
<strong style="color: #ec4899;">🎯 Optimization:</strong><br>
|
| 233 |
-
• TensorFlow Lite<br>
|
| 234 |
-
• Mobile-ready
|
| 235 |
-
</div>
|
| 236 |
</div>
|
| 237 |
-
<div
|
| 238 |
-
<
|
| 239 |
-
|
|
|
|
| 240 |
</div>
|
| 241 |
</div>
|
| 242 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
# Set up event handlers
|
| 245 |
classify_btn.click(
|
|
@@ -253,7 +289,6 @@ with gr.Blocks(
|
|
| 253 |
example_dog.click(lambda: load_example_image("Dog"), outputs=input_image)
|
| 254 |
example_car.click(lambda: load_example_image("Car"), outputs=input_image)
|
| 255 |
example_food.click(lambda: load_example_image("Food"), outputs=input_image)
|
| 256 |
-
example_nature.click(lambda: load_example_image("Nature"), outputs=input_image)
|
| 257 |
|
| 258 |
# Auto-classify when image is uploaded
|
| 259 |
input_image.change(
|
|
|
|
| 7 |
from huggingface_hub import hf_hub_download
|
| 8 |
|
| 9 |
# Download the TFLite model and labels from your Hugging Face repository
|
| 10 |
+
MODEL_REPO = "JahnaviBhansali/mobilenet-v2-ethos-u55"
|
| 11 |
+
MODEL_FILE = "mobilenet_v2_1.0_224_INT8.tflite" # Using original INT8 model for Gradio compatibility
|
| 12 |
+
VELA_MODEL_FILE = "mobilenet_v2_1.0_224_INT8_vela.tflite" # Vela-optimized model for Ethos-U55
|
| 13 |
LABELS_FILE = "labelmappings.txt"
|
| 14 |
|
| 15 |
print("Downloading model and labels from Hugging Face...")
|
| 16 |
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
| 17 |
+
vela_model_path = hf_hub_download(repo_id=MODEL_REPO, filename=VELA_MODEL_FILE) # Download Vela model for reference
|
| 18 |
labels_path = hf_hub_download(repo_id=MODEL_REPO, filename=LABELS_FILE)
|
| 19 |
|
| 20 |
# Load the TFLite model
|
|
|
|
| 31 |
|
| 32 |
print(f"Model loaded successfully! Input shape: {input_details[0]['shape']}")
|
| 33 |
print(f"Number of classes: {len(class_labels)}")
|
| 34 |
+
print(f"Vela-optimized model also available: {VELA_MODEL_FILE}")
|
| 35 |
+
print(f"Repository: {MODEL_REPO}")
|
| 36 |
|
| 37 |
def preprocess_image(image):
|
| 38 |
"""
|
|
|
|
| 59 |
|
| 60 |
def classify_image(image):
|
| 61 |
"""
|
| 62 |
+
Classify the input image and return top-3 predictions with confidence scores.
|
| 63 |
"""
|
| 64 |
if image is None:
|
| 65 |
return "Please upload an image."
|
|
|
|
| 97 |
# Apply softmax to get probabilities
|
| 98 |
predictions = tf.nn.softmax(predictions).numpy()
|
| 99 |
|
| 100 |
+
# Get top-3 predictions
|
| 101 |
+
top3_indices = np.argsort(predictions)[-3:][::-1]
|
| 102 |
|
| 103 |
# Format results
|
| 104 |
results = []
|
| 105 |
+
for i, idx in enumerate(top3_indices):
|
| 106 |
class_name = class_labels[idx] if idx < len(class_labels) else f"Class {idx}"
|
| 107 |
confidence = predictions[idx]
|
| 108 |
results.append(f"**{class_name}**: {confidence:.1%}")
|
| 109 |
|
| 110 |
# Create formatted output
|
| 111 |
+
result_text = "\n".join(f"{i+1}. {result}" for i, result in enumerate(results))
|
| 112 |
|
| 113 |
return result_text
|
| 114 |
|
| 115 |
except Exception as e:
|
| 116 |
+
return f"Error processing image: {str(e)}"
|
| 117 |
|
| 118 |
def load_example_image(example_path):
|
| 119 |
"""Load example images for demonstration."""
|
|
|
|
| 135 |
|
| 136 |
# Create Gradio interface
|
| 137 |
with gr.Blocks(
|
| 138 |
+
theme=gr.themes.Default(),
|
| 139 |
+
title="MobileNetV2 Classification",
|
| 140 |
css="""
|
| 141 |
.gradio-container {
|
| 142 |
+
max-width: 1200px !important;
|
| 143 |
margin: auto !important;
|
| 144 |
+
background-color: white !important;
|
| 145 |
}
|
| 146 |
.main-header {
|
| 147 |
text-align: center;
|
| 148 |
margin-bottom: 2rem;
|
| 149 |
+
color: #1975cf !important;
|
| 150 |
+
font-weight: bold;
|
|
|
|
|
|
|
| 151 |
}
|
| 152 |
+
.stats-box {
|
| 153 |
+
background: #1975cf !important;
|
| 154 |
+
color: white !important;
|
| 155 |
+
padding: 1rem;
|
| 156 |
+
border-radius: 8px;
|
| 157 |
+
margin-bottom: 1rem;
|
| 158 |
+
}
|
| 159 |
+
.predictions-box {
|
| 160 |
+
background: #1975cf !important;
|
| 161 |
+
color: white !important;
|
| 162 |
+
padding: 1rem;
|
| 163 |
+
border-radius: 8px;
|
| 164 |
}
|
| 165 |
+
.upload-section {
|
| 166 |
+
background: #1975cf !important;
|
| 167 |
+
color: white !important;
|
| 168 |
padding: 1rem;
|
| 169 |
+
border-radius: 8px;
|
| 170 |
+
margin-bottom: 1rem;
|
| 171 |
+
}
|
| 172 |
+
.examples-section {
|
| 173 |
+
background: #1975cf !important;
|
| 174 |
+
color: white !important;
|
| 175 |
+
padding: 1rem;
|
| 176 |
+
border-radius: 8px;
|
| 177 |
+
}
|
| 178 |
+
body {
|
| 179 |
+
background-color: white !important;
|
| 180 |
+
color: black !important;
|
| 181 |
}
|
| 182 |
"""
|
| 183 |
) as demo:
|
| 184 |
|
| 185 |
+
with gr.Row():
|
| 186 |
+
with gr.Column(scale=3):
|
| 187 |
+
gr.HTML("""
|
| 188 |
+
<div class="main-header">
|
| 189 |
+
<h1>MobileNetV2 Classification</h1>
|
| 190 |
+
</div>
|
| 191 |
+
""")
|
| 192 |
+
with gr.Column(scale=1):
|
| 193 |
+
model_dropdown = gr.Dropdown(
|
| 194 |
+
choices=["INT8 Model", "Vela Optimized (FVP)"],
|
| 195 |
+
value="INT8 Model",
|
| 196 |
+
label="Model Selection",
|
| 197 |
+
interactive=True
|
| 198 |
+
)
|
| 199 |
|
| 200 |
with gr.Row():
|
| 201 |
with gr.Column(scale=1):
|
| 202 |
+
gr.HTML("""
|
| 203 |
+
<div class="upload-section">
|
| 204 |
+
<h3 style="margin: 0; color: white;">Upload Image</h3>
|
| 205 |
+
</div>
|
| 206 |
+
""")
|
| 207 |
|
| 208 |
input_image = gr.Image(
|
| 209 |
label="Choose an image to classify",
|
|
|
|
| 212 |
)
|
| 213 |
|
| 214 |
classify_btn = gr.Button(
|
| 215 |
+
"Classify Image",
|
| 216 |
variant="primary",
|
| 217 |
size="lg"
|
| 218 |
)
|
| 219 |
|
| 220 |
gr.HTML("""
|
| 221 |
+
<div class="examples-section">
|
| 222 |
+
<h3 style="margin: 0; color: white; margin-bottom: 1rem;">Example Images</h3>
|
| 223 |
</div>
|
| 224 |
""")
|
| 225 |
|
| 226 |
with gr.Row():
|
| 227 |
example_cat = gr.Button("Cat", size="sm")
|
| 228 |
example_dog = gr.Button("Dog", size="sm")
|
|
|
|
| 229 |
|
| 230 |
with gr.Row():
|
| 231 |
+
example_car = gr.Button("Car", size="sm")
|
| 232 |
example_food = gr.Button("Food", size="sm")
|
|
|
|
| 233 |
|
| 234 |
with gr.Column(scale=1):
|
| 235 |
+
gr.HTML("""
|
| 236 |
+
<div class="stats-box">
|
| 237 |
+
<h3 style="margin: 0; color: white; margin-bottom: 1rem;">Vela Stats</h3>
|
| 238 |
+
<div style="margin-bottom: 1rem;">
|
| 239 |
+
<strong>Performance:</strong><br>
|
| 240 |
+
• 6M cycles/inference<br>
|
| 241 |
+
• 15.14ms @ 400MHz<br>
|
| 242 |
+
• NPU Coverage: 100%
|
| 243 |
+
</div>
|
| 244 |
+
<div>
|
| 245 |
+
<strong>Memory Usage:</strong><br>
|
| 246 |
+
• SRAM: 353.5 KiB<br>
|
| 247 |
+
• Flash: 3.6 MiB
|
| 248 |
+
</div>
|
| 249 |
+
</div>
|
| 250 |
+
""")
|
| 251 |
|
| 252 |
+
gr.HTML("""
|
| 253 |
+
<div class="stats-box">
|
| 254 |
+
<h3 style="margin: 0; color: white; margin-bottom: 1rem;">Model Stats</h3>
|
| 255 |
+
<div style="margin-bottom: 1rem;">
|
| 256 |
+
<strong>Architecture:</strong><br>
|
| 257 |
+
• MobileNetV2<br>
|
| 258 |
+
• INT8 Quantized<br>
|
| 259 |
+
• Input: 224x224x3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
</div>
|
| 261 |
+
<div>
|
| 262 |
+
<strong>Accuracy:</strong><br>
|
| 263 |
+
• ImageNet Top-1: 69.7%<br>
|
| 264 |
+
• Classes: 1000
|
| 265 |
</div>
|
| 266 |
</div>
|
| 267 |
""")
|
| 268 |
+
|
| 269 |
+
gr.HTML("""
|
| 270 |
+
<div class="predictions-box">
|
| 271 |
+
<h3 style="margin: 0; color: white; margin-bottom: 1rem;">Top 3 Predictions</h3>
|
| 272 |
+
</div>
|
| 273 |
+
""")
|
| 274 |
+
|
| 275 |
+
output_text = gr.Markdown(
|
| 276 |
+
value="Upload an image to see predictions...",
|
| 277 |
+
label=""
|
| 278 |
+
)
|
| 279 |
|
| 280 |
# Set up event handlers
|
| 281 |
classify_btn.click(
|
|
|
|
| 289 |
example_dog.click(lambda: load_example_image("Dog"), outputs=input_image)
|
| 290 |
example_car.click(lambda: load_example_image("Car"), outputs=input_image)
|
| 291 |
example_food.click(lambda: load_example_image("Food"), outputs=input_image)
|
|
|
|
| 292 |
|
| 293 |
# Auto-classify when image is uploaded
|
| 294 |
input_image.change(
|