|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import Image from 'next/image'; |
| 4 | +import React, { useCallback, useState } from 'react'; |
| 5 | +import { useDropzone } from 'react-dropzone'; |
| 6 | + |
| 7 | +const Home = () => { |
| 8 | + const [filePreview, setFilePreview] = useState(""); |
| 9 | + const [base64Image, setBase64Image] = useState(""); |
| 10 | + const [similarImages, setSimilarImages] = useState([] as any[]); |
| 11 | + const [loading, setLoading] = useState(false) |
| 12 | + |
| 13 | + const onDrop = useCallback(async (acceptedFiles: File[]) => { |
| 14 | + const file = acceptedFiles[0] |
| 15 | + const previewUrl = URL.createObjectURL(file) |
| 16 | + setFilePreview(previewUrl) |
| 17 | + const arrayBuffer = await file.arrayBuffer(); |
| 18 | + setBase64Image(Buffer.from(arrayBuffer).toString('base64')) |
| 19 | + }, []); |
| 20 | + |
| 21 | + const { getRootProps, getInputProps, isDragActive } = useDropzone({ |
| 22 | + onDrop, |
| 23 | + multiple: false, |
| 24 | + }); |
| 25 | + |
| 26 | + return ( |
| 27 | + <main className='bg-gray-950 min-h-screen flex flex-col items-center justify-center'> |
| 28 | + <div className='max-w-screen-xl m-8 xl:m-auto flex flex-col items-center justify-center'> |
| 29 | + <h2 className='text-lg sm:text-xl md:text-2xl xl:text-4xl font-semibold text-white text-center mt-8'><span className='bg-gradient-to-br from-green-600 to-green-300 bg-clip-text text-transparent p-1'>Search</span> for similar images 🚀</h2> |
| 30 | + <p className='text-gray-300 text-xs md:text-sm max-w-[300px] text-center mt-0 xl:mt-2 2xl:mt-3'>Upload an image of Lilly, Lotus, Orchid, Sunflower, or Tulip and find images similar to it.</p> |
| 31 | + |
| 32 | + <div className='text-white border border-white/30 rounded-lg cursor-pointer mt-8' {...getRootProps()}> |
| 33 | + <input {...getInputProps()} /> |
| 34 | + { |
| 35 | + isDragActive ? |
| 36 | + <p className='my-10 mx-5 text-center'>Drop the file here ...</p> : |
| 37 | + filePreview ? |
| 38 | + <div> |
| 39 | + <Image width={512} height={512} src={filePreview} alt="Preview" className='h-[200px] w-[200px] md:h-[300px] md:w-auto m-1 md:m-2 xl:m-4 rounded-lg' /> |
| 40 | + </div> : |
| 41 | + <p className='my-10 mx-5 text-center text-xs lg:text-lg'>Drag 'n' drop a file here, or click to select a file</p> |
| 42 | + } |
| 43 | + </div> |
| 44 | + <button |
| 45 | + onClick={async () => { |
| 46 | + setLoading(true) |
| 47 | + const res = await fetch('/api/predict', { |
| 48 | + method: "POST", |
| 49 | + body: JSON.stringify({ |
| 50 | + base64Image: base64Image |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + const data = await res.json() |
| 55 | + setSimilarImages(data.result) |
| 56 | + setLoading(false) |
| 57 | + }} |
| 58 | + disabled={loading} |
| 59 | + className='bg-gradient-to-br disabled:opacity-70 from-green-600 to-green-300 border border-green-600 rounded-lg w-full md:w-2/4 xl:w-4/12 my-4 text-white font-semibold text-sm xl:text-lg py-2 hover:opacity-90 transition duration-200' |
| 60 | + >Search |
| 61 | + </button> |
| 62 | + <div className='grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4 mb-8'> |
| 63 | + {similarImages.map((image, index) => ( |
| 64 | + <div key={index} className='flex flex-col items-center border border-white/30 rounded-lg pb-2'> |
| 65 | + <Image width={512} height={512} src={image.image} alt={image.name} className='h-[200px] w-[200px] md:h-[250px] md:w-auto m-1 md:m-2 xl:m-4 rounded-lg' /> |
| 66 | + <p className='text-white font-semibold text-lg xl:-mt-2'>{image.type}</p> |
| 67 | + </div> |
| 68 | + ))} |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + </main> |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +export default Home; |
0 commit comments