Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.metrics import mean_absolute_error
|
| 7 |
+
|
| 8 |
+
# عنوان برنامه
|
| 9 |
+
st.title('Zali Ai - پیشبینی رشد مزارع نیشکر')
|
| 10 |
+
|
| 11 |
+
# تابع برای محاسبه میانگین رشد و ارتفاع
|
| 12 |
+
def calculate_growth_and_height(data):
|
| 13 |
+
data['mean_growth'] = data[[f'growth_station_{i}' for i in range(1, 6)]].mean(axis=1)
|
| 14 |
+
data['mean_height'] = data[[f'height_station_{i}' for i in range(1, 6)]].mean(axis=1)
|
| 15 |
+
return data
|
| 16 |
+
|
| 17 |
+
# تابع برای ساخت مدل ماشین لرنینگ
|
| 18 |
+
def train_model(data):
|
| 19 |
+
X = data.drop('mean_growth', axis=1)
|
| 20 |
+
y = data['mean_growth']
|
| 21 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 22 |
+
model = RandomForestRegressor(n_estimators=100, random_state=42)
|
| 23 |
+
model.fit(X_train, y_train)
|
| 24 |
+
predictions = model.predict(X_test)
|
| 25 |
+
mae = mean_absolute_error(y_test, predictions)
|
| 26 |
+
return model, mae
|
| 27 |
+
|
| 28 |
+
# آپلود فایل دادهها
|
| 29 |
+
uploaded_file = st.file_uploader("آپلود فایل دادهها (CSV)", type=['csv'])
|
| 30 |
+
|
| 31 |
+
if uploaded_file is not None:
|
| 32 |
+
data = pd.read_csv(uploaded_file)
|
| 33 |
+
data = calculate_growth_and_height(data)
|
| 34 |
+
st.write("نمایش دادهها", data)
|
| 35 |
+
|
| 36 |
+
model, mae = train_model(data)
|
| 37 |
+
st.write(f"مدل با موفقیت آموزش داده شد! خطای مطلق میانگین: {mae:.2f}")
|
| 38 |
+
|
| 39 |
+
# ورود دستی دادهها
|
| 40 |
+
st.write("ورود دستی دادهها:")
|
| 41 |
+
num_farms = 165
|
| 42 |
+
num_stations = 5
|
| 43 |
+
num_wells = 2
|
| 44 |
+
|
| 45 |
+
manual_data = []
|
| 46 |
+
for farm in range(1, num_farms+1):
|
| 47 |
+
st.write(f"مزرعه شماره {farm}")
|
| 48 |
+
growth = []
|
| 49 |
+
for station in range(1, num_stations+1):
|
| 50 |
+
growth.append(st.number_input(f"ایستگاه {station} - رشد", value=0))
|
| 51 |
+
for station in range(1, num_stations+1):
|
| 52 |
+
growth.append(st.number_input(f"ایستگاه {station} - ارتفاع", value=0))
|
| 53 |
+
for well in range(1, num_wells+1):
|
| 54 |
+
growth.append(st.number_input(f"چاهک {well} - سطح آب", value=0))
|
| 55 |
+
growth.append(st.number_input("رطوبت غلاف", value=0))
|
| 56 |
+
growth.append(st.number_input("نیتروژن", value=0))
|
| 57 |
+
manual_data.append(growth)
|
| 58 |
+
|
| 59 |
+
if st.button("پیشبینی رشد هفته آینده"):
|
| 60 |
+
manual_data = pd.DataFrame(manual_data, columns=[f'growth_station_{i}' for i in range(1, 6)] +
|
| 61 |
+
[f'height_station_{i}' for i in range(1, 6)] +
|
| 62 |
+
[f'well_{i}_water_level' for i in range(1, 3)] +
|
| 63 |
+
['moisture', 'nitrogen'])
|
| 64 |
+
manual_data = calculate_growth_and_height(manual_data)
|
| 65 |
+
predictions = model.predict(manual_data.drop('mean_growth', axis=1))
|
| 66 |
+
results = pd.DataFrame({
|
| 67 |
+
'مزرعه': range(1, num_farms+1),
|
| 68 |
+
'پیشبینی رشد هفته آینده': predictions
|
| 69 |
+
})
|
| 70 |
+
st.write("نتایج پیشبینی:", results)
|
| 71 |
+
else:
|
| 72 |
+
st.write("لطفا فایل دادهها را آپلود کنید یا دادهها را به صورت دستی وارد کنید.")
|
| 73 |
+
|
| 74 |
+
if st.checkbox("نمایش ساختار دادهها"):
|
| 75 |
+
st.write("""
|
| 76 |
+
### ساختار دادهها:
|
| 77 |
+
- growth_station_1 تا growth_station_5: رشد ایستگاهها
|
| 78 |
+
- height_station_1 تا height_station_5: ارتفاع ایستگاهها
|
| 79 |
+
- well_1_water_level و well_2_water_level: سطح آب چاهکها
|
| 80 |
+
- moisture: رطوبت غلاف
|
| 81 |
+
- nitrogen: نیتروژن
|
| 82 |
+
- mean_growth: میانگین رشد (هدف پیشبینی)
|
| 83 |
+
""")
|