Spaces:
Sleeping
Sleeping
deployment
Browse files- app.py +64 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from sklearn.model_selection import train_test_split
|
3 |
+
from sklearn.linear_model import LinearRegression
|
4 |
+
from sklearn.svm import SVR
|
5 |
+
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
|
6 |
+
from sklearn.preprocessing import LabelEncoder
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
def load_data():
|
10 |
+
url = 'https://raw.githubusercontent.com/NarutoOp/Crop-Recommendation/master/cropdata.csv'
|
11 |
+
data = pd.read_csv(url)
|
12 |
+
return data
|
13 |
+
|
14 |
+
data = load_data()
|
15 |
+
|
16 |
+
label_encoders = {}
|
17 |
+
for column in ['STATE', 'CROP']:
|
18 |
+
le = LabelEncoder()
|
19 |
+
data[column] = le.fit_transform(data[column])
|
20 |
+
label_encoders[column] = le
|
21 |
+
|
22 |
+
X = data[['YEAR', 'STATE', 'CROP', 'YEILD']] # Feature columns
|
23 |
+
y = data['PROFIT'] # Target column
|
24 |
+
|
25 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
26 |
+
|
27 |
+
models = {
|
28 |
+
'Linear Regression': LinearRegression(),
|
29 |
+
'SVR': SVR(),
|
30 |
+
'Random Forest': RandomForestRegressor(),
|
31 |
+
'Gradient Boosting': GradientBoostingRegressor()
|
32 |
+
}
|
33 |
+
|
34 |
+
for name, model in models.items():
|
35 |
+
model.fit(X_train, y_train)
|
36 |
+
|
37 |
+
def predict(model_name, year, state, crop, yield_):
|
38 |
+
if model_name in models:
|
39 |
+
model = models[model_name]
|
40 |
+
state_encoded = label_encoders['STATE'].transform([state])[0]
|
41 |
+
crop_encoded = label_encoders['CROP'].transform([crop])[0]
|
42 |
+
prediction = model.predict([[year, state_encoded, crop_encoded, yield_]])[0]
|
43 |
+
return prediction
|
44 |
+
else:
|
45 |
+
return "Model not found"
|
46 |
+
|
47 |
+
inputs = [
|
48 |
+
gr.Dropdown(choices=list(models.keys()), label='Model'),
|
49 |
+
gr.Number(label='Year'),
|
50 |
+
gr.Textbox(label='State'),
|
51 |
+
gr.Textbox(label='Crop'),
|
52 |
+
gr.Number(label='Yield')
|
53 |
+
]
|
54 |
+
outputs = gr.Textbox(label='Predicted Profit')
|
55 |
+
|
56 |
+
demo = gr.Interface(
|
57 |
+
fn=predict,
|
58 |
+
inputs=inputs,
|
59 |
+
outputs=outputs,
|
60 |
+
title="Profit Prediction using various ML models",
|
61 |
+
theme=gr.themes.Soft()
|
62 |
+
)
|
63 |
+
|
64 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
pandas
|
2 |
+
scikit-learn
|
3 |
+
gradio
|