|
from dotenv import load_dotenv |
|
import streamlit as st |
|
import os |
|
from groq import Groq |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
groq_api_key = os.getenv("GROQ_API_KEY") |
|
if not groq_api_key: |
|
raise ValueError("GROQ_API_KEY environment variable not set.") |
|
|
|
|
|
client = Groq(api_key=groq_api_key) |
|
|
|
|
|
def get_groq_response(prompt): |
|
try: |
|
chat_completion = client.chat.completions.create( |
|
messages=[{"role": "user", "content": prompt}], |
|
model="llama3-8b-8192" |
|
) |
|
return chat_completion.choices[0].message.content |
|
except Exception as e: |
|
return f"Error: {str(e)}" |
|
|
|
|
|
st.title("Crop Advisory Chatbot") |
|
st.write("Provide your details to get personalized crop advice.") |
|
|
|
|
|
soil_type = st.selectbox("Soil Type:", [ |
|
"Select", "Alluvial Soil (Loamy and Clay)", "Sandy Soil", |
|
"Mountain Soil", "Clay Soil", "Saline and Alkaline Soil" |
|
]) |
|
|
|
|
|
climate = st.selectbox("Climate:", [ |
|
"Select", "Hot", "Moderate", "Cold" |
|
]) |
|
|
|
|
|
area_size = st.number_input("Area size in acres:", min_value=0.1, step=0.1) |
|
season = st.selectbox("Current Season:", ["Select", "Summer", "Winter", "Rainy", "Autumn", "Spring"]) |
|
|
|
|
|
if st.button("Get Advisory"): |
|
|
|
if soil_type != "Select" and climate != "Select" and area_size > 0 and season != "Select": |
|
|
|
crop_recommendation_prompt = ( |
|
f"Suggest suitable crops for soil type '{soil_type}', climate '{climate}', " |
|
f"area size '{area_size}' acres, and season '{season}'." |
|
) |
|
|
|
fertilizer_prompt = ( |
|
f"Provide fertilizer recommendations for crops suitable in soil '{soil_type}' " |
|
f"and climate '{climate}'." |
|
) |
|
|
|
irrigation_prompt = ( |
|
f"Suggest irrigation methods and water requirements for crops in " |
|
f"'{soil_type}' soil with '{climate}' climate." |
|
) |
|
|
|
harvest_prompt = ( |
|
f"When is the best harvesting time for crops in '{climate}' climate during the '{season}' season?" |
|
) |
|
|
|
|
|
with st.spinner("Fetching advisory..."): |
|
crop_recommendation = get_groq_response(crop_recommendation_prompt) |
|
fertilizer_advice = get_groq_response(fertilizer_prompt) |
|
irrigation_advice = get_groq_response(irrigation_prompt) |
|
harvest_advice = get_groq_response(harvest_prompt) |
|
|
|
|
|
st.subheader("Crop Recommendations") |
|
st.write(crop_recommendation) |
|
|
|
st.subheader("Fertilizer Recommendations") |
|
st.write(fertilizer_advice) |
|
|
|
st.subheader("Irrigation Advice") |
|
st.write(irrigation_advice) |
|
|
|
st.subheader("Harvesting Advice") |
|
st.write(harvest_advice) |
|
|
|
else: |
|
st.warning("Please fill in all the details to get the advisory.") |
|
|
|
|
|
|
|
|