Spaces:
Build error
Build error
Download app.py from Esmaeilkiani/CroploggingSugarcane: direct link, hf CLI and curl.
- Browser
- Download file 4.4 kB
-
https://huggingface.co/spaces/Esmaeilkiani/CroploggingSugarcane/resolve/main/app.py
- Command line
-
hf download hf://spaces/Esmaeilkiani/CroploggingSugarcane/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Esmaeilkiani/CroploggingSugarcane/resolve/main/app.py
4.4 kB
| import streamlit as st | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| import plotly.graph_objects as go | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.linear_model import LinearRegression | |
| # Page Configuration | |
| st.set_page_config(page_title="Agriculture and Crop Monitoring", layout="wide") | |
| # Prediction Function | |
| def predict_growth(crop_data, weeks_to_predict=8): | |
| X = crop_data[['هفته', 'رشد']].dropna().values | |
| y = crop_data['رشد'].dropna().values | |
| model = LinearRegression() | |
| model.fit(X, y) | |
| future_weeks = [[week, crop_data[crop_data['هفته'] == week]['رشد'].mean()] for week in range(1, weeks_to_predict + 1)] | |
| future_weeks_df = pd.DataFrame(future_weeks, columns=['هفته', 'رشد']) | |
| predictions = model.predict(future_weeks_df) | |
| future_weeks_df['Prediction Growth'] = predictions | |
| return future_weeks_df | |
| # Sidebar | |
| st.sidebar.title("Menu") | |
| menu = st.sidebar.selectbox("Choose an option", ["Show Charts", "Predict Crop Growth using Machine Learning", "Chat Bot"]) | |
| # Chat Bot | |
| if menu == "Chat Bot": | |
| st.sidebar.markdown(""" | |
| <script src="https://cdn.botpress.cloud/webchat/v2/inject.js"></script> | |
| <script> | |
| window.botpressWebChat.init({ | |
| hostUrl: "https://cdn.botpress.cloud/webchat/v2", | |
| messagingUrl: "https://mediafiles.botpress.cloud/29d24895-b0fa-4941-a3c4-8afa638f8b28", | |
| botId: "your-bot-id", | |
| }) | |
| </script> | |
| """, unsafe_allow_html=True) | |
| # Show Charts | |
| elif menu == "Show Charts": | |
| st.title("Charts and Graphs of Sugarcane Farm Growth and Height") | |
| if 'crop_data' not in st.session_state: | |
| uploaded_file = st.file_uploader("Upload growth and height data file", type="csv") | |
| if uploaded_file: | |
| st.session_state.crop_data = pd.read_csv(uploaded_file) | |
| st.write("Growth and height data uploaded.") | |
| else: | |
| crop_data = st.session_state.crop_data | |
| farms = crop_data['مزرعه'].unique() | |
| selected_farm = st.selectbox("Select Farm", farms) | |
| farm_data = crop_data[crop_data['مزرعه'] == selected_farm] | |
| st.subheader("Growth and Height of Plant Over Different Weeks") | |
| fig, ax = plt.subplots(1, 2, figsize=(14, 6)) | |
| sns.lineplot(x="هفته", y="رشد", data=farm_data, ax=ax[0], color="b") | |
| ax[0].set_title("Plant Growth") | |
| ax[0].set_xlabel("Week") | |
| ax[0].set_ylabel("Growth") | |
| sns.lineplot(x="هفته", y="ارتفاع", data=farm_data, ax=ax[1], color="g") | |
| ax[1].set_title("Plant Height") | |
| ax[1].set_xlabel("Week") | |
| ax[1].set_ylabel("Height") | |
| st.pyplot(fig) | |
| st.subheader("3D Plant Height Chart") | |
| fig = go.Figure(data=[go.Surface(z=farm_data.pivot(index='هفته', columns='مزرعه', values='ارتفاع').values)]) | |
| fig.update_layout( | |
| title='3D Plant Height Chart', | |
| autosize=True, | |
| scene=dict( | |
| zaxis_title='Height', | |
| xaxis_title='Week', | |
| yaxis_title='Farm' | |
| ), | |
| margin=dict(l=65, r=50, b=65, t=90) | |
| ) | |
| st.plotly_chart(fig) | |
| # Predict Crop Growth using Machine Learning | |
| elif menu == "Predict Crop Growth using Machine Learning": | |
| st.title("Predict Crop Growth using Machine Learning") | |
| if 'crop_data' not in st.session_state: | |
| uploaded_file = st.file_uploader("Upload growth and height data file", type="csv") | |
| if uploaded_file: | |
| st.session_state.crop_data = pd.read_csv(uploaded_file) | |
| st.write("Growth and height data uploaded.") | |
| else: | |
| crop_data = st.session_state.crop_data | |
| farms = crop_data['مزرعه'].unique() | |
| selected_farm = st.selectbox("Select Farm", farms) | |
| farm_data = crop_data[crop_data['مزرعه'] == selected_farm] | |
| predicted_growth = predict_growth(farm_data) | |
| st.subheader("Predicted Growth for Week 8") | |
| st.write(predicted_growth) | |
| st.subheader("Predicted Growth Chart") | |
| fig, ax = plt.subplots(figsize=(10, 6)) | |
| sns.lineplot(x=predicted_growth['هفته'], y=predicted_growth['Prediction Growth'], ax=ax, color="m") | |
| ax.set_title("Predicted Growth") | |
| ax.set_xlabel("Week") | |
| ax.set_ylabel("Predicted Growth") | |
| st.pyplot(fig) | |