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( "