Spaces:
Build error
Build error
this is pushing mew visual model
Browse files- __pycache__/app.cpython-312.pyc +0 -0
- app.py +94 -12
- requirements.txt +5 -1
__pycache__/app.cpython-312.pyc
ADDED
|
Binary file (2.92 kB). View file
|
|
|
app.py
CHANGED
|
@@ -5,7 +5,18 @@ 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)
|
|
@@ -34,7 +45,7 @@ models = {
|
|
| 34 |
for name, model in models.items():
|
| 35 |
model.fit(X_train, y_train)
|
| 36 |
|
| 37 |
-
def
|
| 38 |
if model_name in models:
|
| 39 |
model = models[model_name]
|
| 40 |
state_encoded = label_encoders['STATE'].transform([state])[0]
|
|
@@ -44,21 +55,92 @@ def predict(model_name, year, state, crop, yield_):
|
|
| 44 |
else:
|
| 45 |
return "Model not found"
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
demo.launch()
|
|
|
|
| 5 |
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
|
| 6 |
from sklearn.preprocessing import LabelEncoder
|
| 7 |
import gradio as gr
|
| 8 |
+
import os
|
| 9 |
+
import numpy as np
|
| 10 |
+
import tensorflow as tf
|
| 11 |
+
from tensorflow.keras.models import Sequential
|
| 12 |
+
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Input
|
| 13 |
+
from tensorflow.keras.optimizers import Adam
|
| 14 |
+
from PIL import Image
|
| 15 |
+
import rasterio
|
| 16 |
+
from tensorflow.keras.applications import ResNet50
|
| 17 |
+
from tensorflow.keras.models import Model
|
| 18 |
|
| 19 |
+
# Load crop data
|
| 20 |
def load_data():
|
| 21 |
url = 'https://raw.githubusercontent.com/NarutoOp/Crop-Recommendation/master/cropdata.csv'
|
| 22 |
data = pd.read_csv(url)
|
|
|
|
| 45 |
for name, model in models.items():
|
| 46 |
model.fit(X_train, y_train)
|
| 47 |
|
| 48 |
+
def predict_traditional(model_name, year, state, crop, yield_):
|
| 49 |
if model_name in models:
|
| 50 |
model = models[model_name]
|
| 51 |
state_encoded = label_encoders['STATE'].transform([state])[0]
|
|
|
|
| 55 |
else:
|
| 56 |
return "Model not found"
|
| 57 |
|
| 58 |
+
# Load pre-trained deep learning models
|
| 59 |
+
def load_deep_learning_model(model_name):
|
| 60 |
+
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(128, 128, 3))
|
| 61 |
+
base_model.trainable = False
|
| 62 |
+
|
| 63 |
+
inputs = Input(shape=(128, 128, 3))
|
| 64 |
+
x = base_model(inputs, training=False)
|
| 65 |
+
x = GlobalAveragePooling2D()(x)
|
| 66 |
+
outputs = Dense(1, activation='linear')(x)
|
| 67 |
+
|
| 68 |
+
model = Model(inputs, outputs)
|
| 69 |
+
model.compile(optimizer=Adam(), loss='mean_squared_error', metrics=['mae'])
|
| 70 |
+
|
| 71 |
+
return model
|
| 72 |
+
|
| 73 |
+
deep_learning_models = {
|
| 74 |
+
'ResNet50': load_deep_learning_model('ResNet50'),
|
| 75 |
+
# Add other models here if needed
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
def predict_deep_learning(model_name, file):
|
| 79 |
+
if model_name in deep_learning_models:
|
| 80 |
+
if file is not None:
|
| 81 |
+
with rasterio.open(file.name) as src:
|
| 82 |
+
img_data = src.read(1)
|
| 83 |
+
|
| 84 |
+
patch_size = 128
|
| 85 |
+
n_patches_x = img_data.shape[1] // patch_size
|
| 86 |
+
n_patches_y = img_data.shape[0] // patch_size
|
| 87 |
+
|
| 88 |
+
patches = []
|
| 89 |
+
for i in range(n_patches_y):
|
| 90 |
+
for j in range(n_patches_x):
|
| 91 |
+
patch = img_data[i*patch_size:(i+1)*patch_size, j*patch_size:(j+1)*patch_size]
|
| 92 |
+
patches.append(patch)
|
| 93 |
+
patches = np.array(patches)
|
| 94 |
+
|
| 95 |
+
preprocessed_patches = []
|
| 96 |
+
for patch in patches:
|
| 97 |
+
img = Image.fromarray(patch)
|
| 98 |
+
img = img.convert('RGB')
|
| 99 |
+
img = img.resize((128, 128))
|
| 100 |
+
img_array = np.array(img) / 255.0
|
| 101 |
+
preprocessed_patches.append(img_array)
|
| 102 |
+
preprocessed_patches = np.array(preprocessed_patches)
|
| 103 |
+
|
| 104 |
+
model = deep_learning_models[model_name]
|
| 105 |
+
predictions = model.predict(preprocessed_patches)
|
| 106 |
+
predictions = predictions.reshape((n_patches_y, n_patches_x))
|
| 107 |
+
|
| 108 |
+
return predictions
|
| 109 |
+
else:
|
| 110 |
+
return "No file uploaded"
|
| 111 |
+
else:
|
| 112 |
+
return "Model not found"
|
| 113 |
+
|
| 114 |
+
inputs_traditional = [
|
| 115 |
gr.Dropdown(choices=list(models.keys()), label='Model'),
|
| 116 |
gr.Number(label='Year'),
|
| 117 |
gr.Textbox(label='State'),
|
| 118 |
gr.Textbox(label='Crop'),
|
| 119 |
+
gr.Number(label='Yield'),
|
| 120 |
+
]
|
| 121 |
+
outputs_traditional = gr.Textbox(label='Predicted Profit')
|
| 122 |
+
|
| 123 |
+
inputs_deep_learning = [
|
| 124 |
+
gr.Dropdown(choices=list(deep_learning_models.keys()), label='Model'),
|
| 125 |
+
gr.File(label='Upload TIFF File')
|
| 126 |
]
|
| 127 |
+
outputs_deep_learning = gr.Textbox(label='Predictions')
|
| 128 |
+
|
| 129 |
+
with gr.Blocks() as demo:
|
| 130 |
+
with gr.Tab("Traditional ML Models"):
|
| 131 |
+
gr.Interface(
|
| 132 |
+
fn=predict_traditional,
|
| 133 |
+
inputs=inputs_traditional,
|
| 134 |
+
outputs=outputs_traditional,
|
| 135 |
+
title="Profit Prediction using Traditional ML Models"
|
| 136 |
+
).launch()
|
| 137 |
+
|
| 138 |
+
with gr.Tab("Deep Learning Models"):
|
| 139 |
+
gr.Interface(
|
| 140 |
+
fn=predict_deep_learning,
|
| 141 |
+
inputs=inputs_deep_learning,
|
| 142 |
+
outputs=outputs_deep_learning,
|
| 143 |
+
title="Crop Yield Prediction using Deep Learning Models"
|
| 144 |
+
).launch()
|
| 145 |
|
| 146 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,3 +1,7 @@
|
|
| 1 |
pandas
|
| 2 |
scikit-learn
|
| 3 |
-
gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
pandas
|
| 2 |
scikit-learn
|
| 3 |
+
gradio
|
| 4 |
+
tensorflow
|
| 5 |
+
rasterio
|
| 6 |
+
Pillow
|
| 7 |
+
matplotlib
|