-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
772 lines (679 loc) · 33.4 KB
/
Copy pathmain.py
File metadata and controls
772 lines (679 loc) · 33.4 KB
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# main.py (Final Workflow Version with Contextual Chat Fix)
# Description: Implements a clear user workflow and a context-aware chat agent.
from fastapi import FastAPI, HTTPException, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
from contextlib import asynccontextmanager
from apscheduler.schedulers.asyncio import AsyncIOScheduler
import datetime
import uuid
from pydantic import BaseModel
from fastapi.responses import StreamingResponse, JSONResponse
import base64
import tempfile
import os
import re
from rapidfuzz import fuzz
# --- Import Core Logic ---
try:
from data_sources import get_weather_forecast, get_market_prices, get_weather_brief, get_price_quote, compare_market_prices, get_price_trend, agmark_qna_answer, get_coords_for_location
from qna import get_answer_from_books, generate_advisory_answer
from ner_utils import extract_location_from_query
from translator import detect_language, translate_text, transliterate_to_latin, is_latin_script
except ImportError as e:
print(f"Error importing modules: {e}")
exit()
# --- Optional: Whisper ASR and gTTS (lazy-loaded) ---
whisper_model = None
faster_whisper_model = None
def _transcribe_file(tmp_path: str, lang: str | None = "auto") -> str:
global whisper_model, faster_whisper_model
# Try Whisper (may fail with NumPy/Numba mismatch)
try:
import whisper
if whisper_model is None:
whisper_model = whisper.load_model("small")
kw = {"fp16": False}
if lang and lang != "auto":
kw["language"] = lang
result = whisper_model.transcribe(tmp_path, **kw)
return (result.get('text') or '').strip()
except Exception as e:
print(f"Whisper init/usage failed: {e}")
# Fallback: faster-whisper (no Numba dependency)
try:
from faster_whisper import WhisperModel
if faster_whisper_model is None:
faster_whisper_model = WhisperModel("small", device="cpu", compute_type="int8")
kwargs = {"task": "transcribe"}
if lang and lang != "auto":
kwargs = {"language": lang, "task": "transcribe"}
segments, info = faster_whisper_model.transcribe(tmp_path, **kwargs)
text = " ".join([seg.text for seg in segments])
# Normalize language to hi/en when auto-detected other scripts (e.g., Urdu)
try:
detected = getattr(info, 'language', None)
if detected not in ('hi', 'en'):
if any('\u0900' <= ch <= '\u097f' for ch in text):
detected = 'hi'
else:
detected = 'en'
except Exception:
pass
return text.strip()
except Exception as e:
print(f"faster-whisper failed: {e}")
return ""
tts_model = None
async def _tts_bytes_async(text: str, voice: str = 'en-IN-NeerjaNeural') -> bytes:
"""Generate TTS audio using Edge TTS (Indian voices), returns MP3 bytes."""
try:
import edge_tts
communicate = edge_tts.Communicate(text=text, voice=voice)
audio_bytes = bytearray()
async for chunk in communicate.stream():
if chunk["type"] == "audio":
audio_bytes.extend(chunk["data"])
return bytes(audio_bytes)
except Exception as e:
print(f"Edge TTS error: {e}")
return b""
# --- In-Memory Storage (for Hackathon) ---
user_profiles = {}
user_alerts = {}
onboarding_sessions = {}
from ner_utils import extract_location_from_query
def detect_intent_nlp(q: str):
"""
Smart intent detection that understands context and nuances
"""
ql = q.lower().strip()
# Smart growing cost detection
if any(word in ql for word in ['cost to grow', 'growing cost', 'cultivation cost', 'farm cost', 'production cost']):
return "growing_cost"
# Smart weather patterns with context
weather_keywords = ['rain', 'weather', 'forecast', 'temp', 'temperature', 'humidity', 'wind',
'sunny', 'cloudy', 'storm', 'hot', 'cold', 'warm', 'cool', 'dry', 'wet',
'frost', 'heat stress', 'et0', 'wind gusts']
if any(word in ql for word in weather_keywords):
return "weather"
# Smart market/price patterns with context
market_keywords = ['price', 'rate', 'modal', 'mandi', 'msp', 'bhav', 'cost', 'value', 'market',
'sell', 'buy', 'commodity', 'trend', 'arrival', 'liquidity']
if any(word in ql for word in market_keywords):
return "market"
# Smart agricultural decisions
agri_keywords = ['crop', 'farming', 'soil', 'fertilizer', 'pest', 'harvest', 'plant', 'seed',
'water', 'season', 'intercrop', 'variety', 'irrigation', 'spray', 'disease']
if any(word in ql for word in agri_keywords):
return "agriculture"
# Smart policy/scheme detection
policy_keywords = ['pm-kisan', 'kalia', 'rythu bandhu', 'pmfby', 'fasal bima', 'soil health card',
'subsidy', 'loan', 'kcc', 'e-nam', 'procurement', 'msp']
if any(word in ql for word in policy_keywords):
return "policy"
# Smart logistics/storage
logistics_keywords = ['sell now', 'store', 'harvest', 'cold storage', 'warehouse', 'logistics',
'timing', 'when to', 'best day', 'procurement window']
if any(word in ql for word in logistics_keywords):
return "logistics"
# Smart compliance/export
compliance_keywords = ['mrl', 'residue', 'export', 'certification', 'organic', 'grading', 'quality',
'compliance', 'penalty', 'pesticide']
if any(word in ql for word in compliance_keywords):
return "compliance"
return "general"
def extract_commodity_from_text(q: str):
"""
Smart commodity extraction that understands context and handles typos
"""
# Enhanced patterns for better coverage
patterns = [
r"(?:price|rate|bhav|cost)\s+of\s+([a-z\s]+?)(?:\s+in\b|$)",
r"([a-z\s]+)\s+(?:price|rate|bhav|cost)\b",
r"(?:what|how much)\s+(?:is|are)\s+(?:the\s+)?(?:price|rate|bhav|cost)\s+of\s+([a-z\s]+)",
r"(?:price|rate|bhav|cost)\s+(?:of|for)\s+([a-z\s]+)",
r"([a-z\s]+)\s+(?:price|rate|bhav|cost)\s+(?:in|at|for)",
r"(?:market\s+)?prices?\s+(?:for|of)\s+([a-z\s]+?)(?:\s+in\b|$)",
r"([a-z\s]+)\s+(?:in|at|for)\s+[a-z\s]+(?:price|rate|bhav|cost)",
r"(?:price|rate|bhav|cost)\s+([a-z\s]+)\s+in",
r"([a-z\s]+)\s+(?:price|rate|bhav|cost)\s+in",
# Growing cost patterns
r"(?:cost|expense)\s+to\s+grow\s+([a-z\s]+)",
r"(?:growing|cultivation|production)\s+cost\s+of\s+([a-z\s]+)",
r"([a-z\s]+)\s+(?:growing|cultivation|production)\s+cost"
]
for pattern in patterns:
m = re.search(pattern, q, flags=re.IGNORECASE)
if m:
commodity = m.group(1).strip()
# Clean up common words that aren't commodities
commodity = re.sub(r'\b(in|at|for|the|a|an|is|are|what|how|much|does|cost|price|of|market|prices|grow|growing|cultivation|production)\b', '', commodity, flags=re.IGNORECASE).strip()
if commodity and len(commodity) > 2:
print(f"Extracted commodity: '{commodity}' from pattern: {pattern}")
return commodity
# Fallback: look for common agricultural commodities in the query with typo handling
common_commodities = [
'rice', 'wheat', 'maize', 'corn', 'potato', 'tomato', 'tomatoes', 'onion', 'garlic', 'ginger',
'turmeric', 'chilli', 'pepper', 'cardamom', 'cinnamon', 'clove', 'nutmeg',
'cotton', 'jute', 'sugarcane', 'tea', 'coffee', 'cocoa', 'rubber',
'pulses', 'lentils', 'chickpea', 'chikpea', 'pigeon pea', 'mung bean', 'black gram',
'oilseeds', 'mustard', 'sesame', 'sunflower', 'groundnut', 'soybean',
'fruits', 'apple', 'banana', 'orange', 'mango', 'grapes', 'papaya',
'vegetables', 'carrot', 'cabbage', 'cauliflower', 'brinjal', 'cucumber',
'basmati', 'groundnut', 'bajra', 'berseem', 'oats', 'okra'
]
# Typo correction mapping
typo_corrections = {
'chikpea': 'chickpea',
'chana': 'chickpea',
'dal': 'pulses',
'dhal': 'pulses',
'bajra': 'pearl millet',
'jowar': 'sorghum',
'ragi': 'finger millet'
}
q_lower = q.lower()
# First check for exact matches
for commodity in common_commodities:
if commodity in q_lower:
print(f"Found commodity in fallback: {commodity}")
return commodity
# Then check for typos and correct them
for typo, correct in typo_corrections.items():
if typo in q_lower:
print(f"Corrected typo: {typo} -> {correct}")
return correct
# Finally, look for partial matches
for commodity in common_commodities:
if len(commodity) > 3 and commodity in q_lower:
print(f"Found commodity in partial match: {commodity}")
return commodity
return None
def extract_growing_cost_context(query: str):
"""
Extract context for growing cost queries
"""
context = {}
# Extract land size
land_match = re.search(r'(\d+(?:\.\d+)?)\s*(?:acres?|hectares?|ha)', query, re.IGNORECASE)
if land_match:
context['land_size'] = land_match.group(1)
# Extract location if mentioned
location = extract_location_from_query(query)
if location:
context['location'] = location
# Extract crop type
crop = extract_commodity_from_text(query)
if crop:
context['crop'] = crop
return context
# --- Proactive Alerting Logic ---
def check_for_personalized_alerts():
print(f"\n--- Running scheduled alert check at {datetime.datetime.now()} ---")
for user_id, profile in list(user_profiles.items()):
location = profile.get("location")
if not location or not profile.get("profileComplete"):
continue
print(f"Checking alerts for user {user_id} in {location}...")
weather_context = get_weather_forecast(location)
# Ask LLM to always provide 2-3 concise suggestions when any alert/risk exists
alert_prompt = (
f"Analyze this weather data for {location}.\n"
"Return: one 'ALERT: ' line if any risk, then 3 items prefixed with 'SUGGESTION: ', each 2-3 lines only (<=250 chars), no markdown, bullets, asterisks, hashtags, or emojis.\n"
"If no risk, still provide 3 'SUGGESTION: ' items in 2-3 lines.\n\n"
f"Data:\n{weather_context}"
)
response_text = generate_advisory_answer(alert_prompt)
try:
def _sanitize_line(s: str) -> str:
s = s.replace("\u200b", " ")
for ch in ["*", "#", "`", ">"]:
s = s.replace(ch, "")
return " ".join(s.split())
lines = [ln.strip() for ln in response_text.splitlines() if ln.strip()]
alert_line = next((ln for ln in lines if ln.lower().startswith("alert:")), None)
suggestion_lines = [ln for ln in lines if ln.lower().startswith("suggestion:")]
# Sanitize and convert to one-liners, keep max 3
suggestion_lines = [ _sanitize_line(ln).replace("SUGGESTION:", "").strip() for ln in suggestion_lines ]
suggestion_lines = [ s for s in suggestion_lines if s ]
if len(suggestion_lines) < 3:
# Fallback minimal safe suggestions
suggestion_lines += [
"Plan field work in cooler hours; avoid midday heat.",
"Secure harvested produce; check drainage before rain.",
"Review irrigation schedule based on latest forecast."
]
suggestion_lines = suggestion_lines[:3]
if user_id not in user_alerts:
user_alerts[user_id] = []
if alert_line or suggestion_lines:
user_alerts[user_id].insert(0, {
"id": str(uuid.uuid4()),
"alert": _sanitize_line((alert_line or "ALERT: General advisory")),
"suggestions": suggestion_lines,
"status": "new",
"timestamp": datetime.datetime.now().isoformat()
})
print(f"SUCCESS: Alert generated for user {user_id} with {len(suggestion_lines)} suggestion(s).")
except Exception as e:
print(f"Error parsing LLM alert response for user {user_id}: {e}")
# Secondary: Government schemes and programs based on profile
try:
scheme_prompt = (
"Based on this farmer profile, list 3 relevant CURRENT Indian government schemes or programs (central/state). "
"Each must be returned as a 'SUGGESTION: ' line in 2-3 lines only (<=250 chars), no markdown, bullets, asterisks, hashtags, or emojis, with one concrete next step.\n\n"
f"Profile: {profile}\n"
"Fields: location (state), land size, age, gender, crops."
)
scheme_text = generate_advisory_answer(scheme_prompt)
scheme_lines = [ln.strip() for ln in scheme_text.splitlines() if ln.strip().lower().startswith("suggestion:")]
if scheme_lines:
scheme_lines = [ _sanitize_line(ln).replace("SUGGESTION:", "").strip() for ln in scheme_lines ]
scheme_lines = [ s for s in scheme_lines if s ]
if len(scheme_lines) < 3:
scheme_lines += [
"Check PM-KISAN eligibility and update eKYC if pending.",
"Explore local crop insurance under PMFBY before sowing.",
"Visit nearest KVK for input subsidy or advisory schedule."
]
scheme_lines = scheme_lines[:3]
if user_id not in user_alerts:
user_alerts[user_id] = []
user_alerts[user_id].insert(0, {
"id": str(uuid.uuid4()),
"alert": "ALERT: Updates on applicable schemes",
"suggestions": scheme_lines,
"status": "new",
"timestamp": datetime.datetime.now().isoformat()
})
print(f"SCHEMES: Added {len(scheme_lines)} scheme suggestions for {user_id}.")
except Exception as e:
print(f"Scheme suggestion error for user {user_id}: {e}")
# --- FastAPI App Lifecycle (for Scheduler) ---
scheduler = AsyncIOScheduler()
@asynccontextmanager
async def lifespan(app: FastAPI):
scheduler.add_job(check_for_personalized_alerts, 'interval', hours=1)
scheduler.start()
yield
scheduler.shutdown()
# Manual trigger to generate alerts immediately (defined after app initialization)
# --- Initialize FastAPI App ---
app = FastAPI(
title="Agroculture Agent",
version="3.3.0", # Final fix version
lifespan=lifespan
)
# --- Add CORS Middleware ---
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:8000", "http://127.0.0.1:8000", "http://localhost:5173"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Manual trigger to generate alerts immediately
@app.post("/alerts/run-now", summary="Trigger alert generation immediately and return latest alerts")
async def run_alerts_now(user_id: str):
check_for_personalized_alerts()
return {"data": user_alerts.get(user_id, [])}
# --- Pydantic Models for Request Bodies ---
class ChatMessage(BaseModel):
message: str
class AskRequest(BaseModel):
user_id: str
query: str
# --- API Endpoints ---
@app.get("/status", summary="Check user's onboarding status")
async def get_user_status(user_id: str):
if user_id in user_profiles and user_profiles[user_id].get("profileComplete"):
return {"status": "profile_complete"}
else:
return {"status": "new_user"}
@app.post("/chat", summary="Handle the onboarding conversation")
async def onboarding_chat(user_id: str, request: ChatMessage):
message = request.message
if user_id not in onboarding_sessions:
onboarding_sessions[user_id] = {"stage": "asking_location", "profile": {}}
session = onboarding_sessions[user_id]
stage = session["stage"]
if stage == "asking_location":
session["stage"] = "asking_land_size"
return {"response": "Welcome! To get started, please tell me your location (city or district)."}
elif stage == "asking_land_size":
session["profile"]["location"] = message
session["stage"] = "asking_budget"
return {"response": f"Got it, {message}. How many acres of land do you have? (e.g., '5 acres', 'NA')"}
elif stage == "asking_budget":
session["profile"]["land_size"] = message
session["stage"] = "asking_age_gender"
return {"response": "Understood. What is your approximate budget for this season? (e.g., '50000 rupees', 'NA')"}
elif stage == "asking_age_gender":
session["profile"]["budget"] = message
session["stage"] = "asking_crops"
return {"response": "Thanks. What is your age and gender?"}
elif stage == "asking_crops":
session["profile"]["age"] = ''.join(filter(str.isdigit, message))
session["profile"]["gender"] = "female" if "female" in message.lower() else "male"
session["stage"] = "generating_recommendation"
return {"response": "Almost done. What are you currently growing, or have you not planned yet?"}
elif stage == "generating_recommendation":
session["profile"]["current_crops"] = message
user_profiles[user_id] = {**session["profile"], "profileComplete": True, "email": user_id}
del onboarding_sessions[user_id]
return {"response": "Thank you! Your profile is now complete."}
return {"response": "I'm sorry, something went wrong during setup."}
@app.get("/get-suggestion", summary="Get a timely, on-demand suggestion")
async def get_suggestion(user_id: str, category: str | None = None):
if user_id not in user_profiles or not user_profiles[user_id].get("profileComplete"):
return {"suggestion": "Your personalized suggestions will appear here once your profile is complete."}
profile = user_profiles[user_id]
weather_context = get_weather_forecast(profile['location'])
# Tailor the prompt based on requested category
cat = (category or "general").lower()
if cat == "crop":
focus = "Focus on CROP choices, varieties, sowing window, and pest/disease vigilance."
elif cat == "land":
focus = "Focus on LAND preparation: soil testing, residue management, irrigation scheduling, mulching."
elif cat == "budget":
focus = "Focus on BUDGET optimization: input savings, subsidies/schemes, and ROI-first actions."
else:
focus = "Provide the most useful action for this farmer right now."
suggestion_prompt = (
"You are a concise agricultural assistant. "
f"{focus} Provide exactly ONE actionable suggestion in 2-3 lines only (<=250 chars). "
"Avoid markdown, bullets, asterisks, hashtags, or emojis. Keep it practical and specific.\n\n"
f"Profile:\n{profile}\n\nWeather:\n{weather_context}\n\nSuggestion (2-3 lines, no markdown):"
)
suggestion = generate_advisory_answer(suggestion_prompt)
return {"suggestion": suggestion}
@app.get("/alerts", summary="Get personalized alerts and suggestions")
async def get_alerts(user_id: str):
data = user_alerts.get(user_id, [])
if not data:
# Provide a minimal fallback alert with 3 suggestions, sanitized
fallback = {
"id": str(uuid.uuid4()),
"alert": "General advisory",
"suggestions": [
"Plan field work in cooler hours; avoid midday heat.",
"Secure harvested produce; check drainage before rain.",
"Review irrigation schedule based on latest forecast."
],
"status": "new",
"timestamp": datetime.datetime.now().isoformat()
}
return {"data": [fallback]}
return {"data": data}
@app.post("/apply-suggestion", summary="Mark a suggestion as applied")
async def apply_suggestion(user_id: str, suggestion_id: str):
if user_id in user_alerts:
for item in user_alerts[user_id]:
if item["id"] == suggestion_id:
item["status"] = "applied"
return {"message": "Suggestion status updated."}
raise HTTPException(status_code=404, detail="Suggestion or User ID not found.")
from data_sources import (
get_weather_brief,
get_market_prices_smart,
AGMARKNET_API_KEY,
)
@app.post("/ask", summary="Ask a context-aware question")
async def ask_question(request: AskRequest):
user_id = request.user_id
query = request.query.strip()
profile = user_profiles.get(user_id, {})
# Enhanced location extraction with better pincode handling
place_mention = extract_location_from_query(query)
# Prefer user profile location if extraction fails or returns a generic/noisy token
if not place_mention or place_mention.lower() in {"such", "budget", "profit", "crops", "crop"}:
place_mention = profile.get("location")
print(f"Extracted location: {place_mention} from query: {query}")
intent = detect_intent_nlp(query)
print(f"Detected intent: {intent} for query: {query}")
# Handle growing cost queries intelligently
if intent == "growing_cost":
context = extract_growing_cost_context(query)
crop = context.get('crop', 'rice')
location = place_mention or profile.get("location") or "India"
growing_cost_prompt = f"""
Provide a concise, practical estimate of the cost to grow {crop} in {location}.
Include: seed cost, fertilizer, pesticides, labor, and total per acre.
Format: 2-3 bullet points with actual cost estimates.
If specific data unavailable, provide reasonable estimates based on {location} conditions.
"""
answer, _ = get_answer_from_books(growing_cost_prompt)
return {"answer": answer}
# Handle weather queries with context
if intent == "weather":
place = place_mention or profile.get("location") or "Jaipur"
print(f"Fetching weather for: {place}")
# Fetch compact, structured forecast context (no hardcoded replies)
context = get_weather_forecast(place)
prompt = (
"You are a concise weather assistant for farmers.\n"
"Use ONLY the provided weather data context to answer the user's exact question.\n"
"Rules:\n"
"- If asked 'will it rain tomorrow', answer Yes/No with probability if present.\n"
"- If asked for a metric (humidity, wind, temperature), reply with just the number and unit if known.\n"
"- Mention the day (today/tomorrow) only if needed.\n"
"- Do NOT add extra details or tips.\n"
"- If the data does not include the requested value, say 'Data not available'.\n\n"
f"Weather data context:\n{context}\n\n"
f"Question: {query}\n"
"Answer succinctly in one or two sentences maximum."
)
ans = generate_advisory_answer(prompt)
return {"answer": ans}
# Handle market/price queries intelligently
if intent == "market":
# Delegate to Agmark QnA workflow end-to-end
place = place_mention or profile.get("location")
# We pass user_profile to help resolve scope if needed
answer = agmark_qna_answer(query, user_profile=profile if profile else {"location": place})
return {"answer": answer}
# Handle agricultural decisions intelligently
if intent == "agriculture":
if "vs" in query.lower() or "comparison" in query.lower():
comparison_prompt = f"Provide a smart comparison for this agricultural decision: {query}. Include pros/cons and recommendation based on {place_mention or 'your location'}."
answer, _ = get_answer_from_books(comparison_prompt)
return {"answer": answer}
elif "when to" in query.lower() or "timing" in query.lower():
timing_prompt = f"Provide optimal timing advice for this agricultural activity: {query}. Consider weather, season, and best practices."
answer, _ = get_answer_from_books(timing_prompt)
return {"answer": answer}
else:
agri_prompt = f"Provide smart, actionable agricultural advice for: {query}. Consider location: {place_mention or 'your area'}. Keep it practical and specific."
answer, _ = get_answer_from_books(agri_prompt)
return {"answer": answer}
# Handle policy/scheme queries
if intent == "policy":
policy_prompt = f"""
Answer this policy/scheme question intelligently: {query}
User Profile:
- Location: {profile.get('location', 'N/A')}
- Land Size: {profile.get('land_size', 'N/A')}
- Age: {profile.get('age', 'N/A')}
- Gender: {profile.get('gender', 'N/A')}
Provide: eligibility status (yes/no), key requirements, and next steps.
Format: 2-3 bullet points maximum.
"""
answer, _ = get_answer_from_books(policy_prompt)
return {"answer": answer}
# Handle logistics/storage queries
if intent == "logistics":
logistics_prompt = f"""
Provide smart logistics advice for: {query}
Consider: timing, market conditions, storage options, and cost-benefit analysis.
Give specific, actionable recommendations.
"""
answer, _ = get_answer_from_books(logistics_prompt)
return {"answer": answer}
# Handle compliance/export queries
if intent == "compliance":
compliance_prompt = f"""
Answer this compliance/export question: {query}
Provide: requirements, steps, costs, and timeline.
Keep it practical and actionable.
"""
answer, _ = get_answer_from_books(compliance_prompt)
return {"answer": answer}
# General questions - try to be helpful and smart
if not user_id or user_id not in user_profiles:
general_prompt = f"""
Answer this question intelligently: {query}
If it's about agriculture, farming, or rural development, provide practical advice.
If it's about weather, markets, or policies, be specific and actionable.
Keep response to 2-3 sentences maximum.
"""
answer, _ = get_answer_from_books(general_prompt)
return {"answer": answer}
# For users with profiles, provide contextual answers
contextual_prompt = f"""
Answer this question intelligently and contextually: {query}
User Profile:
- Location: {profile.get('location','N/A')}
- Land Size: {profile.get('land_size','N/A')}
- Budget: {profile.get('budget','N/A')}
- Age: {profile.get('age','N/A')}
- Gender: {profile.get('gender','N/A')}
- Current Crops: {profile.get('current_crops','N/A')}
Provide smart, actionable advice considering their profile.
If agricultural question, be location-specific and practical.
Keep response to 2-3 sentences maximum.
"""
answer, _ = get_answer_from_books(contextual_prompt)
return {"answer": answer}
# --- Crop Planting Decision ---
class PlantDecisionRequest(BaseModel):
crop: str | None = None
user_id: str | None = None
location: str | None = None
@app.post("/plan/plant", summary="Return a go/no-go planting decision based on next-day weather")
async def plant_decision(req: PlantDecisionRequest):
profile = user_profiles.get(req.user_id or "", {})
place = req.location or profile.get("location") or "Jaipur"
context = get_weather_forecast(place)
crop = (req.crop or profile.get("current_crops") or "crop").strip()
prompt = (
"You are an agronomy assistant. Using ONLY the weather context below, decide if it is suitable to PLANT the specified crop in the next 24-48 hours.\n"
"Reply in strict JSON with keys: decision ('Plant'|'Wait'), reason (<=140 chars).\n"
f"Crop: {crop}\nLocation: {place}\n\nWeather Context:\n{context}\n\nJSON:"
)
text = generate_advisory_answer(prompt)
try:
import json as _json
js = _json.loads(text)
decision = js.get("decision") or "Wait"
reason = js.get("reason") or "Insufficient data."
return {"decision": decision, "reason": reason}
except Exception:
return {"decision": "Wait", "reason": "Could not parse decision."}
# --- Weather Summary for Dashboard ---
@app.get("/weather_summary", summary="Get compact weather metrics for dashboard")
async def weather_summary(user_id: str | None = None, location: str | None = None):
"""Return today's compact weather metrics for a location.
Prefers explicit location, else user's profile location, else Jaipur.
"""
try:
place = (location or (user_profiles.get(user_id or "", {}).get("location") if user_id else None) or "Jaipur")
coords = get_coords_for_location(place)
if not coords:
raise HTTPException(status_code=400, detail="Could not resolve location")
lat, lon = coords["lat"], coords["lon"]
import requests
api = "https://api.open-meteo.com/v1/forecast"
daily = (
"precipitation_sum,precipitation_probability_max,temperature_2m_max,"
"temperature_2m_min,relative_humidity_2m_mean,windspeed_10m_max"
)
r = requests.get(f"{api}?latitude={lat}&longitude={lon}&daily={daily}&timezone=Asia/Kolkata", timeout=12)
r.raise_for_status()
d = r.json().get("daily", {})
idx = 0 # today
def gv(key: str):
arr = d.get(key) or []
return arr[idx] if len(arr) > idx else None
res = {
"location": place,
"date": (d.get("time") or [None])[idx] if d.get("time") else None,
"tmin": gv("temperature_2m_min"),
"tmax": gv("temperature_2m_max"),
"humidity": gv("relative_humidity_2m_mean"),
"rain_probability": gv("precipitation_probability_max"),
"rain_sum_mm": gv("precipitation_sum"),
"wind_max_kmh": gv("windspeed_10m_max"),
}
return res
except HTTPException:
raise
except Exception as e:
print(f"weather_summary failed: {e}")
raise HTTPException(status_code=500, detail="Weather summary failed")
# ================== Voice Support Endpoints ==================
class VoiceAskResponse(BaseModel):
answer: str
audio_b64: str | None = None
@app.post("/voice/transcribe", summary="Transcribe audio to text (Whisper/faster-whisper)")
async def transcribe_audio(file: UploadFile = File(...), lang: str | None = "auto"):
try:
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename or '')[-1] or '.wav') as tmp:
data = await file.read()
tmp.write(data)
tmp_path = tmp.name
text = _transcribe_file(tmp_path, lang=lang)
os.unlink(tmp_path)
if not text:
raise RuntimeError("Empty transcription")
return {"text": text}
except Exception as e:
print(f"Transcription error: {e}")
raise HTTPException(status_code=500, detail="Failed to transcribe audio")
@app.post("/voice/ask", response_model=VoiceAskResponse, summary="Ask via audio and get TTS reply")
async def voice_ask(file: UploadFile = File(...), user_id: str | None = None, lang: str | None = "auto"):
# 1) Transcribe (multilingual auto by default)
try:
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename or '')[-1] or '.wav') as tmp:
data = await file.read()
tmp.write(data)
tmp_path = tmp.name
query_text = _transcribe_file(tmp_path, lang=lang)
os.unlink(tmp_path)
except Exception as e:
print(f"Voice ask transcription error: {e}")
query_text = ""
if not query_text:
raise HTTPException(status_code=400, detail="No speech detected")
# 2) Route into existing pipeline (/ask logic) by calling ask_question internals
req = AskRequest(user_id=user_id or "voice_user", query=query_text)
answer_json = await ask_question(req)
answer_text = answer_json.get("answer") or ""
# 3) TTS (Indian voice)
audio_bytes = await _tts_bytes_async(answer_text, voice='en-IN-NeerjaNeural')
audio_b64 = base64.b64encode(audio_bytes).decode("utf-8") if audio_bytes else None
return VoiceAskResponse(answer=answer_text, audio_b64=audio_b64)
class TtsRequest(BaseModel):
text: str
language: str | None = None
@app.post("/tts", summary="Convert text to speech (Edge TTS en-IN/hi-IN)")
async def tts_endpoint(req: TtsRequest):
if not req.text:
raise HTTPException(status_code=400, detail="Missing text")
# Choose Indian voice based on requested language; default to Hindi if Devanagari is present
text = req.text
req_lang = (req.language or 'en').lower()
if any('\u0900' <= ch <= '\u097f' for ch in text):
req_lang = 'hi'
voice = 'hi-IN-SwaraNeural' if req_lang.startswith('hi') else 'en-IN-NeerjaNeural'
audio_bytes = await _tts_bytes_async(text, voice=voice)
if not audio_bytes:
raise HTTPException(status_code=500, detail="TTS failed")
audio_b64 = base64.b64encode(audio_bytes).decode("utf-8")
return {"audio_b64": audio_b64}
if __name__ == "__main__":
import uvicorn
print("🚀 Starting Agroculture Chatbot Server...")
print("📱 Server will be available at: http://127.0.0.1:8000")
print("🔧 API Documentation at: http://127.0.0.1:8000/docs")
uvicorn.run(app, host="127.0.0.1", port=8000)