Spaces:
Runtime error
Runtime error
File size: 10,522 Bytes
10ac650 f84688d 10ac650 f84688d 10ac650 cbd23a5 10ac650 f84688d 10ac650 cbd23a5 10ac650 f84688d 10ac650 f84688d 10ac650 f84688d 10ac650 f84688d 10ac650 f84688d 10ac650 f84688d 10ac650 cbd23a5 10ac650 f84688d 10ac650 f84688d 10ac650 f84688d 10ac650 f84688d 10ac650 2be3111 10ac650 2be3111 10ac650 2be3111 f84688d 2be3111 10ac650 2be3111 10ac650 2be3111 10ac650 2be3111 10ac650 f84688d cbd23a5 f84688d 10ac650 2be3111 10ac650 f84688d | 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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | import { useState } from 'react';
import { Check, X, Send, MessageSquare, HelpCircle } from 'lucide-react';
import { FETAL_VIEW_LABELS, submitFeedback, FeedbackCreate, ClassificationResult } from '../lib/api';
interface FeedbackSectionProps {
sessionId: string;
filename: string;
fileType: 'dicom' | 'image';
predictions: ClassificationResult[];
topPrediction: ClassificationResult | null;
patientId?: string;
imageHash?: string;
preprocessedImageBase64?: string;
onFeedbackSubmitted?: () => void;
onViewCorrected?: (correctedLabel: string) => void;
disabled?: boolean;
}
export function FeedbackSection({
sessionId,
filename,
fileType,
predictions,
topPrediction,
patientId,
imageHash,
preprocessedImageBase64,
onFeedbackSubmitted,
onViewCorrected,
disabled = false,
}: FeedbackSectionProps) {
const [feedbackState, setFeedbackState] = useState<'none' | 'correct' | 'incorrect' | 'not_sure'>('none');
const [correctLabel, setCorrectLabel] = useState<string>('');
const [notes, setNotes] = useState<string>('');
const [isSubmitting, setIsSubmitting] = useState(false);
const [submitted, setSubmitted] = useState(false);
const handleFeedback = async (isCorrect: boolean | null) => {
if (disabled || !topPrediction) return;
if (isCorrect === true) {
// Submit immediately for correct predictions
setFeedbackState('correct');
await submitFeedbackData(true);
} else if (isCorrect === null) {
// Show notes form for uncertain predictions
setFeedbackState('not_sure');
} else {
// Show correction form for incorrect predictions
setFeedbackState('incorrect');
}
};
const submitFeedbackData = async (isCorrect: boolean | null, overrideLabel?: string, overrideNotes?: string) => {
if (!topPrediction || isSubmitting) return;
setIsSubmitting(true);
try {
const feedbackData: FeedbackCreate = {
session_id: sessionId,
filename,
file_type: fileType,
predicted_label: topPrediction.label,
predicted_confidence: topPrediction.confidence,
all_predictions: predictions.map(p => ({
label: p.label,
probability: p.confidence
})),
is_correct: isCorrect,
correct_label: overrideLabel || correctLabel || undefined,
reviewer_notes: overrideNotes || notes || undefined,
patient_id: patientId,
image_hash: imageHash,
preprocessed_image_base64: preprocessedImageBase64,
};
await submitFeedback(feedbackData);
setSubmitted(true);
onFeedbackSubmitted?.();
} catch (error) {
console.error('Failed to submit feedback:', error);
} finally {
setIsSubmitting(false);
}
};
const handleSubmitCorrection = async () => {
if (!correctLabel) return;
await submitFeedbackData(false, correctLabel, notes);
onViewCorrected?.(correctLabel);
};
const handleSubmitNotSure = async () => {
await submitFeedbackData(null, undefined, notes);
};
if (submitted) {
return (
<div className="bg-primary/10 border border-primary/30 rounded-lg p-3">
<div className="flex items-center gap-2 text-primary">
<Check className="w-4 h-4" />
<span className="text-sm font-medium">Feedback recorded</span>
{feedbackState === 'correct' ? (
<span className="text-xs text-text-muted ml-auto">Confirmed correct</span>
) : feedbackState === 'not_sure' ? (
<span className="text-xs text-text-muted ml-auto">Marked as uncertain</span>
) : (
<span className="text-xs text-text-muted ml-auto">Corrected to: {correctLabel}</span>
)}
</div>
</div>
);
}
if (disabled || !topPrediction) {
return (
<div className="bg-surface-secondary rounded-lg p-3 opacity-50">
<div className="flex items-center gap-2 text-text-muted">
<MessageSquare className="w-4 h-4" />
<span className="text-sm">Run classification to provide feedback</span>
</div>
</div>
);
}
return (
<div className="bg-surface-secondary rounded-lg p-3 space-y-3">
{/* Feedback header */}
<div className="flex items-center justify-between">
<div className="flex items-center gap-2 text-text-muted">
<MessageSquare className="w-4 h-4" />
<span className="text-sm font-medium">Is this prediction correct?</span>
</div>
{feedbackState === 'none' && (
<div className="flex gap-2">
<button
onClick={() => handleFeedback(true)}
className="flex items-center gap-1.5 px-3 py-1.5 bg-green-500/20 hover:bg-green-500/30 text-green-600 rounded-md text-sm font-medium transition-colors"
>
<Check className="w-3.5 h-3.5" />
Correct
</button>
<button
onClick={() => handleFeedback(null)}
className="flex items-center gap-1.5 px-3 py-1.5 bg-amber-500/20 hover:bg-amber-500/30 text-amber-600 rounded-md text-sm font-medium transition-colors"
>
<HelpCircle className="w-3.5 h-3.5" />
Not Sure
</button>
<button
onClick={() => handleFeedback(false)}
className="flex items-center gap-1.5 px-3 py-1.5 bg-red-500/20 hover:bg-red-500/30 text-red-600 rounded-md text-sm font-medium transition-colors"
>
<X className="w-3.5 h-3.5" />
Incorrect
</button>
</div>
)}
</div>
{/* Correction form */}
{feedbackState === 'incorrect' && (
<div className="space-y-3 pt-2 border-t border-border">
{/* Native select - handles viewport boundaries automatically */}
<div>
<label className="block text-xs font-medium text-gray-600 mb-1.5">Correct Label</label>
<select
value={correctLabel}
onChange={(e) => setCorrectLabel(e.target.value)}
className="w-full px-3 py-2.5 bg-white border border-gray-300 rounded-lg text-sm text-gray-800 focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary/50 transition-all cursor-pointer"
>
<option value="" disabled>Select correct view...</option>
{FETAL_VIEW_LABELS.map((label) => (
<option
key={label}
value={label}
disabled={label === topPrediction?.label}
>
{label}{label === topPrediction?.label ? ' (predicted)' : ''}
</option>
))}
</select>
</div>
{/* Notes input */}
<div>
<label className="block text-xs font-medium text-gray-600 mb-1.5">Notes (optional)</label>
<textarea
value={notes}
onChange={(e) => setNotes(e.target.value)}
placeholder="Any additional notes..."
className="w-full px-3 py-2 bg-white border border-gray-300 rounded-lg text-sm text-gray-800 resize-none h-14 focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary/50 transition-all"
/>
</div>
{/* Action buttons - inline */}
<div className="flex gap-2 items-center">
<button
onClick={() => {
setFeedbackState('none');
setCorrectLabel('');
setNotes('');
}}
className="px-4 py-2 text-sm text-gray-600 hover:text-gray-800 bg-gray-100 hover:bg-gray-200 rounded-lg transition-colors font-medium"
>
Cancel
</button>
<button
onClick={handleSubmitCorrection}
disabled={!correctLabel || isSubmitting}
className={`flex-1 flex items-center justify-center gap-2 px-4 py-2 rounded-lg text-sm font-medium transition-colors ${!correctLabel || isSubmitting
? 'bg-gray-200 text-gray-400 cursor-not-allowed'
: 'bg-green-600 hover:bg-green-700 text-white'
}`}
>
{isSubmitting ? (
<span>Submitting...</span>
) : (
<>
<Send className="w-3.5 h-3.5" />
<span>Submit Correction</span>
</>
)}
</button>
</div>
</div>
)}
{/* Not Sure form */}
{feedbackState === 'not_sure' && (
<div className="space-y-3 pt-2 border-t border-border">
<p className="text-xs text-amber-600">
You can add optional notes to explain your uncertainty.
</p>
{/* Notes input */}
<div>
<label className="block text-xs font-medium text-gray-600 mb-1.5">Notes (optional)</label>
<textarea
value={notes}
onChange={(e) => setNotes(e.target.value)}
placeholder="Why are you unsure about this prediction?"
className="w-full px-3 py-2 bg-white border border-gray-300 rounded-lg text-sm text-gray-800 resize-none h-14 focus:outline-none focus:ring-2 focus:ring-amber-500/20 focus:border-amber-500/50 transition-all"
/>
</div>
{/* Action buttons - inline */}
<div className="flex gap-2 items-center">
<button
onClick={() => {
setFeedbackState('none');
setNotes('');
}}
className="px-4 py-2 text-sm text-gray-600 hover:text-gray-800 bg-gray-100 hover:bg-gray-200 rounded-lg transition-colors font-medium"
>
Cancel
</button>
<button
onClick={handleSubmitNotSure}
disabled={isSubmitting}
className="flex-1 flex items-center justify-center gap-2 px-4 py-2 bg-amber-500 hover:bg-amber-600 text-white rounded-lg text-sm font-medium disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
>
{isSubmitting ? (
<span>Submitting...</span>
) : (
<>
<Send className="w-3.5 h-3.5" />
<span>Submit</span>
</>
)}
</button>
</div>
</div>
)}
</div>
);
}
|