- Add verification_model field to CodeBatch model (TextField) - Add verification_model field to ScanData model to track model used - Update QrVerifyView to pass model parameter to v5 QR verify endpoint - Create VerificationModel table to track available models - Add VerificationModelResource API endpoint (admin only) - Add verification-model Vue component with GenericManager - Add verification-model route and sidebar navigation entry - Add migrations for all database changes - Add analysis document for feature design
8.8 KiB
Analysis: Admin Configuration for Model Selection per Batch
Executive Summary
This document analyzes how to add a feature that allows admins to configure which AI model to use for each batch during QR verification. Currently, the system uses a default model for all batches when feature_comparison_threshold > 0.01.
Current State
1. Batch Model (CodeBatch)
- Location:
api/products/models.py:162 - Key field:
feature_comparison_threshold(FloatField, default=0.0)- When
> 0.01, triggers v5 QR verification - When
<= 0.01, skips v5 QR verification
- When
2. QR Verification Flow
- Location:
api/products/views.py:665-680 - Method:
QrVerifyView.do_v5_qr_verify() - Current behavior:
- Makes POST request to
https://themblem.com/api/v5/qr_verify - Sends image files only
- Does NOT specify a model parameter
- Server uses default model:
best_model_ep82_pos98.94_neg96.13_20250720_222102.pt
- Makes POST request to
3. V5 QR Verify Server
- Location:
emblem5/ai/server.py:83-114 - Endpoint:
/api/v5/qr_verify - Model selection:
- Accepts
modelparameter from form data:fd.get('model', default_model) - Default model defined in
emblem5/ai/common.py:42 - Models are downloaded from OSS bucket
emblem-modelsif not cached locally
- Accepts
4. Admin UI
- Location:
web/src/views/code-batch.vue - Current editable fields include
feature_comparison_threshold - Uses
GenericManagercomponent for CRUD operations
Requirements
- Database: Add a field to
CodeBatchto store the model identifier - API: Pass the model parameter to v5 QR verify endpoint when specified
- UI: Allow admins to configure the model per batch
- Backward Compatibility: Existing batches should continue using default model if not configured
Design Approach
Option 1: Simple Model Name Field (Recommended)
- Add
verification_modelfield toCodeBatch(CharField, nullable) - Store model filename (e.g.,
best_model_ep82_pos98.94_neg96.13_20250720_222102.pt) - Pass to v5 endpoint when
feature_comparison_threshold > 0.01and model is set - Pros: Simple, minimal changes, flexible
- Cons: No validation of model existence, manual entry required
Option 2: Model Reference with Validation
- Add
verification_modelfield + createVerificationModeltable - Store available models in database
- Validate model exists before allowing selection
- Pros: Better validation, can track model metadata
- Cons: More complex, requires model management UI
Option 3: Use GlobalConfig for Model List
- Store available models in
GlobalConfig - Add
verification_modelfield toCodeBatch - Validate against GlobalConfig list
- Pros: Reuses existing config system, simple validation
- Cons: Still requires manual model management
Recommendation: Option 1 for minimal changes, with future enhancement to Option 3 if needed.
Implementation Plan
Phase 1: Database Changes
-
Create Migration
- Add
verification_modelfield toCodeBatchmodel - Type:
CharField(max_length=255, null=True, blank=True) - Verbose name: "验证模型名称(可选,留空使用默认模型)"
- Default:
None(uses server default)
- Add
-
Update Model
# api/products/models.py class CodeBatch(models.Model): # ... existing fields ... verification_model = models.CharField( max_length=255, null=True, blank=True, verbose_name="验证模型名称(可选,留空使用默认模型)" )
Phase 2: API Changes
-
Update
do_v5_qr_verify()method- Location:
api/products/views.py:668 - Accept
model_nameparameter - Pass
modelin form data if provided - Fallback to default (current behavior) if not provided
- Location:
-
Update
QrVerifyView.post()method- Location:
api/products/views.py:722 - Get model from
sc.batch.verification_model - Pass to
do_v5_qr_verify()when calling
- Location:
-
Code Changes
def do_v5_qr_verify(self, imgs, messages, model_name=None): files = {} for i, img in enumerate(imgs): files[f'frame_{i}_{img[1]}'] = img[0] data = {} if model_name: data['model'] = model_name resp = requests.post( "https://themblem.com/api/v5/qr_verify", files=files, data=data, # Add model parameter if specified ) # ... rest of method ... # In post() method: if sc.batch.feature_comparison_threshold > 0.01: model_name = sc.batch.verification_model if hasattr(sc.batch, 'verification_model') else None self.do_v5_qr_verify(imgs, messages, model_name=model_name)
Phase 3: UI Changes
-
Update Vue Component
- Location:
web/src/views/code-batch.vue - Add
verification_modeltoeditable_fieldsarray - Add to
visible_fieldsarray (optional, for display)
- Location:
-
Optional: Model Selection Helper
- Could add a dropdown/autocomplete if model list is available
- For now, text input is sufficient (admins know model names)
Phase 4: Testing
-
Unit Tests
- Test
do_v5_qr_verify()with and without model parameter - Test backward compatibility (null model uses default)
- Test
-
Integration Tests
- Test QR verification with custom model
- Test QR verification without model (default behavior)
- Test with
feature_comparison_threshold = 0(should skip)
-
Manual Testing
- Create batch with custom model
- Verify model is passed to v5 endpoint
- Verify default model used when field is empty
Database Schema Change
# Migration file: api/products/migrations/XXXX_add_verification_model_to_codebatch.py
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('products', 'XXXX_previous_migration'),
]
operations = [
migrations.AddField(
model_name='codebatch',
name='verification_model',
field=models.CharField(
max_length=255,
blank=True,
null=True,
verbose_name='验证模型名称(可选,留空使用默认模型)'
),
),
]
API Changes Summary
Modified Endpoints
- None (internal method change only)
New Behavior
QrVerifyView.do_v5_qr_verify()now accepts optionalmodel_nameparameter- When
CodeBatch.verification_modelis set, it's passed to v5 endpoint - When
CodeBatch.verification_modelis null/empty, server uses default model
UI Changes Summary
Modified Views
web/src/views/code-batch.vue- Add
verification_modelto editable fields - Optional: Add to visible fields for display
- Add
User Experience
- Admin can edit batch and set
verification_modelfield - Field is optional - leaving empty uses default model
- Text input allows entering model filename directly
Backward Compatibility
✅ Fully Compatible
- Existing batches have
verification_model = None - When
None, v5 endpoint uses default model (current behavior) - No breaking changes to existing functionality
Future Enhancements
-
Model Management UI
- List available models from OSS bucket
- Show model metadata (accuracy, date, etc.)
- Auto-complete model selection
-
Model Validation
- Check if model exists before saving
- Show warning if model not found
-
Model Versioning
- Track model versions per batch
- Allow rollback to previous models
-
A/B Testing
- Support multiple models per batch
- Compare model performance
Files to Modify
api/products/models.py- Addverification_modelfieldapi/products/views.py- Updatedo_v5_qr_verify()andQrVerifyView.post()web/src/views/code-batch.vue- Add field to editable fields- Create migration file in
api/products/migrations/
Estimated Effort
- Database Migration: 15 minutes
- Backend Changes: 30 minutes
- Frontend Changes: 15 minutes
- Testing: 1 hour
- Total: ~2 hours
Risks and Considerations
-
Model Availability: No validation that model exists in OSS bucket
- Mitigation: Server will return error if model not found, which is acceptable
-
Model Naming: Admins must know exact model filename
- Mitigation: Document model names, or add helper UI later
-
Default Model Changes: If default model changes in server, batches without explicit model will use new default
- Mitigation: This is expected behavior, but could be documented
-
Performance: No performance impact expected
- Model download is cached on server side
Conclusion
This feature can be implemented with minimal changes following Option 1 (Simple Model Name Field). The implementation is backward compatible and allows admins to configure models per batch while maintaining default behavior for existing batches.