An end-to-end Streamlit application that provides Kerala-specific agricultural advice using a hybrid RAG (Retrieval-Augmented Generation) system, seasonal crop calendar, district-wise advisory, weather-based guidance, and optional voice features. Supports Malayalam and English.
- Chat with an AI advisor grounded in Kerala Agricultural Development Policy (PDF) and a curated FAQ dataset (CSV)
- FAISS vector search with multilingual sentence-transformer embeddings and keyword fallback index
- Optional local LLM via Ollama; default cloud LLM via Gemini
- Crop disease image analysis using Gemini Vision
- Seasonal calendar with month-wise crops, activities, and tips
- District-wise advisory: crops, soils, schemes, and recommendations
- Weather advisory with mock Kerala patterns and actionable guidance
- Voice input/output using browser Web Speech API (no backend dependency)
KrishiMitraAI/
├─ app.py # Streamlit UI and app entrypoint
├─ rag_system.py # RAG pipeline (FAISS + Gemini/Ollama) and image analysis
├─ document_processor.py # PDF/CSV ingestion and chunking
├─ utils.py # Language detection, text cleaning/formatting, fallbacks
├─ crop_calendar.py # Kerala agricultural calendar (month-wise)
├─ kerala_districts.py # District-wise agri info and tips
├─ weather_integration.py # Weather-based advisory (mock data)
├─ voice_assistant.py # Web Speech API integrations for voice I/O
├─ attached_assets/
│ ├─ Agri-Dev-Policy_*.pdf # Kerala policy PDF (knowledge source)
│ ├─ Farming_FAQ_*.csv # FAQ dataset (knowledge source)
│ ├─ questionsv4.csv # Optional additional Q&A
│ └─ index/ # FAISS index files (auto-built/loaded)
├─ pyproject.toml # Dependencies (uv/pip compatible)
└─ uv.lock # Lockfile (optional)
- Python 3.11+
- Windows, macOS, or Linux
- Internet access for first-time model downloads and Gemini API (unless using Ollama)
- Optional: GPU with CUDA for faster FAISS (auto-detected)
- Gemini: set
GEMINI_API_KEYin your environment.- The code currently contains a placeholder; for production, always use an environment variable.
- Install Ollama from
https://ollama.com - Pull a compatible model (configured default is
llama2-uncensored:7b):
ollama pull llama2-uncensored:7bThe app auto-detects Ollama; if not found, it falls back to Gemini.
git clone <your-repo-url>
cd KrishiMitraAI- Windows (PowerShell):
python -m venv .venv
. .venv\Scripts\Activate.ps1- macOS/Linux (bash):
python3 -m venv .venv
source .venv/bin/activateUsing pip:
pip install -U pip
pip install -r requirements.txt # if you create one
# or install via pyproject
pip install .Or using uv (fast, recommended):
pip install uv
uv syncEnsure the following exist under attached_assets/:
Agri-Dev-Policy_*.pdf(provided)Farming_FAQ_Assistant_Dataset_*.csv(provided)- Optional:
questionsv4.csv
- Windows (PowerShell):
$env:GEMINI_API_KEY="YOUR_KEY"- macOS/Linux:
export GEMINI_API_KEY="YOUR_KEY"Start the Streamlit app:
streamlit run app.pyThen open the provided local URL in your browser.
First launch builds/loads the FAISS index and keyword index in background threads. The sidebar shows build status, device (CPU/GPU), and knowledge base stats.
- Chat Tab: Ask questions in Malayalam or English. Answers are grounded in the PDF/CSV knowledge base.
- Crop Disease Detection: Upload a crop image and click “Analyze Crop” to get identification and treatment suggestions.
- Seasonal Calendar: Choose a month to see recommended crops, maintenance, irrigation, fertilizer, and disease watch.
- District Advisory: Pick your district (and optionally a crop) to get localized guidance and schemes.
- Weather Advisory: Select a location to view mock real-time weather and actionable recommendations; shows a 3‑day outlook.
- Voice: Expand Voice Input to speak queries; responses can be played via “Listen to Response.”
flowchart LR
subgraph UI[Streamlit UI]
A[Chat UI]
B[Image Upload]
C[Calendar]
D[District Advisory]
E[Weather Advisory]
V[Voice I/O]
end
A -- query --> RAG
B -- image --> IMG[Gemini Vision]
C --> CAL[KeralaAgriculturalCalendar]
D --> DIST[KeralaDistrictAdvisory]
E --> WTH[WeatherAdvisory]
V --> A
subgraph Core
RAG[KrishiMitraRAG]
DP[DocumentProcessor]
VS[(FAISS Index)]
KW[(Keyword Index)]
end
RAG <--chunks--> VS
RAG <--tokens--> KW
RAG <--docs--> DP
subgraph External
GEM[Gemini API]
OLL[Ollama (optional)]
end
RAG <--> GEM
RAG <--> OLL
sequenceDiagram
participant User
participant UI as Streamlit UI
participant RAG as KrishiMitraRAG
participant VS as FAISS
participant KW as Keyword Index
participant LLM as Gemini/Ollama
User->>UI: Enter question (ML/EN)
UI->>RAG: get_response(question)
RAG->>VS: similarity_search(k=8) [if ready]
alt Vector store unavailable
RAG->>KW: keyword search + scoring
end
RAG->>RAG: prepare_context + extract_facts(FAQ)
RAG->>LLM: system+user prompt (language-aware)
LLM-->>RAG: response text
alt Empty/weak response
RAG->>RAG: compose_answer_from_facts
end
RAG-->>UI: formatted answer
UI-->>User: Display answer + optional TTS
flowchart TB
Start[App Start] --> Init[Initialize RAG + build/load indexes]
Init --> Sidebar[Sidebar shows status and KB stats]
Sidebar --> Tabs{Select Tab}
Tabs -->|Chat| Chat
Tabs -->|Disease| Disease
Tabs -->|Calendar| Calendar
Tabs -->|District| District
Tabs -->|Weather| Weather
Chat --> Answer[Display answer + TTS]
Disease --> Analyze[Gemini Vision analysis]
Calendar --> MonthAdvice[Monthly advice]
District --> DistrictAdvice[District recommendations]
Weather --> WeatherAdvice[Mock data + guidance]
- Embeddings:
sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2loaded on CPU or CUDA when available. - Index directory:
attached_assets/index/(auto-created). Safe to delete; will rebuild on next run. - Language detection: simple Malayalam/English heuristic in
utils.detect_language. - Voice features: implemented via Web Speech API in
voice_assistant.py; no server-side speech libs required.
- Vector store building forever:
- Ensure
attached_assetsfiles exist and readable. - Delete
attached_assets/index/and restart to rebuild.
- Ensure
- GPU not used:
- Requires CUDA and
torch.cuda.is_available(); otherwise runs on CPU.
- Requires CUDA and
- Gemini errors:
- Verify
GEMINI_API_KEYis set and valid. - Network/firewall may block API; check connectivity.
- Verify
- Ollama not used:
- Confirm
ollama listshows the model. App will fall back to Gemini if unavailable.
- Confirm
- Image analysis fails:
- Ensure JPEG/PNG is clear and under typical browser upload limits.
- Do not commit real API keys. Use environment variables.
- Uploaded images are temporarily saved for processing and then deleted.
- Answers are advisory only; for official guidance, consult local Krishi Bhavan.
- Run in dev mode and iterate on Streamlit components.
- Key modules to tweak:
rag_system.py: retrieval configs, prompts, and LLM provider.document_processor.py: chunk sizes and metadata handling.app.py: UI layout and interactions.
Specify your license here.