selva1909 commited on
Commit
91fe40e
·
verified ·
1 Parent(s): 8a77264

Update app.py

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