Spaces:
Sleeping
Sleeping
Commit
·
f08ef39
1
Parent(s):
0645a90
1st commit
Browse files- app.py +81 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from sklearn.linear_model import LinearRegression
|
5 |
+
import joblib
|
6 |
+
from huggingface_hub import hf_hub_download, upload_file
|
7 |
+
|
8 |
+
# Load or initialize the model
|
9 |
+
def load_model():
|
10 |
+
try:
|
11 |
+
# Attempt to download model from Hugging Face
|
12 |
+
model_path = hf_hub_download(repo_id="your-huggingface-repo", filename="crop_yield_model.pkl")
|
13 |
+
model = joblib.load(model_path)
|
14 |
+
return model
|
15 |
+
except Exception as e:
|
16 |
+
st.warning("Model not found on Hugging Face. Initializing a new one.")
|
17 |
+
# Initialize with dummy training data
|
18 |
+
model = LinearRegression()
|
19 |
+
dummy_features = np.array([[100, 50, 25]]) # Example realistic values for rainfall, fertilizer, temperature
|
20 |
+
dummy_target = np.array([2]) # Example realistic yield value
|
21 |
+
model.fit(dummy_features, dummy_target)
|
22 |
+
return model
|
23 |
+
|
24 |
+
def save_model(model):
|
25 |
+
joblib.dump(model, "crop_yield_model.pkl")
|
26 |
+
upload_file(
|
27 |
+
path_or_fileobj="crop_yield_model.pkl",
|
28 |
+
path_in_repo="crop_yield_model.pkl",
|
29 |
+
repo_id="your-huggingface-repo",
|
30 |
+
repo_type="model",
|
31 |
+
)
|
32 |
+
|
33 |
+
# Streamlit app
|
34 |
+
st.title("Crop Yield Prediction")
|
35 |
+
|
36 |
+
# Input features
|
37 |
+
st.sidebar.header("Input Features")
|
38 |
+
rainfall = st.sidebar.number_input("Rainfall (mm)", min_value=0.0, max_value=5000.0, step=0.1)
|
39 |
+
fertilizer = st.sidebar.number_input("Fertilizer Used (kg/ha)", min_value=0.0, max_value=1000.0, step=0.1)
|
40 |
+
temperature = st.sidebar.number_input("Temperature (°C)", min_value=-10.0, max_value=50.0, step=0.1)
|
41 |
+
|
42 |
+
# Train new model option
|
43 |
+
train_new = st.sidebar.checkbox("Train New Model")
|
44 |
+
|
45 |
+
# Load data for training if needed
|
46 |
+
if train_new:
|
47 |
+
st.sidebar.header("Training Data")
|
48 |
+
uploaded_file = st.sidebar.file_uploader("Upload a CSV file", type="csv")
|
49 |
+
if uploaded_file:
|
50 |
+
data = pd.read_csv(uploaded_file)
|
51 |
+
st.write("### Training Data Preview", data.head())
|
52 |
+
features = data[["Rainfall", "Fertilizer", "Temperature"]]
|
53 |
+
target = data["Yield"]
|
54 |
+
|
55 |
+
# Train model
|
56 |
+
model = LinearRegression()
|
57 |
+
model.fit(features, target)
|
58 |
+
save_model(model)
|
59 |
+
st.success("Model trained and saved successfully!")
|
60 |
+
else:
|
61 |
+
st.warning("Please upload a CSV file to train a new model.")
|
62 |
+
else:
|
63 |
+
model = load_model()
|
64 |
+
|
65 |
+
# Predict crop yield
|
66 |
+
if st.button("Predict Crop Yield"):
|
67 |
+
if model is not None:
|
68 |
+
input_data = np.array([[rainfall, fertilizer, temperature]])
|
69 |
+
try:
|
70 |
+
prediction = model.predict(input_data)
|
71 |
+
st.write(f"### Predicted Crop Yield: {prediction[0]:.2f} tons/ha")
|
72 |
+
except Exception as e:
|
73 |
+
st.error("An error occurred during prediction. Please ensure the model is properly trained.")
|
74 |
+
else:
|
75 |
+
st.warning("Model not found or not trained. Please train a new model.")
|
76 |
+
|
77 |
+
# Notes for Hugging Face integration
|
78 |
+
st.sidebar.markdown("---")
|
79 |
+
st.sidebar.write("Hugging Face Integration:")
|
80 |
+
st.sidebar.write("- Replace `your-huggingface-repo` with your repository name.")
|
81 |
+
st.sidebar.write("- Ensure the repository permissions allow model uploads and downloads.")
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
streamlit
|