Spaces:
Sleeping
Sleeping
File size: 6,188 Bytes
3e369f2 bf5da6b 3e369f2 bf5da6b 3e369f2 bf5da6b 3e369f2 bf5da6b 3e369f2 bf5da6b 3e369f2 bf5da6b 3e369f2 bf5da6b 3e369f2 bf5da6b |
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 |
import { useState, useEffect } from "react";
import { DownloadIcon, InfoIcon, Loader2Icon } from "lucide-react";
interface ResultsPanelProps {
uploadedImage: string | null;
result?: any;
loading?: boolean;
}
export function ResultsPanel({ uploadedImage, result, loading }: ResultsPanelProps) {
const [isModalOpen, setIsModalOpen] = useState(false);
const [modalImage, setModalImage] = useState<string | null>(null);
useEffect(() => {
if (result?.annotated_image_url) {
setModalImage(result.annotated_image_url);
} else if (uploadedImage) {
setModalImage(uploadedImage);
} else {
setModalImage(null);
}
}, [result, uploadedImage]);
// Loading state UI
if (loading) {
return (
<div className="bg-white rounded-lg shadow-sm p-8 flex flex-col items-center justify-center">
<Loader2Icon className="w-12 h-12 text-transparent bg-clip-text bg-gradient-to-r from-blue-800 to-teal-600 animate-spin mb-4" />
<p className="text-gray-700 font-medium mb-2">Analyzing image... Please wait</p>
<div className="w-56 h-2 rounded-full overflow-hidden mt-2">
<div className="h-full bg-gradient-to-r from-blue-800 to-teal-600 animate-pulse" />
</div>
</div>
);
}
if (!result) {
return (
<div className="bg-white rounded-lg shadow-sm p-6 text-center text-gray-500">
No analysis result available yet.
</div>
);
}
const {
prediction,
confidence,
probabilities,
detections,
summary,
annotated_image_url,
model_name,
analysis_type,
} = result;
const handleDownload = () => {
if (annotated_image_url) {
const link = document.createElement("a");
link.href = annotated_image_url;
link.download = "analysis_result.jpg";
link.click();
}
};
const openModal = (url?: string) => {
setModalImage(url || annotated_image_url || uploadedImage);
setIsModalOpen(true);
};
const closeModal = () => {
setIsModalOpen(false);
};
return (
<div className="bg-white rounded-lg shadow-sm p-6">
{/* Header */}
<div className="flex items-center justify-between mb-6">
<div>
<h2 className="text-2xl font-bold text-gray-800">
{model_name ? model_name.toUpperCase() : "Analysis Result"}
</h2>
<p className="text-sm text-gray-500 capitalize">{analysis_type || "Test Type"}</p>
</div>
{annotated_image_url && (
<button
onClick={handleDownload}
className="flex items-center gap-2 bg-gradient-to-r from-blue-800 to-teal-600 text-white px-4 py-2 rounded-lg hover:opacity-90 transition-all"
>
<DownloadIcon className="w-4 h-4" />
Download Image
</button>
)}
</div>
{/* Image (click to zoom) */}
<div className="relative mb-6 rounded-lg overflow-hidden border border-gray-200">
<img
src={annotated_image_url || uploadedImage || "/ui.jpg"}
alt="Analysis Result"
className="w-full h-64 object-cover cursor-pointer transition-transform hover:scale-105"
onClick={() => openModal()}
/>
</div>
{/* Results Summary */}
<div className="mb-6">
{prediction && (
<h3
className={`text-3xl font-bold ${
prediction.toLowerCase().includes("malignant") || prediction.toLowerCase().includes("abnormal")
? "text-red-600"
: "text-green-600"
}`}
>
{prediction}
</h3>
)}
{confidence && (
<div className="mt-2">
<div className="flex items-center justify-between mb-1">
<span className="font-semibold text-gray-900">Confidence: {(confidence * 100).toFixed(2)}%</span>
<InfoIcon className="w-4 h-4 text-gray-400" />
</div>
<div className="w-full h-3 bg-gray-200 rounded-full overflow-hidden">
<div
className={`h-full ${
confidence > 0.7 ? "bg-green-500" : confidence > 0.4 ? "bg-yellow-500" : "bg-red-500"
}`}
style={{ width: `${confidence * 100}%` }}
/>
</div>
</div>
)}
{summary && (
<p className="mt-4 text-gray-700 text-sm leading-relaxed">{summary}</p>
)}
</div>
{/* Detections / Probabilities */}
{detections && detections.length > 0 && (
<div className="mb-6">
<h4 className="font-semibold text-gray-900 mb-3">Detected Regions:</h4>
<ul className="text-sm text-gray-700 list-disc list-inside space-y-1">
{detections.map((det: any, i: number) => (
<li key={i}>{det.name || "object"} – {(det.confidence * 100).toFixed(1)}%</li>
))}
</ul>
</div>
)}
{probabilities && (
<div className="mb-6">
<h4 className="font-semibold text-gray-900 mb-3">Class Probabilities:</h4>
<pre className="bg-gray-100 rounded-lg p-3 text-sm">{JSON.stringify(probabilities, null, 2)}</pre>
</div>
)}
{/* Report Button */}
<button className="w-full bg-gradient-to-r from-blue-800 to-teal-600 text-white py-3 rounded-lg font-medium hover:opacity-95 transition-colors flex items-center justify-center gap-2">
<DownloadIcon className="w-5 h-5" />
Generate Report
</button>
{/* Modal for zoom */}
{isModalOpen && modalImage && (
<div
className="fixed inset-0 bg-black/70 z-50 flex items-center justify-center p-4"
onClick={closeModal}
>
<div className="max-w-5xl max-h-[90vh] w-full" onClick={(e) => e.stopPropagation()}>
<img src={modalImage} alt="Zoomed result" className="w-full h-auto object-contain rounded-lg shadow-2xl" />
<div className="mt-3 text-right">
<button onClick={closeModal} className="px-4 py-2 bg-white rounded-md">Close</button>
</div>
</div>
</div>
)}
</div>
);
} |