bapi143 commited on
Commit
363acab
·
1 Parent(s): 8593ffe

support Gemini_API secret name and universal Google models

Browse files
Files changed (1) hide show
  1. backend/pipeline.py +34 -14
backend/pipeline.py CHANGED
@@ -44,17 +44,20 @@ from .models import DiagnosisResponse, AlternativeDiagnosis, TreatmentPlan
44
 
45
  logger = logging.getLogger("cropai.pipeline")
46
 
47
- # Cascading model priority list (optimized for sub-second response, headed by Gemini 3.8 Flash)
48
  CASCADING_MODELS = [
 
 
 
 
 
 
 
49
  "gemini-3.8-flash",
50
- "gemini-3.8-flash-lite",
51
- "gemini-3.8-flash-preview",
52
  "gemini-3.5-flash",
53
- "gemini-2.5-flash",
54
- "gemini-2.5-flash-lite",
55
- "gemini-flash-lite-latest",
56
  ]
57
 
 
58
  AGRONOMIC_SYSTEM_PROMPT = """
59
  You are an expert Agricultural Plant Pathologist, Senior Agronomist, and Crop Protection Specialist with deep expertise in Indian agriculture, especially Chhattisgarh and central/northern India.
60
  Inspect the provided agricultural image with high botanical precision and provide a complete diagnosis.
@@ -170,25 +173,45 @@ class PureGeminiInferenceEngine:
170
 
171
  @property
172
  def api_key(self) -> Optional[str]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  for var_name in [
 
 
174
  "GEMINI_API_KEY",
175
  "GOOGLE_API_KEY",
176
  "GEMINI_KEY",
177
  "gemini_api_key",
178
  "google_api_key",
179
  "gemini_key",
180
- "API_KEY"
181
  ]:
182
  val = os.getenv(var_name)
183
  if val:
184
  cleaned = val.strip().strip('"').strip("'").strip()
185
- if len(cleaned) > 10:
186
  return cleaned
187
 
188
  cfg_key = getattr(settings, "gemini_api_key", None)
189
  if cfg_key:
190
  cleaned = cfg_key.strip().strip('"').strip("'").strip()
191
- if len(cleaned) > 10:
192
  return cleaned
193
 
194
  return None
@@ -246,7 +269,7 @@ class PureGeminiInferenceEngine:
246
  {
247
  "text": (
248
  f"{AGRONOMIC_SYSTEM_PROMPT}\n\n"
249
- f"MULTI-PLATFORM AGRONOMIC GROUNDING CONTEXT (Gemini 3.8 Flash Vision):\n"
250
  f"- Grounded on 196,000+ benchmark image samples across Hugging Face (Project-AgML India, PlantDoc In-The-Wild, Jharkhand Plateau), ICAR-CRRI Cuttack, and IGKV Raipur registries.\n"
251
  f"- Evaluate foliar symptoms and pest injuries against these verified Indian agro-climatic standards.\n"
252
  )
@@ -263,10 +286,7 @@ class PureGeminiInferenceEngine:
263
  "generationConfig": {
264
  "temperature": 0.2,
265
  "maxOutputTokens": 2048,
266
- "responseMimeType": "application/json",
267
- "thinkingConfig": {
268
- "thinkingBudget": 0
269
- }
270
  }
271
  }
272
 
 
44
 
45
  logger = logging.getLogger("cropai.pipeline")
46
 
47
+ # Cascading model priority list (optimized for speed and high availability)
48
  CASCADING_MODELS = [
49
+ "gemini-2.5-flash",
50
+ "gemini-2.0-flash",
51
+ "gemini-2.0-flash-lite",
52
+ "gemini-1.5-flash",
53
+ "gemini-1.5-flash-8b",
54
+ "gemini-1.5-pro",
55
+ "gemini-2.5-pro",
56
  "gemini-3.8-flash",
 
 
57
  "gemini-3.5-flash",
 
 
 
58
  ]
59
 
60
+
61
  AGRONOMIC_SYSTEM_PROMPT = """
62
  You are an expert Agricultural Plant Pathologist, Senior Agronomist, and Crop Protection Specialist with deep expertise in Indian agriculture, especially Chhattisgarh and central/northern India.
63
  Inspect the provided agricultural image with high botanical precision and provide a complete diagnosis.
 
173
 
174
  @property
175
  def api_key(self) -> Optional[str]:
176
+ # 1. Check if any environment variable value starts with Google API prefix 'AIzaSy'
177
+ for k, v in os.environ.items():
178
+ if isinstance(v, str):
179
+ cleaned = v.strip().strip('"').strip("'").strip()
180
+ if cleaned.startswith("AIzaSy") and len(cleaned) >= 30:
181
+ return cleaned
182
+
183
+ # 2. Case-insensitive check across all environment variable names
184
+ for k, v in os.environ.items():
185
+ if not isinstance(v, str):
186
+ continue
187
+ k_lower = k.lower().replace("-", "_")
188
+ if any(term in k_lower for term in ["gemini", "google_api"]):
189
+ cleaned = v.strip().strip('"').strip("'").strip()
190
+ if len(cleaned) > 10 and not cleaned.startswith("hf_"):
191
+ return cleaned
192
+
193
+ # 3. Explicit aliases (including exact names like Gemini_API from Hugging Face Space secrets)
194
  for var_name in [
195
+ "Gemini_API",
196
+ "GEMINI_API",
197
  "GEMINI_API_KEY",
198
  "GOOGLE_API_KEY",
199
  "GEMINI_KEY",
200
  "gemini_api_key",
201
  "google_api_key",
202
  "gemini_key",
203
+ "API_KEY",
204
  ]:
205
  val = os.getenv(var_name)
206
  if val:
207
  cleaned = val.strip().strip('"').strip("'").strip()
208
+ if len(cleaned) > 10 and not cleaned.startswith("hf_"):
209
  return cleaned
210
 
211
  cfg_key = getattr(settings, "gemini_api_key", None)
212
  if cfg_key:
213
  cleaned = cfg_key.strip().strip('"').strip("'").strip()
214
+ if len(cleaned) > 10 and not cleaned.startswith("hf_"):
215
  return cleaned
216
 
217
  return None
 
269
  {
270
  "text": (
271
  f"{AGRONOMIC_SYSTEM_PROMPT}\n\n"
272
+ f"MULTI-PLATFORM AGRONOMIC GROUNDING CONTEXT (Gemini Vision):\n"
273
  f"- Grounded on 196,000+ benchmark image samples across Hugging Face (Project-AgML India, PlantDoc In-The-Wild, Jharkhand Plateau), ICAR-CRRI Cuttack, and IGKV Raipur registries.\n"
274
  f"- Evaluate foliar symptoms and pest injuries against these verified Indian agro-climatic standards.\n"
275
  )
 
286
  "generationConfig": {
287
  "temperature": 0.2,
288
  "maxOutputTokens": 2048,
289
+ "responseMimeType": "application/json"
 
 
 
290
  }
291
  }
292