import React, { useState, useEffect } from 'react'; import './App.css'; function App() { const [modelInfo, setModelInfo] = useState(null); const [trainingMetrics, setTrainingMetrics] = useState(null); const [prediction, setPrediction] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); // Form data for prediction const [formData, setFormData] = useState({ plant: '', scientificName: '', age: '', sex: '', year: '' }); const API_URL = 'http://localhost:5000/api'; useEffect(() => { checkModelStatus(); }, []); const checkModelStatus = async () => { try { const response = await fetch(`${API_URL}/model/info`); const data = await response.json(); if (data.success) { setModelInfo(data.info); } } catch (err) { console.log('Model not yet trained'); } }; const handleFileUpload = async (event) => { const file = event.target.files[0]; if (!file) return; setLoading(true); setError(null); const formData = new FormData(); formData.append('file', file); try { const response = await fetch(`${API_URL}/train`, { method: 'POST', body: formData }); const data = await response.json(); if (data.success) { setTrainingMetrics(data.metrics); checkModelStatus(); alert('Model trained successfully!'); } else { setError(data.error || 'Training failed'); } } catch (err) { setError('Error training model: ' + err.message); } finally { setLoading(false); } }; const handleInputChange = (e) => { setFormData({ ...formData, [e.target.name]: e.target.value }); }; const handlePredict = async (e) => { e.preventDefault(); setLoading(true); setError(null); setPrediction(null); // Convert form data to model input format const inputData = createInputData(formData); try { const response = await fetch(`${API_URL}/predict`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(inputData) }); const data = await response.json(); if (data.success) { setPrediction(data.predictions); } else { setError(data.error || 'Prediction failed'); } } catch (err) { setError('Error making prediction: ' + err.message); } finally { setLoading(false); } }; const createInputData = (form) => { // Initialize all features to 0 const input = {}; // Set plant name const plantKey = `Plant_Name_${form.plant}`; input[plantKey] = 1; // Set scientific name const sciKey = `Scientific_Name_${form.scientificName}`; input[sciKey] = 1; // Set sex counts (simplified) input['Sex_Male_Count'] = form.sex === 'Male' ? 1 : 0; input['Sex_Female_Count'] = form.sex === 'Female' ? 1 : 0; // Set age range if (form.age >= 18 && form.age <= 44) input['18-44_Years'] = 1; else if (form.age >= 45 && form.age <= 64) input['45-64_Years'] = 1; else if (form.age >= 65 && form.age <= 74) input['65-74_Years'] = 1; else if (form.age >= 75) input['>=75_years'] = 1; // Set year if (form.year) input[form.year] = 1; // Set geography input['Geo_Asia'] = 1; return input; }; return (
Linear Regression Model for Herbal Medicine ADR Prediction
✅ Model Status: Trained
Features: {modelInfo.n_features} | Targets: {modelInfo.n_targets}