ThreadNode / frontend /src /app.jsx
abdurrehmansaeed's picture
Upload 11 files
4031985 verified
Raw History Blame Contribute Delete
6.66 kB
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>
);
}