Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
| 3 |
+
from sklearn.compose import ColumnTransformer
|
| 4 |
+
from sklearn.pipeline import Pipeline
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.linear_model import LinearRegression
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import ee
|
| 9 |
+
import folium
|
| 10 |
+
|
| 11 |
+
# Setup Earth Engine
|
| 12 |
+
service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com' # Replace with your service account
|
| 13 |
+
credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json') # Replace with your credentials file
|
| 14 |
+
ee.Initialize(credentials)
|
| 15 |
+
|
| 16 |
+
# Load and preprocess data
|
| 17 |
+
data = pd.read_csv('merged_data_for_model.csv') # Replace with your data file path
|
| 18 |
+
features = ['Age', 'Variety', 'Date']
|
| 19 |
+
target = ['Brix', 'Purity', 'Pol', 'RS']
|
| 20 |
+
X = data[features]
|
| 21 |
+
y = data[target]
|
| 22 |
+
|
| 23 |
+
numerical_features = ['Age']
|
| 24 |
+
categorical_features = ['Variety']
|
| 25 |
+
preprocessor = ColumnTransformer(
|
| 26 |
+
transformers=[
|
| 27 |
+
('num', StandardScaler(), numerical_features),
|
| 28 |
+
('cat', OneHotEncoder(handle_unknown='ignore', sparse_output=False), categorical_features),
|
| 29 |
+
])
|
| 30 |
+
|
| 31 |
+
X['Date'] = pd.to_datetime(X['Date']).astype(int) / 10**9
|
| 32 |
+
|
| 33 |
+
# Model pipeline
|
| 34 |
+
pipeline = Pipeline([
|
| 35 |
+
('preprocessor', preprocessor),
|
| 36 |
+
('regressor', LinearRegression()),
|
| 37 |
+
])
|
| 38 |
+
|
| 39 |
+
# Split data and train model
|
| 40 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 41 |
+
pipeline.fit(X_train, y_train)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def predict_brix_purity_pol_rs(farm_name, age, variety, date):
|
| 45 |
+
# Convert inputs to appropriate format
|
| 46 |
+
age = float(age)
|
| 47 |
+
date = pd.to_datetime(date).astype(int) / 10**9
|
| 48 |
+
input_data = pd.DataFrame([[age, variety, date]], columns=['Age', 'Variety', 'Date'])
|
| 49 |
+
|
| 50 |
+
# Preprocess input data
|
| 51 |
+
input_data_processed = preprocessor.transform(input_data)
|
| 52 |
+
|
| 53 |
+
# Make prediction
|
| 54 |
+
prediction = pipeline.predict(input_data_processed)[0]
|
| 55 |
+
|
| 56 |
+
return prediction[0], prediction[1], prediction[2], prediction[3]
|
| 57 |
+
|
| 58 |
+
# Create Gradio interface
|
| 59 |
+
iface = gr.Interface(
|
| 60 |
+
fn=predict_brix_purity_pol_rs,
|
| 61 |
+
inputs=[
|
| 62 |
+
gr.Textbox(label="نام مزرعه"),
|
| 63 |
+
gr.Number(label="سن"),
|
| 64 |
+
gr.Textbox(label="واریته"),
|
| 65 |
+
gr.Textbox(label="تاریخ (YYYY-MM-DD)"),
|
| 66 |
+
],
|
| 67 |
+
outputs=[
|
| 68 |
+
gr.Number(label="Brix"),
|
| 69 |
+
gr.Number(label="Purity"),
|
| 70 |
+
gr.Number(label="Pol"),
|
| 71 |
+
gr.Number(label="RS"),
|
| 72 |
+
],
|
| 73 |
+
title="پیشبینی Brix، Purity، Pol و RS",
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
iface.launch()
|