|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import Spinner from "../Spinner.jsx"; |
| 3 | +import img2 from "../../assets/images/mushroom_bg.jpg"; |
| 4 | + |
| 5 | +const MushroomEdibility = () => { |
| 6 | + useEffect(() => { |
| 7 | + const timer = setTimeout(() => { |
| 8 | + setLoading(false); |
| 9 | + }, 1000); |
| 10 | + return () => clearTimeout(timer); |
| 11 | + }, []); |
| 12 | + |
| 13 | + const toPascalCase = str => (str.match(/[a-zA-Z0-9]+/g) || []).map(w => `${w.charAt(0).toUpperCase()}${w.slice(1)}`).join(' '); |
| 14 | + const [loading, setLoading] = useState(true); |
| 15 | + const [formData, setFormData] = useState({ |
| 16 | + 'cap-shape': "", |
| 17 | + 'cap-surface': "", |
| 18 | + 'cap-color': "", |
| 19 | + 'bruises': "", |
| 20 | + 'odor': "", |
| 21 | + 'gill-attachment': "", |
| 22 | + 'gill-spacing': "", |
| 23 | + 'gill-size': "", |
| 24 | + 'gill-color': "", |
| 25 | + 'stalk-shape': "", |
| 26 | + 'stalk-root': "", |
| 27 | + 'stalk-surface-above-ring': "", |
| 28 | + 'stalk-surface-below-ring': "", |
| 29 | + 'stalk-color-above-ring': "", |
| 30 | + 'stalk-color-below-ring': "", |
| 31 | + 'veil-type': "", |
| 32 | + 'veil-color': "", |
| 33 | + 'ring-number': "", |
| 34 | + 'ring-type': "", |
| 35 | + 'spore-print-color': "", |
| 36 | + 'population': "", |
| 37 | + 'habitat': "" |
| 38 | + }); |
| 39 | + |
| 40 | + |
| 41 | + const options = { |
| 42 | + "cap_shape": ["bell", "conical", "convex", "flat", "knobbed", "sunken"], |
| 43 | + "cap_surface": ["fibrous", "grooves", "scaly", "smooth"], |
| 44 | + "cap_color": ["brown", "buff", "cinnamon", "gray", "green", "pink", "purple", "red", "white", "yellow"], |
| 45 | + "bruises": ["bruises", "no bruises"], |
| 46 | + "odor": ["almond", "anise", "creosote", "fishy", "foul", "musty", "none", "pungent", "spicy"], |
| 47 | + "gill_attachment": ["attached", "free"], |
| 48 | + "gill_spacing": ["close", "crowded"], |
| 49 | + "gill_size": ["broad", "narrow"], |
| 50 | + "gill_color": ["black", "brown", "buff", "chocolate", "gray", "green", "orange", "pink", "purple", "red", "white", "yellow"], |
| 51 | + "stalk_shape": ["enlarging", "tapering"], |
| 52 | + "stalk_root": ["bulbous", "club", "equal", "rooted", "missing"], |
| 53 | + "stalk_surface_above_ring": ["fibrous", "scaly", "silky", "smooth"], |
| 54 | + "stalk_surface_below_ring": ["fibrous", "scaly", "silky", "smooth"], |
| 55 | + "stalk_color_above_ring": ["brown", "buff", "cinnamon", "gray", "orange", "pink", "red", "white", "yellow"], |
| 56 | + "stalk_color_below_ring": ["brown", "buff", "cinnamon", "gray", "orange", "pink", "red", "white", "yellow"], |
| 57 | + "veil_type": ["partial"], |
| 58 | + "veil_color": ["brown", "orange", "white", "yellow"], |
| 59 | + "ring_number": ["none", "one", "two"], |
| 60 | + "ring_type": ["evanescent", "flaring", "large", "none", "pendant"], |
| 61 | + "spore_print_color": ["black", "brown", "buff", "chocolate", "green", "orange", "purple", "white", "yellow"], |
| 62 | + "population": ["abundant", "clustered", "numerous", "scattered", "several", "solitary"], |
| 63 | + "habitat": ["grasses", "leaves", "meadows", "paths", "urban", "waste", "woods"] |
| 64 | + }; |
| 65 | + |
| 66 | + |
| 67 | + const keyMapping = { |
| 68 | + 'cap-shape': 'cap_shape', |
| 69 | + 'cap-surface': 'cap_surface', |
| 70 | + 'cap-color': 'cap_color', |
| 71 | + 'gill-attachment': 'gill_attachment', |
| 72 | + 'gill-spacing': 'gill_spacing', |
| 73 | + 'gill-size': 'gill_size', |
| 74 | + 'gill-color': 'gill_color', |
| 75 | + 'stalk-shape': 'stalk_shape', |
| 76 | + 'stalk-root': 'stalk_root', |
| 77 | + 'stalk-surface-above-ring': 'stalk_surface_above_ring', |
| 78 | + 'stalk-surface-below-ring': 'stalk_surface_below_ring', |
| 79 | + 'stalk-color-above-ring': 'stalk_color_above_ring', |
| 80 | + 'stalk-color-below-ring': 'stalk_color_below_ring', |
| 81 | + 'veil-type': 'veil_type', |
| 82 | + 'veil-color': 'veil_color', |
| 83 | + 'ring-number': 'ring_number', |
| 84 | + 'ring-type': 'ring_type', |
| 85 | + 'spore-print-color': 'spore_print_color', |
| 86 | + 'population': 'population', |
| 87 | + 'habitat': 'habitat', |
| 88 | + 'bruises': 'bruises', |
| 89 | + 'odor': 'odor' |
| 90 | + }; |
| 91 | + |
| 92 | + const [errorMessage, setErrorMessage] = useState(""); |
| 93 | + const [result, setResult] = useState(null); |
| 94 | + |
| 95 | + const handleSubmit = async () => { |
| 96 | + if (Object.values(formData).some((field) => field === "")) { |
| 97 | + setErrorMessage("Please fill out all the fields."); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + try { |
| 102 | + const response = await fetch("http://localhost:5000/mushroom_edibility", { |
| 103 | + method: "POST", |
| 104 | + body: new URLSearchParams(formData), |
| 105 | + }); |
| 106 | + |
| 107 | + const data = await response.json(); |
| 108 | + |
| 109 | + if (data.error) { |
| 110 | + setErrorMessage(data.error); |
| 111 | + } else { |
| 112 | + setResult(data.edibility); |
| 113 | + } |
| 114 | + } catch (error) { |
| 115 | + setErrorMessage("Failed to connect to the server."); |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + const handleChange = (e) => { |
| 120 | + setFormData({ |
| 121 | + ...formData, |
| 122 | + [e.target.name]: e.target.value, |
| 123 | + }); |
| 124 | + setResult(null); |
| 125 | + setErrorMessage(""); |
| 126 | + }; |
| 127 | + |
| 128 | + return ( |
| 129 | + <> |
| 130 | + {loading ? ( |
| 131 | + <Spinner /> |
| 132 | + ) : ( |
| 133 | + <div |
| 134 | + className="flex items-center justify-center min-h-screen p-24 pt-40" |
| 135 | + style={{ |
| 136 | + backgroundImage: `url(${img2})`, |
| 137 | + backgroundSize: "cover", |
| 138 | + backgroundPosition: "center", |
| 139 | + backgroundRepeat: "no-repeat", |
| 140 | + }} |
| 141 | + > |
| 142 | + |
| 143 | + <div className="absolute inset-0 bg-black opacity-50" /> |
| 144 | + |
| 145 | + <div className="relative bg-white bg-opacity-30 backdrop-blur-md rounded-lg shadow-lg p-10 w-full max-w-3xl z-10"> |
| 146 | + <h1 className="text-3xl font-bold text-center text-white mb-12"> |
| 147 | + Mushroom Edibility Prediction |
| 148 | + </h1> |
| 149 | + |
| 150 | + <div className="flex flex-col justify-center mb-8"> |
| 151 | + <p className="text-lg text-white text-justify"> |
| 152 | + 🍄 Enter mushroom characteristics to predict whether it is edible or poisonous. |
| 153 | + </p> |
| 154 | + <p className="md:text-xl text-lg font-bold py-4 text-white"> |
| 155 | + 🤔 How it Works! |
| 156 | + </p> |
| 157 | + <p className="text-lg text-white text-left"> |
| 158 | + 🌱 Input various mushroom features, such as cap shape, color, bruising, and more. |
| 159 | + <br /> |
| 160 | + 📊 The system predicts the edibility based on the data. |
| 161 | + </p> |
| 162 | + </div> |
| 163 | + |
| 164 | + <form className="space-y-6" name="mushroomEdibility"> |
| 165 | + <div className="flex flex-wrap -mx-2 justify-center"> |
| 166 | + {Object.keys(formData).map((field) => ( |
| 167 | + <div className="relative w-1/3 px-2 mb-4" key={field}> {/* Adjusted width to 1/3 */} |
| 168 | + <label htmlFor={field} className="block text-gray-200 text-left font-bold capitalize"> |
| 169 | + {field.replace(/-/g, " ")}: |
| 170 | + </label> |
| 171 | + <select |
| 172 | + name={field} |
| 173 | + id={field} |
| 174 | + value={formData[field]} |
| 175 | + onChange={handleChange} |
| 176 | + className="peer w-full p-4 border border-transparent rounded-md focus:outline-none focus:ring-2 focus:ring-green-500 focus:ring-opacity-60 backdrop-blur-md mt-2" |
| 177 | + required |
| 178 | + > |
| 179 | + <option value="" selected disabled style={{ color: 'lightgray' }}> |
| 180 | + Select {toPascalCase(field.replace(/-/g, " "))} |
| 181 | + </option> |
| 182 | + {options[keyMapping[field]].map((option) => ( |
| 183 | + <option key={option} value={option}> |
| 184 | + {option.charAt(0).toUpperCase() + option.slice(1)} |
| 185 | + </option> |
| 186 | + ))} |
| 187 | + </select> |
| 188 | + </div> |
| 189 | + ))} |
| 190 | + </div> |
| 191 | + |
| 192 | + <button |
| 193 | + type="button" |
| 194 | + onClick={handleSubmit} |
| 195 | + className="w-full bg-green-600 hover:bg-green-700 text-white font-bold py-3 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500 focus:ring-opacity-60" |
| 196 | + > |
| 197 | + Predict |
| 198 | + </button> |
| 199 | +</form> |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | + {result && ( |
| 204 | + <div className={`mt-8 p-4 rounded-md ${result === "Edible" ? "bg-green-200" : "bg-red-200"}`}> |
| 205 | + <h2 className={`text-2xl font-bold mb-4 ${result === "Edible" ? "text-green-800" : "text-red-800"}`}> |
| 206 | + Prediction: |
| 207 | + </h2> |
| 208 | + <p className={`text-lg ${result === "Edible" ? "text-green-600" : "text-red-600"}`}> |
| 209 | + The mushroom is <span className="font-bold">{result.toLowerCase()}</span>. |
| 210 | + </p> |
| 211 | + </div> |
| 212 | + )} |
| 213 | + |
| 214 | + {errorMessage && ( |
| 215 | + <div |
| 216 | + className={`mt-6 bg-red-500 text-white font-bold py-3 px-6 rounded-lg text-center transition-opacity duration-1000 ${errorMessage ? 'opacity-100' : 'opacity-0'}`} |
| 217 | + > |
| 218 | + {errorMessage} |
| 219 | + </div> |
| 220 | + )} |
| 221 | + </div> |
| 222 | + </div> |
| 223 | + )} |
| 224 | + </> |
| 225 | + ); |
| 226 | +}; |
| 227 | + |
| 228 | +export default MushroomEdibility; |
0 commit comments