Spaces:
Sleeping
Sleeping
File size: 6,658 Bytes
4031985 | 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 | import React, { useState, useRef } from 'react';
export default function App() {
const [image, setImage] = useState(null);
const [preview, setPreview] = useState(null);
const [isDragging, setIsDragging] = useState(false);
const [loading, setLoading] = useState(false);
const [results, setResults] = useState(null);
const [error, setError] = useState(null);
const fileInputRef = useRef(null);
const API_URL = import.meta.env.DEV ? 'http://localhost:8000/predict' : '/predict';
const handleDragOver = (e) => {
e.preventDefault();
setIsDragging(true);
};
const handleDragLeave = (e) => {
e.preventDefault();
setIsDragging(false);
};
const processFile = (file) => {
if (!file || !file.type.startsWith('image/')) {
setError('Please upload a valid image file.');
return;
}
setError(null);
setImage(file);
setPreview(URL.createObjectURL(file));
setResults(null);
};
const handleDrop = (e) => {
e.preventDefault();
setIsDragging(false);
processFile(e.dataTransfer.files[0]);
};
const handleRunInference = async () => {
if (!image) return;
setLoading(true);
setError(null);
const formData = new FormData();
formData.append('file', image);
try {
const response = await fetch(API_URL, {
method: 'POST',
body: formData,
});
if (!response.ok) throw new Error('Inference engine failed to process the image.');
const data = await response.json();
setResults(data);
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
};
return (
<div className="min-h-screen bg-[#09090b] text-zinc-100 font-sans flex flex-col items-center justify-center p-6">
<div className="max-w-4xl w-full mb-8">
<h1 className="text-3xl font-light tracking-tight text-white mb-1">
Apparel <span className="font-semibold text-blue-500">Taxonomy Engine</span>
</h1>
</div>
<div className="max-w-4xl w-full grid grid-cols-1 md:grid-cols-2 gap-6">
<div
className={`relative flex flex-col items-center justify-center border border-zinc-800 rounded-xl p-8 transition-all duration-300 min-h-[400px] ${
isDragging ? 'border-blue-500 bg-blue-500/5' : 'bg-[#121214] hover:border-zinc-700'
}`}
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
onDrop={handleDrop}
>
{preview ? (
<div className="relative w-full h-full flex flex-col items-center justify-between">
<div className="flex-1 flex items-center justify-center mb-6">
<img
src={preview}
alt="Upload preview"
className="max-h-[280px] object-contain rounded-md shadow-2xl"
/>
</div>
<button
onClick={handleRunInference}
disabled={loading}
className="w-full py-3 px-4 bg-zinc-100 text-zinc-900 font-medium rounded-lg hover:bg-white transition-colors disabled:opacity-50"
>
{loading ? 'Running Inference...' : 'Execute Classification'}
</button>
</div>
) : (
<div className="text-center w-full cursor-pointer" onClick={() => fileInputRef.current?.click()}>
<input
type="file"
accept="image/*"
className="hidden"
ref={fileInputRef}
onChange={(e) => processFile(e.target.files[0])}
/>
<p className="text-zinc-400 text-sm">Drag & drop or click to upload</p>
</div>
)}
</div>
<div className="flex flex-col bg-[#121214] border border-zinc-800 rounded-xl p-8 min-h-[400px]">
<h2 className="text-xs uppercase tracking-widest text-zinc-500 mb-6 border-b border-zinc-800 pb-4">
Telemetry & Output
</h2>
{error && (
<div className="bg-red-500/10 border border-red-500/20 text-red-400 p-4 rounded-md text-sm mb-4">
{error}
</div>
)}
{!results && !loading && !error && (
<div className="flex-1 flex items-center justify-center text-zinc-600 text-sm">
Awaiting payload...
</div>
)}
{loading && (
<div className="flex-1 flex flex-col items-center justify-center space-y-4">
<div className="w-5 h-5 border-2 border-zinc-700 border-t-blue-500 rounded-full animate-spin"></div>
</div>
)}
{results && !loading && (
<div className="flex-1 flex flex-col space-y-8 animate-in fade-in duration-500">
<div>
<p className="text-[10px] text-zinc-500 uppercase tracking-widest mb-3">Extracted Taxonomy</p>
<div className="flex flex-wrap items-center gap-3">
{results.tags.length > 0 ? results.tags.map((tag, index) => (
<React.Fragment key={tag}>
<span className="px-4 py-1.5 bg-blue-500/10 border border-blue-500/20 text-blue-400 rounded-md text-sm font-medium tracking-wide">
{tag}
</span>
{index < results.tags.length - 1 && (
<svg className="w-3 h-3 text-zinc-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M9 5l7 7-7 7"></path>
</svg>
)}
</React.Fragment>
)) : (
<span className="text-zinc-500 italic text-sm">No classes detected above threshold.</span>
)}
</div>
</div>
<div className="pt-6 border-t border-zinc-800">
<p className="text-[10px] text-zinc-500 uppercase tracking-widest mb-2">Status</p>
<p className="text-emerald-400 font-mono text-xs uppercase flex items-center gap-2">
<span className="w-1.5 h-1.5 rounded-full bg-emerald-400"></span> {results.status}
</p>
</div>
</div>
)}
</div>
</div>
</div>
);
} |