Spaces:
Sleeping
Sleeping
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
!pip install gradio scikit-learn joblib
import numpy as npimport pandas as pdimport gradio as grfrom sklearn.model_selection import train_test_splitfrom sklearn.feature_selection import SelectKBest, f_regressionfrom sklearn.preprocessing import StandardScalerfrom sklearn.pipeline import Pipelinefrom sklearn.ensemble import RandomForestRegressorfrom sklearn.svm import SVRfrom sklearn.metrics import mean_squared_error, r2_scoreimport joblib
np.random.seed(42)n_samples = 500
X = pd.DataFrame({ "leaf_thickness": np.random.uniform(0.2, 1.0, n_samples), "water_content": np.random.uniform(60, 95, n_samples), "osmotic_pressure": np.random.uniform(0.5, 2.5, n_samples), "temperature": np.random.uniform(15, 40, n_samples),})
y = 0.3*X["leaf_thickness"] + 0.5*X["water_content"] - 0.2*X["temperature"] + \ 0.8*X["osmotic_pressure"] + np.random.normal(0, 2, n_samples)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
pipeline = Pipeline([ ("scaler", StandardScaler()), ("select", SelectKBest(score_func=f_regression, k=3)), # Select top 3 features ("regressor", RandomForestRegressor(n_estimators=100, random_state=42))])
pipeline.fit(X_train, y_train)
y_pred = pipeline.predict(X_test)print("MSE:", mean_squared_error(y_test, y_pred))print("R2 Score:", r2_score(y_test, y_pred))
# Save modeljoblib.dump(pipeline, "turgor_model.pkl")
model = joblib.load("turgor_model.pkl")
def predict_turgor(leaf_thickness, water_content, osmotic_pressure, temperature): input_data = np.array([[leaf_thickness, water_content, osmotic_pressure, temperature]]) pred = model.predict(input_data)[0] return f"Predicted Turgor Pressure: {pred:.2f} units"
with gr.Blocks() as demo: gr.Markdown("# 🌱 Turgor Pressure Detection") gr.Markdown("Predict plant turgor pressure using physiological and environmental features.") with gr.Row(): leaf_thickness = gr.Slider(0.2, 1.0, value=0.5, step=0.01, label="Leaf Thickness (mm)") water_content = gr.Slider(60, 95, value=80, step=0.1, label="Water Content (%)") osmotic_pressure = gr.Slider(0.5, 2.5, value=1.2, step=0.01, label="Osmotic Pressure (MPa)") temperature = gr.Slider(15, 40, value=25, step=0.5, label="Temperature (°C)")
output = gr.Textbox(label="Prediction")
predict_btn = gr.Button("Predict") predict_btn.click( fn=predict_turgor, inputs=[leaf_thickness, water_content, osmotic_pressure, temperature], outputs=output )
demo.launch()
app.pyAdded+78