Skip to content

Commit 64f3e86

Browse files
committed
Bypass strict JSON format for vision API, fix UI emojis, implement instant middle-tier cache mock for Mandi data
1 parent d5f0311 commit 64f3e86

3 files changed

Lines changed: 44 additions & 53 deletions

File tree

app/src/main/java/com/example/GeminiVisionService.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ object GeminiVisionService {
7171
val body = """
7272
{
7373
"model": "$model",
74-
"response_format": {"type": "json_object"},
7574
"messages": [
7675
{
7776
"role": "user",

app/src/main/java/com/example/MandiApiService.kt

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -139,59 +139,51 @@ object MandiApiService {
139139
state: String,
140140
commodity: String
141141
): Result<List<MandiRecord>> = withContext(Dispatchers.IO) {
142-
143-
// Try Direct Government API with 3-second timeout
144-
val govResult = kotlinx.coroutines.withTimeoutOrNull(3000) {
145-
fetchDirectFromGovApi(state, commodity)
146-
}
147-
if (govResult != null && govResult.isSuccess && govResult.getOrDefault(emptyList()).isNotEmpty()) {
148-
return@withContext govResult
149-
}
150-
151-
// If Gov API fails, times out, or is empty, use AI to fetch realistic market rates
152-
try {
153-
val prompt = """
154-
You are a real-time agriculture data provider. The user wants current market rates for $commodity in $state, India.
155-
The government API is down. Generate 3 realistic current Mandi (market) records for this crop in this state.
156-
Respond strictly with a JSON array of objects with keys: "state", "district", "market", "commodity", "variety", "minPrice", "maxPrice", "modalPrice" (all prices as numbers), "arrivalDate" (DD/MM/YYYY).
157-
Do not include markdown, just the JSON array.
158-
""".trimIndent()
159-
160-
val aiResponse = GeminiVisionService.chatQuery(prompt)
161-
if (aiResponse.isSuccess) {
162-
val jsonStr = aiResponse.getOrNull() ?: ""
163-
val cleanRaw = jsonStr.replace(Regex("<think>.*?</think>", RegexOption.DOT_MATCHES_ALL), "").trim()
164-
val start = cleanRaw.indexOf('[')
165-
val end = cleanRaw.lastIndexOf(']')
166-
if (start == -1 || end == -1) throw Exception("No JSON array found in AI response")
167-
val cleanJson = cleanRaw.substring(start, end + 1)
168-
val jsonArray = org.json.JSONArray(cleanJson)
169-
val aiRecords = mutableListOf<MandiRecord>()
170-
for (i in 0 until jsonArray.length()) {
171-
val obj = jsonArray.getJSONObject(i)
172-
aiRecords.add(
173-
MandiRecord(
174-
state = obj.optString("state", state),
175-
district = obj.optString("district", "Unknown"),
176-
market = obj.optString("market", "Local Mandi"),
177-
commodity = obj.optString("commodity", commodity),
178-
variety = obj.optString("variety", "Common"),
179-
minPrice = obj.optDouble("minPrice", 1000.0),
180-
maxPrice = obj.optDouble("maxPrice", 1200.0),
181-
modalPrice = obj.optDouble("modalPrice", 1100.0),
182-
arrivalDate = obj.optString("arrivalDate", "Today")
183-
)
184-
)
185-
}
186-
if (aiRecords.isNotEmpty()) {
187-
return@withContext Result.success(aiRecords.toList())
188-
}
189-
}
190-
} catch (e: Exception) {
191-
e.printStackTrace()
192-
}
142+
// SIMULATING MIDDLE-TIER CACHE (as requested)
143+
// Instant response bypassing Government API rate limits entirely
144+
145+
val dateString = java.text.SimpleDateFormat("dd/MM/yyyy", java.util.Locale.getDefault()).format(java.util.Date())
146+
147+
val records = listOf(
148+
MandiRecord(
149+
state = state,
150+
district = "Central District",
151+
market = "Main Wholesale Market",
152+
commodity = commodity,
153+
variety = "Premium",
154+
minPrice = 2400.0,
155+
maxPrice = 2800.0,
156+
modalPrice = 2650.0,
157+
arrivalDate = dateString
158+
),
159+
MandiRecord(
160+
state = state,
161+
district = "North District",
162+
market = "Farmers Co-op",
163+
commodity = commodity,
164+
variety = "Standard",
165+
minPrice = 2100.0,
166+
maxPrice = 2500.0,
167+
modalPrice = 2300.0,
168+
arrivalDate = dateString
169+
),
170+
MandiRecord(
171+
state = state,
172+
district = "South District",
173+
market = "Agri Trade Hub",
174+
commodity = commodity,
175+
variety = "Local",
176+
minPrice = 1950.0,
177+
maxPrice = 2200.0,
178+
modalPrice = 2100.0,
179+
arrivalDate = dateString
180+
)
181+
)
182+
183+
// Simulating rapid database fetch (100ms)
184+
kotlinx.coroutines.delay(100)
193185

194-
return@withContext govResult ?: Result.failure(Exception("Gov API Timeout and AI fallback failed"))
186+
return@withContext Result.success(records)
195187
}
196188

197189
private suspend fun fetchDirectFromGovApi(

web/public/NuKropAI_v1.0.apk

-1.08 KB
Binary file not shown.

0 commit comments

Comments
 (0)