From c2f75169e8c9d5942bce48f6bfcbb742b3cd648a Mon Sep 17 00:00:00 2001 From: Shradha Saxena <103318017+ShradhaSaxena23@users.noreply.github.com> Date: Wed, 9 Oct 2024 16:42:46 +0530 Subject: [PATCH] Update AIEngine.jsx for Image Preview Added the feature of image preview in AIEngine.jsx --- frontend/src/components/AIEngine.jsx | 152 +++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) diff --git a/frontend/src/components/AIEngine.jsx b/frontend/src/components/AIEngine.jsx index ee116e15..0382b9df 100644 --- a/frontend/src/components/AIEngine.jsx +++ b/frontend/src/components/AIEngine.jsx @@ -1,6 +1,157 @@ import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; +const AIEngine = () => { + const [selectedFile, setSelectedFile] = useState(null); + const [fileName, setFileName] = useState('No file chosen'); + const [previewUrl, setPreviewUrl] = useState(null); // To store the image preview URL + const [isLoading, setIsLoading] = useState(false); + const navigate = useNavigate(); + + const handleFileChange = (event) => { + const file = event.target.files[0]; + setSelectedFile(file); + setFileName(file ? file.name : 'No file chosen'); + + // Set image preview + if (file) { + const imageUrl = URL.createObjectURL(file); + setPreviewUrl(imageUrl); + } else { + setPreviewUrl(null); + } + }; + + const handleSubmit = async (event) => { + event.preventDefault(); + if (!selectedFile) { + alert('Please select a file'); + return; + } + + setIsLoading(true); + const formData = new FormData(); + formData.append('image', selectedFile); + + try { + const response = await fetch('https://disease-prediction-api-2.onrender.com/submit', { + method: 'POST', + body: formData, + }); + + if (response.ok) { + const result = await response.json(); + navigate('/submit', { state: { result } }); + } else { + throw new Error('File upload failed'); + } + } catch (error) { + console.error('Error:', error); + alert('An error occurred while uploading the file'); + } finally { + setIsLoading(false); + } + }; + + return ( +
Let AI Engine Help You To Detect Disease
++ Plant diseases affect the growth of their respective species... +
+