Krina2005 commited on
Commit
1bcdfce
Β·
1 Parent(s): dabc7ce

readme configure

Browse files
Files changed (1) hide show
  1. README.md +25 -88
README.md CHANGED
@@ -1,99 +1,36 @@
1
- # FarmRisk Backend
2
-
3
- This is the backend service for FarmRisk, a multilingual agro-meteorological decision support platform. It is built using **FastAPI** and integrates Open-Meteo forecasts, Pinecone vector retrievals (RAG), and Gemini/Groq LLMs to generate crop-specific agricultural advisories.
4
-
5
  ---
6
-
7
- ## Directory Structure
8
-
9
- ```
10
- backend/
11
- β”œβ”€β”€ app/ # Main application package
12
- β”‚ β”œβ”€β”€ api/ # API routers (FastAPI endpoints)
13
- β”‚ β”‚ β”œβ”€β”€ advisory.py # Handles /api/advisory (orchestrating generation & translation)
14
- β”‚ β”‚ └── location.py # Handles /api/location (location utilities)
15
- β”‚ β”‚
16
- β”‚ β”œβ”€β”€ core/ # Core system modules
17
- β”‚ β”‚ β”œβ”€β”€ caching.py # In-memory spatial & translation cache manager
18
- β”‚ β”‚ β”œβ”€β”€ config.py # Global application configurations & environment loading
19
- β”‚ β”‚ └── logging.py # Structured logging configuration
20
- β”‚ β”‚
21
- β”‚ β”œβ”€β”€ llm/ # Large Language Model services
22
- β”‚ β”‚ β”œβ”€β”€ advisory_engine.py# Core advisory prompt synthesis and generation
23
- β”‚ β”‚ └── providers.py # LLM clients for Gemini and Groq (JSON/Text generation)
24
- β”‚ β”‚
25
- β”‚ β”œβ”€β”€ models/ # Data validation schemas (Pydantic)
26
- β”‚ β”‚ └── schemas.py # Request, Response, and Location models
27
- β”‚ β”‚
28
- β”‚ β”œβ”€β”€ rag/ # Retrieval-Augmented Generation
29
- β”‚ β”‚ └── retriever.py # Pinecone query retriever for scientific guidelines
30
- β”‚ β”‚
31
- β”‚ β”œβ”€β”€ services/ # External services and utility classes
32
- β”‚ β”‚ β”œβ”€β”€ location.py # Nominatim reverse geocoding
33
- β”‚ β”‚ β”œβ”€β”€ season.py # Month-based agricultural season resolver (Kharif/Rabi)
34
- β”‚ β”‚ β”œβ”€β”€ translation.py # Multi-language translation pipeline
35
- β”‚ β”‚ └── weather.py # Open-Meteo 10-day forecast integration & risk rules
36
- β”‚ β”‚
37
- β”‚ └── main.py # FastAPI entry point & lifespan events
38
- β”‚
39
- β”œβ”€β”€ config/ # Static JSON configuration files
40
- β”‚ β”œβ”€β”€ crops.json # Supported crops configuration
41
- β”‚ └── states.json # Standardized Indian States configuration
42
- β”‚
43
- β”œβ”€β”€ data/ # Data directory (scientific guides, JSON extracts)
44
- β”‚
45
- β”œβ”€β”€ pipeline/ # Data parsing, chunking, and index uploading scripts
46
- β”‚ β”œβ”€β”€ 02_parse.py # Parse raw ICAR guides into JSON
47
- β”‚ β”œβ”€β”€ 03_validate.py # Validate guidelines format
48
- β”‚ β”œβ”€β”€ 04_chunk.py # Text chunking for vector database
49
- β”‚ β”œβ”€β”€ 05_embed_upload.py # Embed text chunks and upload to Pinecone
50
- β”‚ └── run_all.py # Master script to run the pipeline
51
- β”‚
52
- β”œβ”€β”€ rag/ # RAG data extraction script
53
- β”‚ └── extract.py # Extract content text from PDFs
54
- β”‚
55
- β”œβ”€β”€ .env # Local environment variable configuration
56
- β”œβ”€β”€ requirements.txt # Python dependencies
57
- └── venv/ # Python virtual environment
58
- ```
59
-
60
  ---
61
 
62
- ## Core Components
63
 
64
- ### 1. API Endpoints (`app/api/`)
65
- * **Advisory (`advisory.py`)**: Receives farmer coordinate location, crop, target language, and crop stage. It resolves the coordinates into village/district boundaries, retrieves the 10-day weather forecast, performs semantic vector search for ICAR guidelines, triggers Gemini/Groq to generate the summary, caches results, and runs translation.
66
- * **Location (`location.py`)**: Provides endpoints for searching or geocoding locations.
67
 
68
- ### 2. LLM Advisory Engine (`app/llm/`)
69
- * **Advisory Engine (`advisory_engine.py`)**: Synthesizes the weather forecast context, risk alerts, and scientific guidelines into a prompt. It enforces strict two-paragraph output constraints, plain text styling, word count range (120-180 words), and structural partitions (expected weather in paragraph 1, recommendations in paragraph 2 ending with outlook).
70
- * **LLM Providers (`providers.py`)**: Integrates with the official `google-genai` SDK and the `groq` SDK, implementing retry/backoff wrappers for transient error tolerance, supporting JSON and plain-text output formats.
71
 
72
- ### 3. Translation Pipeline (`app/services/translation.py`)
73
- * Extracts the generated `advisory_summary` and calls the translation provider.
74
- * Prompt rules enforce the strict preservation of double-newline paragraph breaks (`\n\n`), sentence order, wording, and meaning without rephrasing or summarizing.
 
 
 
75
 
76
- ### 4. RAG Retriever (`app/rag/`)
77
- * Performs a metadata-filtered vector similarity search on the Pinecone index `farmrisk` based on selected crop, state, and resolved agricultural season to retrieve precise scientific guidelines.
78
 
79
- ### 5. Weather Service (`app/services/weather.py`)
80
- * Connects to the Open-Meteo Forecast API.
81
- * Runs local rule-based evaluations for extreme temperatures, high winds, or heavy rainfall to output alert flags.
82
- * Generates a stable forecast hash used for temporal caching.
 
 
83
 
84
- ---
85
 
86
- ## Running the Backend
87
 
88
- 1. **Activate the Virtual Environment**:
89
- ```powershell
90
- .\venv\Scripts\Activate.ps1
91
- ```
92
- 2. **Install Dependencies**:
93
- ```bash
94
- pip install -r requirements.txt
95
- ```
96
- 3. **Start the Development Server**:
97
- ```bash
98
- python -m uvicorn app.main:app --reload --host 127.0.0.1 --port 8000
99
- ```
 
 
 
 
 
1
  ---
2
+ title: FarmRisk
3
+ emoji: 🌾
4
+ colorFrom: green
5
+ colorTo: blue
6
+ sdk: docker
7
+ pinned: false
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  ---
9
 
10
+ # FarmRisk
11
 
12
+ FarmRisk is an AI-powered agro-meteorological advisory backend built with FastAPI.
 
 
13
 
14
+ ## Features
 
 
15
 
16
+ - 🌦️ Weather forecast APIs
17
+ - πŸ€– AI-generated crop advisories
18
+ - πŸ“ Village and location resolution
19
+ - 🌱 Crop-specific recommendations
20
+ - πŸ” Semantic search using Sentence Transformers (`BAAI/bge-small-en-v1.5`)
21
+ - ⚑ Production-ready FastAPI backend
22
 
23
+ ## Tech Stack
 
24
 
25
+ - FastAPI
26
+ - Python 3.12.1
27
+ - Sentence Transformers
28
+ - Hugging Face Transformers
29
+ - PyTorch
30
+ - Uvicorn
31
 
32
+ ## Deployment
33
 
34
+ This project is configured to run as a **Docker Space** on Hugging Face.
35
 
36
+ The application starts automatically using the provided `Dockerfile`.