Spaces:
Sleeping
Sleeping
File size: 23,930 Bytes
e03d777 | 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | # TerraSyncra AI β Product & System Overview
## 1. Product Introduction
**TerraSyncra** is a multilingual agricultural intelligence agent designed specifically for Nigerian (and African) farmers. It provides comprehensive agricultural support through AI-powered assistance.
**Key Capabilities:**
- **Agricultural Q&A**: Answers questions about crops, livestock, soil, weather, pests, and diseases in multiple languages
- **Soil Analysis**: Provides expert soil health assessments from lab reports and field data using Gemini 3 Flash
- **Disease Detection**: Identifies plant and animal diseases from images, text descriptions, or voice input using Gemini 2.5 Flash
- **Live Agricultural Updates**: Delivers real-time weather information and agricultural news through RAG (Retrieval-Augmented Generation)
- **Live Voice Interaction**: Supports real-time voice conversations via WebSocket in local languages (Igbo, Hausa, Yoruba, English)
**Developer**: Ifeanyi Amogu Shalom
**Target Users**: Farmers, agronomists, agricultural extension officers, and agricultural support workers in Nigeria and similar contexts
---
## 2. Problem Statement
Nigerian smallholder farmers face significant challenges:
### 2.1 Limited Access to Agricultural Experts
- **Scarcity of agronomists and veterinarians** relative to the large farming population
- **Geographic barriers** preventing farmers from accessing expert advice
- **High consultation costs** that many smallholder farmers cannot afford
- **Long waiting times** for professional consultations, especially during critical periods (disease outbreaks, planting seasons)
### 2.2 Language Barriers
- Most agricultural information and resources are in **English**, while many farmers primarily speak **Hausa, Igbo, or Yoruba**
- **Technical terminology** is not easily accessible in local languages
- **Translation services** are often unavailable or unreliable
### 2.3 Fragmented Information Sources
- Weather data, soil reports, disease information, and market prices are scattered across different platforms
- **No unified system** to integrate and interpret multiple data sources
- **Information overload** without proper context or prioritization
### 2.4 Time-Sensitive Decision Making
- **Disease outbreaks** require immediate identification and treatment
- **Weather changes** affect planting, harvesting, and irrigation decisions
- **Pest attacks** can devastate crops if not addressed quickly
- **Delayed responses** lead to significant economic losses
### 2.5 Solution Approach
TerraSyncra addresses these challenges by providing:
- **Fast, AI-powered responses** available 24/7
- **Multilingual support** (English, Igbo, Hausa, Yoruba)
- **Integrated intelligence** combining expert models, RAG, and live data
- **Accessible interface** via text, voice, and image inputs
- **Professional consultation reminders** to ensure farmers seek expert confirmation when needed
---
## 3. System Architecture & Request Flows
### 3.1 General Agricultural Q&A β `POST /ask`
**Step-by-Step Process:**
1. **Input Reception**
- User sends `query` (text) with optional `session_id` for conversation continuity
2. **Language Detection**
- FastText model (`facebook/fasttext-language-identification`) detects input language
- Supports: English, Igbo, Hausa, Yoruba
3. **Translation (if needed)**
- If language β English, translates to English using NLLB (`drrobot9/nllb-ig-yo-ha-finetuned`)
- Preserves original language for back-translation
4. **Intent Detection**
- Classifies query into categories:
- **Weather question**: Requests weather information (with/without Nigerian state)
- **Live update**: Requests current agricultural news or updates
- **Normal question**: General agricultural Q&A
- **Low confidence**: Falls back to RAG when intent is unclear
5. **Context Building**
- **Weather intent**: Calls WeatherAPI for state-specific weather data, embeds summary into context
- **Live update intent**: Queries live FAISS vectorstore index for latest agricultural documents
- **Low confidence**: Falls back to static FAISS index for safer, more general responses
6. **Conversation Memory**
- Loads per-session history from `MemoryStore` (TTL cache, 1-hour expiration)
- Trims to `MAX_HISTORY_MESSAGES` (default: 30) to prevent context overflow
7. **Expert Model Generation**
- Uses **Qwen/Qwen1.5-1.8B** (finetuned for Nigerian agriculture)
- Loaded lazily via `model_manager` (CPU-optimized, first-use loading)
- Builds chat messages: system prompt + conversation history + current user message + context
- System prompt restricts responses to **agriculture/farming topics only**
- Generates bounded-length answer (reduced token limit: 400 tokens for general, 256 for weather)
- Cleans response to remove any "Human: / Assistant:" style example continuations
8. **Back-Translation**
- If original language β English, translates answer back to user's language using NLLB
9. **Response**
- Returns JSON: `{ query, answer, session_id, detected_language }`
**Safety & Focus:**
- System prompt enforces agriculture-only topic handling
- Unrelated questions are redirected back to farming topics
- Response cleaning prevents off-topic example continuations
---
### 3.2 Soil Analysis β `POST /analyze-soil`
**Step-by-Step Process:**
1. **Input Reception**
- `report_data`: Text description of soil report or lab results (required)
- Optional fields: `location`, `crop_type`, `field_size`, `previous_crops`, `additional_notes`
2. **Agent Processing**
- `soil_agent.analyze_soil()` builds comprehensive prompt with:
- Soil report data
- Field information (location, crop type, size, history)
- Regional context (Nigerian states, climate patterns)
3. **Gemini API Call**
- Model: `GEMINI_SOIL_MODEL = "gemini-3-flash-preview"`
- Prompt style: Brief, direct, actionable
- Focuses on:
- Current soil condition (short summary)
- Key nutrient issues (deficiencies or excesses)
- 1β3 best crops for this soil type
- Clear fertilizer and amendment recommendations
- Simple soil improvement steps
4. **Output**
- JSON response: `{ success, analysis, model_used }`
**Important Note:**
> Soil analysis is **advisory only** β not a formal agronomy diagnosis. The UI should encourage farmers to confirm with a local agronomist or extension officer for critical decisions.
---
### 3.3 Disease Detection
#### 3.3.1 Image-Based Detection β `POST /detect-disease-image`
**Step-by-Step Process:**
1. **Input Reception**
- Image file (JPEG, PNG, etc.)
- Optional `query`: Text description or question
2. **Agent Processing**
- `disease_agent.classify_disease_from_image()` processes:
- Image bytes + MIME type
- User query (if provided)
- Builds structured prompt for Gemini
3. **Gemini API Call**
- Model: `GEMINI_DISEASE_MODEL = "gemini-2.5-flash"`
- Prompt instructs Gemini to provide:
- Disease name (scientific + common name) in 1 short line
- **Threat level: Low / Moderate / High / Uncertain** (MANDATORY)
- 2β3 key symptoms visible in image
- 2β3 clear treatment steps (bullets)
- 1β2 simple prevention tips
- Brief, direct language with short sentences
4. **Backend Safety Enforcement**
- Backend **always appends** disclaimer:
> "IMPORTANT: This threat level is an estimate based only on the image/description. For an accurate diagnosis and treatment plan, please consult a qualified agronomist, veterinary doctor, or local agricultural extension officer."
5. **Output**
- JSON response: `{ success, classification, model_used, input_type }`
#### 3.3.2 Text/Voice-Based Detection β `POST /detect-disease-text`
**Step-by-Step Process:**
1. **Input Reception**
- `description`: Text description of disease symptoms or condition
- `language`: Language code (en, ig, ha, yo)
2. **Agent Processing**
- `disease_agent.classify_disease_from_text()` processes:
- Text description
- Language context
- Builds structured prompt for Gemini
3. **Gemini API Call**
- Same model and prompt structure as image-based detection
- Threat level assessment based on described symptoms
4. **Backend Safety Enforcement**
- Same disclaimer appended as image-based detection
5. **Output**
- JSON response: `{ success, classification, model_used, input_type }`
**Threat Level Guidelines:**
- **Low**: Mild or early-stage issue, unlikely to cause major losses if addressed soon
- **Moderate**: Noticeable risk that can reduce yield/health if not treated
- **High**: Serious or fast-spreading issue that can cause major losses or death (use cautiously, only when clearly severe)
- **Uncertain**: Insufficient or ambiguous data; model cannot safely rate risk (encouraged when not confident)
---
### 3.4 Live Voice Interaction β `WS /live-voice` & `POST /live-voice-start`
**Step-by-Step Process:**
1. **WebSocket Connection**
- Client connects to `/live-voice` endpoint
- Optional: Send image as JSON (base64 encoded) at session start
- Audio chunks streamed as raw PCM bytes (16kHz, mono, 16-bit)
2. **Agent Processing**
- `live_voice_agent.handle_live_voice_websocket()` manages:
- WebSocket connection lifecycle
- Image context (if provided)
- Audio streaming to Gemini Live API
- Audio response streaming back to client
3. **Gemini Live API**
- Model: `gemini-2.5-flash` via Gemini Live API
- System prompt: Brief, clear, focused on "what to do next" (2β4 key steps)
- Supports: Disease detection, soil analysis, general farming, weather
- Prefers short sentences and bullet points
4. **Response Streaming**
- Audio responses streamed back as PCM bytes
- Optional JSON messages for status/transcripts
5. **Safety Expectations**
- Same professional advice principle applies
- Frontends should display clear "not a replacement for a professional" banner
---
## 4. Technologies Used
### 4.1 Backend Framework & Infrastructure
- **FastAPI**: Modern Python web framework for building REST APIs and WebSocket endpoints
- **Uvicorn**: ASGI server for running FastAPI applications
- **Python 3.10**: Programming language
- **Docker**: Containerization for deployment
- **Hugging Face Spaces**: Deployment platform (Docker runtime, CPU-only environment)
### 4.2 Core Language Models
#### 4.2.1 Expert Model: Qwen/Qwen1.5-1.8B
- **Model**: `Qwen/Qwen1.5-1.8B` (via Hugging Face Transformers)
- **Purpose**: Primary agricultural Q&A and conversation
- **Specialization**: **Finetuned/specialized** for Nigerian agricultural context through:
- Custom system prompts focused on Nigerian farming practices
- Domain-specific training data integration
- Response formatting optimized for agricultural advice
- **Optimization**:
- Lazy loading via `model_manager` (loads on first use)
- CPU-optimized inference (float32, device_map="cpu")
- Reduced token limits to prevent over-generation
#### 4.2.2 Gemini Models (Google AI)
- **google-genai**: Official Python client for Google's Gemini API
- **gemini-3-flash-preview**: Used for soil analysis
- **gemini-2.5-flash**: Used for disease detection and live voice interaction
- **API Version**: v1alpha for advanced features (disease detection, live voice)
### 4.3 Retrieval-Augmented Generation (RAG)
- **LangChain**: Framework for building LLM applications
- **LangChain Community**: Community integrations and tools
- **SentenceTransformers**:
- Model: `paraphrase-multilingual-MiniLM-L12-v2`
- Purpose: Text embeddings for semantic search
- **FAISS (Facebook AI Similarity Search)**:
- Vector database for efficient similarity search
- Two indices: Static (general knowledge) and Live (current updates)
- **APScheduler**: Background job scheduler for periodic RAG updates
### 4.4 Language Processing
- **FastText**:
- Model: `facebook/fasttext-language-identification`
- Purpose: Language detection (English, Igbo, Hausa, Yoruba)
- **NLLB (No Language Left Behind)**:
- Model: `drrobot9/nllb-ig-yo-ha-finetuned`
- Purpose: Translation between English and Nigerian languages (Hausa, Igbo, Yoruba)
- Bidirectional translation support
### 4.5 External APIs & Data Sources
- **WeatherAPI**:
- Provides state-level weather data for Nigerian states
- Real-time weather information integration
- **AgroNigeria / HarvestPlus**:
- Agricultural news feeds for RAG updates
- News scraping and processing
### 4.6 Additional Libraries
- **transformers**: Hugging Face library for loading and using transformer models
- **torch**: PyTorch (CPU-optimized version)
- **numpy**: Numerical computing
- **requests**: HTTP library for API calls
- **beautifulsoup4**: Web scraping for news aggregation
- **python-multipart**: File upload support for FastAPI
- **python-dotenv**: Environment variable management
---
## 5. Threat Level & Safety Policy
### 5.1 Domain Scope
- **Plant and animal diseases only** β **NOT human health**
- Focuses on agricultural and veterinary contexts
- Does not provide medical advice for humans
### 5.2 Threat Level Categories
#### Low
- **Definition**: Mild or early-stage issue, unlikely to cause major losses if addressed soon
- **Characteristics**:
- Localized symptoms
- Slow progression
- Easily manageable with standard treatments
- **Example**: Minor leaf spots, early nutrient deficiency
#### Moderate
- **Definition**: Noticeable risk that can reduce yield/health if not treated
- **Characteristics**:
- Moderate spread or impact
- Requires timely intervention
- Can cause economic losses if ignored
- **Example**: Moderate pest infestation, developing fungal infection
#### High
- **Definition**: Serious or fast-spreading issue that can cause major losses or death
- **Characteristics**:
- Rapid spread or severe symptoms
- High potential for significant economic impact
- May require immediate professional intervention
- **Example**: Severe bacterial blight, fast-spreading viral disease
- **Usage Caution**: Only assigned when signs are **clearly severe** or fast-spreading
#### Uncertain
- **Definition**: Insufficient or ambiguous data; model cannot safely rate risk
- **Characteristics**:
- Unclear symptoms
- Multiple possible diagnoses
- Poor image quality or vague description
- **Usage**: Encouraged when model is not confident β **better to be uncertain than wrong**
### 5.3 Accuracy & Caution Approach
**Threat Level Assessment:**
- Based **only** on image + description β **no lab tests or physical examination**
- Prompts instruct Gemini to be **conservative and cautious**
- Model encouraged to use `Uncertain` when not clearly sure
- Final responses always embed a strong "consult professionals" reminder
**Professional Consultation Reminder:**
- Backend **always appends** disclaimer to disease detection responses
- Frontends should visually emphasize: "This is not a medical/veterinary/agronomic diagnosis"
- System is a **decision-support tool**, not a definitive diagnostic engine
**Important Note:**
> **This system is a decision-support tool, not a definitive diagnosis engine.**
> All disease/threat outputs must be treated as preliminary guidance only.
> Farmers should always consult qualified professionals for critical decisions.
---
## 6. Limitations & Issues Faced
### 6.1 Diagnostic Limitations
#### Input Quality Dependencies
- **Image Quality**: Blurry, poorly lit, or low-resolution images reduce accuracy
- **Description Clarity**: Vague or incomplete symptom descriptions limit diagnostic precision
- **Context Missing**: Lack of field history, crop variety, or environmental conditions affects recommendations
#### Inherent Limitations
- **No Physical Examination**: Cannot inspect internal plant structures or perform lab tests
- **No Real-Time Monitoring**: Cannot track disease progression over time
- **Regional Variations**: Some regional diseases may be under-represented in training data
- **Seasonal Factors**: Disease presentation may vary by season, which may not always be captured
### 6.2 Language & Translation Challenges
#### Translation Accuracy
- **NLLB Limitations**: Can misread slang, mixed-language (e.g., Pidgin + Hausa), or regional dialects
- **Technical Terminology**: Agricultural terms may not have direct translations, leading to approximations
- **Context Loss**: Subtle meaning can be lost across translation steps (user language β English β user language)
#### Language Detection
- **FastText Edge Cases**: May misclassify mixed-language inputs or code-switching
- **Dialect Variations**: Regional variations within languages may not be fully captured
### 6.3 Model Behavior Issues
#### Hallucination Risk
- **Qwen/Gemini Limitations**: Can generate confident but incorrect answers
- **Mitigations Applied**:
- Stricter system prompts with domain restrictions
- Shorter output limits (400 tokens for general, 256 for weather)
- Response cleaning to remove example continuations
- Topic redirection for unrelated questions
- **Not Bulletproof**: Hallucination can still occur, especially for edge cases
#### Response Drift
- **Off-Topic Continuations**: Models may continue with example conversations or unrelated content
- **Mitigation**: Response cleaning logic removes "Human: / Assistant:" patterns and unrelated content
### 6.4 Latency & Compute Constraints
#### First-Request Latency
- **Model Loading**: First Qwen/NLLB call is slower due to model + weights loading on CPU
- **Cold Start**: ~5-10 seconds for first request after deployment
- **Subsequent Requests**: Faster due to cached models in memory
#### CPU-Only Environment
- **Inference Speed**: CPU inference is slower than GPU (acceptable for Hugging Face Spaces CPU tier)
- **Memory Constraints**: Limited RAM requires careful model management (lazy loading, model caching)
### 6.5 External Dependencies
#### WeatherAPI Issues
- **Outages**: WeatherAPI downtime affects weather-related responses
- **Rate Limits**: API quota limits may restrict frequent requests
- **Data Accuracy**: Weather data quality depends on third-party provider
#### News Source Reliability
- **Scraping Fragility**: News sources may change HTML structure, breaking scrapers
- **Update Frequency**: RAG updates are scheduled; failures can cause stale information
- **Content Quality**: News article quality and relevance vary
### 6.6 RAG & Data Freshness
#### Update Scheduling
- **Periodic Updates**: RAG indices updated on schedule (not real-time)
- **Job Failures**: If update job fails, index can lag behind real-world events
- **Index Rebuilding**: Full index rebuilds can be time-consuming
#### Vectorstore Limitations
- **Embedding Quality**: Semantic search quality depends on embedding model performance
- **Retrieval Accuracy**: Retrieved documents may not always be most relevant
- **Context Window**: Limited context window may truncate important information
### 6.7 Deployment & Infrastructure
#### Hugging Face Spaces Constraints
- **CPU-Only**: No GPU acceleration available
- **Memory Limits**: Limited RAM requires optimization (lazy loading, model size reduction)
- **Build Time**: Docker builds can be slow, especially with large dependencies
- **Cold Starts**: Spaces may spin down after inactivity, causing cold start delays
#### Docker Build Issues
- **Dependency Conflicts**: Some Python packages may conflict (e.g., pyaudio requiring system libraries)
- **Build Timeouts**: Long build times may cause deployment failures
- **Cache Management**: Docker layer caching can be inconsistent
---
## 7. Recommended UX & Safety Reminders
### 7.1 Visual Disclaimers
**Always display a clear banner near disease/soil results:**
> "β οΈ **This is AI-generated guidance. Always confirm with a local agronomist, veterinary doctor, or agricultural extension officer before taking major actions.**"
### 7.2 Threat Level Display
- **Visual Highlighting**: Display threat level prominently with color coding:
- π’ **Low**: Green
- π‘ **Moderate**: Yellow
- π΄ **High**: Red
- βͺ **Uncertain**: Gray
- **Tooltips**: Provide explanations for each threat level
- **Always Pair with Disclaimer**: Never show threat level without the professional consultation reminder
### 7.3 Call-to-Action Buttons
Provide quick access to professional help:
- **"Contact an Extension Officer"** button/link
- **"Find a Vet/Agronomist Near You"** button/link
- **"Schedule a Consultation"** option (if available)
### 7.4 Response Quality Indicators
- Show **confidence indicators** when available (e.g., "High confidence" vs "Uncertain")
- Display **input quality warnings** (e.g., "Image quality may affect accuracy")
- Provide **feedback mechanisms** for users to report incorrect diagnoses
### 7.5 Language Support
- Clearly indicate **detected language** in responses
- Provide **language switcher** for users to change language preference
- Show **translation quality warnings** if translation may be approximate
---
## 8. System Summary
### 8.1 Problem Addressed
Nigerian smallholder farmers face critical challenges:
- **Limited access to agricultural experts** (agronomists, veterinarians)
- **Language barriers** (most resources in English, farmers speak Hausa/Igbo/Yoruba)
- **Fragmented information sources** (weather, soil, disease data scattered)
- **Time-sensitive decision making** (disease outbreaks, weather changes, pest attacks)
### 8.2 Solution Provided
TerraSyncra combines multiple AI technologies to provide:
- **Fast, 24/7 AI-powered responses** in multiple languages
- **Integrated intelligence**:
- **Finetuned Qwen 1.8B** expert model for agricultural Q&A
- **Gemini 3/2.5 Flash** for soil analysis and disease detection
- **RAG + Weather + News** for live, contextual information
- **CPU-optimized, multilingual backend** (FastAPI on Hugging Face Spaces)
- **Multiple input modalities**: Text, voice, and image support
### 8.3 Safety & Professional Consultation
**Every disease assessment includes:**
- Explicit **Threat level** (Low / Moderate / High / Uncertain)
- Clear **professional consultation reminder**
- Emphasis that threat levels are **estimates**, not definitive diagnoses
### 8.4 Key Technologies
- **Expert Model**: Qwen/Qwen1.5-1.8B (finetuned for Nigerian agriculture)
- **Gemini Models**: gemini-3-flash-preview (soil), gemini-2.5-flash (disease, voice)
- **RAG**: LangChain + FAISS + SentenceTransformers
- **Language Processing**: FastText (detection) + NLLB (translation)
- **Backend**: FastAPI + Uvicorn + Docker
- **Deployment**: Hugging Face Spaces (CPU-optimized)
### 8.5 Developer & Credits
**Developer**: Ifeanyi Amogu Shalom
**Intended Users**: Farmers, agronomists, agricultural extension officers, and agricultural support workers in Nigeria and similar contexts
---
## 9. Future Improvements & Roadmap
### 9.1 Potential Enhancements
- **Model Fine-tuning**: Further fine-tune Qwen on Nigerian agricultural datasets
- **Multi-modal RAG**: Integrate images into RAG for visual similarity search
- **Offline Mode**: Support for offline operation in areas with poor connectivity
- **Mobile App**: Native mobile applications for better user experience
- **Expert Network Integration**: Direct connection to network of agronomists/veterinarians
- **Historical Tracking**: Track disease progression and treatment outcomes over time
### 9.2 Technical Improvements
- **Response Caching**: Cache common queries to reduce latency
- **Model Quantization**: Further optimize models for CPU inference
- **Better Error Handling**: More robust error messages and fallback mechanisms
- **Monitoring & Analytics**: Track system performance and user feedback
---
**Last Updated**: 2026
**Version**: 1.0
**Status**: Production (Hugging Face Spaces)
|