from dotenv import load_dotenv import streamlit as st import os from groq import Groq # Load environment variables from the .env fil load_dotenv() # Securely get the GROQ API key from environment variables groq_api_key = os.getenv("GROQ_API_KEY") if not groq_api_key: raise ValueError("GROQ_API_KEY environment variable not set.") # Initialize the client with the API key client = Groq(api_key=groq_api_key) # Function to get a response from Groq API 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)}" # Streamlit app layout st.title("Crop Advisory Chatbot") st.write("Provide your details to get personalized crop advice.") # Multiple-choice options for soil type soil_type = st.selectbox("Soil Type:", [ "Select", "Alluvial Soil (Loamy and Clay)", "Sandy Soil", "Mountain Soil", "Clay Soil", "Saline and Alkaline Soil" ]) # Multiple-choice options for climate climate = st.selectbox("Climate:", [ "Select", "Hot", "Moderate", "Cold" ]) # Input for area size and season selection 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"]) # Button to get advisory if st.button("Get Advisory"): # Check if all fields are filled if soil_type != "Select" and climate != "Select" and area_size > 0 and season != "Select": # Prompts for different advisory features 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?" ) # Fetch responses from Groq API 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) # Display results 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.") # To run the app in Colab, you need to use ngrok or run it locally with: # `streamlit run app.py`