Eng-Musa commited on
Commit
ec13069
·
1 Parent(s): 65a2e47

add cv processing fallback

Browse files
config.py CHANGED
@@ -16,6 +16,7 @@ load_dotenv(dotenv_path=env_path)
16
  # Google Gemini
17
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "")
18
  GEMINI_MODEL = "gemini-3.5-flash-lite"
 
19
 
20
  MAX_OUTPUT_TOKENS = 4096
21
  TEMPERATURE = 0.1
 
16
  # Google Gemini
17
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "")
18
  GEMINI_MODEL = "gemini-3.5-flash-lite"
19
+ GEMINI_FALLBACK_MODEL = "gemini-3.1-flash-lite"
20
 
21
  MAX_OUTPUT_TOKENS = 4096
22
  TEMPERATURE = 0.1
requirements.txt CHANGED
@@ -17,6 +17,7 @@ pypandoc
17
  pandoc
18
 
19
  # V2 — lightweight PDF extraction (no ML models)
 
20
  pymupdf4llm
21
 
22
  # V2 — local LLM inference + model download
 
17
  pandoc
18
 
19
  # V2 — lightweight PDF extraction (no ML models)
20
+ pymupdf
21
  pymupdf4llm
22
 
23
  # V2 — local LLM inference + model download
services/new/cv_markdown_processor.py CHANGED
@@ -40,7 +40,7 @@ class CvMarkdownProcessor:
40
  return finalize_v2_cv_payload(
41
  markdown=markdown,
42
  extracted=extracted,
43
- model_used=GEMINI_MODEL,
44
  file_type="markdown",
45
  is_scanned=False,
46
  page_count=1,
 
40
  return finalize_v2_cv_payload(
41
  markdown=markdown,
42
  extracted=extracted,
43
+ model_used=extracted.get("model_used", GEMINI_MODEL),
44
  file_type="markdown",
45
  is_scanned=False,
46
  page_count=1,
services/new/cv_processor_v2.py CHANGED
@@ -69,7 +69,7 @@ class CvProcessorV2:
69
  return finalize_v2_cv_payload(
70
  markdown=markdown,
71
  extracted=extracted,
72
- model_used=GEMINI_MODEL,
73
  file_type=conversion.file_type,
74
  is_scanned=conversion.is_scanned,
75
  page_count=conversion.page_count,
 
69
  return finalize_v2_cv_payload(
70
  markdown=markdown,
71
  extracted=extracted,
72
+ model_used=extracted.get("model_used", GEMINI_MODEL),
73
  file_type=conversion.file_type,
74
  is_scanned=conversion.is_scanned,
75
  page_count=conversion.page_count,
services/new/google/gemini_extractor.py CHANGED
@@ -16,6 +16,7 @@ import httpx
16
 
17
  from config import (
18
  GEMINI_API_KEY,
 
19
  GEMINI_MODEL,
20
  MAX_OUTPUT_TOKENS,
21
  TEMPERATURE,
@@ -119,14 +120,80 @@ CV MARKDOWN:
119
 
120
 
121
  class GeminiCvExtractor:
122
- """Directly calls Google Gemini Interactions API without any fallback."""
123
-
124
- def __init__(self, api_key: Optional[str] = None, model: Optional[str] = None):
 
 
 
 
 
125
  self.api_key = api_key or GEMINI_API_KEY
126
  self.model = model or GEMINI_MODEL
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
  def _call_interactions(self, prompt: str, system_instruction: str = "") -> dict:
129
- """Call /v1beta/interactions with 3 retries on 503 or transient errors."""
130
  if not self.api_key or self.api_key == "YOUR_GEMINI_API_KEY_HERE":
131
  raise ValueError("GEMINI_API_KEY is not set in processor/.env")
132
 
@@ -135,53 +202,34 @@ class GeminiCvExtractor:
135
  "x-goog-api-key": self.api_key,
136
  "Content-Type": "application/json",
137
  }
138
- body: Dict[str, Any] = {
139
- "model": self.model,
140
- "input": prompt,
141
- }
142
- if system_instruction:
143
- body["system_instruction"] = system_instruction
144
 
145
- max_retries = 3
146
- last_error = ""
 
147
 
 
148
  with httpx.Client(timeout=60.0) as client:
149
- for attempt in range(1, max_retries + 1):
150
  try:
151
- resp = client.post(url, headers=headers, json=body)
152
- if resp.status_code == 200:
153
- data = resp.json()
154
- reply = ""
155
- for step in data.get("steps", []):
156
- for item in step.get("content", []):
157
- if item.get("type") == "text":
158
- reply += item.get("text", "")
159
-
160
- return {
161
- "text": reply.strip(),
162
- "usage": data.get("usage", {}),
163
- }
164
-
165
- last_error = f"Gemini Interactions API error ({resp.status_code}): {resp.text}"
166
- if resp.status_code in (429, 500, 502, 503, 504) and attempt < max_retries:
167
- sleep_time = attempt * 1.5
168
  logger.warning(
169
- "Gemini attempt %d/%d failed (%d). Retrying in %.1fs...",
170
- attempt, max_retries, resp.status_code, sleep_time
 
 
171
  )
172
- time.sleep(sleep_time)
173
- continue
174
- else:
175
- raise RuntimeError(last_error)
176
-
177
- except httpx.RequestError as exc:
178
- last_error = f"Network error during Gemini call: {exc}"
179
- if attempt < max_retries:
180
- time.sleep(attempt * 1.5)
181
- continue
182
- raise RuntimeError(last_error)
183
 
184
- raise RuntimeError(last_error or "Gemini API failed after 3 retries.")
185
 
186
  def ping(self, prompt: str = "Say hello in one short sentence.") -> Dict[str, Any]:
187
  """Test API key and latency directly."""
@@ -205,6 +253,7 @@ class GeminiCvExtractor:
205
  data["completeness_score"] = self._completeness(data)
206
  data["markdown"] = markdown
207
  data["usage_metadata"] = res.get("usage", {})
 
208
  return data
209
 
210
  @staticmethod
 
16
 
17
  from config import (
18
  GEMINI_API_KEY,
19
+ GEMINI_FALLBACK_MODEL,
20
  GEMINI_MODEL,
21
  MAX_OUTPUT_TOKENS,
22
  TEMPERATURE,
 
120
 
121
 
122
  class GeminiCvExtractor:
123
+ """Calls Google Gemini Interactions API with model-level fallback."""
124
+
125
+ def __init__(
126
+ self,
127
+ api_key: Optional[str] = None,
128
+ model: Optional[str] = None,
129
+ fallback_model: Optional[str] = None,
130
+ ):
131
  self.api_key = api_key or GEMINI_API_KEY
132
  self.model = model or GEMINI_MODEL
133
+ self.fallback_model = (
134
+ fallback_model if fallback_model is not None else GEMINI_FALLBACK_MODEL
135
+ )
136
+
137
+ def _call_model(
138
+ self,
139
+ client: httpx.Client,
140
+ url: str,
141
+ headers: Dict[str, str],
142
+ model_name: str,
143
+ prompt: str,
144
+ system_instruction: str = "",
145
+ ) -> dict:
146
+ """Call /v1beta/interactions for a single model with up to 2 attempts."""
147
+ body: Dict[str, Any] = {
148
+ "model": model_name,
149
+ "input": prompt,
150
+ }
151
+ if system_instruction:
152
+ body["system_instruction"] = system_instruction
153
+
154
+ max_retries = 2
155
+ last_error = ""
156
+
157
+ for attempt in range(1, max_retries + 1):
158
+ try:
159
+ resp = client.post(url, headers=headers, json=body)
160
+ if resp.status_code == 200:
161
+ data = resp.json()
162
+ reply = ""
163
+ for step in data.get("steps", []):
164
+ for item in step.get("content", []):
165
+ if item.get("type") == "text":
166
+ reply += item.get("text", "")
167
+
168
+ return {
169
+ "text": reply.strip(),
170
+ "usage": data.get("usage", {}),
171
+ "model": model_name,
172
+ }
173
+
174
+ last_error = f"Gemini Interactions API error ({resp.status_code}): {resp.text}"
175
+ if resp.status_code in (429, 500, 502, 503, 504) and attempt < max_retries:
176
+ sleep_time = attempt * 1.5
177
+ logger.warning(
178
+ "Gemini (%s) attempt %d/%d failed (%d). Retrying in %.1fs...",
179
+ model_name, attempt, max_retries, resp.status_code, sleep_time
180
+ )
181
+ time.sleep(sleep_time)
182
+ continue
183
+ else:
184
+ raise RuntimeError(last_error)
185
+
186
+ except httpx.RequestError as exc:
187
+ last_error = f"Network error during Gemini call: {exc}"
188
+ if attempt < max_retries:
189
+ time.sleep(attempt * 1.5)
190
+ continue
191
+ raise RuntimeError(last_error)
192
+
193
+ raise RuntimeError(last_error or f"Gemini API failed after {max_retries} attempts.")
194
 
195
  def _call_interactions(self, prompt: str, system_instruction: str = "") -> dict:
196
+ """Call /v1beta/interactions with 2 attempts per model and fallback to GEMINI_FALLBACK_MODEL."""
197
  if not self.api_key or self.api_key == "YOUR_GEMINI_API_KEY_HERE":
198
  raise ValueError("GEMINI_API_KEY is not set in processor/.env")
199
 
 
202
  "x-goog-api-key": self.api_key,
203
  "Content-Type": "application/json",
204
  }
 
 
 
 
 
 
205
 
206
+ models = [self.model]
207
+ if self.fallback_model and self.fallback_model != self.model:
208
+ models.append(self.fallback_model)
209
 
210
+ last_exc: Optional[Exception] = None
211
  with httpx.Client(timeout=60.0) as client:
212
+ for idx, model_name in enumerate(models):
213
  try:
214
+ return self._call_model(
215
+ client=client,
216
+ url=url,
217
+ headers=headers,
218
+ model_name=model_name,
219
+ prompt=prompt,
220
+ system_instruction=system_instruction,
221
+ )
222
+ except Exception as exc:
223
+ last_exc = exc
224
+ if idx + 1 < len(models):
 
 
 
 
 
 
225
  logger.warning(
226
+ "Gemini model '%s' failed (%s). Falling back to '%s'...",
227
+ model_name,
228
+ exc,
229
+ models[idx + 1],
230
  )
 
 
 
 
 
 
 
 
 
 
 
231
 
232
+ raise last_exc or RuntimeError("Gemini API call failed.")
233
 
234
  def ping(self, prompt: str = "Say hello in one short sentence.") -> Dict[str, Any]:
235
  """Test API key and latency directly."""
 
253
  data["completeness_score"] = self._completeness(data)
254
  data["markdown"] = markdown
255
  data["usage_metadata"] = res.get("usage", {})
256
+ data["model_used"] = res.get("model", self.model)
257
  return data
258
 
259
  @staticmethod