Spaces:
Sleeping
Sleeping
fig: API error by using loop
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ from collections import deque
|
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
from google import genai
|
| 11 |
from google.genai import types
|
|
|
|
| 12 |
|
| 13 |
load_dotenv()
|
| 14 |
|
|
@@ -192,8 +193,21 @@ def validate_and_evaluate(description: str, code: str):
|
|
| 192 |
prompt = build_prompt(description, code)
|
| 193 |
try:
|
| 194 |
raw_output = generate_response(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
except Exception as exc:
|
| 196 |
-
|
|
|
|
| 197 |
|
| 198 |
verdict, metrics, issues = format_for_display(raw_output)
|
| 199 |
return verdict, metrics, issues, ""
|
|
@@ -243,16 +257,37 @@ Code to review:
|
|
| 243 |
# ---------------------------------------------------------------------------
|
| 244 |
# MODULE 4 β Generation Module
|
| 245 |
# ---------------------------------------------------------------------------
|
| 246 |
-
def generate_response(prompt: str) ->
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
# Build the Gradio UI and connect it to the validation and evaluation function
|
| 258 |
app = build_ui(validate_and_evaluate)
|
|
|
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
from google import genai
|
| 11 |
from google.genai import types
|
| 12 |
+
from google.genai.errors import APIError
|
| 13 |
|
| 14 |
load_dotenv()
|
| 15 |
|
|
|
|
| 193 |
prompt = build_prompt(description, code)
|
| 194 |
try:
|
| 195 |
raw_output = generate_response(prompt)
|
| 196 |
+
except APIError as exc:
|
| 197 |
+
# 1. If it's a 500 error (and the retries failed), show the friendly timeout message
|
| 198 |
+
if exc.code == 500:
|
| 199 |
+
timeout_msg = (
|
| 200 |
+
"β API Connection Timeout: The upstream AI provider is currently overloaded "
|
| 201 |
+
"or unresponsive. Please try again in a few moments."
|
| 202 |
+
)
|
| 203 |
+
return "", "", "", timeout_msg
|
| 204 |
+
|
| 205 |
+
# 2. If it's a different API error (e.g., 400 Bad Request, 403 Invalid Key)
|
| 206 |
+
return "", "", "", f"β Upstream API Error ({exc.code}): {exc}"
|
| 207 |
+
|
| 208 |
except Exception as exc:
|
| 209 |
+
# 3. Catch any local application bugs or JSON parsing failures here
|
| 210 |
+
return "", "", "", f"β Internal Application Error: {exc}"
|
| 211 |
|
| 212 |
verdict, metrics, issues = format_for_display(raw_output)
|
| 213 |
return verdict, metrics, issues, ""
|
|
|
|
| 257 |
# ---------------------------------------------------------------------------
|
| 258 |
# MODULE 4 β Generation Module
|
| 259 |
# ---------------------------------------------------------------------------
|
| 260 |
+
def generate_response(prompt: str, retries: int = 3, initial_delay: float = 1.5) -> dict:
|
| 261 |
+
delay = initial_delay
|
| 262 |
+
|
| 263 |
+
for i in range(retries):
|
| 264 |
+
try:
|
| 265 |
+
response = client.models.generate_content(
|
| 266 |
+
model=LLM_MODEL,
|
| 267 |
+
contents=prompt,
|
| 268 |
+
config=types.GenerateContentConfig(
|
| 269 |
+
response_mime_type="application/json",
|
| 270 |
+
response_schema=response_schema,
|
| 271 |
+
)
|
| 272 |
+
)
|
| 273 |
+
# If successful, parse the JSON and return immediately
|
| 274 |
+
print("β
Successful response from Gemini API.")
|
| 275 |
+
return json.loads(response.text)
|
| 276 |
+
|
| 277 |
+
except APIError as exc:
|
| 278 |
+
# Catch transient 500 Internal Server Errors and retry
|
| 279 |
+
if exc.code == 500 and i <= retries - 1:
|
| 280 |
+
print(f"β οΈ Gemini API 500 error. Retrying attempt {i + 1}/{retries} in {delay}s...")
|
| 281 |
+
time.sleep(delay)
|
| 282 |
+
delay *= 2 # Exponential backoff
|
| 283 |
+
continue
|
| 284 |
+
|
| 285 |
+
# If out of retries, or if it's a 400/403 error, raise it up to validate_and_evaluate
|
| 286 |
+
raise exc
|
| 287 |
+
|
| 288 |
+
except json.JSONDecodeError as exc:
|
| 289 |
+
# Failsafe: In rare cases, if the API drops a malformed payload, catch the JSON error
|
| 290 |
+
raise RuntimeError(f"API returned invalid JSON format: {exc}")
|
| 291 |
|
| 292 |
# Build the Gradio UI and connect it to the validation and evaluation function
|
| 293 |
app = build_ui(validate_and_evaluate)
|