Thrishul3549x commited on
Commit
e56a409
·
verified ·
1 Parent(s): e08f721

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ from sklearn.model_selection import train_test_split
4
+ from sklearn.ensemble import RandomForestClassifier
5
+ from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
6
+ from sklearn.preprocessing import LabelEncoder
7
+ import gradio as gr
8
+
9
+ # Load dataset
10
+ data = pd.read_csv("cpdata.csv")
11
+
12
+ # Split features and target
13
+ y = data["label"]
14
+ x = data.drop("label", axis=1)
15
+
16
+ # Encode target labels
17
+ encoder = LabelEncoder()
18
+ y_encoded = encoder.fit_transform(y)
19
+
20
+ # Train-test split
21
+ xtrain, xtest, ytrain, ytest = train_test_split(x, y_encoded, test_size=0.2, random_state=0)
22
+
23
+ # Train model
24
+ classifier = RandomForestClassifier(n_estimators=100, random_state=0)
25
+ classifier.fit(xtrain, ytrain)
26
+
27
+ # Evaluate
28
+ output = classifier.predict(xtest)
29
+ accuracy = accuracy_score(ytest, output)
30
+ precision = precision_score(ytest, output, average="weighted")
31
+ recall = recall_score(ytest, output, average="weighted")
32
+ f1 = f1_score(ytest, output, average="weighted")
33
+
34
+ print("Model Performance:")
35
+ print(f"Accuracy: {accuracy:.2f}")
36
+ print(f"Precision: {precision:.2f}")
37
+ print(f"Recall: {recall:.2f}")
38
+ print(f"F1-score: {f1:.2f}")
39
+
40
+ # Gradio prediction function
41
+ def predict(temp, humi, ph, rain, N, P, K):
42
+ new_data = [[temp, humi, ph, rain, N, P, K]]
43
+ pred = classifier.predict(new_data)
44
+ plant = encoder.inverse_transform(pred)[0]
45
+ return f"Predicted plant for given condition: {plant}"
46
+
47
+ # Gradio UI
48
+ demo = gr.Interface(
49
+ fn=predict,
50
+ inputs=[
51
+ gr.Number(label="Temperature"),
52
+ gr.Number(label="Humidity"),
53
+ gr.Number(label="pH"),
54
+ gr.Number(label="Rainfall"),
55
+ gr.Number(label="Nitrogen (N)"),
56
+ gr.Number(label="Phosphorus (P)"),
57
+ gr.Number(label="Potassium (K)")
58
+ ],
59
+ outputs=gr.Textbox(label="Prediction"),
60
+ title="Crop Prediction App",
61
+ description="Enter soil and climate conditions to predict the best plant."
62
+ )
63
+
64
+ if __name__ == "__main__":
65
+ demo.launch()