File size: 3,335 Bytes
a1c0116
 
 
 
 
941f62d
a1c0116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
946f87e
 
 
 
 
 
 
 
 
 
 
 
a1c0116
946f87e
a1c0116
946f87e
a1c0116
 
946f87e
a1c0116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
858b0ec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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`