Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
!pip install gradio scikit-learn joblib
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from sklearn.model_selection import train_test_split
|
| 8 |
+
from sklearn.feature_selection import SelectKBest, f_regression
|
| 9 |
+
from sklearn.preprocessing import StandardScaler
|
| 10 |
+
from sklearn.pipeline import Pipeline
|
| 11 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 12 |
+
from sklearn.svm import SVR
|
| 13 |
+
from sklearn.metrics import mean_squared_error, r2_score
|
| 14 |
+
import joblib
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
np.random.seed(42)
|
| 18 |
+
n_samples = 500
|
| 19 |
+
|
| 20 |
+
X = pd.DataFrame({
|
| 21 |
+
"leaf_thickness": np.random.uniform(0.2, 1.0, n_samples),
|
| 22 |
+
"water_content": np.random.uniform(60, 95, n_samples),
|
| 23 |
+
"osmotic_pressure": np.random.uniform(0.5, 2.5, n_samples),
|
| 24 |
+
"temperature": np.random.uniform(15, 40, n_samples),
|
| 25 |
+
})
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
y = 0.3*X["leaf_thickness"] + 0.5*X["water_content"] - 0.2*X["temperature"] + \
|
| 29 |
+
0.8*X["osmotic_pressure"] + np.random.normal(0, 2, n_samples)
|
| 30 |
+
|
| 31 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
pipeline = Pipeline([
|
| 35 |
+
("scaler", StandardScaler()),
|
| 36 |
+
("select", SelectKBest(score_func=f_regression, k=3)), # Select top 3 features
|
| 37 |
+
("regressor", RandomForestRegressor(n_estimators=100, random_state=42))
|
| 38 |
+
])
|
| 39 |
+
|
| 40 |
+
pipeline.fit(X_train, y_train)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
y_pred = pipeline.predict(X_test)
|
| 44 |
+
print("MSE:", mean_squared_error(y_test, y_pred))
|
| 45 |
+
print("R2 Score:", r2_score(y_test, y_pred))
|
| 46 |
+
|
| 47 |
+
# Save model
|
| 48 |
+
joblib.dump(pipeline, "turgor_model.pkl")
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
model = joblib.load("turgor_model.pkl")
|
| 52 |
+
|
| 53 |
+
def predict_turgor(leaf_thickness, water_content, osmotic_pressure, temperature):
|
| 54 |
+
input_data = np.array([[leaf_thickness, water_content, osmotic_pressure, temperature]])
|
| 55 |
+
pred = model.predict(input_data)[0]
|
| 56 |
+
return f"Predicted Turgor Pressure: {pred:.2f} units"
|
| 57 |
+
|
| 58 |
+
with gr.Blocks() as demo:
|
| 59 |
+
gr.Markdown("# 🌱 Turgor Pressure Detection")
|
| 60 |
+
gr.Markdown("Predict plant turgor pressure using physiological and environmental features.")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
leaf_thickness = gr.Slider(0.2, 1.0, value=0.5, step=0.01, label="Leaf Thickness (mm)")
|
| 64 |
+
water_content = gr.Slider(60, 95, value=80, step=0.1, label="Water Content (%)")
|
| 65 |
+
osmotic_pressure = gr.Slider(0.5, 2.5, value=1.2, step=0.01, label="Osmotic Pressure (MPa)")
|
| 66 |
+
temperature = gr.Slider(15, 40, value=25, step=0.5, label="Temperature (°C)")
|
| 67 |
+
|
| 68 |
+
output = gr.Textbox(label="Prediction")
|
| 69 |
+
|
| 70 |
+
predict_btn = gr.Button("Predict")
|
| 71 |
+
predict_btn.click(
|
| 72 |
+
fn=predict_turgor,
|
| 73 |
+
inputs=[leaf_thickness, water_content, osmotic_pressure, temperature],
|
| 74 |
+
outputs=output
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
demo.launch()
|
| 78 |
+
|