Update app.py
Browse files
app.py
CHANGED
@@ -1,96 +1,95 @@
|
|
1 |
-
# Import required libraries
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
f"
|
26 |
-
f"
|
27 |
-
f"The
|
28 |
-
f"The
|
29 |
-
f"The
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
"
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
st.
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
st.sidebar.
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
st.sidebar.
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
st.sidebar.
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
st.write("Predicted Yield:", prediction)
|
|
|
1 |
+
# Import required libraries
|
2 |
+
import streamlit as st
|
3 |
+
from groq import Groq
|
4 |
+
|
5 |
+
# Set your API key (replace 'your_groq_api_key_here' with the actual API key)
|
6 |
+
GROQ_API_KEY = "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"
|
7 |
+
|
8 |
+
# Initialize the Groq client
|
9 |
+
client = Groq(
|
10 |
+
api_key="GROQ_API_KEY"
|
11 |
+
)
|
12 |
+
|
13 |
+
# Function to handle predictions
|
14 |
+
def predict_yield(climate_zone=None, region=None, yield_units=None, farm_size=None, fertilizer_rate=None,
|
15 |
+
fertilizer_type=None, historical_weather=None, temperature=None, soil_moisture=None,
|
16 |
+
soil_type=None, weather_condition=None, crop_type=None, irrigation_method=None,
|
17 |
+
prediction_period=None, custom_prompt=None):
|
18 |
+
try:
|
19 |
+
if custom_prompt:
|
20 |
+
prompt = custom_prompt
|
21 |
+
else:
|
22 |
+
# Construct the prompt for the model using individual inputs
|
23 |
+
prompt = (
|
24 |
+
f"Predict the agricultural yield for a farm in the {climate_zone} climate zone, "
|
25 |
+
f"located in the {region} region. The farm size is {farm_size} acres, and the desired yield units are {yield_units}. "
|
26 |
+
f"The fertilizer application rate is {fertilizer_rate} using {fertilizer_type}. Historical weather data indicates {historical_weather}. "
|
27 |
+
f"The average temperature is {temperature} degrees, soil moisture levels are {soil_moisture}, and the soil type is {soil_type}. "
|
28 |
+
f"The current weather condition is {weather_condition}. The crop type is {crop_type}, and the irrigation method used is {irrigation_method}. "
|
29 |
+
f"The yield prediction period is {prediction_period}."
|
30 |
+
)
|
31 |
+
|
32 |
+
# Call the Groq API
|
33 |
+
chat_completion = client.chat.completions.create(
|
34 |
+
messages=[
|
35 |
+
{
|
36 |
+
"role": "user",
|
37 |
+
"content": prompt,
|
38 |
+
}
|
39 |
+
],
|
40 |
+
model="llama3-8b-8192",
|
41 |
+
)
|
42 |
+
|
43 |
+
# Return the response
|
44 |
+
return chat_completion.choices[0].message.content
|
45 |
+
|
46 |
+
except Exception as e:
|
47 |
+
return f"An error occurred during prediction: {e}"
|
48 |
+
|
49 |
+
# Streamlit Interface
|
50 |
+
st.title("Agricultural Yield Prediction App")
|
51 |
+
st.write("Predict agricultural yield based on various factors.")
|
52 |
+
|
53 |
+
# Sidebar for input method selection
|
54 |
+
st.sidebar.title("Input Method")
|
55 |
+
input_method = st.sidebar.radio("Choose input method:", ("Use Custom Prompt", "Use Parameters"))
|
56 |
+
|
57 |
+
if input_method == "Use Parameters":
|
58 |
+
# Sidebar inputs for parameters
|
59 |
+
st.sidebar.title("Input Parameters")
|
60 |
+
climate_zone = st.sidebar.text_input("Climate Zone")
|
61 |
+
region = st.sidebar.text_input("Region")
|
62 |
+
yield_units = st.sidebar.text_input("Desired Yield Units (e.g., tons per acre, bushels per acre)")
|
63 |
+
farm_size = st.sidebar.text_input("Farm Size (acres or hectares)")
|
64 |
+
fertilizer_rate = st.sidebar.text_input("Fertilizer Application Rate")
|
65 |
+
fertilizer_type = st.sidebar.text_input("Fertilizer Type")
|
66 |
+
historical_weather = st.sidebar.text_input("Historical Weather Data")
|
67 |
+
temperature = st.sidebar.text_input("Temperature (degrees)")
|
68 |
+
soil_moisture = st.sidebar.text_input("Soil Moisture Levels")
|
69 |
+
soil_type = st.sidebar.text_input("Soil Type")
|
70 |
+
weather_condition = st.sidebar.text_input("Weather Condition")
|
71 |
+
crop_type = st.sidebar.text_input("Crop Type")
|
72 |
+
irrigation_method = st.sidebar.text_input("Irrigation Method")
|
73 |
+
prediction_period = st.sidebar.text_input("Yield Prediction Period (e.g., weekly, monthly, seasonal)")
|
74 |
+
custom_prompt = None
|
75 |
+
|
76 |
+
else:
|
77 |
+
# Sidebar input for custom prompt
|
78 |
+
st.sidebar.title("Custom Prompt")
|
79 |
+
custom_prompt = st.sidebar.text_area("Enter your custom prompt here", value="Enter your prompt...")
|
80 |
+
climate_zone = region = yield_units = farm_size = fertilizer_rate = fertilizer_type = historical_weather = None
|
81 |
+
temperature = soil_moisture = soil_type = weather_condition = crop_type = irrigation_method = prediction_period = None
|
82 |
+
|
83 |
+
# Main page layout for buttons and output
|
84 |
+
col1, col2 = st.columns([1, 2])
|
85 |
+
|
86 |
+
# Clear button functionality
|
87 |
+
if col2.button("Clear"):
|
88 |
+
st.rerun()
|
89 |
+
|
90 |
+
# Predict button and display result
|
91 |
+
if col1.button("Predict Yield"):
|
92 |
+
prediction = predict_yield(climate_zone, region, yield_units, farm_size, fertilizer_rate, fertilizer_type,
|
93 |
+
historical_weather, temperature, soil_moisture, soil_type, weather_condition,
|
94 |
+
crop_type, irrigation_method, prediction_period, custom_prompt)
|
95 |
+
st.write("Predicted Yield:", prediction)
|
|