Spaces:
Build error
Build error
CB commited on
Update streamlit_app.py
Browse files- streamlit_app.py +58 -27
streamlit_app.py
CHANGED
|
@@ -298,38 +298,69 @@ def get_runtime_api_key():
|
|
| 298 |
return os.getenv("GOOGLE_API_KEY", "").strip() or None
|
| 299 |
|
| 300 |
# Robust responses caller: SDK preferred, then attempt HTTP fallback via requests
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
def responses_generate(model, messages, files, max_output_tokens, api_key):
|
| 302 |
if not api_key:
|
| 303 |
raise RuntimeError("No API key for responses_generate")
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
import requests
|
| 307 |
-
url = "https://gen-ai.googleapis.com/v1/responses"
|
| 308 |
-
payload = {"model": model, "messages": messages, "max_output_tokens": max_output_tokens}
|
| 309 |
-
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
| 310 |
try:
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
except Exception as e:
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
#
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
if (st.session_state.get("busy") is False) and ('generate_now' in locals() and generate_now):
|
| 335 |
if not st.session_state.get("videos"):
|
|
|
|
| 298 |
return os.getenv("GOOGLE_API_KEY", "").strip() or None
|
| 299 |
|
| 300 |
# Robust responses caller: SDK preferred, then attempt HTTP fallback via requests
|
| 301 |
+
# --- REPLACE/ADD START ---
|
| 302 |
+
import json
|
| 303 |
+
import requests
|
| 304 |
+
|
| 305 |
def responses_generate(model, messages, files, max_output_tokens, api_key):
|
| 306 |
if not api_key:
|
| 307 |
raise RuntimeError("No API key for responses_generate")
|
| 308 |
+
# If genai SDK present and has responses.generate, prefer it
|
| 309 |
+
if HAS_GENAI and genai is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
try:
|
| 311 |
+
genai.configure(api_key=api_key)
|
| 312 |
+
if hasattr(genai, "responses") and getattr(genai, "responses") is not None:
|
| 313 |
+
return genai.responses.generate(model=model, messages=messages, files=files, max_output_tokens=max_output_tokens)
|
| 314 |
except Exception as e:
|
| 315 |
+
# allow fallback to HTTP and surface SDK error in debug later
|
| 316 |
+
sdk_err = str(e)
|
| 317 |
+
else:
|
| 318 |
+
sdk_err = "SDK not available"
|
| 319 |
+
|
| 320 |
+
# HTTP fallback using requests with retries
|
| 321 |
+
url = "https://gen-ai.googleapis.com/v1/responses"
|
| 322 |
+
payload = {"model": model, "messages": messages, "max_output_tokens": max_output_tokens}
|
| 323 |
+
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
| 324 |
+
last_exc = None
|
| 325 |
+
for attempt in range(3):
|
| 326 |
+
try:
|
| 327 |
+
resp = requests.post(url, json=payload, headers=headers, timeout=30)
|
| 328 |
+
# Try to parse JSON on success
|
| 329 |
+
if resp.status_code == 200:
|
| 330 |
+
try:
|
| 331 |
+
return resp.json()
|
| 332 |
+
except Exception:
|
| 333 |
+
return {"text": resp.text}
|
| 334 |
+
else:
|
| 335 |
+
last_exc = RuntimeError(f"HTTP {resp.status_code}: {resp.text}")
|
| 336 |
+
time.sleep(1 + attempt)
|
| 337 |
+
except Exception as e:
|
| 338 |
+
last_exc = e
|
| 339 |
+
time.sleep(1 + attempt)
|
| 340 |
+
|
| 341 |
+
# If we get here, both SDK and HTTP failed. Provide diagnostic info.
|
| 342 |
+
diag = {
|
| 343 |
+
"sdk_error": sdk_err if 'sdk_err' in locals() else None,
|
| 344 |
+
"http_error": str(last_exc),
|
| 345 |
+
"http_url": url,
|
| 346 |
+
"payload_sample": json.dumps(payload)[:1000],
|
| 347 |
+
"note": "See Streamlit 'Debug (compact)' output for response keys/body"
|
| 348 |
+
}
|
| 349 |
+
raise RuntimeError(f"genai.responses not available and HTTP fallback failed: {diag}")
|
| 350 |
+
|
| 351 |
+
# call wrapper that returns response or raises; replace the previous call_responses_once with this:
|
| 352 |
+
def call_responses_once(model_used, system_msg, user_msg, fname, max_tokens):
|
| 353 |
+
files = [{"name": fname}] if fname else None
|
| 354 |
+
for attempt in range(2):
|
| 355 |
+
try:
|
| 356 |
+
return responses_generate(model_used, [system_msg, user_msg], files, max_tokens, api_key=get_runtime_api_key())
|
| 357 |
+
except Exception as e:
|
| 358 |
+
if attempt == 0:
|
| 359 |
+
time.sleep(1.0)
|
| 360 |
+
continue
|
| 361 |
+
raise
|
| 362 |
+
# --- REPLACE/ADD END ---
|
| 363 |
+
|
| 364 |
|
| 365 |
if (st.session_state.get("busy") is False) and ('generate_now' in locals() and generate_now):
|
| 366 |
if not st.session_state.get("videos"):
|