Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from sklearn.preprocessing import LabelEncoder
|
| 6 |
+
from sklearn.model_selection import train_test_split
|
| 7 |
+
from sklearn.neighbors import KNeighborsClassifier
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
data = pd.read_excel('crop.xlsx')
|
| 11 |
+
data['CROP'].unique()
|
| 12 |
+
sorted(data['CROP'].unique())
|
| 13 |
+
le=LabelEncoder()
|
| 14 |
+
data['CROP_EN']=le.fit_transform(data['CROP'])
|
| 15 |
+
x=data.iloc[:,:-2]
|
| 16 |
+
y=data['CROP_EN']
|
| 17 |
+
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)
|
| 18 |
+
model=KNeighborsClassifier(n_neighbors=3)
|
| 19 |
+
model.fit(x_train,y_train)
|
| 20 |
+
y_pred=model.predict(x_test)
|
| 21 |
+
res = [
|
| 22 |
+
'apple', 'banana', 'blackgram', 'chickpea', 'coconut', 'coffee', 'cotton',
|
| 23 |
+
'grapes', 'jute', 'kidneybeans', 'lentil', 'maize', 'mango', 'mothbeans',
|
| 24 |
+
'mungbean', 'muskmelon', 'orange', 'papaya', 'pigeonpeas', 'pomegranate',
|
| 25 |
+
'rice', 'watermelon'
|
| 26 |
+
]
|
| 27 |
+
def predict(n,p,k,temp,hum,ph,rain):
|
| 28 |
+
pred = model.predict([[n,p,k,temp,hum,ph,rain]])
|
| 29 |
+
return res[pred[0]]
|
| 30 |
+
|
| 31 |
+
interface = gr.Interface(
|
| 32 |
+
fn=predict,
|
| 33 |
+
inputs=[
|
| 34 |
+
gr.Number(label="Nitrogen"),
|
| 35 |
+
gr.Number(label="PHOSPHORUS"),
|
| 36 |
+
gr.Number(label="Potassium"),
|
| 37 |
+
gr.Number(label="Temperature"),
|
| 38 |
+
gr.Number(label="Humidity"),
|
| 39 |
+
gr.Number(label="Soil PH"),
|
| 40 |
+
gr.Number(label="Rainfall"),
|
| 41 |
+
],
|
| 42 |
+
outputs=[
|
| 43 |
+
gr.Textbox(label="Predicted Crop"),
|
| 44 |
+
],
|
| 45 |
+
title="Crop Prediction Using machine Learning",
|
| 46 |
+
# description="This interface takes some input and generates 7 numerical outputs along with one text output."
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
interface.launch()
|
| 51 |
+
|