File size: 3,671 Bytes
e0b0834
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
import streamlit as st
import pandas as pd
from streamlit_extras.colored_header import colored_header
from streamlit_extras.add_vertical_space import add_vertical_space
from groq import Groq

# Initialize Groq API
GROQ_API_KEY = "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"
client = Groq(api_key=GROQ_API_KEY)

# Function to query Groq API
def query_groq(prompt, model="llama3-8b-8192"):
    chat_completion = client.chat.completions.create(
        messages=[{"role": "user", "content": prompt}],
        model=model,
    )
    return chat_completion.choices[0].message.content

# Streamlit UI
st.set_page_config(
    page_title="Crop Yield Insights",
    page_icon="๐ŸŒพ",
    layout="wide",
)

# Sidebar
with st.sidebar:
    st.image("https://cdn-icons-png.flaticon.com/512/868/868909.png", width=120)
    st.title("Crop Yield Assistant ๐ŸŒฑ")
    st.markdown("Get recommendations and predictions for crops based on data insights.")
    add_vertical_space(3)
    st.info("Upload your CSV file to get started!")

# Main app
st.title("๐ŸŒพ Crop Yield Insights")
st.markdown("Upload your dataset and select an ID to get relevant insights, predictions, and recommendations.")

# File uploader
uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"], accept_multiple_files=False)

if uploaded_file:
    df = pd.read_csv(uploaded_file)

    # Data preprocessing
    if df.isnull().sum().any():
        st.warning("Missing values detected. Filling with median values.")
        df.fillna(df.median(numeric_only=True), inplace=True)

    # Display dataset preview
    st.subheader("๐Ÿ“Š Dataset Overview")
    st.dataframe(df.head())

    # Ensure there is an 'ID' column
    if 'ID' not in df.columns:
        st.error("The dataset must contain an 'ID' column.")
    else:
        # Select ID
        record_id = st.selectbox("Select an ID:", df['ID'].unique())

        if st.button("Generate Insights"):
            # Generate insights
            record = df[df['ID'] == record_id]
            
            if record.empty:
                st.error(f"No record found for ID: {record_id}")
            else:
                soil_quality = record.iloc[0]['Soil_Quality']
                seed_variety = record.iloc[0]['Seed_Variety']
                fertilizer_amount = record.iloc[0]['Fertilizer_Amount_kg_per_hectare']
                sunny_days = record.iloc[0]['Sunny_Days']
                rainfall = record.iloc[0]['Rainfall_mm']
                irrigation_schedule = record.iloc[0]['Irrigation_Schedule']

                prompt = (
                    f"The dataset includes the following information for ID {record_id}:\n"
                    f"- Soil Quality: {soil_quality}\n"
                    f"- Seed Variety: {seed_variety}\n"
                    f"- Fertilizer Amount (kg/ha): {fertilizer_amount}\n"
                    f"- Sunny Days: {sunny_days}\n"
                    f"- Rainfall (mm): {rainfall}\n"
                    f"- Irrigation Schedule: {irrigation_schedule}\n\n"
                    "Using this data, provide insights into expected crop yield, "
                    "recommendations for improving productivity, and potential challenges."
                )

                with st.spinner("Fetching insights..."):
                    response = query_groq(prompt)
                
                st.success(f"๐ŸŒŸ Insights for ID {record_id}")
                st.markdown(response)

else:
    st.info("Please upload a CSV file to proceed.")

# Footer
st.markdown("---")
st.markdown(
    "<h4 style='text-align: center;'>Powered by ๐Ÿง  Groq AI | Designed with โค๏ธ Streamlit</h4>",
    unsafe_allow_html=True,
)