Abbas0786 commited on
Commit
d9e2880
1 Parent(s): 69e5402

Update app.py

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